github.com/sunvim/utils@v0.1.0/linear_ac/linear_ac_dbg_test.go (about) 1 // reference to 2 // https://github.com/crazybie/linear_ac 3 4 package linear_ac 5 6 import ( 7 "runtime" 8 "testing" 9 ) 10 11 func Test_CheckArray(t *testing.T) { 12 DbgMode = true 13 ac := BindNew() 14 defer func() { 15 if err := recover(); err == nil { 16 t.Errorf("faile to check") 17 } 18 }() 19 20 type D struct { 21 v [4]*int 22 } 23 var d *D 24 ac.New(&d) 25 26 for i := 0; i < len(d.v); i++ { 27 d.v[i] = new(int) 28 *d.v[i] = i 29 } 30 ac.Release() 31 } 32 33 func Test_CheckInternalSlice(t *testing.T) { 34 DbgMode = true 35 ac := BindNew() 36 37 type D struct { 38 v []int 39 } 40 var d *D 41 ac.New(&d) 42 ac.NewSlice(&d.v, 1, 0) 43 ac.Release() 44 } 45 46 func Test_CheckExternalSlice(t *testing.T) { 47 DbgMode = true 48 ac := BindNew() 49 defer func() { 50 if err := recover(); err == nil { 51 t.Errorf("faile to check") 52 } 53 }() 54 55 type D struct { 56 v []*int 57 } 58 var d *D 59 ac.New(&d) 60 61 d.v = make([]*int, 3) 62 for i := 0; i < len(d.v); i++ { 63 d.v[i] = new(int) 64 *d.v[i] = i 65 } 66 ac.Release() 67 } 68 69 func TestUseAfterFree_Pointer(t *testing.T) { 70 DbgMode = true 71 defer func() { 72 DbgMode = false 73 if err := recover(); err == nil { 74 t.Errorf("failed to check") 75 } 76 }() 77 ac := BindNew() 78 var d *PbData 79 ac.New(&d) 80 d.Age = ac.Int(11) 81 ac.Release() 82 if *d.Age == 11 { 83 t.Errorf("not panic") 84 } 85 } 86 87 func TestUseAfterFree_StructPointer(t *testing.T) { 88 DbgMode = true 89 defer func() { 90 DbgMode = false 91 if err := recover(); err == nil { 92 t.Errorf("failed to check") 93 } 94 }() 95 ac := BindNew() 96 97 var d *PbData 98 ac.New(&d) 99 var item *PbItem 100 ac.New(&item) 101 d.InUse = item 102 103 ac.Release() 104 c := *d.InUse 105 t.Errorf("should panic") 106 _ = c 107 } 108 109 func TestUseAfterFree_Slice(t *testing.T) { 110 DbgMode = true 111 defer func() { 112 DbgMode = false 113 if err := recover(); err == nil { 114 t.Errorf("failed to check") 115 } 116 }() 117 118 ac := BindNew() 119 var d *PbData 120 ac.New(&d) 121 ac.NewSlice(&d.Items, 1, 1) 122 ac.Release() 123 124 if cap(d.Items) == 1 { 125 t.Errorf("not panic") 126 } 127 d.Items[0] = new(PbItem) 128 } 129 130 func Test_WorkWithGc(t *testing.T) { 131 type D struct { 132 v [10]*int 133 } 134 135 ac := BindNew() 136 137 var d *D 138 ac.New(&d) 139 140 for i := 0; i < len(d.v); i++ { 141 d.v[i] = ac.Int(i) 142 //d.v[i] = new(int) 143 *d.v[i] = i 144 runtime.GC() 145 } 146 147 for i, p := range d.v { 148 if *p != i { 149 t.Errorf("int %v is gced", i) 150 } 151 } 152 ac.Release() 153 } 154 155 func TestLinearAllocator_ExternalMap(t *testing.T) { 156 DbgMode = true 157 ac := BindNew() 158 defer func() { 159 if err := recover(); err == nil { 160 t.Errorf("faile to check") 161 } 162 }() 163 164 type D struct { 165 m map[int]*int 166 } 167 var d *D 168 ac.New(&d) 169 d.m = make(map[int]*int) 170 ac.Release() 171 } 172 173 func TestAllocator_CheckExternalEnum(t *testing.T) { 174 ac := BindNew() 175 defer func() { 176 if err := recover(); err == nil { 177 t.Errorf("failed to check") 178 } 179 }() 180 181 var item *PbItem 182 ac.New(&item) 183 item.EnumVal = new(EnumA) 184 ac.Release() 185 }