github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/collection/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  	"testing"
    19  	"unsafe"
    20  
    21  	"github.com/bytedance/gopkg/lang/fastrand"
    22  )
    23  
    24  type dummy struct {
    25  	data optionalArray
    26  }
    27  
    28  func TestOpArray(t *testing.T) {
    29  	n := new(dummy)
    30  	n.data.extra = new([op2]unsafe.Pointer)
    31  
    32  	var array [maxLevel]unsafe.Pointer
    33  	for i := 0; i < maxLevel; i++ {
    34  		value := unsafe.Pointer(&dummy{})
    35  		array[i] = value
    36  		n.data.store(i, value)
    37  	}
    38  
    39  	for i := 0; i < maxLevel; i++ {
    40  		if array[i] != n.data.load(i) || array[i] != n.data.atomicLoad(i) {
    41  			t.Fatal(i, array[i], n.data.load(i))
    42  		}
    43  	}
    44  
    45  	for i := 0; i < 1000; i++ {
    46  		r := int(fastrand.Uint32n(maxLevel))
    47  		value := unsafe.Pointer(&dummy{})
    48  		if i%100 == 0 {
    49  			value = nil
    50  		}
    51  		array[r] = value
    52  		if fastrand.Uint32n(2) == 0 {
    53  			n.data.store(r, value)
    54  		} else {
    55  			n.data.atomicStore(r, value)
    56  		}
    57  	}
    58  
    59  	for i := 0; i < maxLevel; i++ {
    60  		if array[i] != n.data.load(i) || array[i] != n.data.atomicLoad(i) {
    61  			t.Fatal(i, array[i], n.data.load(i))
    62  		}
    63  	}
    64  }