2024-02-02 08:04:48 +00:00
|
|
|
package tind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_GenTinD_With_Defaults(t *testing.T) {
|
|
|
|
id := Gen()
|
|
|
|
if len(id.String()) != 4 {
|
|
|
|
t.Errorf("expecting 4 bytes, got %d", len(id.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GenTinD_With_VaryingSizes(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
size int
|
|
|
|
runes []rune
|
|
|
|
expectedRuneSize int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
size: 4,
|
|
|
|
runes: []rune("abcdefghijklmnopqrstuvwxyz"),
|
|
|
|
expectedRuneSize: 26,
|
|
|
|
}, {
|
|
|
|
size: 8,
|
|
|
|
runes: []rune("HGHHGHGJJHSHGABBJEBJBE"),
|
|
|
|
expectedRuneSize: 7,
|
|
|
|
}, {
|
|
|
|
size: 8,
|
|
|
|
runes: []rune("😀😡🤕🎃🤚🫲🙀😭"),
|
|
|
|
expectedRuneSize: 8,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, v := range tests {
|
2024-02-24 20:16:04 +00:00
|
|
|
tt := New().WithRuneset(v.runes).WithSize(v.size).Gen()
|
2024-02-02 08:04:48 +00:00
|
|
|
if len(tt.Runes()) != v.size {
|
|
|
|
t.Errorf("config set size to %d, but generated an id of size %d. id: %s", v.size, len([]rune(tt.String())), tt.String())
|
|
|
|
}
|
|
|
|
if tt.config.runesize != v.expectedRuneSize {
|
|
|
|
t.Errorf("expected runsize of %d, got runesize of %d", len(v.runes), tt.config.runesize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 04:47:26 +00:00
|
|
|
func checkCollisions(tindSize int) {
|
2024-02-02 08:04:48 +00:00
|
|
|
ids := make(map[string]struct{})
|
|
|
|
collided := false
|
2024-02-24 20:16:04 +00:00
|
|
|
tc := New().WithSize(tindSize)
|
2024-02-02 08:04:48 +00:00
|
|
|
var iters uint64
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
log.Printf("Starting check with size %d", tindSize)
|
|
|
|
for !collided {
|
|
|
|
iters++
|
|
|
|
nt := tc.Gen()
|
|
|
|
if _, ok := ids[nt.String()]; ok {
|
|
|
|
collided = true
|
|
|
|
} else {
|
|
|
|
ids[nt.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
// Print a message every 2000000 iterations saying we are still working
|
|
|
|
if iters%2000000 == 0 {
|
|
|
|
log.Printf("Still no collions on size %d after %d iterations", tindSize, iters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("Collision found after %v and %d iterations with size of %d", time.Since(start), iters, tindSize)
|
|
|
|
}
|
2024-02-24 04:47:26 +00:00
|
|
|
|
|
|
|
func Test_GenerateConfigFromByteSlice(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
bytes []byte
|
|
|
|
expectedSize int
|
|
|
|
expectedMod byte
|
|
|
|
runeset []rune
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Default runeset with size of 10",
|
|
|
|
bytes: []byte("0123456789"),
|
|
|
|
expectedSize: 10,
|
|
|
|
expectedMod: 51,
|
|
|
|
runeset: []rune(defaultRunes),
|
|
|
|
},
|
|
|
|
{
|
2024-02-24 20:16:04 +00:00
|
|
|
name: "Custom runeset of 8 runes with size of 14",
|
|
|
|
bytes: []byte("01234567890123"),
|
2024-02-24 04:47:26 +00:00
|
|
|
expectedSize: 14,
|
2024-02-24 20:16:04 +00:00
|
|
|
expectedMod: 7,
|
|
|
|
runeset: []rune("aBcDeFgH"),
|
2024-02-24 04:47:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, v := range tests {
|
|
|
|
t.Log(v.name)
|
2024-02-24 20:16:04 +00:00
|
|
|
tt := NewFrom(v.bytes).WithRuneset(v.runeset)
|
2024-02-24 04:47:26 +00:00
|
|
|
if tt.mod != v.expectedMod {
|
|
|
|
t.Errorf("expected mod %d, but got %d", v.expectedMod, tt.mod)
|
|
|
|
}
|
|
|
|
if tt.size != v.expectedSize {
|
|
|
|
t.Errorf("expected size of %d, got %d", v.expectedSize, tt.size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|