github.com/songzhibin97/gkit@v1.2.13/structure/skipset/oparry_test.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package skipset 16 17 import ( 18 "github.com/songzhibin97/gkit/sys/fastrand" 19 "testing" 20 "unsafe" 21 ) 22 23 type dummy struct { 24 data optionalArray 25 } 26 27 func TestOpArray(t *testing.T) { 28 n := new(dummy) 29 n.data.extra = new([op2]unsafe.Pointer) 30 31 var array [maxLevel]unsafe.Pointer 32 for i := 0; i < maxLevel; i++ { 33 value := unsafe.Pointer(&dummy{}) 34 array[i] = value 35 n.data.store(i, value) 36 } 37 38 for i := 0; i < maxLevel; i++ { 39 if array[i] != n.data.load(i) || array[i] != n.data.atomicLoad(i) { 40 t.Fatal(i, array[i], n.data.load(i)) 41 } 42 } 43 44 for i := 0; i < 1000; i++ { 45 r := int(fastrand.Uint32n(maxLevel)) 46 value := unsafe.Pointer(&dummy{}) 47 if i%100 == 0 { 48 value = nil 49 } 50 array[r] = value 51 if fastrand.Uint32n(2) == 0 { 52 n.data.store(r, value) 53 } else { 54 n.data.atomicStore(r, value) 55 } 56 } 57 58 for i := 0; i < maxLevel; i++ { 59 if array[i] != n.data.load(i) || array[i] != n.data.atomicLoad(i) { 60 t.Fatal(i, array[i], n.data.load(i)) 61 } 62 } 63 }