| 1 | /* Copyright (c) 2013, 2026, 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 designed to work 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 either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include "my_inttypes.h" |
| 28 | #include "my_murmur3.h" |
| 29 | #include "template_utils.h" |
| 30 | |
| 31 | /* |
| 32 | Putting everything in a namespace prevents any (unintentional) |
| 33 | name clashes with the code under test. |
| 34 | */ |
| 35 | |
| 36 | namespace murmur3_unittest { |
| 37 | |
| 38 | /* Simple test checking that hash for fixed key has correct value. */ |
| 39 | |
| 40 | TEST(Murmur3, Basic) { |
| 41 | const char *str = "To be, or not to be, that is the question;"; |
| 42 | |
| 43 | uint hash = murmur3_32(pointer_cast<const uchar *>(str), strlen(str), 0); |
| 44 | EXPECT_EQ(2385370181U, hash); |
| 45 | } |
| 46 | |
| 47 | /* Test for empty key. */ |
| 48 | |
| 49 | TEST(Murmur3, Empty) { |
| 50 | uint hash = murmur3_32(nullptr, 0, 0); |
| 51 | EXPECT_EQ(0U, hash); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | Test that shows that when we hash zero-keys of different length we |
| 56 | get different results. Our my_hash_sort_bin is not good at that. |
| 57 | */ |
| 58 | |
| 59 | TEST(Murmur3, Zeroes) { |
| 60 | uchar buff[32]; |
| 61 | memset(buff, 0, sizeof(buff)); |
| 62 | |
| 63 | uint hash1 = murmur3_32(buff, sizeof(buff) / 2, 0); |
| 64 | uint hash2 = murmur3_32(buff, sizeof(buff), 0); |
| 65 | EXPECT_NE(hash1, hash2); |
| 66 | } |
| 67 | |
| 68 | /* Test that seed matters. */ |
| 69 | |
| 70 | TEST(Murmur3, Seed) { |
| 71 | const char *str = "Whether 'tis nobler in the mind to suffer"; |
| 72 | |
| 73 | uint hash1 = murmur3_32(pointer_cast<const uchar *>(str), strlen(str), 0); |
| 74 | uint hash2 = murmur3_32(pointer_cast<const uchar *>(str), strlen(str), 1); |
| 75 | EXPECT_NE(hash1, hash2); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | Test for bug #16396598 "MDL HASH CAN STILL BE CONCURRENCY BOTTLENECK". |
| 80 | Hashes for 8 keys from the bug report should have sufficiently different |
| 81 | lower bits, so corresponding MDL objects won't fall into the same MDL map |
| 82 | partitions. |
| 83 | */ |
| 84 | |
| 85 | TEST(Murmur3, Bug16396598) { |
| 86 | const char keys[8][14] = {"\002test\000sbtest1", "\002test\000sbtest2", |
| 87 | "\002test\000sbtest3", "\002test\000sbtest4", |
| 88 | "\002test\000sbtest5", "\002test\000sbtest6", |
| 89 | "\002test\000sbtest7", "\002test\000sbtest8"}; |
| 90 | /* Array for number of keys falling into n-th bucket. */ |
| 91 | uint buckets[8]; |
| 92 | int i; |
| 93 | |
| 94 | memset(buckets, 0, sizeof(buckets)); |
| 95 | |
| 96 | for (i = 0; i < 8; ++i) |
| 97 | buckets[murmur3_32((const uchar *)keys[i], sizeof(keys[0]), 0) % 8]++; |
| 98 | |
| 99 | /* |
| 100 | It is OK for a few keys to fall into the same bucket. |
| 101 | But not for the half of the keys. |
| 102 | */ |
| 103 | for (i = 0; i < 8; ++i) EXPECT_GT(4U, buckets[i]); |
| 104 | } |
| 105 | |
| 106 | } // namespace murmur3_unittest |