tind/tind_test.go

112 lines
2.8 KiB
Go
Raw Normal View History

package tind
import (
"testing"
"time"
2024-04-07 18:27:05 +00:00
log "git.staur.ca/stobbsm/simplelog"
)
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-24 22:17:44 +00:00
t.Logf("Generated string: %s", tt.String())
t.Logf("Generated bytes: %v", tt.Bytes())
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)
}
}
}
func checkCollisions(tindSize int) {
ids := make(map[string]struct{})
collided := false
2024-02-24 20:16:04 +00:00
tc := New().WithSize(tindSize)
var iters uint64
start := time.Now()
2024-04-07 17:38:55 +00:00
log.Info("test.checkCollisions").Int("size", tindSize).Msg("Starting collision check")
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 {
2024-04-07 18:27:05 +00:00
log.Info("test.checkCollisions").Uint64("iterations", iters).Int("size", tindSize).Msg("Still no collions")
}
}
2024-04-07 18:27:05 +00:00
log.Info("test.checkCollisions").
Uint64("iterations", iters).
Int("size", tindSize).
Dur("duration", time.Since(start)).
Msg("Collision found")
}
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"),
expectedSize: 14,
2024-02-24 20:16:04 +00:00
expectedMod: 7,
runeset: []rune("aBcDeFgH"),
},
}
for _, v := range tests {
t.Log(v.name)
2024-02-24 20:16:04 +00:00
tt := NewFrom(v.bytes).WithRuneset(v.runeset)
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)
}
}
}