leb.io/aeshash@v0.1.2/aeshash_test.go (about) 1 package aeshash 2 3 import ( 4 "testing" 5 ) 6 7 func TestUnseededHash(t *testing.T) { 8 m := map[uint64]struct{}{} 9 for i := 0; i < 1000; i++ { 10 h := NewAES(uint64(i)) 11 m[h.Sum64()] = struct{}{} 12 } 13 if len(m) < 900 { 14 t.Errorf("empty hash not sufficiently random: got %d, want 1000", len(m)) 15 } 16 } 17 18 func TestSeededHash(t *testing.T) { 19 m := map[uint64]struct{}{} 20 for i := 0; i < 1000; i++ { 21 h := NewAES(1) 22 m[h.Sum64()] = struct{}{} 23 } 24 if len(m) != 1 { 25 t.Errorf("seeded hash is random: got %d, want 1", len(m)) 26 } 27 } 28 29 /* 30 func TestFirstTenHashes(t *testing.T) { 31 var firstTenHashes = []uint64{ 32 0x54de6ee3c89ad535, 33 0x54de6ee3c89ad535, 34 0xd569b4f2ca17ba57, 35 0x02e52663b7589218, 36 0xbaba7ca05ac2c5e8, 37 0xd2446078b660d417, 38 0x89485dc9a8a45ce3, 39 0xd72a00044a27326f, 40 0x4af060bd80b05767, 41 0xb457cd9b9b95cdbf, 42 0x8782428a7c5cd1be, 43 } 44 */ 45 46 func TestHash64(t *testing.T) { 47 m := map[uint64]struct{}{} 48 for i := 0; i < 1000; i++ { 49 h := NewAES(1) 50 m[h.Sum64()] = struct{}{} 51 } 52 if len(m) != 1 { 53 t.Errorf("seeded hash is random: got %d, want 1", len(m)) 54 } 55 } 56 57 func benchmarkStdSize(b *testing.B, size int) { 58 h := NewAES(1) 59 buf := make([]byte, size) 60 b.SetBytes(int64(size)) 61 b.ResetTimer() 62 63 for i := 0; i < b.N; i++ { 64 h.Reset() 65 h.Write(buf) 66 h.Sum64() 67 } 68 } 69 70 func benchmarkSize(b *testing.B, size int) { 71 buf := make([]byte, size) 72 b.SetBytes(int64(size)) 73 b.ResetTimer() 74 75 for i := 0; i < b.N; i++ { 76 Hash(buf, uint64(i)) 77 } 78 } 79 80 func BenchmarkHash8Bytes(b *testing.B) { 81 benchmarkSize(b, 8) 82 } 83 84 func BenchmarkHash320Bytes(b *testing.B) { 85 benchmarkSize(b, 320) 86 } 87 88 func BenchmarkHash1K(b *testing.B) { 89 benchmarkSize(b, 1024) 90 } 91 92 func BenchmarkHash8K(b *testing.B) { 93 benchmarkSize(b, 8192) 94 } 95 96 func BenchmarkHash32(b *testing.B) { 97 b.ResetTimer() 98 99 for i := 0; i < b.N; i++ { 100 Hash32(uint32(i), uint64(0)) 101 } 102 } 103 104 func BenchmarkHash64(b *testing.B) { 105 b.ResetTimer() 106 107 for i := 0; i < b.N; i++ { 108 Hash64(uint64(i), uint64(0)) 109 } 110 }