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

Side by Side Diff: test/unittests/base/functional-unittest.cc

Issue 624153003: Add C++11 compatible base::hash function object. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: NodeCache Created 6 years, 2 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 | « src/compiler/node-cache.cc ('k') | test/unittests/test-utils.h » ('j') | 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 V8 project 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 "src/base/functional.h"
6
7 #include <limits>
8 #include <set>
9
10 #include "test/unittests/test-utils.h"
11
12 namespace v8 {
13 namespace base {
14
15 TEST(FunctionalTest, HashBool) {
16 hash<bool> h, h1, h2;
17 EXPECT_EQ(h1(true), h2(true));
18 EXPECT_EQ(h1(false), h2(false));
19 EXPECT_NE(h(true), h(false));
20 }
21
22
23 TEST(FunctionalTest, HashFloatZero) {
24 hash<float> h;
25 EXPECT_EQ(h(0.0f), h(-0.0f));
26 }
27
28
29 TEST(FunctionalTest, HashDoubleZero) {
30 hash<double> h;
31 EXPECT_EQ(h(0.0), h(-0.0));
32 }
33
34
35 template <typename T>
36 class FunctionalTest : public TestWithRandomNumberGenerator {};
37
38 typedef ::testing::Types<signed char, unsigned char,
39 short, // NOLINT(runtime/int)
40 unsigned short, // NOLINT(runtime/int)
41 int, unsigned int, long, // NOLINT(runtime/int)
42 unsigned long, // NOLINT(runtime/int)
43 long long, // NOLINT(runtime/int)
44 unsigned long long, // NOLINT(runtime/int)
45 int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t,
46 int64_t, uint64_t, float, double> FunctionalTypes;
47
48 TYPED_TEST_CASE(FunctionalTest, FunctionalTypes);
49
50
51 TYPED_TEST(FunctionalTest, EqualToImpliesSameHashCode) {
52 hash<TypeParam> h;
53 std::equal_to<TypeParam> e;
54 TypeParam values[32];
55 this->rng()->NextBytes(values, sizeof(values));
56 TRACED_FOREACH(TypeParam, v1, values) {
57 TRACED_FOREACH(TypeParam, v2, values) {
58 if (e(v1, v2)) EXPECT_EQ(h(v1), h(v2));
59 }
60 }
61 }
62
63
64 TYPED_TEST(FunctionalTest, HashEqualsHashValue) {
65 for (int i = 0; i < 128; ++i) {
66 TypeParam v;
67 this->rng()->NextBytes(&v, sizeof(v));
68 hash<TypeParam> h;
69 EXPECT_EQ(h(v), hash_value(v));
70 }
71 }
72
73
74 TYPED_TEST(FunctionalTest, HashIsStateless) {
75 hash<TypeParam> h1, h2;
76 for (int i = 0; i < 128; ++i) {
77 TypeParam v;
78 this->rng()->NextBytes(&v, sizeof(v));
79 EXPECT_EQ(h1(v), h2(v));
80 }
81 }
82
83
84 TYPED_TEST(FunctionalTest, HashIsOkish) {
85 const size_t kValues = 128;
86 const size_t kMinHashes = kValues / (sizeof(uint64_t) / sizeof(size_t));
87 std::set<TypeParam> vs;
88 while (vs.size() != kValues) {
89 TypeParam v;
90 this->rng()->NextBytes(&v, sizeof(v));
91 vs.insert(v);
92 }
93 std::set<size_t> hs;
94 for (const auto& v : vs) {
95 hash<TypeParam> h;
96 hs.insert(h(v));
97 }
98 EXPECT_LE(kMinHashes, hs.size());
99 }
100
101
102 namespace {
103
104 struct Foo {
105 int x;
106 double y;
107 };
108
109
110 size_t hash_value(Foo const& v) { return hash_combine(v.x, v.y); }
111
112 } // namespace
113
114
115 TEST(FunctionalTest, HashUsesArgumentDependentLookup) {
116 const int kIntValues[] = {std::numeric_limits<int>::min(), -1, 0, 1, 42,
117 std::numeric_limits<int>::max()};
118 const double kDoubleValues[] = {
119 std::numeric_limits<double>::min(), -1, -0, 0, 1,
120 std::numeric_limits<double>::max()};
121 TRACED_FOREACH(int, x, kIntValues) {
122 TRACED_FOREACH(double, y, kDoubleValues) {
123 hash<Foo> h;
124 Foo foo = {x, y};
125 EXPECT_EQ(hash_combine(x, y), h(foo));
126 }
127 }
128 }
129
130 } // namespace base
131 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/node-cache.cc ('k') | test/unittests/test-utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698