79 lines
2.0 KiB
Go
79 lines
2.0 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 Test_Generate_With_Collisions(t *testing.T) {
|
||
|
// log.Println("Test TinD for collisions, base on number of iterations and time")
|
||
|
//
|
||
|
// maxSize := 16
|
||
|
// for size := 4; size < maxSize; size++ {
|
||
|
// CheckCollisions(size)
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
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)
|
||
|
}
|