Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve test consistency #99

Merged
merged 1 commit into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}