当前位置: 首页>>代码示例>>Golang>>正文


Golang Hash32.Sum方法代码示例

本文整理汇总了Golang中hash.Hash32.Sum方法的典型用法代码示例。如果您正苦于以下问题:Golang Hash32.Sum方法的具体用法?Golang Hash32.Sum怎么用?Golang Hash32.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在hash.Hash32的用法示例。


在下文中一共展示了Hash32.Sum方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestRef

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)
		}
	}
}
开发者ID:twmb,项目名称:murmur3,代码行数:55,代码来源:murmur_test.go

示例2: becnchmark

func becnchmark(b *testing.B, h hash.Hash32, n int64) {
	b.SetBytes(n)
	data := make([]byte, n)
	for i := range data {
		data[i] = byte(i)
	}
	in := make([]byte, 0, h.Size())
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		h.Reset()
		h.Write(data)
		h.Sum(in)
	}
}
开发者ID:wangtuanjie,项目名称:crc32,代码行数:15,代码来源:crc32_test.go

示例3: benchmark

func benchmark(b *testing.B, h hash.Hash32, n, alignment int64) {
	b.SetBytes(n)
	data := make([]byte, n+alignment)
	data = data[alignment:]
	for i := range data {
		data[i] = byte(i)
	}
	in := make([]byte, 0, h.Size())

	// Warm up
	h.Reset()
	h.Write(data)
	h.Sum(in)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		h.Reset()
		h.Write(data)
		h.Sum(in)
	}
}
开发者ID:achanda,项目名称:go,代码行数:21,代码来源:crc32_test.go

示例4: testGolden

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)
		}

	}
}
开发者ID:rakitzis,项目名称:dgohash,代码行数:39,代码来源:hashes_test.go

示例5: doHash

func doHash(in string) string {
	var h hash.Hash32 = murmur3.New32()
	h.Write([]byte(in))
	return hex.EncodeToString(h.Sum(nil))
}
开发者ID:leochencipher,项目名称:tyk,代码行数:5,代码来源:storage_handlers.go

示例6:

	Describe("Sum32", func() {

		It("defaults to 0", func() {
			Expect(jhash.Sum32()).To(Equal(uint32(0)))
		})

		It("sums hash", func() {
			jhash.Write(key)
			Expect(jhash.Sum32()).To(Equal(uint32(884782484)))
		})

	})

	Describe("Sum", func() {

		It("default 0 hash byte returned", func() {
			expected := []byte{0x41, 0x70, 0x70, 0x6c, 0x65, 0x0, 0x0, 0x0, 0x0}
			Expect(jhash.Sum(key)).To(Equal(expected))
		})

		It("returns sum byte array", func() {
			jhash.Write(key)
			expected := []byte{0x41, 0x70, 0x70, 0x6c, 0x65, 0x34, 0xbc, 0xb5, 0x94}
			Expect(jhash.Sum(key)).To(Equal(expected))
		})

	})

})
开发者ID:andradeandrey,项目名称:go-ipfs,代码行数:29,代码来源:jenkins_test.go


注:本文中的hash.Hash32.Sum方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。