From 5e2931dca70407a83c9df69a35b4a55c0eb202fb Mon Sep 17 00:00:00 2001 From: Andrei Pechkurov <37772591+puzpuzpuz@users.noreply.github.com> Date: Sun, 4 Jun 2023 20:46:18 +0300 Subject: [PATCH] Improve test consistency (#99) --- export_test.go | 4 ++++ util_test.go | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/export_test.go b/export_test.go index b526526..7d82818 100644 --- a/export_test.go +++ b/export_test.go @@ -51,3 +51,7 @@ func DisableAssertions() { func Fastrand() uint32 { return fastrand() } + +func NextPowOf2(v uint32) uint32 { + return nextPowOf2(v) +} diff --git a/util_test.go b/util_test.go index cef9bee..c4bbf22 100644 --- a/util_test.go +++ b/util_test.go @@ -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{}{} } @@ -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 }