Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: base/hash_unittest.cc

Issue 182593002: Added unit test for base/hash. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/hash.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13
14 TEST(HashTest, String) {
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 // Extremely long string.
29 // Also tests strings with high bit set, and null byte.
30 std::vector<char> long_string_buffer;
31 for (int i = 0; i < 4096; ++i)
32 long_string_buffer.push_back((i % 256) - 128);
33 str.assign(&long_string_buffer.front(), long_string_buffer.size());
34 EXPECT_EQ(2797962408u, Hash(str));
35
36 // 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).
38 // Note that the 1 and 3 cases have a weird bug where the final byte is
39 // treated as a signed char. It was decided on the above bug discussion to
40 // enshrine that behaviour as "correct" to avoid invalidating existing hashes.
41
42 // Length mod 4 == 0.
43 str = "hello w\xab";
44 EXPECT_EQ(615571198u, Hash(str));
45 // Length mod 4 == 1.
46 str = "hello wo\xab";
47 EXPECT_EQ(623474296u, Hash(str));
48 // Length mod 4 == 2.
49 str = "hello wor\xab";
50 EXPECT_EQ(4278562408u, Hash(str));
51 // Length mod 4 == 3.
52 str = "hello worl\xab";
53 EXPECT_EQ(3224633008u, Hash(str));
54 }
55
56 TEST(HashTest, CString) {
57 const char* str;
58 // Empty string (should hash to 0).
59 str = "";
60 EXPECT_EQ(0u, Hash(str, strlen(str)));
61
62 // Simple test.
63 str = "hello world";
64 EXPECT_EQ(2794219650u, Hash(str, strlen(str)));
65
66 // Ensure that it stops reading after the given length, and does not expect a
67 // null byte.
68 str = "hello world; don't read this part";
69 EXPECT_EQ(2794219650u, Hash(str, strlen("hello world")));
70 }
71
72 } // namespace base
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698