49 lines
906 B
Go
Raw Normal View History

2023-07-22 15:02:22 +00:00
package hash_test
import (
"bytes"
"testing"
"git.akyoto.dev/go/hash"
)
2023-07-22 22:46:39 +00:00
var hashes = map[uint64][]byte{}
2023-07-22 15:02:22 +00:00
2023-07-22 22:46:39 +00:00
func TestTinyCollisions(t *testing.T) {
2023-07-22 15:02:22 +00:00
for size := 1; size < 8; size++ {
tmp := make([]byte, size)
index := 0
2023-07-22 22:46:39 +00:00
for i := 0; i < 256; i++ {
2023-07-22 15:02:22 +00:00
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)
}
2023-07-22 22:46:39 +00:00
save := make([]byte, size)
copy(save, tmp)
hashes[h] = save
2023-07-22 15:02:22 +00:00
index = (index + 1) % size
}
}
}
func TestZeroedCollisions(t *testing.T) {
2023-07-22 22:46:39 +00:00
zero := make([]byte, 8192)
2023-07-22 15:02:22 +00:00
2023-07-22 22:46:39 +00:00
for size := 1; size <= len(zero); size++ {
tmp := zero[:size]
2023-07-22 15:02:22 +00:00
h := hash.Bytes(tmp)
previous, found := hashes[h]
if found && !bytes.Equal(tmp, previous) {
2023-07-22 22:46:39 +00:00
t.Fatalf("collision between %v and %v:\nhash %064b", previous, tmp, h)
2023-07-22 15:02:22 +00:00
}
hashes[h] = tmp
}
}