def test_startswith(self): """startswith() method""" def T(lhs, rhs): lhs = Bits(lhs) rhs = Bits(rhs) self.assertTrue(lhs.startswith(rhs)) def F(lhs, rhs): lhs = Bits(lhs) rhs = Bits(rhs) self.assertFalse(lhs.startswith(rhs)) # Everything starts with nothing T([], []) T([0], []) T([0] * 8, []) T([0] * 9, []) # While nothing doesn't start with anything (other than nothing) F([], [0]) F([], [0] * 2) F([], [0] * 8) F([], [0] * 9) F([], [1]) F([], [1] * 2) F([], [1] * 8) F([], [1] * 9) # brute-force test combinations for n in range(23): for m in range(23 - n): lhs = Bits([0] * n + [1] + [0] * m) # lhs should start with all subsets of itself for l in range(len(lhs) + 1): lhs_prefix = lhs[0:l] self.assertTrue(lhs.startswith(lhs_prefix)) # lhs does not start with itself plus a bit self.assertFalse(lhs.startswith(lhs + Bits([0]))) # or any prefix of itself with the first or last bit changed for l in range(1, len(lhs)): inverted_msb = Bits([not lhs[0]]) + lhs[1:l] self.assertFalse(lhs.startswith(inverted_msb)) inverted_lsb = lhs[:l - 1] + Bits([not lhs[l - 1]]) self.assertFalse(lhs.startswith(inverted_lsb)) with self.assertRaises(TypeError): Bits().startswith(None) with self.assertRaises(TypeError): Bits().startswith([])
def test_startswith(self): """startswith() method""" def T(lhs, rhs): lhs = Bits(lhs) rhs = Bits(rhs) self.assertTrue(lhs.startswith(rhs)) def F(lhs, rhs): lhs = Bits(lhs) rhs = Bits(rhs) self.assertFalse(lhs.startswith(rhs)) # Everything starts with nothing T([], []) T([0], []) T([0]*8, []) T([0]*9, []) # While nothing doesn't start with anything (other than nothing) F([], [0]) F([], [0]*2) F([], [0]*8) F([], [0]*9) F([], [1]) F([], [1]*2) F([], [1]*8) F([], [1]*9) # brute-force test combinations for n in range(23): for m in range(23-n): lhs = Bits([0]*n + [1] + [0]*m) # lhs should start with all subsets of itself for l in range(len(lhs)+1): lhs_prefix = lhs[0:l] self.assertTrue(lhs.startswith(lhs_prefix)) # lhs does not start with itself plus a bit self.assertFalse(lhs.startswith(lhs + Bits([0]))) # or any prefix of itself with the first or last bit changed for l in range(1,len(lhs)): inverted_msb = Bits([not lhs[0]]) + lhs[1:l] self.assertFalse(lhs.startswith(inverted_msb)) inverted_lsb = lhs[:l-1] + Bits([not lhs[l-1]]) self.assertFalse(lhs.startswith(inverted_lsb)) with self.assertRaises(TypeError): Bits().startswith(None) with self.assertRaises(TypeError): Bits().startswith([])
def F(lhs, rhs): lhs = Bits(lhs) rhs = Bits(rhs) self.assertFalse(lhs.startswith(rhs))
def T(lhs, rhs): lhs = Bits(lhs) rhs = Bits(rhs) self.assertTrue(lhs.startswith(rhs))