tind/tind_test.go

112 lines
2.8 KiB
Go

package tind
import (
"testing"
"time"
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 {
tt := New().WithRuneset(v.runes).WithSize(v.size).Gen()
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
tc := New().WithSize(tindSize)
var iters uint64
start := time.Now()
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 {
log.Info("test.checkCollisions").Uint64("iterations", iters).Int("size", tindSize).Msg("Still no collions")
}
}
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),
},
{
name: "Custom runeset of 8 runes with size of 14",
bytes: []byte("01234567890123"),
expectedSize: 14,
expectedMod: 7,
runeset: []rune("aBcDeFgH"),
},
}
for _, v := range tests {
t.Log(v.name)
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)
}
}
}