X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=unittests%2FADT%2FHashingTest.cpp;h=1b3d0617a5e3de55a1a524ed390ee6d59699e4cb;hb=da07e9df843015f8c306ed7863dbb8c8055fd85f;hp=449b6afae239a678026a8a3c6c43283ae41e9951;hpb=1c1448984d43f1f02c0235d35ebe8460c9b57afd;p=oota-llvm.git diff --git a/unittests/ADT/HashingTest.cpp b/unittests/ADT/HashingTest.cpp index 449b6afae23..1b3d0617a5e 100644 --- a/unittests/ADT/HashingTest.cpp +++ b/unittests/ADT/HashingTest.cpp @@ -30,6 +30,15 @@ void PrintTo(const hash_code &code, std::ostream *os) { // objects. struct LargeTestInteger { uint64_t arr[8]; }; +struct NonPOD { + uint64_t x, y; + NonPOD(uint64_t x, uint64_t y) : x(x), y(y) {} + ~NonPOD() {} + friend hash_code hash_value(const NonPOD &obj) { + return hash_combine(obj.x, obj.y); + } +}; + namespace hashing { namespace detail { template <> struct is_hashable_data : true_type {}; @@ -42,16 +51,11 @@ using namespace llvm; namespace { -struct NonPOD { - uint64_t x, y; - NonPOD(uint64_t x, uint64_t y) : x(x), y(y) {} - ~NonPOD() {} - friend hash_code hash_value(const NonPOD &obj) { - return hash_combine(obj.x, obj.y); - } +enum TestEnumeration { + TE_Foo = 42, + TE_Bar = 43 }; - TEST(HashingTest, HashValueBasicTest) { int x = 42, y = 43, c = 'x'; void *p = 0; @@ -61,7 +65,9 @@ TEST(HashingTest, HashValueBasicTest) { const volatile int cvi = 71; uintptr_t addr = reinterpret_cast(&y); EXPECT_EQ(hash_value(42), hash_value(x)); + EXPECT_EQ(hash_value(42), hash_value(TE_Foo)); EXPECT_NE(hash_value(42), hash_value(y)); + EXPECT_NE(hash_value(42), hash_value(TE_Bar)); EXPECT_NE(hash_value(42), hash_value(p)); EXPECT_EQ(hash_value(71), hash_value(i)); EXPECT_EQ(hash_value(71), hash_value(ci)); @@ -70,7 +76,9 @@ TEST(HashingTest, HashValueBasicTest) { EXPECT_EQ(hash_value(c), hash_value('x')); EXPECT_EQ(hash_value('4'), hash_value('0' + 4)); EXPECT_EQ(hash_value(addr), hash_value(&y)); +} +TEST(HashingTest, HashValueStdPair) { EXPECT_EQ(hash_combine(42, 43), hash_value(std::make_pair(42, 43))); EXPECT_NE(hash_combine(43, 42), hash_value(std::make_pair(42, 43))); EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42ull, 43ull))); @@ -95,6 +103,23 @@ TEST(HashingTest, HashValueBasicTest) { hash_value(std::make_pair(obj1, std::make_pair(obj2, obj3)))); } +TEST(HashingTest, HashValueStdString) { + std::string s = "Hello World!"; + EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size()), hash_value(s)); + EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size() - 1), + hash_value(s.substr(0, s.size() - 1))); + EXPECT_EQ(hash_combine_range(s.c_str() + 1, s.c_str() + s.size() - 1), + hash_value(s.substr(1, s.size() - 2))); + + std::wstring ws = L"Hello Wide World!"; + EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size()), + hash_value(ws)); + EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size() - 1), + hash_value(ws.substr(0, ws.size() - 1))); + EXPECT_EQ(hash_combine_range(ws.c_str() + 1, ws.c_str() + ws.size() - 1), + hash_value(ws.substr(1, ws.size() - 2))); +} + template T *begin(T (&arr)[N]) { return arr; } template T *end(T (&arr)[N]) { return arr + N; } @@ -320,7 +345,7 @@ TEST(HashingTest, HashCombineBasicTest) { EXPECT_EQ(hash_combine_range(arr1, arr1 + 6), hash_combine(i1, i2, i3, i4, i5, i6)); - // Hashing a sequence of heterogenous types which *happen* to all produce the + // Hashing a sequence of heterogeneous types which *happen* to all produce the // same data for hashing produces the same as a range-based hash of the // fundamental values. const size_t s1 = 1024, s2 = 8888, s3 = 9000000;