tind/tind_test.go
Matthew Stobbs 198d509789 add docs
Using obsidian to add markdown formatted documentation
2024-02-23 21:47:26 -07:00

105 lines
2.6 KiB
Go

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 {
tt := NewTinDConfig().WithRuneset(v.runes).WithSize(v.size).Gen()
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 := NewTinDConfig().WithSize(tindSize)
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)
}
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 := NewConfigFrom(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)
}
}
}