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
3 // found in the LICENSE file.
10 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(HashTest
, String
) {
16 // Empty string (should hash to 0).
18 EXPECT_EQ(0u, Hash(str
));
22 EXPECT_EQ(2794219650u, Hash(str
));
26 EXPECT_EQ(1006697176u, Hash(str
));
28 // Insert a null byte.
31 EXPECT_EQ(2319902537u, Hash(str
));
33 // Test that the bytes after the null contribute to the hash.
36 EXPECT_EQ(553904462u, Hash(str
));
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
));
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.
54 EXPECT_EQ(615571198u, Hash(str
));
57 EXPECT_EQ(623474296u, Hash(str
));
59 str
= "hello wor\xab";
60 EXPECT_EQ(4278562408u, Hash(str
));
62 str
= "hello worl\xab";
63 EXPECT_EQ(3224633008u, Hash(str
));
66 TEST(HashTest
, CString
) {
68 // Empty string (should hash to 0).
70 EXPECT_EQ(0u, Hash(str
, strlen(str
)));
74 EXPECT_EQ(2794219650u, Hash(str
, strlen(str
)));
76 // Ensure that it stops reading after the given length, and does not expect a
78 str
= "hello world; don't read this part";
79 EXPECT_EQ(2794219650u, Hash(str
, strlen("hello world")));