xref: /aosp_15_r20/external/cronet/base/hash/hash_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/hash/hash.h"
6 
7 #include <string>
8 #include <vector>
9 
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace base {
13 
TEST(HashTest,DeprecatedHashFromString)14 TEST(HashTest, DeprecatedHashFromString) {
15   std::string str;
16   // Empty string (should hash to 0).
17   str = "";
18   EXPECT_EQ(0u, Hash(str));
19 
20   // Simple test.
21   str = "hello world";
22   EXPECT_EQ(2794219650u, Hash(str));
23 
24   // Change one bit.
25   str = "helmo world";
26   EXPECT_EQ(1006697176u, Hash(str));
27 
28   // Insert a null byte.
29   str = "hello  world";
30   str[5] = '\0';
31   EXPECT_EQ(2319902537u, Hash(str));
32 
33   // Test that the bytes after the null contribute to the hash.
34   str = "hello  worle";
35   str[5] = '\0';
36   EXPECT_EQ(553904462u, Hash(str));
37 
38   // Extremely long string.
39   // Also tests strings with high bit set, and null byte.
40   std::vector<char> long_string_buffer;
41   for (int i = 0; i < 4096; ++i)
42     long_string_buffer.push_back((i % 256) - 128);
43   str.assign(&long_string_buffer.front(), long_string_buffer.size());
44   EXPECT_EQ(2797962408u, Hash(str));
45 
46   // All possible lengths (mod 4). Tests separate code paths. Also test with
47   // final byte high bit set (regression test for http://crbug.com/90659).
48   // Note that the 1 and 3 cases have a weird bug where the final byte is
49   // treated as a signed char. It was decided on the above bug discussion to
50   // enshrine that behaviour as "correct" to avoid invalidating existing hashes.
51 
52   // Length mod 4 == 0.
53   str = "hello w\xab";
54   EXPECT_EQ(615571198u, Hash(str));
55   // Length mod 4 == 1.
56   str = "hello wo\xab";
57   EXPECT_EQ(623474296u, Hash(str));
58   // Length mod 4 == 2.
59   str = "hello wor\xab";
60   EXPECT_EQ(4278562408u, Hash(str));
61   // Length mod 4 == 3.
62   str = "hello worl\xab";
63   EXPECT_EQ(3224633008u, Hash(str));
64 }
65 
TEST(HashTest,DeprecatedHashFromCString)66 TEST(HashTest, DeprecatedHashFromCString) {
67   const char* str;
68   // Empty string (should hash to 0).
69   str = "";
70   EXPECT_EQ(0u, Hash(str));
71 
72   // Simple test.
73   str = "hello world";
74   EXPECT_EQ(2794219650u, Hash(str));
75 }
76 
TEST(HashTest,PersistentHashFromSpan)77 TEST(HashTest, PersistentHashFromSpan) {
78   // Empty span (should hash to 0).
79   EXPECT_EQ(0u, PersistentHash(base::span<const uint8_t>()));
80 
81   // Simple test.
82   const char* str = "hello world";
83   EXPECT_EQ(2794219650u, PersistentHash(as_byte_span(std::string(str))));
84 }
85 
TEST(HashTest,FastHash)86 TEST(HashTest, FastHash) {
87   std::string s;
88   constexpr char kEmptyString[] = "";
89   EXPECT_EQ(FastHash(s), FastHash(kEmptyString));
90 }
91 
92 }  // namespace base
93