Skip to content

Commit dcefe31

Browse files
committed
Add unit tests
1 parent c8af3b7 commit dcefe31

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

llvm/unittests/ADT/BitVectorTest.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,26 +835,118 @@ TYPED_TEST(BitVectorTest, BinOps) {
835835
A.resize(65);
836836
EXPECT_FALSE(A.anyCommon(B));
837837
EXPECT_FALSE(B.anyCommon(B));
838+
EXPECT_TRUE(A.subsetOf(B));
839+
EXPECT_TRUE(B.subsetOf(A));
838840

839841
B.resize(64);
840842
A.set(64);
841843
EXPECT_FALSE(A.anyCommon(B));
842844
EXPECT_FALSE(B.anyCommon(A));
845+
EXPECT_FALSE(A.subsetOf(B));
846+
EXPECT_TRUE(B.subsetOf(A));
843847

844848
B.set(63);
845849
EXPECT_FALSE(A.anyCommon(B));
846850
EXPECT_FALSE(B.anyCommon(A));
851+
EXPECT_FALSE(A.subsetOf(B));
852+
EXPECT_FALSE(B.subsetOf(A));
847853

848854
A.set(63);
849855
EXPECT_TRUE(A.anyCommon(B));
850856
EXPECT_TRUE(B.anyCommon(A));
857+
EXPECT_FALSE(A.subsetOf(B));
858+
EXPECT_TRUE(B.subsetOf(A));
851859

852860
B.resize(70);
853861
B.set(64);
854862
B.reset(63);
855863
A.resize(64);
856864
EXPECT_FALSE(A.anyCommon(B));
857865
EXPECT_FALSE(B.anyCommon(A));
866+
EXPECT_FALSE(A.subsetOf(B));
867+
EXPECT_FALSE(B.subsetOf(A));
868+
869+
B.set(63);
870+
B.reset(64);
871+
EXPECT_TRUE(A.anyCommon(B));
872+
EXPECT_TRUE(B.anyCommon(A));
873+
EXPECT_TRUE(A.subsetOf(B));
874+
EXPECT_TRUE(B.subsetOf(A));
875+
}
876+
877+
template <typename VecType>
878+
static inline VecType createBitVectorFromBits(
879+
uint32_t Size, const std::vector<int> &SetBits) {
880+
VecType V;
881+
V.resize(Size);
882+
for (auto &BitIndex : SetBits)
883+
V.set(BitIndex);
884+
return V;
885+
}
886+
887+
TYPED_TEST(BitVectorTest, BinOpsLiteral) {
888+
// More tests of binary operations with more focus on the semantics and
889+
// less focus on mutability.
890+
891+
auto AnyCommon = [](uint32_t SizeLHS, const std::vector<int> &SetBitsLHS,
892+
uint32_t SizeRHS, const std::vector<int> &SetBitsRHS) {
893+
auto LHS = createBitVectorFromBits<TypeParam>(SizeLHS, SetBitsLHS);
894+
auto RHS = createBitVectorFromBits<TypeParam>(SizeRHS, SetBitsRHS);
895+
return LHS.anyCommon(RHS);
896+
};
897+
auto SubsetOf = [](uint32_t SizeLHS, const std::vector<int> &SetBitsLHS,
898+
uint32_t SizeRHS, const std::vector<int> &SetBitsRHS) {
899+
auto LHS = createBitVectorFromBits<TypeParam>(SizeLHS, SetBitsLHS);
900+
auto RHS = createBitVectorFromBits<TypeParam>(SizeRHS, SetBitsRHS);
901+
return LHS.subsetOf(RHS);
902+
};
903+
904+
// clang-format off
905+
906+
// Test small-sized vectors.
907+
908+
EXPECT_TRUE (AnyCommon(10, {1, 2, 3}, 10, {3, 4, 5}));
909+
EXPECT_FALSE(AnyCommon(10, {1, 2, 3}, 10, {4, 5}));
910+
911+
EXPECT_FALSE(SubsetOf(10, {1, 2, 3}, 10, {2, 3, 4}));
912+
EXPECT_TRUE (SubsetOf(10, {2, 3}, 10, {2, 3, 4}));
913+
EXPECT_FALSE(SubsetOf(10, {1, 2, 3}, 10, {2, 3}));
914+
EXPECT_TRUE (SubsetOf(10, {1, 2, 3}, 10, {1, 2, 3}));
915+
916+
// Test representations of empty sets of various sizes.
917+
918+
EXPECT_FALSE(AnyCommon(10, {}, 10, {}));
919+
EXPECT_FALSE(AnyCommon(10, {}, 123, {}));
920+
EXPECT_FALSE(AnyCommon(123, {}, 10, {}));
921+
EXPECT_FALSE(AnyCommon(123, {}, 123, {}));
922+
EXPECT_TRUE(SubsetOf(10, {}, 10, {}));
923+
EXPECT_TRUE(SubsetOf(10, {}, 123, {}));
924+
EXPECT_TRUE(SubsetOf(123, {}, 10, {}));
925+
EXPECT_TRUE(SubsetOf(123, {}, 123, {}));
926+
927+
// Test handling of the remainder words.
928+
929+
EXPECT_FALSE(AnyCommon(10, {1, 2}, 123, {5, 70}));
930+
EXPECT_TRUE (AnyCommon(10, {1, 2}, 123, {1, 70}));
931+
EXPECT_FALSE(AnyCommon(123, {5, 70}, 10, {1, 2}));
932+
EXPECT_TRUE (AnyCommon(123, {1, 70}, 10, {1, 2}));
933+
934+
EXPECT_FALSE(AnyCommon(10, {1, 2}, 123, {5}));
935+
EXPECT_TRUE (AnyCommon(10, {1, 2}, 123, {1}));
936+
EXPECT_FALSE(AnyCommon(123, {5}, 10, {1, 2}));
937+
EXPECT_TRUE (AnyCommon(123, {1}, 10, {1, 2}));
938+
939+
EXPECT_FALSE(SubsetOf(10, {1, 2}, 123, {2, 70}));
940+
EXPECT_TRUE (SubsetOf(10, {1, 2}, 123, {1, 2, 70}));
941+
EXPECT_FALSE(SubsetOf(123, {2, 70}, 10, {1, 2}));
942+
EXPECT_FALSE(SubsetOf(123, {1, 2, 70}, 10, {1, 2}));
943+
944+
EXPECT_FALSE(SubsetOf(10, {1, 2}, 123, {2}));
945+
EXPECT_TRUE (SubsetOf(10, {1, 2}, 123, {1, 2}));
946+
EXPECT_TRUE (SubsetOf(123, {2}, 10, {1, 2}));
947+
EXPECT_TRUE (SubsetOf(123, {1, 2}, 10, {1, 2}));
948+
949+
// clang-format on
858950
}
859951

860952
using RangeList = std::vector<std::pair<int, int>>;

0 commit comments

Comments
 (0)