Skip to content

Commit

Permalink
Improve test consistency (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
puzpuzpuz authored Jun 4, 2023
1 parent 6c2e259 commit 5e2931d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ func DisableAssertions() {
func Fastrand() uint32 {
return fastrand()
}

func NextPowOf2(v uint32) uint32 {
return nextPowOf2(v)
}
22 changes: 13 additions & 9 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package xsync
package xsync_test

import (
"math/rand"
"testing"

. "github.com/puzpuzpuz/xsync/v2"
)

func TestNextPowOf2(t *testing.T) {
if nextPowOf2(0) != 1 {
if NextPowOf2(0) != 1 {
t.Error("nextPowOf2 failed")
}
if nextPowOf2(1) != 1 {
if NextPowOf2(1) != 1 {
t.Error("nextPowOf2 failed")
}
if nextPowOf2(2) != 2 {
if NextPowOf2(2) != 2 {
t.Error("nextPowOf2 failed")
}
if nextPowOf2(3) != 4 {
if NextPowOf2(3) != 4 {
t.Error("nextPowOf2 failed")
}
}

// This test is here to catch potential problems
// with fastrand-related changes.
func TestFastrand(t *testing.T) {
count := 10000
set := make(map[uint32]struct{}, count)

for i := 0; i < count; i++ {
num := fastrand()
num := Fastrand()
set[num] = struct{}{}
}

Expand All @@ -36,14 +40,14 @@ func TestFastrand(t *testing.T) {

func BenchmarkFastrand(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = fastrand()
_ = Fastrand()
}
// about 1.5 ns/op
// about 1.4 ns/op on x86-64
}

func BenchmarkRand(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = rand.Uint32()
}
// about 12 ns/op
// about 12 ns/op on x86-64
}

0 comments on commit 5e2931d

Please # to comment.