github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/skipmap/oparry_test.go (about)

     1  package skipmap
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  
     7  	"github.com/songzhibin97/go-baseutils/sys/fastrand"
     8  )
     9  
    10  type dummy struct {
    11  	data optionalArray
    12  }
    13  
    14  func TestOpArray(t *testing.T) {
    15  	n := new(dummy)
    16  	n.data.extra = new([op2]unsafe.Pointer)
    17  
    18  	var array [maxLevel]unsafe.Pointer
    19  	for i := 0; i < maxLevel; i++ {
    20  		value := unsafe.Pointer(&dummy{})
    21  		array[i] = value
    22  		n.data.store(i, value)
    23  	}
    24  
    25  	for i := 0; i < maxLevel; i++ {
    26  		if array[i] != n.data.load(i) || array[i] != n.data.atomicLoad(i) {
    27  			t.Fatal(i, array[i], n.data.load(i))
    28  		}
    29  	}
    30  
    31  	for i := 0; i < 1000; i++ {
    32  		r := int(fastrand.Uint32n(maxLevel))
    33  		value := unsafe.Pointer(&dummy{})
    34  		if i%100 == 0 {
    35  			value = nil
    36  		}
    37  		array[r] = value
    38  		if fastrand.Uint32n(2) == 0 {
    39  			n.data.store(r, value)
    40  		} else {
    41  			n.data.atomicStore(r, value)
    42  		}
    43  	}
    44  
    45  	for i := 0; i < maxLevel; i++ {
    46  		if array[i] != n.data.load(i) || array[i] != n.data.atomicLoad(i) {
    47  			t.Fatal(i, array[i], n.data.load(i))
    48  		}
    49  	}
    50  }