diff options
Diffstat (limited to 'llvm/unittests')
| -rw-r--r-- | llvm/unittests/ADT/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | llvm/unittests/ADT/ReverseIterationTest.cpp | 52 | ||||
| -rw-r--r-- | llvm/unittests/Support/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | llvm/unittests/Support/ReverseIterationTest.cpp | 111 |
4 files changed, 112 insertions, 53 deletions
diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt index fa977ac5d3f..d0313951dfa 100644 --- a/llvm/unittests/ADT/CMakeLists.txt +++ b/llvm/unittests/ADT/CMakeLists.txt @@ -42,7 +42,6 @@ set(ADTSources PostOrderIteratorTest.cpp PriorityWorklistTest.cpp RangeAdapterTest.cpp - ReverseIterationTest.cpp SCCIteratorTest.cpp STLExtrasTest.cpp ScopeExitTest.cpp diff --git a/llvm/unittests/ADT/ReverseIterationTest.cpp b/llvm/unittests/ADT/ReverseIterationTest.cpp deleted file mode 100644 index 1e2dedf083f..00000000000 --- a/llvm/unittests/ADT/ReverseIterationTest.cpp +++ /dev/null @@ -1,52 +0,0 @@ -//===- llvm/unittest/ADT/ReverseIterationTest.cpp ------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// ReverseIteration unit tests. -// -//===----------------------------------------------------------------------===// - -#include "llvm/ADT/SmallPtrSet.h" -#include "gtest/gtest.h" - -#if LLVM_ENABLE_ABI_BREAKING_CHECKS -using namespace llvm; - -TEST(ReverseIterationTest, SmallPtrSetTest) { - - SmallPtrSet<void*, 4> Set; - void *Ptrs[] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - void *ReversePtrs[] = { (void*)0x4, (void*)0x3, (void*)0x2, (void*)0x1 }; - - for (auto *Ptr: Ptrs) - Set.insert(Ptr); - - // Check forward iteration. - ReverseIterate<bool>::value = false; - for (const auto &Tuple : zip(Set, Ptrs)) - ASSERT_EQ(std::get<0>(Tuple), std::get<1>(Tuple)); - - // Check operator++ (post-increment) in forward iteration. - int i = 0; - for (auto begin = Set.begin(), end = Set.end(); - begin != end; i++) - ASSERT_EQ(*begin++, Ptrs[i]); - - // Check reverse iteration. - ReverseIterate<bool>::value = true; - for (const auto &Tuple : zip(Set, ReversePtrs)) - ASSERT_EQ(std::get<0>(Tuple), std::get<1>(Tuple)); - - // Check operator++ (post-increment) in reverse iteration. - i = 0; - for (auto begin = Set.begin(), end = Set.end(); - begin != end; i++) - ASSERT_EQ(*begin++, ReversePtrs[i]); - -} -#endif diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index 641163e39ed..f2a9b472d90 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -42,6 +42,7 @@ add_llvm_unittest(SupportTests ProcessTest.cpp ProgramTest.cpp RegexTest.cpp + ReverseIterationTest.cpp ReplaceFileTest.cpp ScaledNumberTest.cpp SourceMgrTest.cpp diff --git a/llvm/unittests/Support/ReverseIterationTest.cpp b/llvm/unittests/Support/ReverseIterationTest.cpp new file mode 100644 index 00000000000..486c928e47f --- /dev/null +++ b/llvm/unittests/Support/ReverseIterationTest.cpp @@ -0,0 +1,111 @@ +//===- llvm/unittest/Support/ReverseIterationTest.cpp ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// +// Reverse Iteration unit tests. +// +//===---------------------------------------------------------------------===// + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/Support/ReverseIteration.h" +#include "gtest/gtest.h" + +using namespace llvm; + +TEST(ReverseIterationTest, DenseMapTest1) { + static_assert(detail::IsPointerLike<int *>::value, + "int * is pointer-like"); + static_assert(detail::IsPointerLike<uintptr_t>::value, + "uintptr_t is pointer-like"); + struct IncompleteType; + static_assert(detail::IsPointerLike<IncompleteType *>::value, + "incomplete * is pointer-like"); + + // Test reverse iteration for a DenseMap with pointer-like keys. + // DenseMap should reverse iterate if its keys are pointer-like. + DenseMap<int *, int> Map; + int a = 1, b = 2, c = 3, d = 4; + int *Keys[] = { &a, &b, &c, &d }; + + // Insert keys into the DenseMap. + for (auto *Key: Keys) + Map[Key] = 0; + + // Note: This is the observed order of keys in the DenseMap. + // If there is any change in the behavior of the DenseMap, this order would + // need to be adjusted accordingly. + int *IterKeys[] = { &a, &b, &c, &d }; + if (shouldReverseIterate<int *>()) + std::reverse(&IterKeys[0], &IterKeys[4]); + + // Check that the DenseMap is iterated in the expected order. + for (const auto &Tuple : zip(Map, IterKeys)) + ASSERT_EQ(*(std::get<0>(Tuple).first), *(std::get<1>(Tuple))); + + // Check operator++ (post-increment). + int i = 0; + for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) + ASSERT_EQ(iter->first, IterKeys[i]); +} + +TEST(ReverseIterationTest, DenseMapTest2) { + static_assert(!detail::IsPointerLike<int>::value, + "int is not pointer-like"); + + // For a DenseMap with non-pointer-like keys, forward iteration equals + // reverse iteration. + DenseMap<int, int> Map; + int Keys[] = { 1, 2, 3, 4 }; + + // Insert keys into the DenseMap. + for (auto Key: Keys) + Map[Key] = 0; + + // Note: This is the observed order of keys in the DenseMap. + // If there is any change in the behavior of the DenseMap, this order + // would need to be adjusted accordingly. + int IterKeys[] = { 2, 4, 1, 3 }; + + // Check that the DenseMap is iterated in the expected order. + for (const auto &Tuple : zip(Map, IterKeys)) + ASSERT_EQ(std::get<0>(Tuple).first, std::get<1>(Tuple)); + + // Check operator++ (post-increment). + int i = 0; + for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) + ASSERT_EQ(iter->first, IterKeys[i]); +} + +TEST(ReverseIterationTest, SmallPtrSetTest) { + static_assert(detail::IsPointerLike<void *>::value, + "void * is pointer-like"); + + SmallPtrSet<void *, 4> Set; + int a = 1, b = 2, c = 3, d = 4; + int *Ptrs[] = { &a, &b, &c, &d }; + + for (auto *Ptr: Ptrs) + Set.insert(Ptr); + + // Note: This is the observed order of keys in the SmallPtrSet. + // If there is any change in the behavior of the SmallPtrSet, this order + // would need to be adjusted accordingly. + int *IterPtrs[] = { &a, &b, &c, &d }; + if (shouldReverseIterate<int *>()) + std::reverse(&IterPtrs[0], &IterPtrs[4]); + + // Check that the SmallPtrSet is iterated in the expected order. + for (const auto &Tuple : zip(Set, IterPtrs)) + ASSERT_EQ(std::get<0>(Tuple), std::get<1>(Tuple)); + + // Check operator++ (post-increment). + int i = 0; + for (auto iter = Set.begin(), end = Set.end(); iter != end; iter++, ++i) + ASSERT_EQ(*iter, IterPtrs[i]); +} |

