1 /* Copyright (c) 2013, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 
24 // First include (the generated) my_config.h, to get correct platform defines.
25 #include "my_config.h"
26 #include <gtest/gtest.h>
27 
28 #include "my_murmur3.h"
29 
30 /*
31   Putting everything in a namespace prevents any (unintentional)
32   name clashes with the code under test.
33 */
34 
35 namespace murmur3_unittest {
36 
37 /* Simple test checking that hash for fixed key has correct value. */
38 
TEST(Murmur3,Basic)39 TEST(Murmur3, Basic)
40 {
41   const char *str= "To be, or not to be, that is the question;";
42 
43   uint hash= murmur3_32((uchar*)str, strlen(str), 0);
44   EXPECT_EQ(2385370181U, hash);
45 }
46 
47 
48 /* Test for empty key. */
49 
TEST(Murmur3,Empty)50 TEST(Murmur3, Empty)
51 {
52   uint hash= murmur3_32(NULL, 0, 0);
53   EXPECT_EQ(0U, hash);
54 }
55 
56 
57 /*
58   Test that shows that when we hash zero-keys of different length we
59   get different results. Our my_hash_sort_bin is not good at that.
60 */
61 
TEST(Murmur3,Zeroes)62 TEST(Murmur3, Zeroes)
63 {
64   uchar buff[32];
65   memset(buff, 0, sizeof(buff));
66 
67   uint hash1= murmur3_32(buff, sizeof(buff)/2, 0);
68   uint hash2= murmur3_32(buff, sizeof(buff), 0);
69   EXPECT_NE(hash1, hash2);
70 }
71 
72 
73 /* Test that seed matters. */
74 
TEST(Murmur3,Seed)75 TEST(Murmur3, Seed)
76 {
77   const char *str= "Whether 'tis nobler in the mind to suffer";
78 
79   uint hash1= murmur3_32((uchar *)str, strlen(str), 0);
80   uint hash2= murmur3_32((uchar *)str, strlen(str), 1);
81   EXPECT_NE(hash1, hash2);
82 }
83 
84 
85 /*
86   Test for bug #16396598 "MDL HASH CAN STILL BE CONCURRENCY BOTTLENECK".
87   Hashes for 8 keys from the bug report should have sufficiently different
88   lower bits, so corresponding MDL objects won't fall into the same MDL map
89   partitions.
90 */
91 
TEST(Murmur3,Bug16396598)92 TEST(Murmur3, Bug16396598)
93 {
94   const char keys[8][14]= {"\002test\000sbtest1", "\002test\000sbtest2",
95                            "\002test\000sbtest3", "\002test\000sbtest4",
96                            "\002test\000sbtest5", "\002test\000sbtest6",
97                            "\002test\000sbtest7", "\002test\000sbtest8" };
98   /* Array for number of keys falling into n-th bucket. */
99   uint buckets[8];
100   int i;
101 
102   memset(buckets, 0, sizeof(buckets));
103 
104   for (i= 0; i < 8; ++i)
105     buckets[murmur3_32((const uchar *)keys[i], sizeof(keys[0]), 0) % 8]++;
106 
107   /*
108     It is OK for a few keys to fall into the same bucket.
109     But not for the half of the keys.
110   */
111   for (i= 0; i < 8; ++i)
112     EXPECT_GT(4U, buckets[i]);
113 }
114 
115 }  // namespace
116