def test_unknown_ops_last_bits(self): # The last byte is ignored for no-op unknown ops for suffix in [b"\x3f", b"\x0f", b"\x00", b"\x2c"]: # the cost is unchanged by the last byte self.assertEqual(default_unknown_op(b"\x3c" + suffix, SExp.null()), (61, SExp.null()))
def test_unknown_op_reserved(self): # any op that starts with ffff is reserved, and results in a hard # failure with self.assertRaises(EvalError): default_unknown_op(b"\xff\xff", SExp.null()) for suffix in [b"\xff", b"0", b"\x00", b"\xcc\xcc\xfe\xed\xfa\xce"]: with self.assertRaises(EvalError): default_unknown_op(b"\xff\xff" + suffix, SExp.null()) with self.assertRaises(EvalError): # an empty atom is not a valid opcode self.assertEqual(default_unknown_op(b"", SExp.null()), (1, SExp.null())) # a single ff is not sufficient to be treated as a reserved opcode self.assertEqual(default_unknown_op(b"\xff", SExp.null()), (CONCAT_BASE_COST, SExp.null())) # leading zeroes count, and this does not count as a ffff-prefix # the cost is 0xffff00 = 16776960 self.assertEqual( default_unknown_op(b"\x00\xff\xff\x00\x00", SExp.null()), (16776961, SExp.null()))
def test_long_list(self): d = [1337] * 1000 v = SExp.to(d) for i in range(1000 - 1): self.assertEqual(v.as_pair()[0].as_int(), d[i]) v = v.as_pair()[1] self.assertEqual(v.as_atom(), SExp.null())
def test_cons(self): # list self.assertEqual( SExp.to(H01).cons(SExp.to(H02).cons(SExp.null())).as_python(), [H01, H02], ) # cons-box of two values self.assertEqual(SExp.to(H01).cons(SExp.to(H02).as_python()), (H01, H02))
def test_long_linked_list(self): d = b"" for i in range(1000): d = (b"2", d) v = SExp.to(d) for i in range(1000): self.assertEqual(v.as_pair()[0].as_atom(), d[0]) v = v.as_pair()[1] d = d[1] self.assertEqual(v.as_atom(), SExp.null()) self.assertEqual(d, b"")
def test_deep_recursion(self): d = b"2" for i in range(1000): d = [d] v = SExp.to(d) for i in range(1000): self.assertEqual(v.as_pair()[1].as_atom(), SExp.null()) v = v.as_pair()[0] d = d[0] self.assertEqual(v.as_atom(), b"2") self.assertEqual(d, b"2")
def test_constants(self): self.assertEqual(SExp.__null__.nullp(), True) self.assertEqual(SExp.null().nullp(), True) self.assertEqual(SExp.true, True) self.assertEqual(SExp.false, False)