github.com/sandwich-go/boost@v1.3.29/xhash/nhash/jenkins/jenkins_test.go (about) 1 // Copyright © 2014 Lawrence E. Bakst. All rights reserved. 2 package jenkins_test 3 4 //import "flag" 5 import ( 6 "fmt" 7 "testing" 8 "time" 9 "unsafe" 10 11 "github.com/sandwich-go/boost/misc/hrff" 12 "github.com/sandwich-go/boost/xhash/nhash/jenkins" 13 ) 14 15 //import "math" 16 //import "math/rand" 17 //import "runtime" 18 19 func stu(s string) []uint32 { 20 //fmt.Printf("stu: s=%q\n", s) 21 l := (len(s) + 3) / 4 22 d := make([]uint32, l, l) 23 d = d[0:0] 24 b := ([]byte)(s) 25 //fmt.Printf("b=%x\n", b) 26 for i := 0; i < l; i++ { 27 t := *(*uint32)(unsafe.Pointer(&b[i*4])) 28 //fmt.Printf("t=%x \n", t) 29 d = append(d, t) 30 } 31 //fmt.Printf("stu: len(s)=%d, len(d)=%d, d=%x\n", len(s), len(d), d) 32 return d 33 } 34 35 func tdiff(begin, end time.Time) time.Duration { 36 d := end.Sub(begin) 37 return d 38 } 39 40 func TestCheck(t *testing.T) { 41 jenkins.Check() 42 } 43 44 func TestBasic(t *testing.T) { 45 46 q := "This is the time for all good men to come to the aid of their country..." 47 //qq := []byte{"xThis is the time for all good men to come to the aid of their country..."} 48 //qqq := []byte{"xxThis is the time for all good men to come to the aid of their country..."} 49 //qqqq[] := []byte{"xxxThis is the time for all good men to come to the aid of their country..."} 50 51 u := stu(q) 52 fmt.Printf("len(q)=%d, len(u)=%d\n", len(q), len(u)) 53 h1 := jenkins.HashWordsLen(u, 13, 0) 54 fmt.Printf("%08x, %0x8, %08x\n", h1, h1, h1) 55 56 b, c := uint32(0), uint32(0) 57 c, b = jenkins.HashString("", c, b) 58 //fmt.Printf("%08x, %08x\n", c, b) 59 if c != 0xdeadbeef || b != 0xdeadbeef { 60 t.Logf("c=0x%x != 0xdeadbeef || b=0x%x != 0xdeadbeef\n", c, b) 61 t.FailNow() 62 } 63 64 b, c = 0xdeadbeef, 0 65 c, b = jenkins.HashString("", c, b) 66 //fmt.Printf("%08x, %08x\n", c, b) // bd5b7dde deadbeef 67 if c != 0xbd5b7dde || b != 0xdeadbeef { 68 t.Logf("c=0x%x != 0xbd5b7dde || b=0x%x != 0xdeadbeef\n", c, b) 69 t.FailNow() 70 } 71 72 b, c = 0xdeadbeef, 0xdeadbeef 73 c, b = jenkins.HashString("", c, b) 74 //fmt.Printf("%08x, %08x\n", c, b) // 9c093ccd bd5b7dde 75 if c != 0x9c093ccd || b != 0xbd5b7dde { 76 t.Logf("c=0x%x != 0x9c093ccd || b=0x%x != 0xbd5b7dde\n", c, b) 77 t.FailNow() 78 } 79 80 b, c = 0, 0 81 c, b = jenkins.HashString("Four score and seven years ago", c, b) 82 //fmt.Printf("%08x, %08x\n", c, b) // 17770551 ce7226e6 83 if c != 0x17770551 || b != 0xce7226e6 { 84 t.Logf("c=0x%x != 0x17770551 || b=0x%x != 0xce7226e6\n", c, b) 85 t.FailNow() 86 } 87 88 b, c = 1, 0 89 c, b = jenkins.HashString("Four score and seven years ago", c, b) 90 //fmt.Printf("%08x, %08x\n", c, b) // e3607cae bd371de4 91 if c != 0xe3607cae || b != 0xbd371de4 { 92 t.Logf("c=0x%x != 0xe3607cae || b=0x%x != 0xbd371de4\n", c, b) 93 t.FailNow() 94 } 95 96 b, c = 0, 1 97 c, b = jenkins.HashString("Four score and seven years ago", c, b) 98 //fmt.Printf("%08x, %08x\n", c, b) // cd628161 6cbea4b3 99 if c != 0xcd628161 || b != 0x6cbea4b3 { 100 t.Logf("c=0x%x != 0xcd628161 || b=0x%x != 0x6cbea4b3\n", c, b) 101 t.FailNow() 102 } 103 104 } 105 106 func TestHash32v(t *testing.T) { 107 k := make([]byte, 30, 30) 108 seed := uint32(0) 109 for i := 0; i < len(k); i++ { 110 h := jenkins.Hash232(k[:i], seed) 111 fmt.Printf("i=%03d, h=0x%08x, k=%v\n", i, h, k[:i]) // 0x8965bbe9 112 } 113 } 114 115 func TestHash32(t *testing.T) { 116 k := make([]byte, 4, 4) 117 k = k[:] 118 seed := uint32(0) 119 h := jenkins.Hash232(k, seed) 120 fmt.Printf("k=%v, h=0x%08x\n", k, h) // 0x8965bbe9 121 } 122 123 func TestHash64(t *testing.T) { 124 k := make([]byte, 24, 24) 125 k = k[:] 126 seed := uint64(0) 127 h := jenkins.Hash264(k, seed) 128 fmt.Printf("k=%v, h=0x%016x\n", k, h) // 0x74882dd69da95fae 129 } 130 131 func BenchmarkJenkins332(b *testing.B) { 132 //tmp := make([]byte, 4, 4) 133 us := make([]uint32, 1) 134 start := time.Now() 135 b.SetBytes(int64(b.N * 4)) 136 for i := 1; i <= b.N; i++ { 137 us[0] = uint32(i) 138 //tmp[0], tmp[1], tmp[2], tmp[3] = byte(key&0xFF), byte((key>>8)&0xFF), byte((key>>16)&0xFF), byte((key>>24)&0xFF) 139 jenkins.HashWords332(us, 0) 140 } 141 stop := time.Now() 142 dur := tdiff(start, stop) 143 hsec := hrff.Float64{(float64(b.N) / dur.Seconds()), "hashes/sec"} 144 fmt.Printf("bench: %h\n", hsec) 145 bsec := hrff.Float64{(float64(b.N) * 4 / dur.Seconds()), "B/sec"} 146 fmt.Printf("bench: %h\n", bsec) 147 } 148 149 func BenchmarkJenkins232(b *testing.B) { 150 bs := make([]byte, 4, 4) 151 start := time.Now() 152 b.SetBytes(int64(b.N * 4)) 153 for i := 1; i <= b.N; i++ { 154 bs[0], bs[1], bs[2], bs[3] = byte(i)&0xFF, (byte(i)>>8)&0xFF, (byte(i)>>16)&0xFF, (byte(i)>>24)&0xFF 155 //tmp[0], tmp[1], tmp[2], tmp[3] = byte(key&0xFF), byte((key>>8)&0xFF), byte((key>>16)&0xFF), byte((key>>24)&0xFF) 156 jenkins.Hash232(bs, 0) 157 } 158 stop := time.Now() 159 dur := tdiff(start, stop) 160 hsec := hrff.Float64{(float64(b.N) / dur.Seconds()), "hashes/sec"} 161 fmt.Printf("bench: %h\n", hsec) 162 bsec := hrff.Float64{(float64(b.N) * 4 / dur.Seconds()), "B/sec"} 163 fmt.Printf("bench: %h\n", bsec) 164 } 165 166 func BenchmarkJenkins264Bytes4(b *testing.B) { 167 bs := make([]byte, 4, 4) 168 start := time.Now() 169 b.SetBytes(int64(b.N * 4)) 170 for i := 1; i <= b.N; i++ { 171 bs[0], bs[1], bs[2], bs[3] = byte(i)&0xFF, (byte(i)>>8)&0xFF, (byte(i)>>16)&0xFF, (byte(i)>>24)&0xFF 172 //tmp[0], tmp[1], tmp[2], tmp[3] = byte(key&0xFF), byte((key>>8)&0xFF), byte((key>>16)&0xFF), byte((key>>24)&0xFF) 173 jenkins.Hash264(bs, 0) 174 } 175 stop := time.Now() 176 dur := tdiff(start, stop) 177 hsec := hrff.Float64{(float64(b.N) / dur.Seconds()), "hashes/sec"} 178 fmt.Printf("bench: %h\n", hsec) 179 bsec := hrff.Float64{(float64(b.N) * 4 / dur.Seconds()), "B/sec"} 180 fmt.Printf("bench: %h\n", bsec) 181 } 182 183 func BenchmarkJenkins264Bytes24(b *testing.B) { 184 bs := make([]byte, 24, 24) 185 start := time.Now() 186 b.SetBytes(int64(b.N * 4)) 187 for i := 1; i <= b.N; i++ { 188 bs[0], bs[1], bs[2], bs[3] = byte(i)&0xFF, (byte(i)>>8)&0xFF, (byte(i)>>16)&0xFF, (byte(i)>>24)&0xFF 189 bs[4], bs[5], bs[6], bs[7] = bs[0], bs[1], bs[2], bs[3] 190 bs[8], bs[9], bs[10], bs[11], bs[12], bs[13], bs[14], bs[15] = bs[0], bs[1], bs[2], bs[3], bs[4], bs[5], bs[6], bs[7] 191 bs[16], bs[17], bs[18], bs[19], bs[20], bs[21], bs[22], bs[23] = bs[0], bs[1], bs[2], bs[3], bs[4], bs[5], bs[6], bs[7] 192 jenkins.Hash264(bs, 0) 193 } 194 stop := time.Now() 195 dur := tdiff(start, stop) 196 hsec := hrff.Float64{(float64(b.N) / dur.Seconds()), "hashes/sec"} 197 fmt.Printf("bench: %h\n", hsec) 198 bsec := hrff.Float64{(float64(b.N) * 4 / dur.Seconds()), "B/sec"} 199 fmt.Printf("bench: %h\n", bsec) 200 } 201 202 /* 203 func main() { 204 q := "This is the time for all good men to come to the aid of their country..." 205 //qq := []byte{"xThis is the time for all good men to come to the aid of their country..."} 206 //qqq := []byte{"xxThis is the time for all good men to come to the aid of their country..."} 207 //qqqq[] := []byte{"xxxThis is the time for all good men to come to the aid of their country..."} 208 209 u := stu(q) 210 h1 := hashword(u, (len(q)-1)/4, 13) 211 h2 := hashword(u, (len(q)-5)/4, 13) 212 h3 := hashword(u, (len(q)-9)/4, 13) 213 fmt.Printf("%08x, %0x8, %08x\n", h1, h2, h3) 214 215 216 } 217 */