github.com/better-concurrent/guc@v0.0.0-20190520022744-eb29266403a1/unit_test.go (about) 1 package guc 2 3 import ( 4 "fmt" 5 "sync" 6 "sync/atomic" 7 "testing" 8 "time" 9 "unsafe" 10 ) 11 12 func TestRuntimeLink(t *testing.T) { 13 fmt.Println("now test " + t.Name()) 14 if SyncRuntimeCanSpin(0) { 15 t.Error("SyncRuntimeCanSpin failed!") 16 } 17 // nanoTime 18 fmt.Println(SyncRuntimeNanoTime()) 19 // do spin 20 SyncRuntimeDoSpin() 21 22 // sema 23 var sema uint32 = 0 24 c := make(chan string, 1) 25 go func() { 26 SyncRuntimeSemacquire(&sema) 27 c <- "ok" 28 }() 29 30 go func() { 31 time.Sleep(1 * time.Second) 32 SyncRuntimeSemrelease(&sema, false) 33 }() 34 35 select { 36 case ok := <-c: 37 fmt.Println("sema " + ok) 38 case <-time.After(2 * time.Second): 39 t.Error("sema error!") 40 } 41 } 42 43 type testS struct { 44 i int32 45 s string 46 } 47 48 type testS2 struct { 49 i int32 50 } 51 52 type testS3 struct { 53 i int32 54 //arr []testS 55 arr unsafe.Pointer 56 } 57 58 func TestEface(t *testing.T) { 59 test1 := testS{i: 1, s: "test"} 60 fmt.Println(unpackEFace(test1)) 61 } 62 63 func TestSizeof(t *testing.T) { 64 test := testS{} 65 fmt.Println(unsafe.Sizeof(test)) 66 test2 := testS2{} 67 fmt.Println(unsafe.Sizeof(test2)) 68 var a interface{} 69 fmt.Println(unsafe.Sizeof(a)) 70 var arr [2]interface{} 71 fmt.Println(unsafe.Sizeof(arr)) 72 } 73 74 func TestStructHash(t *testing.T) { 75 test1 := testS{i: 1, s: "test"} 76 test2 := testS{i: 1, s: "test"} 77 h1 := Nilinterhash(unsafe.Pointer(unpackEFace(test1)), 0xffff) 78 h2 := Nilinterhash(unsafe.Pointer(unpackEFace(test2)), 0xffff) 79 if h1 != h2 { 80 t.Error("hash error!") 81 } 82 } 83 84 func TestStructEquals(t *testing.T) { 85 test1 := testS{i: 1, s: "test"} 86 test2 := testS{i: 1, s: "test"} 87 if !Nilinterequal(unsafe.Pointer(unpackEFace(test1)), unsafe.Pointer(unpackEFace(test2))) { 88 t.Error("equals error!") 89 } 90 91 if !Nilinterequal(unsafe.Pointer(unpackEFace(*&test1)), unsafe.Pointer(unpackEFace(test2))) { 92 t.Error("equals error!") 93 } 94 } 95 96 func TestUnsafeLoad(t *testing.T) { 97 test := testS3{i: 1, arr: nil} 98 fmt.Println(test) 99 arr := []testS{{i: 1}} 100 value := atomic.Value{} 101 fmt.Println(value.Load()) 102 value.Store(arr) 103 fmt.Println(value.Load()) 104 105 arrp := [2]*testS{} 106 arrp[0] = &testS{i: 1} 107 arrup := [2]unsafe.Pointer{} 108 p := unsafe.Pointer(&testS{i: 3}) 109 arrup[0] = p 110 atomic.StorePointer(&arrup[1], p) 111 fmt.Println((*testS)(atomic.LoadPointer(&arrup[0]))) 112 fmt.Println((*testS)(atomic.LoadPointer(&arrup[1]))) 113 } 114 115 type testS4 struct { 116 i int32 117 t *testS 118 } 119 120 func (test *testS4) getT() interface{} { 121 return *test.t 122 } 123 124 func TestObjectCopy(t *testing.T) { 125 test := testS4{i: 1, t: &testS{1, "s"}} 126 fmt.Println(test) 127 var local interface{} 128 local = test.getT() 129 fmt.Println(&local) 130 fmt.Println(unpackEFace(local).rtype) 131 fmt.Println(unpackEFace(local).data) 132 fmt.Printf("%T\n", local) 133 fmt.Println(local == *test.t) 134 } 135 136 type testS5 struct { 137 i int32 138 p unsafe.Pointer // is *[]*testS 139 } 140 141 func TestPointerLoad(t *testing.T) { 142 test := testS5{i: 2, p: nil} 143 fmt.Println(test) 144 arr := make([]*testS, test.i) 145 arr[0] = &testS{i: 1, s: "a"} 146 test.p = unsafe.Pointer(&arr) 147 pp := (*[]*testS)(test.p) 148 fmt.Println((*pp)[0]) 149 150 newT := &testS{i: 2, s: "b"} 151 //p := unsafe.Pointer((*pp)[0]) 152 p := (unsafe.Pointer)((*pp)[0]) 153 fmt.Println((*testS)(p)) 154 test.p = unsafe.Pointer(newT) 155 atomic.StorePointer(&p, unsafe.Pointer(newT)) 156 fmt.Println((*testS)(p)) 157 fmt.Println((*pp)[0]) 158 } 159 160 func TestMutex(t *testing.T) { 161 fmt.Println(unsafe.Sizeof(sync.Mutex{})) 162 } 163 164 func TestGolangInt(t *testing.T) { 165 for i := 0; i < 100000; i++ { 166 n := int(Fastrand()) & 0xffffffff 167 if n < 0 { 168 fmt.Println(n) 169 } 170 } 171 } 172 173 func TestArrayPointRef(t *testing.T) { 174 arr := make([]testS, 2) 175 arr[0].s = "a" 176 arr[1].s = "b" 177 ap := &arr 178 acp0 := &(*ap)[0] 179 acp1 := &(*ap)[0] 180 fmt.Printf("%p\n", acp0) 181 fmt.Printf("%p\n", acp1) 182 }