SparseBitVectorTest.cpp 4.03 KB
//===- llvm/unittest/ADT/SparseBitVectorTest.cpp - SparseBitVector tests --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/SparseBitVector.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

TEST(SparseBitVectorTest, TrivialOperation) {
  SparseBitVector<> Vec;
  EXPECT_EQ(0U, Vec.count());
  EXPECT_FALSE(Vec.test(17));
  Vec.set(5);
  EXPECT_TRUE(Vec.test(5));
  EXPECT_FALSE(Vec.test(17));
  Vec.reset(6);
  EXPECT_TRUE(Vec.test(5));
  EXPECT_FALSE(Vec.test(6));
  Vec.reset(5);
  EXPECT_FALSE(Vec.test(5));
  EXPECT_TRUE(Vec.test_and_set(17));
  EXPECT_FALSE(Vec.test_and_set(17));
  EXPECT_TRUE(Vec.test(17));
  Vec.clear();
  EXPECT_FALSE(Vec.test(17));

  Vec.set(5);
  const SparseBitVector<> ConstVec = Vec;
  EXPECT_TRUE(ConstVec.test(5));
  EXPECT_FALSE(ConstVec.test(17));

  Vec.set(1337);
  EXPECT_TRUE(Vec.test(1337));
  Vec = ConstVec;
  EXPECT_FALSE(Vec.test(1337));

  Vec.set(1337);
  EXPECT_FALSE(Vec.empty());
  SparseBitVector<> MovedVec(std::move(Vec));
  EXPECT_TRUE(Vec.empty());
  EXPECT_TRUE(MovedVec.test(5));
  EXPECT_TRUE(MovedVec.test(1337));

  Vec = std::move(MovedVec);
  EXPECT_TRUE(MovedVec.empty());
  EXPECT_FALSE(Vec.empty());
}

TEST(SparseBitVectorTest, IntersectWith) {
  SparseBitVector<> Vec, Other;

  Vec.set(1);
  Other.set(1);
  EXPECT_FALSE(Vec &= Other);
  EXPECT_TRUE(Vec.test(1));

  Vec.clear();
  Vec.set(5);
  Other.clear();
  Other.set(6);
  EXPECT_TRUE(Vec &= Other);
  EXPECT_TRUE(Vec.empty());

  Vec.clear();
  Vec.set(5);
  Other.clear();
  Other.set(225);
  EXPECT_TRUE(Vec &= Other);
  EXPECT_TRUE(Vec.empty());

  Vec.clear();
  Vec.set(225);
  Other.clear();
  Other.set(5);
  EXPECT_TRUE(Vec &= Other);
  EXPECT_TRUE(Vec.empty());
}

TEST(SparseBitVectorTest, SelfAssignment) {
  SparseBitVector<> Vec, Other;

  Vec.set(23);
  Vec.set(234);
  Vec = static_cast<SparseBitVector<> &>(Vec);
  EXPECT_TRUE(Vec.test(23));
  EXPECT_TRUE(Vec.test(234));

  Vec.clear();
  Vec.set(17);
  Vec.set(256);
  EXPECT_FALSE(Vec |= Vec);
  EXPECT_TRUE(Vec.test(17));
  EXPECT_TRUE(Vec.test(256));

  Vec.clear();
  Vec.set(56);
  Vec.set(517);
  EXPECT_FALSE(Vec &= Vec);
  EXPECT_TRUE(Vec.test(56));
  EXPECT_TRUE(Vec.test(517));

  Vec.clear();
  Vec.set(99);
  Vec.set(333);
  EXPECT_TRUE(Vec.intersectWithComplement(Vec));
  EXPECT_TRUE(Vec.empty());
  EXPECT_FALSE(Vec.intersectWithComplement(Vec));

  Vec.clear();
  Vec.set(28);
  Vec.set(43);
  Vec.intersectWithComplement(Vec, Vec);
  EXPECT_TRUE(Vec.empty());

  Vec.clear();
  Vec.set(42);
  Vec.set(567);
  Other.set(55);
  Other.set(567);
  Vec.intersectWithComplement(Vec, Other);
  EXPECT_TRUE(Vec.test(42));
  EXPECT_FALSE(Vec.test(567));

  Vec.clear();
  Vec.set(19);
  Vec.set(21);
  Other.clear();
  Other.set(19);
  Other.set(31);
  Vec.intersectWithComplement(Other, Vec);
  EXPECT_FALSE(Vec.test(19));
  EXPECT_TRUE(Vec.test(31));

  Vec.clear();
  Vec.set(1);
  Other.clear();
  Other.set(59);
  Other.set(75);
  Vec.intersectWithComplement(Other, Other);
  EXPECT_TRUE(Vec.empty());
}

TEST(SparseBitVectorTest, Find) {
  SparseBitVector<> Vec;
  Vec.set(1);
  EXPECT_EQ(1, Vec.find_first());
  EXPECT_EQ(1, Vec.find_last());

  Vec.set(2);
  EXPECT_EQ(1, Vec.find_first());
  EXPECT_EQ(2, Vec.find_last());

  Vec.set(0);
  Vec.set(3);
  EXPECT_EQ(0, Vec.find_first());
  EXPECT_EQ(3, Vec.find_last());

  Vec.reset(1);
  Vec.reset(0);
  Vec.reset(3);
  EXPECT_EQ(2, Vec.find_first());
  EXPECT_EQ(2, Vec.find_last());

  // Set some large bits to ensure we are pulling bits from more than just a
  // single bitword.
  Vec.set(500);
  Vec.set(2000);
  Vec.set(3000);
  Vec.set(4000);
  Vec.reset(2);
  EXPECT_EQ(500, Vec.find_first());
  EXPECT_EQ(4000, Vec.find_last());

  Vec.reset(500);
  Vec.reset(3000);
  Vec.reset(4000);
  EXPECT_EQ(2000, Vec.find_first());
  EXPECT_EQ(2000, Vec.find_last());

  Vec.clear();
}
}