def test_trivial_zero(self): N = 100 ba = BitArray(N) ba.Build() self.assertEqual(ba.length(), N) for i in range(ba.length()): self.assertEqual(ba.Lookup(i), 0) self.assertEqual(ba.Rank(0, i), i) self.assertEqual(ba.Select(0, i + 1), i)
def test_tirivial_one(self): N = 1000 ba = BitArray(N) for i in range(1000): ba.SetBit(1, i) ba.Build() self.assertEqual(ba.length(), N) for i in range(ba.length()): self.assertEqual(ba.Lookup(i), 1) self.assertEqual(ba.Rank(1, i), i) self.assertEqual(ba.Select(1, i + 1), i)
def test_random(self): N = 100000 ba = BitArray(N) B = [] for i in range(N): b = random.randint(0, 1) ba.SetBit(b, i) B.append(b) ba.Build() self.assertEqual(ba.length(), N) sum = 0 for i in range(ba.length()): self.assertEqual(ba.Lookup(i), B[i]) if B[i]: self.assertEqual(ba.Rank(1, i), sum) self.assertEqual(ba.Select(1, sum + 1), i) else: self.assertEqual(ba.Rank(0, i), i - sum) self.assertEqual(ba.Select(0, i - sum + 1), i) sum += B[i]
def test_trivial(self): ba = BitArray() self.assertEqual(ba.length(), 0) self.assertEqual(ba.one_num(), 0) _, filename = tempfile.mkstemp() ba.dump(filename) data = ba.dumps() del ba ba = BitArray() ba.load(filename) os.remove(filename) self.assertEqual(ba.length(), 0) self.assertEqual(ba.one_num(), 0) del ba ba = BitArray() ba.loads(data) self.assertEqual(ba.length(), 0) self.assertEqual(ba.one_num(), 0) del ba