Debian Codegraph @

allocatormethods_test.go

raw
1 // Copyright 2023 Huan Du. All rights reserved.
2 // Licensed under the MIT license that can be found in the LICENSE file.
3
4 package clone
5
6 import (
7 "reflect"
8 "sync"
9 "testing"
10 "unsafe"
11
12 "github.com/huandu/go-assert"
13 )
14
15 func TestAllocatorMethodsParent(t *testing.T) {
16 a := assert.New(t)
17 parent := NewAllocator(nil, &AllocatorMethods{
18 IsScalar: func(k reflect.Kind) bool {
19 return k == reflect.Int
20 },
21 })
22 allocator := NewAllocator(nil, &AllocatorMethods{
23 Parent: parent,
24 })
25
26 a.Assert(parent.parent == defaultAllocator)
27 a.Assert(allocator.parent == parent)
28
29 // Set up customizations in parent.
30 type T1 struct {
31 Data []byte
32 }
33 type T2 struct {
34 Data []byte
35 }
36 type T3 struct {
37 Data []byte
38 }
39 typeOfT1 := reflect.TypeOf(new(T1))
40 typeOfT2 := reflect.TypeOf(new(T2))
41 typeOfT3 := reflect.TypeOf(new(T3))
42 customFuncCalled := 0
43 parent.MarkAsScalar(typeOfT1)
44 parent.MarkAsOpaquePointer(typeOfT2)
45 parent.SetCustomFunc(typeOfT3, func(allocator *Allocator, old, new reflect.Value) {
46 customFuncCalled++
47 })
48
49 // All customizations should be inherited from parent.
50 st1 := allocator.loadStructType(typeOfT1.Elem())
51 st2 := allocator.loadStructType(typeOfT2.Elem())
52 st3 := allocator.loadStructType(typeOfT3.Elem())
53 a.Equal(len(st1.PointerFields), 0)
54 a.Assert(st1.fn == nil)
55 a.Equal(len(st3.PointerFields), 1)
56 a.Assert(st2.fn == nil)
57 a.Equal(len(st3.PointerFields), 1)
58 a.Assert(st3.fn != nil)
59 a.Assert(!allocator.isOpaquePointer(typeOfT1))
60 a.Assert(allocator.isOpaquePointer(typeOfT2))
61 a.Assert(!allocator.isOpaquePointer(typeOfT3))
62 a.Assert(allocator.isScalar(reflect.Int))
63 a.Assert(!allocator.isScalar(reflect.Uint))
64 }
65
66 func TestAllocatorMethodsPool(t *testing.T) {
67 a := assert.New(t)
68 pool1Called := 0
69 pool1 := &sync.Pool{
70 New: func() interface{} {
71 pool1Called++
72 return nil
73 },
74 }
75 pool2Called := 0
76 pool2 := &sync.Pool{
77 New: func() interface{} {
78 pool2Called++
79 return nil
80 },
81 }
82 parent := NewAllocator(unsafe.Pointer(pool1), &AllocatorMethods{
83 New: func(pool unsafe.Pointer, t reflect.Type) reflect.Value {
84 p := (*sync.Pool)(pool)
85 p.Get()
86 return defaultAllocator.New(t)
87 },
88 MakeSlice: func(pool unsafe.Pointer, t reflect.Type, len, cap int) reflect.Value {
89 p := (*sync.Pool)(pool)
90 p.Get()
91 return defaultAllocator.MakeSlice(t, len, cap)
92 },
93 MakeMap: func(pool unsafe.Pointer, t reflect.Type, size int) reflect.Value {
94 p := (*sync.Pool)(pool)
95 p.Get()
96 return defaultAllocator.MakeMap(t, size)
97 },
98 })
99 allocator := NewAllocator(unsafe.Pointer(pool2), &AllocatorMethods{
100 Parent: parent,
101 MakeChan: func(pool unsafe.Pointer, t reflect.Type, size int) reflect.Value {
102 p := (*sync.Pool)(pool)
103 p.Get()
104 return defaultAllocator.MakeChan(t, size)
105 },
106 })
107
108 // All allocation should be implemented by parent.
109 allocator.New(reflect.TypeOf(1))
110 allocator.MakeSlice(reflect.TypeOf([]int{}), 0, 0)
111 allocator.MakeMap(reflect.TypeOf(map[int]int{}), 0)
112 allocator.MakeChan(reflect.TypeOf(make(chan int)), 0)
113
114 // 1 for new parent allocator itself.
115 // 1 for new allocator itself.
116 // 3 for New, MakeSlice and MakeMap.
117 a.Equal(pool1Called, 5)
118
119 // 1 for MakeChan.
120 a.Equal(pool2Called, 1)
121 }