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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/base.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/hash_unittest.cc
diff --git a/base/hash_unittest.cc b/base/hash_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..03f9db2ca40d389ba967d584eae3cd39d7352fac
--- /dev/null
+++ b/base/hash_unittest.cc
@@ -0,0 +1,72 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/hash.h"
+
+#include <string>
+#include <vector>
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+TEST(HashTest, String) {
+ std::string str;
+ // Empty string (should hash to 0).
+ str = "";
+ EXPECT_EQ(0u, Hash(str));
+
+ // Simple test.
+ str = "hello world";
+ EXPECT_EQ(2794219650u, Hash(str));
+
+ // Change one bit.
+ str = "helmo world";
+ EXPECT_EQ(1006697176u, Hash(str));
+
+ // Extremely long string.
+ // Also tests strings with high bit set, and null byte.
+ std::vector<char> long_string_buffer;
+ for (int i = 0; i < 4096; ++i)
+ long_string_buffer.push_back((i % 256) - 128);
+ str.assign(&long_string_buffer.front(), long_string_buffer.size());
+ EXPECT_EQ(2797962408u, Hash(str));
+
+ // All possible lengths (mod 4). Tests separate code paths. Also test with
+ // final byte high bit set (regression test for http://crbug.com/90659).
+ // Note that the 1 and 3 cases have a weird bug where the final byte is
+ // treated as a signed char. It was decided on the above bug discussion to
+ // enshrine that behaviour as "correct" to avoid invalidating existing hashes.
+
+ // Length mod 4 == 0.
+ str = "hello w\xab";
+ EXPECT_EQ(615571198u, Hash(str));
+ // Length mod 4 == 1.
+ str = "hello wo\xab";
+ EXPECT_EQ(623474296u, Hash(str));
+ // Length mod 4 == 2.
+ str = "hello wor\xab";
+ EXPECT_EQ(4278562408u, Hash(str));
+ // Length mod 4 == 3.
+ str = "hello worl\xab";
+ EXPECT_EQ(3224633008u, Hash(str));
+}
+
+TEST(HashTest, CString) {
+ const char* str;
+ // Empty string (should hash to 0).
+ str = "";
+ EXPECT_EQ(0u, Hash(str, strlen(str)));
+
+ // Simple test.
+ str = "hello world";
+ EXPECT_EQ(2794219650u, Hash(str, strlen(str)));
+
+ // Ensure that it stops reading after the given length, and does not expect a
+ // null byte.
+ str = "hello world; don't read this part";
+ EXPECT_EQ(2794219650u, Hash(str, strlen("hello world")));
+}
+
+} // namespace base
« 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