func testIncremental(t *testing.T, h hash.Hash32, result uint32, which string) { h.Reset() var parts = []string{ "h", "ell", "o", "he", "llo", "hellohello", } for _, p := range parts { l, _ := h.Write([]byte(p)) if l != len(p) { t.Errorf("Write(%d bytes) = %d, want %d\n", len(p), l, len(p)) } } h32 := h.Sum32() if h32 != result { t.Errorf("%s: incremental failed: got %08x", which, h32) } h.Reset() h.Write([]byte("hellohellohellohello")) h32 = h.Sum32() if h32 != result { t.Errorf("%s: failed: got %08x", which, h32) } }
func testIncremental(t *testing.T, h hash.Hash32, result uint32, which string) { h.Reset() h.Write([]byte("hello")) h.Write([]byte("h")) h.Write([]byte("e")) h.Write([]byte("l")) h.Write([]byte("l")) h.Write([]byte("o")) h.Write([]byte("hellohello")) h32 := h.Sum32() if h32 != result { t.Errorf("%s: incremental failed: got %08x", which, h32) } h.Reset() h.Write([]byte("hellohellohellohello")) h32 = h.Sum32() if h32 != result { t.Errorf("%s: failed: got %08x", which, h32) } }
func TestRef(t *testing.T) { for _, elem := range data { var h32 hash.Hash32 = New32() h32.Write([]byte(elem.s)) if v := h32.Sum32(); v != elem.h32 { t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32) } if v := Sum32([]byte(elem.s)); v != elem.h32 { t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32) } var h64 hash.Hash64 = New64() h64.Write([]byte(elem.s)) if v := h64.Sum64(); v != elem.h64_1 { t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1) } var h128 Hash128 = New128() h128.Write([]byte(elem.s)) if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 { t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2) } if v1, v2 := Sum128([]byte(elem.s)); v1 != elem.h64_1 || v2 != elem.h64_2 { t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2) } } }
func commonBench(b *testing.B, h hash.Hash32, golden []_Golden) { for i := 0; i < b.N; i++ { for _, g := range golden { h.Reset() h.Write([]byte(g.in)) h.Sum32() } } }
func imageHash(h hash.Hash32, dir *Directory, img *Image) int { h.Reset() h.Write([]byte(dir.RelPat())) h.Write([]byte(img.Name())) bytes, err := img.FileTime().MarshalBinary() if err == nil { h.Write(bytes) } return int(h.Sum32()) }
func TestRef(t *testing.T) { for _, elem := range data { var h32 hash.Hash32 = New32() h32.Write([]byte(elem.s)) if v := h32.Sum32(); v != elem.h32 { t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32) } var h32_byte hash.Hash32 = New32() h32_byte.Write([]byte(elem.s)) target := fmt.Sprintf("%08x", elem.h32) if p := fmt.Sprintf("%x", h32_byte.Sum(nil)); p != target { t.Errorf("'%s': %s (want %s)", elem.s, p, target) } if v := Sum32([]byte(elem.s)); v != elem.h32 { t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32) } var h64 hash.Hash64 = New64() h64.Write([]byte(elem.s)) if v := h64.Sum64(); v != elem.h64_1 { t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1) } var h64_byte hash.Hash64 = New64() h64_byte.Write([]byte(elem.s)) target = fmt.Sprintf("%016x", elem.h64_1) if p := fmt.Sprintf("%x", h64_byte.Sum(nil)); p != target { t.Errorf("Sum64: '%s': %s (want %s)", elem.s, p, target) } if v := Sum64([]byte(elem.s)); v != elem.h64_1 { t.Errorf("Sum64: '%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1) } var h128 Hash128 = New128() h128.Write([]byte(elem.s)) if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 { t.Errorf("New128: '%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2) } var h128_byte Hash128 = New128() h128_byte.Write([]byte(elem.s)) target = fmt.Sprintf("%016x%016x", elem.h64_1, elem.h64_2) if p := fmt.Sprintf("%x", h128_byte.Sum(nil)); p != target { t.Errorf("New128: '%s': %s (want %s)", elem.s, p, target) } if v1, v2 := Sum128([]byte(elem.s)); v1 != elem.h64_1 || v2 != elem.h64_2 { t.Errorf("Sum128: '%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2) } } }
// FilesPathToPartition hashes a file path to a partition. func FilesPathToPartition(h hash.Hash32, partitions []string, path string) string { if len(partitions) <= 0 { return "" } h.Reset() io.WriteString(h, path) i := h.Sum32() % uint32(len(partitions)) return partitions[i] }
// Returns shard under given key func (m ConcurrentMap) GetShard(key string) *ConcurrentMapShared { hasherAsInterface := hashPool.Get() var hasher hash.Hash32 if hasherAsInterface == nil { hasher = fnv.New32() } else { hasher = hasherAsInterface.(hash.Hash32) hasher.Reset() } hasher.Write([]byte(key)) sum := hasher.Sum32() hashPool.Put(hasher) return m[uint(sum)%uint(SHARD_COUNT)] }
func testGolden(t *testing.T, h hash.Hash32, golden []_Golden, which string) { for _, g := range golden { h.Reset() h.Write([]byte(g.in)) sum := h.Sum32() if sum != g.out { t.Errorf("%s(%s) = 0x%x want 0x%x", which, g.in, sum, g.out) } bsum := h.Sum(nil) if len(bsum) != 4 { t.Errorf("%s Sum(nil) returned %d bytes, wanted 4: %s\n", which, len(bsum), bsum) } s := binary.BigEndian.Uint32(bsum) if s != sum { t.Errorf("%s(%s).Sum(nil) = 0x%x want 0x%x", which, g.in, sum, g.out) } bsum = h.Sum([]byte{0x01, 0x02, 0x03, 0x04}) if len(bsum) != 8 { t.Errorf("%s Sum(bsum) returned %d bytes, wanted 8: %x\n", which, len(bsum), bsum) } s = binary.BigEndian.Uint32(bsum[0:]) s2 := binary.BigEndian.Uint32(bsum[4:]) if s != 0x01020304 || s2 != sum { t.Errorf("%s(%s).Sum(bsum) = %x (expected 0x01020304 %x )", which, g.in, bsum, sum) } } }
// SeriesHash returns a hash of the metric, tags pair. The hash falls in uint16 // range. func (c cmd) SeriesHash(hash hash.Hash32) int { hash.Reset() buf := c.Point() // include Metric i := bytes.IndexByte(buf, ' ') hash.Write(buf[:i]) buf = buf[i+1:] // exclude Time i = bytes.IndexByte(buf, ' ') buf = buf[i+1:] // exclude Value i = bytes.IndexByte(buf, ' ') // include Tags hash.Write(buf[i:]) // Sum sum := hash.Sum32() return int(uint16(sum)) }
// Hash32 is a convenience method for hashing a string against a hash.Hash32 func Hash32(s string, h hash.Hash32) uint32 { h.Reset() h.Write([]byte(s)) return h.Sum32() }
var key []byte BeforeEach(func() { jhash = New() key = []byte("Apple") }) Describe("New", func() { It("returns jenkhash", func() { var h *jenkhash Expect(jhash).To(BeAssignableToTypeOf(h)) }) It("initializes offset to 0", func() { Expect(jhash.Sum32()).To(Equal(uint32(0))) }) }) Describe("Write", func() { It("returns key length", func() { length, _ := jhash.Write(key) Expect(length).To(Equal(5)) }) It("has no error", func() { _, err := jhash.Write(key) Expect(err).To(BeNil()) })