47 lines
877 B
Go
47 lines
877 B
Go
package hash_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"git.akyoto.dev/go/hash"
|
|
)
|
|
|
|
func TestTinyCollisions(t *testing.T) {
|
|
hashes := map[uint64][]byte{}
|
|
|
|
for size := 1; size < 8; size++ {
|
|
tmp := make([]byte, size)
|
|
index := 0
|
|
|
|
for i := 0; i < 10; i++ {
|
|
tmp[index] += 1
|
|
h := hash.Bytes(tmp)
|
|
previous, found := hashes[h]
|
|
|
|
if found && !bytes.Equal(tmp, previous) {
|
|
t.Fatalf("collision between %v and %v:\nhash %064b", previous, tmp, h)
|
|
}
|
|
|
|
hashes[h] = tmp
|
|
index = (index + 1) % size
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestZeroedCollisions(t *testing.T) {
|
|
hashes := map[uint64][]byte{}
|
|
|
|
for size := 1; size <= 8192; size++ {
|
|
tmp := make([]byte, size)
|
|
h := hash.Bytes(tmp)
|
|
previous, found := hashes[h]
|
|
|
|
if found && !bytes.Equal(tmp, previous) {
|
|
t.Fatalf("collision between zeroed sizes %d and %d:\nhash %064b", len(previous), size, h)
|
|
}
|
|
|
|
hashes[h] = tmp
|
|
}
|
|
}
|