You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package nrandom_test
|
|
|
|
import (
|
|
"fmt"
|
|
"git.noahlan.cn/noahlan/ntool/narr"
|
|
"git.noahlan.cn/noahlan/ntool/ndef"
|
|
"git.noahlan.cn/noahlan/ntool/nrandom"
|
|
"git.noahlan.cn/noahlan/ntool/ntest/assert"
|
|
"testing"
|
|
)
|
|
|
|
func baseTestRandom[T ~string | ndef.XIntOrFloat](t *testing.T, count int, genFn func() T, singleCheck func(item T)) {
|
|
acts := make([]T, count)
|
|
for i := 0; i < count; i++ {
|
|
act := genFn()
|
|
if i < 2 {
|
|
fmt.Println(act)
|
|
}
|
|
acts[i] = act
|
|
singleCheck(act)
|
|
}
|
|
uniqueLen := len(narr.Unique(acts))
|
|
assert.True(t, len(acts) == uniqueLen)
|
|
}
|
|
|
|
const TestCount = 10000
|
|
|
|
func TestSnowflakeId(t *testing.T) {
|
|
baseTestRandom(t, TestCount, nrandom.SnowflakeId, func(item int64) {
|
|
assert.True(t, item != 0)
|
|
})
|
|
}
|
|
|
|
func TestUUIDV4(t *testing.T) {
|
|
baseTestRandom(t, TestCount, nrandom.UUIDV4, func(item string) {
|
|
assert.NotEmpty(t, item)
|
|
})
|
|
}
|
|
|
|
func TestNewUUIDV4(t *testing.T) {
|
|
baseTestRandom(t, TestCount, func() string {
|
|
return nrandom.NewUUIDV4().String()
|
|
}, func(item string) {
|
|
assert.NotEmpty(t, item)
|
|
})
|
|
}
|
|
|
|
func TestNewUUIDV6(t *testing.T) {
|
|
baseTestRandom(t, TestCount, func() string {
|
|
return nrandom.NewUUIDV6().String()
|
|
}, func(item string) {
|
|
assert.NotEmpty(t, item)
|
|
})
|
|
}
|
|
|
|
func TestNewUUIDV7(t *testing.T) {
|
|
baseTestRandom(t, TestCount, func() string {
|
|
return nrandom.NewUUIDV7().String()
|
|
}, func(item string) {
|
|
assert.NotEmpty(t, item)
|
|
})
|
|
}
|