OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/hash.h" | 5 #include "base/hash.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 TEST(HashTest, String) { | 14 TEST(HashTest, String) { |
15 std::string str; | 15 std::string str; |
16 // Empty string (should hash to 0). | 16 // Empty string (should hash to 0). |
17 str = ""; | 17 str = ""; |
18 EXPECT_EQ(0u, Hash(str)); | 18 EXPECT_EQ(0u, Hash(str)); |
19 | 19 |
20 // Simple test. | 20 // Simple test. |
21 str = "hello world"; | 21 str = "hello world"; |
22 EXPECT_EQ(2794219650u, Hash(str)); | 22 EXPECT_EQ(2794219650u, Hash(str)); |
23 | 23 |
24 // Change one bit. | 24 // Change one bit. |
25 str = "helmo world"; | 25 str = "helmo world"; |
26 EXPECT_EQ(1006697176u, Hash(str)); | 26 EXPECT_EQ(1006697176u, Hash(str)); |
27 | 27 |
28 // Add a null byte. | |
Noel Gordon
2014/03/05 04:47:01
nit: maybe s/Add/Insert/ ?
Matt Giuca
2014/03/05 04:49:17
Done.
| |
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 | |
28 // Extremely long string. | 38 // Extremely long string. |
29 // Also tests strings with high bit set, and null byte. | 39 // Also tests strings with high bit set, and null byte. |
30 std::vector<char> long_string_buffer; | 40 std::vector<char> long_string_buffer; |
31 for (int i = 0; i < 4096; ++i) | 41 for (int i = 0; i < 4096; ++i) |
32 long_string_buffer.push_back((i % 256) - 128); | 42 long_string_buffer.push_back((i % 256) - 128); |
33 str.assign(&long_string_buffer.front(), long_string_buffer.size()); | 43 str.assign(&long_string_buffer.front(), long_string_buffer.size()); |
34 EXPECT_EQ(2797962408u, Hash(str)); | 44 EXPECT_EQ(2797962408u, Hash(str)); |
35 | 45 |
36 // All possible lengths (mod 4). Tests separate code paths. Also test with | 46 // All possible lengths (mod 4). Tests separate code paths. Also test with |
37 // final byte high bit set (regression test for http://crbug.com/90659). | 47 // final byte high bit set (regression test for http://crbug.com/90659). |
(...skipping 25 matching lines...) Expand all Loading... | |
63 str = "hello world"; | 73 str = "hello world"; |
64 EXPECT_EQ(2794219650u, Hash(str, strlen(str))); | 74 EXPECT_EQ(2794219650u, Hash(str, strlen(str))); |
65 | 75 |
66 // Ensure that it stops reading after the given length, and does not expect a | 76 // Ensure that it stops reading after the given length, and does not expect a |
67 // null byte. | 77 // null byte. |
68 str = "hello world; don't read this part"; | 78 str = "hello world; don't read this part"; |
69 EXPECT_EQ(2794219650u, Hash(str, strlen("hello world"))); | 79 EXPECT_EQ(2794219650u, Hash(str, strlen("hello world"))); |
70 } | 80 } |
71 | 81 |
72 } // namespace base | 82 } // namespace base |
OLD | NEW |