[llvm] r319018 - [NFC] Add missing unit tests for EquivalenceClasses
Max Kazantsev via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 27 03:20:58 PST 2017
Author: mkazantsev
Date: Mon Nov 27 03:20:58 2017
New Revision: 319018
URL: http://llvm.org/viewvc/llvm-project?rev=319018&view=rev
Log:
[NFC] Add missing unit tests for EquivalenceClasses
Added:
llvm/trunk/unittests/ADT/EquivalenceClassesTest.cpp
Modified:
llvm/trunk/include/llvm/ADT/EquivalenceClasses.h
llvm/trunk/unittests/ADT/CMakeLists.txt
Modified: llvm/trunk/include/llvm/ADT/EquivalenceClasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/EquivalenceClasses.h?rev=319018&r1=319017&r2=319018&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/EquivalenceClasses.h (original)
+++ llvm/trunk/include/llvm/ADT/EquivalenceClasses.h Mon Nov 27 03:20:58 2017
@@ -239,6 +239,16 @@ public:
return L1;
}
+ // isEquivalent - Return true if V1 is equivalent to V2. This can happen if
+ // V1 is equal to V2 or if they belong to one equivalence class.
+ bool isEquivalent(const ElemTy &V1, const ElemTy &V2) const {
+ // Fast path: any element is equivalent to itself.
+ if (V1 == V2)
+ return true;
+ auto It = findLeader(V1);
+ return It != member_end() && It == findLeader(V2);
+ }
+
class member_iterator : public std::iterator<std::forward_iterator_tag,
const ElemTy, ptrdiff_t> {
friend class EquivalenceClasses;
Modified: llvm/trunk/unittests/ADT/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/CMakeLists.txt?rev=319018&r1=319017&r2=319018&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/CMakeLists.txt (original)
+++ llvm/trunk/unittests/ADT/CMakeLists.txt Mon Nov 27 03:20:58 2017
@@ -16,6 +16,7 @@ set(ADTSources
DenseMapTest.cpp
DenseSetTest.cpp
DepthFirstIteratorTest.cpp
+ EquivalenceClassesTest.cpp
FoldingSet.cpp
FunctionRefTest.cpp
HashingTest.cpp
Added: llvm/trunk/unittests/ADT/EquivalenceClassesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/EquivalenceClassesTest.cpp?rev=319018&view=auto
==============================================================================
--- llvm/trunk/unittests/ADT/EquivalenceClassesTest.cpp (added)
+++ llvm/trunk/unittests/ADT/EquivalenceClassesTest.cpp Mon Nov 27 03:20:58 2017
@@ -0,0 +1,85 @@
+//=== llvm/unittest/ADT/EquivalenceClassesTest.cpp - the structure tests --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/EquivalenceClasses.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace llvm {
+
+TEST(EquivalenceClassesTest, NoMerges) {
+ EquivalenceClasses<int> EqClasses;
+ // Until we merged any sets, check that every element is only equivalent to
+ // itself.
+ for (int i = 0; i < 3; i++)
+ for (int j = 0; j < 3; j++)
+ if (i == j)
+ EXPECT_TRUE(EqClasses.isEquivalent(i, j));
+ else
+ EXPECT_FALSE(EqClasses.isEquivalent(i, j));
+}
+
+TEST(EquivalenceClassesTest, SimpleMerge1) {
+ EquivalenceClasses<int> EqClasses;
+ // Check that once we merge (A, B), (B, C), (C, D), then all elements belong
+ // to one set.
+ EqClasses.unionSets(0, 1);
+ EqClasses.unionSets(1, 2);
+ EqClasses.unionSets(2, 3);
+ for (int i = 0; i < 4; ++i)
+ for (int j = 0; j < 4; ++j)
+ EXPECT_TRUE(EqClasses.isEquivalent(i, j));
+}
+
+TEST(EquivalenceClassesTest, SimpleMerge2) {
+ EquivalenceClasses<int> EqClasses;
+ // Check that once we merge (A, B), (C, D), (A, C), then all elements belong
+ // to one set.
+ EqClasses.unionSets(0, 1);
+ EqClasses.unionSets(2, 3);
+ EqClasses.unionSets(0, 2);
+ for (int i = 0; i < 4; ++i)
+ for (int j = 0; j < 4; ++j)
+ EXPECT_TRUE(EqClasses.isEquivalent(i, j));
+}
+
+TEST(EquivalenceClassesTest, TwoSets) {
+ EquivalenceClasses<int> EqClasses;
+ // Form sets of odd and even numbers, check that we split them into these
+ // two sets correcrly.
+ for (int i = 0; i < 30; i += 2)
+ EqClasses.unionSets(0, i);
+ for (int i = 1; i < 30; i += 2)
+ EqClasses.unionSets(1, i);
+
+ for (int i = 0; i < 30; i++)
+ for (int j = 0; j < 30; j++)
+ if (i % 2 == j % 2)
+ EXPECT_TRUE(EqClasses.isEquivalent(i, j));
+ else
+ EXPECT_FALSE(EqClasses.isEquivalent(i, j));
+}
+
+TEST(EquivalenceClassesTest, MultipleSets) {
+ EquivalenceClasses<int> EqClasses;
+ // Split numbers from [0, 100) into sets so that values in the same set have
+ // equal remainders (mod 17).
+ for (int i = 0; i < 100; i++)
+ EqClasses.unionSets(i % 17, i);
+
+ for (int i = 0; i < 100; i++)
+ for (int j = 0; j < 100; j++)
+ if (i % 17 == j % 17)
+ EXPECT_TRUE(EqClasses.isEquivalent(i, j));
+ else
+ EXPECT_FALSE(EqClasses.isEquivalent(i, j));
+}
+
+} // llvm
More information about the llvm-commits
mailing list