github.com/CAFxX/fastrand@v0.1.0/pcg_test.go (about)

     1  package fastrand
     2  
     3  import "testing"
     4  
     5  var expPCG = []uint32{0xd386fb93, 0xd1bfe43e, 0xc391d32f, 0xc87f4598, 0x8f712c5c, 0xbb219c74, 0xb8e4973d, 0x8440776c}
     6  
     7  func TestPCG(t *testing.T) {
     8  	var r PCG
     9  	r.Seed(1)
    10  	for i, e := range expPCG {
    11  		if g := r.Uint32(); g != e {
    12  			t.Errorf("i=%d, expected=%x, got=%x", i, e, g)
    13  		}
    14  	}
    15  }
    16  
    17  func TestAtomicPCG(t *testing.T) {
    18  	var r AtomicPCG
    19  	r.Seed(1)
    20  	for i, e := range expPCG {
    21  		if g := r.Uint32(); g != e {
    22  			t.Errorf("i=%d, expected=%x, got=%x", i, e, g)
    23  		}
    24  	}
    25  }
    26  
    27  func TestShardedPCGFallback(t *testing.T) {
    28  	var r ShardedPCG // no shards created
    29  	r.fallback.Seed(1)
    30  	for i, e := range expPCG {
    31  		if g := r.Uint32(); g != e {
    32  			t.Errorf("i=%d, expected=%x, got=%x", i, e, g)
    33  		}
    34  	}
    35  }
    36  
    37  func TestShardedPCG(t *testing.T) {
    38  	r := NewShardedPCG()
    39  	id := procPin()
    40  	defer procUnpin()
    41  	if fastrand_nounsafe {
    42  		r.fallback.Seed(1)
    43  	} else {
    44  		r.states[id].Seed(1)
    45  	}
    46  	for i, e := range expPCG {
    47  		if g := r.Uint32(); g != e {
    48  			t.Errorf("i=%d, expected=%x, got=%x", i, e, g)
    49  		}
    50  	}
    51  }