github.com/fufuok/utils@v1.0.10/xhash/hash_test.go (about)

     1  package xhash
     2  
     3  import (
     4  	"encoding/hex"
     5  	"hash/maphash"
     6  	"math"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/fufuok/utils"
    11  	"github.com/fufuok/utils/assert"
    12  )
    13  
    14  var (
    15  	tmpS       = "Fufu 中 文加密/解密~&#123a"
    16  	testString = "  Fufu 中 文\u2728->?\n*\U0001F63A   "
    17  	testBytes  = []byte(testString)
    18  	seed       = maphash.MakeSeed()
    19  )
    20  
    21  func TestSha256Hex(t *testing.T) {
    22  	t.Parallel()
    23  	assert.Equal(t, "ed3772cefd8991edac6d198df7b62c224b92038e2d435a9a1e2734211e5b5e0b",
    24  		Sha256Hex(tmpS))
    25  }
    26  
    27  func TestSha512Hex(t *testing.T) {
    28  	t.Parallel()
    29  	assert.Equal(t, "c3b70022a04f57c1ad335256d2adb2aeec825c6641b2576b48f64abf1bb2c3210dff1087b9f"+
    30  		"27261e4062779e64f29fc39d555164c5a2ea6eb3fd6d8b19ed1d1",
    31  		Sha512Hex(tmpS))
    32  }
    33  
    34  func TestSha1Hex(t *testing.T) {
    35  	t.Parallel()
    36  	assert.Equal(t, "ad4ebd7a388c536ff4fcb494ffb36c047151f751",
    37  		Sha1Hex(tmpS))
    38  }
    39  
    40  func TestHmacSHA256Hex(t *testing.T) {
    41  	t.Parallel()
    42  	assert.Equal(t, "6d502095be042aab03ac7ae36dd0ca504e54eb72569547dca4e16e5de605ae7c",
    43  		HmacSHA256Hex(tmpS, "Fufu"))
    44  }
    45  
    46  func TestHmacSHA512Hex(t *testing.T) {
    47  	t.Parallel()
    48  	assert.Equal(t, "516825116f0c14c563f0fdd377729dd0a2024fb9f16f0dc81d83450e97114683ece5c8886"+
    49  		"aaa912af970b1a40505333fb16e770e6b9df1826556e5fac680782b",
    50  		HmacSHA512Hex(tmpS, "Fufu"))
    51  }
    52  
    53  func TestHmacSHA1Hex(t *testing.T) {
    54  	t.Parallel()
    55  	assert.Equal(t, "e6720f969c4aa396324845bed13e3cbf550b0d6d",
    56  		HmacSHA1Hex(tmpS, "Fufu"))
    57  }
    58  
    59  func TestMD5Hex(t *testing.T) {
    60  	for _, v := range []struct {
    61  		in, out string
    62  	}{
    63  		{"12345", "827ccb0eea8a706c4c34a16891f84e7b"},
    64  		{testString, "8d47309acf79aa15378c82475c167865"},
    65  		{"Fufu 中 文", "0ab5820207b25880bc0a1d09ed64f10c"},
    66  	} {
    67  		assert.Equal(t, v.out, MD5Hex(v.in))
    68  		assert.Equal(t, v.out, MD5BytesHex([]byte(v.in)))
    69  		assert.Equal(t, v.out, hex.EncodeToString(MD5([]byte(v.in))))
    70  	}
    71  }
    72  
    73  func TestMD5Sum(t *testing.T) {
    74  	t.Parallel()
    75  	res, _ := MD5Sum("LICENSE")
    76  	expected := []string{
    77  		// Real result
    78  		"8fad15baa71cfe5901d9ac1bbec2c56c",
    79  		// Result on github windows (LF would be replaced by CRLF, Maybe core.autocrlf is true)
    80  		"cd5c4d3bd8efa894619c1f3eab8a9174",
    81  	}
    82  	assert.Equal(t, true, utils.InStrings(expected, res))
    83  	assert.Equal(t, true, utils.InStrings(expected, MustMD5Sum("LICENSE")))
    84  }
    85  
    86  func TestHashString(t *testing.T) {
    87  	for _, v := range []struct {
    88  		in, out string
    89  	}{
    90  		{"", "14695981039346656037"},
    91  		{"12345", "16534377278781491704"},
    92  		{testString, "13467076781014605639"},
    93  		{"Fufu 中 文", "1485575821508720008"},
    94  	} {
    95  		assert.Equal(t, v.out, HashString(v.in))
    96  		assert.Equal(t, v.out, HashBytes([]byte(v.in)))
    97  	}
    98  
    99  	for _, v := range []struct {
   100  		in  string
   101  		out uint32
   102  	}{
   103  		{"", 2166136261},
   104  		{"12345", 1136836824},
   105  		{testString, 475021159},
   106  		{"Fufu 中 文", 2300112168},
   107  	} {
   108  		assert.Equal(t, v.out, HashString32(v.in))
   109  		assert.Equal(t, v.out, HashBytes32([]byte(v.in)))
   110  	}
   111  }
   112  
   113  func TestHashUint(t *testing.T) {
   114  	for _, v := range []struct {
   115  		in, out uint64
   116  	}{
   117  		{0, 12161962213042174405},
   118  		{1, 12161961113530546194},
   119  		{offset64, 5063649278745802162},
   120  		{prime64, 14714463944532698764},
   121  		{math.MaxUint64, 10157053723145373757},
   122  	} {
   123  		assert.Equal(t, v.out, HashUint64(v.in))
   124  	}
   125  
   126  	for _, v := range []struct {
   127  		in, out uint32
   128  	}{
   129  		{0, 1268118805},
   130  		{1, 1251341186},
   131  		{offset32, 2870680790},
   132  		{prime32, 2389395716},
   133  		{math.MaxUint32, 3809873841},
   134  	} {
   135  		assert.Equal(t, v.out, HashUint32(v.in))
   136  	}
   137  }
   138  
   139  func TestHashStringToInt(t *testing.T) {
   140  	assert.Equal(t, uint64(offset64), Sum64(""))
   141  	assert.Equal(t, uint32(offset32), Sum32(""))
   142  	assert.Equal(t, uint64(offset64), FnvHash(""))
   143  	assert.Equal(t, uint32(offset32), FnvHash32(""))
   144  	assert.Equal(t, uint64(13467076781014605639), Sum64(testString))
   145  	assert.Equal(t, uint32(475021159), Sum32(testString))
   146  	assert.Equal(t, uint64(13467076781014605639), FnvHash(testString))
   147  	assert.Equal(t, uint32(475021159), FnvHash32(testString))
   148  
   149  	v := MemHash(testString)
   150  	for i := 0; i < 100000; i++ {
   151  		assert.Equal(t, v, MemHash(testString))
   152  	}
   153  
   154  	v = MemHashb(testBytes)
   155  	for i := 0; i < 100000; i++ {
   156  		assert.Equal(t, v, MemHashb(testBytes))
   157  	}
   158  
   159  	v32 := MemHash32(testString)
   160  	for i := 0; i < 100000; i++ {
   161  		assert.Equal(t, v32, MemHash32(testString))
   162  	}
   163  
   164  	v32 = Djb33(testString)
   165  	for i := 0; i < 100000; i++ {
   166  		assert.Equal(t, v32, Djb33(testString))
   167  	}
   168  }
   169  
   170  func TestHashSeedString(t *testing.T) {
   171  	const numEntries = 1000
   172  	m := make(map[uint64]uint64)
   173  	for i := 0; i < numEntries; i++ {
   174  		k := HashSeedString(seed, strconv.Itoa(i))
   175  		if _, ok := m[k]; ok {
   176  			t.Fatalf("expect key %d to not exist", k)
   177  		}
   178  		m[k] = k
   179  	}
   180  	if len(m) != numEntries {
   181  		t.Fatalf("expect count of %d, but got: %d", numEntries, len(m))
   182  	}
   183  }
   184  
   185  func TestHashSeedUint64(t *testing.T) {
   186  	const numEntries = 1000
   187  	m := make(map[uint64]uint64)
   188  	for i := 0; i < numEntries; i++ {
   189  		k := HashSeedUint64(seed, uint64(i))
   190  		if _, ok := m[k]; ok {
   191  			t.Fatalf("expect key %d to not exist", k)
   192  		}
   193  		m[k] = k
   194  	}
   195  	if len(m) != numEntries {
   196  		t.Fatalf("expect count of %d, but got: %d", numEntries, len(m))
   197  	}
   198  }
   199  
   200  func BenchmarkHashString(b *testing.B) {
   201  	str := utils.RandString(20)
   202  	b.ResetTimer()
   203  	b.Run("MD5Hex", func(b *testing.B) {
   204  		for i := 0; i < b.N; i++ {
   205  			_ = MD5Hex(str)
   206  		}
   207  	})
   208  	b.Run("HashString", func(b *testing.B) {
   209  		for i := 0; i < b.N; i++ {
   210  			_ = HashString(str)
   211  		}
   212  	})
   213  }
   214  
   215  // go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkHashString
   216  // goos: linux
   217  // goarch: amd64
   218  // pkg: github.com/fufuok/utils
   219  // cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
   220  // BenchmarkHashString/MD5Hex-8             5115880               226.1 ns/op            64 B/op          2 allocs/op
   221  // BenchmarkHashString/MD5Hex-8             5377476               230.3 ns/op            64 B/op          2 allocs/op
   222  // BenchmarkHashString/MD5Hex-8             5227404               226.4 ns/op            64 B/op          2 allocs/op
   223  // BenchmarkHashString/HashString-8        11307057               102.8 ns/op            24 B/op          1 allocs/op
   224  // BenchmarkHashString/HashString-8        12567037               96.83 ns/op            24 B/op          1 allocs/op
   225  // BenchmarkHashString/HashString-8        12295094               101.1 ns/op            24 B/op          1 allocs/op
   226  
   227  func BenchmarkHash(b *testing.B) {
   228  	buf := utils.RandBytes(20)
   229  	str := utils.RandString(20)
   230  	b.ResetTimer()
   231  	b.Run("Sum64", func(b *testing.B) {
   232  		for i := 0; i < b.N; i++ {
   233  			_ = Sum64(str)
   234  		}
   235  	})
   236  	b.Run("Sum32", func(b *testing.B) {
   237  		for i := 0; i < b.N; i++ {
   238  			_ = Sum32(str)
   239  		}
   240  	})
   241  	b.Run("FnvHash", func(b *testing.B) {
   242  		for i := 0; i < b.N; i++ {
   243  			_ = FnvHash(str)
   244  		}
   245  	})
   246  	b.Run("FnvHash32", func(b *testing.B) {
   247  		for i := 0; i < b.N; i++ {
   248  			_ = FnvHash32(str)
   249  		}
   250  	})
   251  	b.Run("MemHashb", func(b *testing.B) {
   252  		for i := 0; i < b.N; i++ {
   253  			_ = MemHashb(buf)
   254  		}
   255  	})
   256  	b.Run("MemHash", func(b *testing.B) {
   257  		for i := 0; i < b.N; i++ {
   258  			_ = MemHash(str)
   259  		}
   260  	})
   261  	b.Run("MemHashb32", func(b *testing.B) {
   262  		for i := 0; i < b.N; i++ {
   263  			_ = MemHashb32(buf)
   264  		}
   265  	})
   266  	b.Run("MemHash32", func(b *testing.B) {
   267  		for i := 0; i < b.N; i++ {
   268  			_ = MemHash32(str)
   269  		}
   270  	})
   271  	b.Run("Djb33", func(b *testing.B) {
   272  		for i := 0; i < b.N; i++ {
   273  			_ = Djb33(str)
   274  		}
   275  	})
   276  	b.Run("SeedString", func(b *testing.B) {
   277  		for i := 0; i < b.N; i++ {
   278  			_ = HashSeedString(seed, str)
   279  		}
   280  	})
   281  }
   282  
   283  // go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkHash$
   284  // goos: linux
   285  // goarch: amd64
   286  // pkg: github.com/fufuok/utils
   287  // cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
   288  // BenchmarkHash/Sum64-8           53414522                22.67 ns/op            0 B/op          0 allocs/op
   289  // BenchmarkHash/Sum64-8           54474142                22.05 ns/op            0 B/op          0 allocs/op
   290  // BenchmarkHash/Sum64-8           47609222                22.30 ns/op            0 B/op          0 allocs/op
   291  // BenchmarkHash/Sum32-8           48003264                21.97 ns/op            0 B/op          0 allocs/op
   292  // BenchmarkHash/Sum32-8           56251669                21.41 ns/op            0 B/op          0 allocs/op
   293  // BenchmarkHash/Sum32-8           51219012                22.23 ns/op            0 B/op          0 allocs/op
   294  // BenchmarkHash/FnvHash-8         28918519                37.55 ns/op            0 B/op          0 allocs/op
   295  // BenchmarkHash/FnvHash-8         33735345                35.68 ns/op            0 B/op          0 allocs/op
   296  // BenchmarkHash/FnvHash-8         36855715                37.35 ns/op            0 B/op          0 allocs/op
   297  // BenchmarkHash/FnvHash32-8       35083820                32.14 ns/op            0 B/op          0 allocs/op
   298  // BenchmarkHash/FnvHash32-8       47042220                28.03 ns/op            0 B/op          0 allocs/op
   299  // BenchmarkHash/FnvHash32-8       46833836                28.16 ns/op            0 B/op          0 allocs/op
   300  // BenchmarkHash/MemHashb-8       153161814                9.470 ns/op            0 B/op          0 allocs/op
   301  // BenchmarkHash/MemHashb-8       152563350                8.079 ns/op            0 B/op          0 allocs/op
   302  // BenchmarkHash/MemHashb-8       142128412                7.964 ns/op            0 B/op          0 allocs/op
   303  // BenchmarkHash/MemHash-8        125632875                8.775 ns/op            0 B/op          0 allocs/op
   304  // BenchmarkHash/MemHash-8        150410337                8.372 ns/op            0 B/op          0 allocs/op
   305  // BenchmarkHash/MemHash-8        135773145                9.427 ns/op            0 B/op          0 allocs/op
   306  // BenchmarkHash/MemHashb32-8     146760392                7.950 ns/op            0 B/op          0 allocs/op
   307  // BenchmarkHash/MemHashb32-8     146271644                7.759 ns/op            0 B/op          0 allocs/op
   308  // BenchmarkHash/MemHashb32-8     154832995                8.686 ns/op            0 B/op          0 allocs/op
   309  // BenchmarkHash/MemHash32-8      100000000                10.13 ns/op            0 B/op          0 allocs/op
   310  // BenchmarkHash/MemHash32-8      100000000                10.30 ns/op            0 B/op          0 allocs/op
   311  // BenchmarkHash/MemHash32-8      143592832                8.360 ns/op            0 B/op          0 allocs/op
   312  // BenchmarkHash/Djb33-8           60568434                18.17 ns/op            0 B/op          0 allocs/op
   313  // BenchmarkHash/Djb33-8           75151240                18.17 ns/op            0 B/op          0 allocs/op
   314  // BenchmarkHash/Djb33-8           68428705                19.79 ns/op            0 B/op          0 allocs/op