def test_bit_or(self): function = BoolFunction(examples.bit_or) for a in range(2): for b in range(2): result = function.simulate(BitVec(1, a), BitVec(1, b)) tmp = BitVec(1, a) | BitVec(1, b) self.assertEqual(result, [bool(tmp[0])])
def test_id(self): function = BoolFunction(examples.identity) self.assertEqual(function.signature_, [[type(BitVec(1)), 1]]) result = examples.identity(BitVec(1, '0')) self.assertEqual(result, BitVec(1, '0')) result = examples.identity(BitVec(1, '1')) self.assertEqual(result, BitVec(1, '1'))
def test_bool_not(self): function = BoolFunction(examples.bool_not) self.assertEqual(function.signature_, [[type(BitVec(1)), 1]]) for a in range(2): tmp = BitVec(1, a) result = examples.bool_not(tmp) self.assertEqual(result, not bool(tmp))
def test_bit_xor_2bit(self): function = BoolFunction(examples.bit_xor_2bit) for a in range(4): for b in range(4): result = function.simulate(BitVec(2, a), BitVec(2, b)) tmp = BitVec(2, a) ^ BitVec(2, b) self.assertEqual(result, [bool(tmp[0]), bool(tmp[1])])
def test_bit_not_2bit(self): function = BoolFunction(examples.bit_not_2bit) self.assertEqual(function.signature_, [[type(BitVec(2)), 2]]) for a in range(4): tmp = BitVec(2, a) result = examples.bit_not_2bit(tmp) self.assertEqual(result, ~tmp)
def test_xor_2bit(self): function = BoolFunction(examples.bit_xor_2bit) truth_table = function.simulate_all() self.assertEqual(len(truth_table), 2) output0 = BitVec(16, 0xaaaa) ^ BitVec(16, 0xf0f0) output1 = BitVec(16, 0xcccc) ^ BitVec(16, 0xff00) self.assertEqual(str(truth_table[0]), str(output0)) self.assertEqual(str(truth_table[1]), str(output1))
def test_one_bit_value(self): zero_str = BitVec(1, 0) self.assertEqual(zero_str.length_, 1) self.assertEqual(zero_str.value_, 0) one_str = BitVec(1, 1) self.assertEqual(one_str.length_, 1) self.assertEqual(one_str.value_, 1)
def setup(self): self.left_ops = list() self.right_ops = list() for i in range(1, 12): left = randrange((1 << i), (1 << (i + 1))) self.left_ops.append(BitVec((1 << i), left)) right = randrange((1 << i), (1 << (i + 1))) self.left_ops.append(BitVec((1 << i), right))
def test_n_bit_strings(self): self.setup_strings() for length, string in self.strings: bv = BitVec(length, string) self.assertEqual(bv.length_, length) self.assertEqual(bv.value_, int(string, 2)) bv = BitVec(string) self.assertEqual(bv.length_, len(string)) self.assertEqual(bv.value_, int(string, 2))
def test_setslice(self): self.setup_strings() for bv in self.bvs[3:]: string = str(bv) result = BitVec(bv.length_, (1 << bv.length_ - 1) - 1) n = int(bv.length_ / 4) for i in range(0, len(string), n): part = string[i:i + n] result[len(string) - i:len(string) - (i + n)] = BitVec( len(part), part) self.assertEqual(result.length_, bv.length_) self.assertEqual(result.value_, bv.value_)
def test_fail_size_value(self): with self.assertRaises(TypeError) as context: BitVec(1, 10) self.assertExceptionMessage( context, "[BitVec] Value requires a bit vector of length 4, but BitVec has " "length 1")
def test_fail_size_str(self): with self.assertRaises(TypeError) as context: BitVec(1, "00") self.assertExceptionMessage( context, "[BitVec] String requires a bit vector of length 2, but BitVec has " "length 1")
def test_setitem(self): self.setup_strings() for bv in self.bvs: string = str(bv) result = BitVec(bv.length_, 0) last = len(string) - 1 for i in range(len(string)): # Remember that the string is little-endian! result[i] = int(string[last - i]) self.assertEqual(bv.length_, result.length_) self.assertEqual(bv.value_, result.value_)
def test_bit_or(self): function = BoolFunction(examples.bit_or) self.assertEqual(function.signature_, [[type(BitVec(1)), 1], [type(BitVec(1)), 1]]) for a in range(2): for b in range(2): result = examples.bit_or(BitVec(1, a), BitVec(1, b)) tmp = BitVec(1, a) | BitVec(1, b) self.assertEqual(result, tmp)
def test_bit_and_2bit(self): function = BoolFunction(examples.bit_and_2bit) self.assertEqual(function.signature_, [[type(BitVec(2)), 2], [type(BitVec(2)), 2]]) for a in range(4): for b in range(4): result = examples.bit_and_2bit(BitVec(2, a), BitVec(2, b)) tmp = BitVec(2, a) & BitVec(2, b) self.assertEqual(result, tmp)
def constant() -> BitVec(1): return BitVec(1, '1')
def bit_not_2bit(a: BitVec(2)) -> BitVec(2): return ~a
def bit_and(a, b: BitVec(1)) -> BitVec(1): return a & b
def bool_and(a, b: BitVec(1)) -> BitVec(1): return a and b
def bool_or(a, b: BitVec(1)) -> BitVec(1): return a or b
def bit_and_2bit(a, b: BitVec(2)) -> BitVec(2): return a & b
def test_bit_not(self): function = BoolFunction(examples.bit_not) result = function.simulate(BitVec(1, '0')) self.assertEqual(result, [True]) result = function.simulate(BitVec(1, '1')) self.assertEqual(result, [False])
def test_bit_not_2bit(self): function = BoolFunction(examples.bit_not_2bit) for a in range(4): tmp = BitVec(2, a) result = function.simulate(tmp) self.assertEqual(result, [not tmp[0], not tmp[1]])
def test_id_2bit(self): function = BoolFunction(examples.identity_2bit) for a in range(4): tmp = BitVec(2, a) result = function.simulate(tmp) self.assertEqual(result, [bool(tmp[0]), bool(tmp[1])])
def bit_or(a, b: BitVec(1)) -> BitVec(1): return a | b
def test_id(self): function = BoolFunction(examples.identity) result = function.simulate(BitVec(1, '0')) self.assertEqual(result, [False]) result = function.simulate(BitVec(1, '1')) self.assertEqual(result, [True])
def test_constant_3bit(self): function = BoolFunction(examples.constant_3bit) self.assertEqual(function.signature_, []) result = examples.constant_3bit() self.assertEqual(result, BitVec(3, '101'))
def id(a: BitVec(1)) -> BitVec(1): return a
def bit_xor(a, b: BitVec(1)) -> BitVec(1): return a ^ b
def bit_xor_2bit(a, b: BitVec(2)) -> BitVec(2): return a ^ b