X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=unittests%2FADT%2FHashingTest.cpp;h=1b3d0617a5e3de55a1a524ed390ee6d59699e4cb;hb=da07e9df843015f8c306ed7863dbb8c8055fd85f;hp=e68cffd909a7ee2f8fd1a2f1b13e3fe0a633647e;hpb=edf24a8be491b35f77951614d242458cf2c54669;p=oota-llvm.git diff --git a/unittests/ADT/HashingTest.cpp b/unittests/ADT/HashingTest.cpp index e68cffd909a..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,17 +51,10 @@ using namespace llvm; namespace { -#if _MSC_VER != 1600 -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 }; -#endif - TEST(HashingTest, HashValueBasicTest) { int x = 42, y = 43, c = 'x'; @@ -63,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)); @@ -72,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))); @@ -86,7 +92,6 @@ TEST(HashingTest, HashValueBasicTest) { EXPECT_EQ(hash_value(std::make_pair(42, std::make_pair(43, 44))), hash_value(std::make_pair(std::make_pair(42, 43), 44))); -#if _MSC_VER != 1600 // Ensure that pairs which have padding bytes *inside* them don't get treated // this way. EXPECT_EQ(hash_combine('0', hash_combine(1ull, '2')), @@ -96,7 +101,23 @@ TEST(HashingTest, HashValueBasicTest) { NonPOD obj1(1, 2), obj2(3, 4), obj3(5, 6); EXPECT_EQ(hash_combine(obj1, hash_combine(obj2, obj3)), hash_value(std::make_pair(obj1, std::make_pair(obj2, obj3)))); -#endif +} + +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; } @@ -324,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;