1 /* Apache License, Version 2.0 */
2 
3 #include "testing/testing.h"
4 
5 #include "BLI_hash_mm2a.h"
6 
7 /* Note: Reference results are taken from reference implementation
8  * (cpp code, CMurmurHash2A variant):
9  * https://smhasher.googlecode.com/svn-history/r130/trunk/MurmurHash2.cpp
10  */
11 
TEST(hash_mm2a,MM2ABasic)12 TEST(hash_mm2a, MM2ABasic)
13 {
14   BLI_HashMurmur2A mm2;
15 
16   const char *data = "Blender";
17 
18   BLI_hash_mm2a_init(&mm2, 0);
19   BLI_hash_mm2a_add(&mm2, (const unsigned char *)data, strlen(data));
20 #ifdef __LITTLE_ENDIAN__
21   EXPECT_EQ(BLI_hash_mm2a_end(&mm2), 1633988145);
22 #else
23   EXPECT_EQ(BLI_hash_mm2a_end(&mm2), 959283772);
24 #endif
25 }
26 
TEST(hash_mm2a,MM2AConcatenateStrings)27 TEST(hash_mm2a, MM2AConcatenateStrings)
28 {
29   BLI_HashMurmur2A mm2;
30   uint32_t hash;
31 
32   const char *data1 = "Blender";
33   const char *data2 = " is ";
34   const char *data3 = "FaNtAsTiC";
35   const char *data123 = "Blender is FaNtAsTiC";
36 
37   BLI_hash_mm2a_init(&mm2, 0);
38   BLI_hash_mm2a_add(&mm2, (const unsigned char *)data1, strlen(data1));
39   BLI_hash_mm2a_add(&mm2, (const unsigned char *)data2, strlen(data2));
40   BLI_hash_mm2a_add(&mm2, (const unsigned char *)data3, strlen(data3));
41   hash = BLI_hash_mm2a_end(&mm2);
42   BLI_hash_mm2a_init(&mm2, 0);
43   BLI_hash_mm2a_add(&mm2, (const unsigned char *)data123, strlen(data123));
44 #ifdef __LITTLE_ENDIAN__
45   EXPECT_EQ(hash, 1545105348);
46 #else
47   EXPECT_EQ(hash, 2604964730);
48 #endif
49   EXPECT_EQ(BLI_hash_mm2a_end(&mm2), hash);
50 }
51 
TEST(hash_mm2a,MM2AIntegers)52 TEST(hash_mm2a, MM2AIntegers)
53 {
54   BLI_HashMurmur2A mm2;
55   uint32_t hash;
56 
57   const int ints[4] = {1, 2, 3, 4};
58 
59   BLI_hash_mm2a_init(&mm2, 0);
60   BLI_hash_mm2a_add_int(&mm2, ints[0]);
61   BLI_hash_mm2a_add_int(&mm2, ints[1]);
62   BLI_hash_mm2a_add_int(&mm2, ints[2]);
63   BLI_hash_mm2a_add_int(&mm2, ints[3]);
64   hash = BLI_hash_mm2a_end(&mm2);
65   BLI_hash_mm2a_init(&mm2, 0);
66   BLI_hash_mm2a_add(&mm2, (const unsigned char *)ints, sizeof(ints));
67   /* Yes, same hash here on little and big endian. */
68 #ifdef __LITTLE_ENDIAN__
69   EXPECT_EQ(hash, 405493096);
70 #else
71   EXPECT_EQ(hash, 405493096);
72 #endif
73   EXPECT_EQ(BLI_hash_mm2a_end(&mm2), hash);
74 }
75