Пример #1
0
    def test_rindex(self):
        self.assertRaises(TypeError, rindex)
        self.assertRaises(TypeError, rindex, None)
        self.assertRaises(TypeError, rindex, bitarray(), 1, 2)
        for endian in 'big', 'little':
            a = bitarray('00010110000', endian)
            self.assertEqual(rindex(a), 6)
            self.assertEqual(rindex(a, 1), 6)
            self.assertEqual(rindex(a, 'A'), 6)
            self.assertEqual(rindex(a, True), 6)

            a = bitarray('00010110111', endian)
            self.assertEqual(rindex(a, 0), 7)
            self.assertEqual(rindex(a, None), 7)
            self.assertEqual(rindex(a, False), 7)

            a = frozenbitarray('00010110111', endian)
            self.assertEqual(rindex(a, 0), 7)
            self.assertEqual(rindex(a, None), 7)
            self.assertEqual(rindex(a, False), 7)

            for v in 0, 1:
                self.assertRaises(ValueError, rindex, bitarray(0, endian), v)
            self.assertRaises(ValueError, rindex, bitarray('000', endian), 1)
            self.assertRaises(ValueError, rindex, bitarray('11111', endian), 0)
Пример #2
0
 def test_rindex4(self):
     for _ in range(100):
         N = randint(1, 10000)
         a = bitarray(N)
         a.setall(0)
         a[randint(0, N - 1)] = 1
         self.assertEqual(rindex(a), a.index(1))
Пример #3
0
 def test_rindex3(self):
     for _ in range(100):
         n = randint(1, 100000)
         v = randint(0, 1)
         a = bitarray(n)
         a.setall(1 - v)
         lst = [randint(0, n - 1) for _ in range(100)]
         for i in lst:
             a[i] = v
         self.assertEqual(rindex(a, v), max(lst))
Пример #4
0
    def _unpad(self, a: bitarray) -> bitarray:
        """
        remove the bitpadding.

        There will be a set of 0s preceded by a 1 at the end of the bitarray, we remove that
        """
        # count the 0 padding at the end until we find the first 1
        # we want to remove the one too
        remove_cnt = util.rindex(a, 1)
        return a[:remove_cnt]
Пример #5
0
 def test_rindex2(self):
     for a in self.randombitarrays():
         v = randint(0, 1)
         try:
             i = rindex(a, v)
         except ValueError:
             i = None
         s = a.to01()
         try:
             j = s.rindex(str(v))
         except ValueError:
             j = None
         self.assertEqual(i, j)