Skip to content

Commit

Permalink
remove test classes from test/clvm/ and switch to pytest (#16488)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Oct 3, 2023
1 parent 13b7410 commit 4e81bfe
Show file tree
Hide file tree
Showing 7 changed files with 906 additions and 884 deletions.
116 changes: 56 additions & 60 deletions tests/clvm/test_chialisp_deserialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from unittest import TestCase

from chia.types.blockchain_format.program import INFINITE_COST, Program
from chia.util.byte_types import hexstr_to_bytes
from chia.wallet.puzzles.load_clvm import load_clvm
Expand Down Expand Up @@ -52,65 +50,63 @@ def serialized_atom_overflow(size):
return size_blob.hex() + extra_str


class TestClvmNativeDeserialization(TestCase):
"""
Test clvm deserialization done from within the clvm
"""
def test_deserialization_simple_list():
# ("hello" "friend")
b = hexstr_to_bytes("ff8568656c6c6fff86667269656e6480")
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
assert prog == Program.from_bytes(b)

def test_deserialization_simple_list(self):
# ("hello" "friend")
b = hexstr_to_bytes("ff8568656c6c6fff86667269656e6480")
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
assert prog == Program.from_bytes(b)

def test_deserialization_password_coin(self):
# (i (= (sha256 2) (q 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)) (c (q 51) (c 5 (c (q 100) (q ())))) (q "wrong password")) # noqa
b = hexstr_to_bytes(
"ff04ffff0affff0bff0280ffff01ffa02cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b98248080ffff05ffff01ff3380ffff05ff05ffff05ffff01ff6480ffff01ff8080808080ffff01ff8e77726f6e672070617373776f72648080" # noqa
) # noqa
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
assert prog == Program.from_bytes(b)
def test_deserialization_password_coin():
# (i (= (sha256 2) (q 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)) (c (q 51) (c 5 (c (q 100) (q ())))) (q "wrong password")) # noqa
b = hexstr_to_bytes(
"ff04ffff0affff0bff0280ffff01ffa02cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b98248080ffff05ffff01ff3380ffff05ff05ffff05ffff01ff6480ffff01ff8080808080ffff01ff8e77726f6e672070617373776f72648080" # noqa
) # noqa
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
assert prog == Program.from_bytes(b)

def test_deserialization_large_numbers(self):
# '(99999999999999999999999999999999999999999999999999999999999999999 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -99999999999999999999999999999999999999999999999999999999999999999999999999999)' # noqa
b = hexstr_to_bytes(
"ff9c00f316271c7fc3908a8bef464e3945ef7a253609ffffffffffffffffffb00fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa1ff22ea0179500526edb610f148ec0c614155678491902d6000000000000000000180" # noqa
) # noqa
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
assert prog == Program.from_bytes(b)

def test_overflow_atoms(self):
b = hexstr_to_bytes(serialized_atom_overflow(0xFFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
b = hexstr_to_bytes(serialized_atom_overflow(0x3FFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
b = hexstr_to_bytes(serialized_atom_overflow(0xFFFFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
b = hexstr_to_bytes(serialized_atom_overflow(0x1FFFFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
def test_deserialization_large_numbers():
# '(99999999999999999999999999999999999999999999999999999999999999999 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -99999999999999999999999999999999999999999999999999999999999999999999999999999)' # noqa
b = hexstr_to_bytes(
"ff9c00f316271c7fc3908a8bef464e3945ef7a253609ffffffffffffffffffb00fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa1ff22ea0179500526edb610f148ec0c614155678491902d6000000000000000000180" # noqa
) # noqa
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
assert prog == Program.from_bytes(b)


def test_overflow_atoms():
b = hexstr_to_bytes(serialized_atom_overflow(0xFFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
b = hexstr_to_bytes(serialized_atom_overflow(0x3FFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
b = hexstr_to_bytes(serialized_atom_overflow(0xFFFFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
b = hexstr_to_bytes(serialized_atom_overflow(0x1FFFFFFFFFF))
try:
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
except Exception:
assert True
else:
assert False
36 changes: 17 additions & 19 deletions tests/clvm/test_clvm_step.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from typing import Any, Optional
from unittest import TestCase

from clvm_tools_rs import start_clvm_program

Expand All @@ -15,25 +14,24 @@
factorial_sym = {factorial_function_hash: "factorial"}


class TestRunProgram(TestCase):
def test_simple_program_run(self) -> None:
p = start_clvm_program(factorial, "ff0580", factorial_sym)
def test_simple_program_run() -> None:
p = start_clvm_program(factorial, "ff0580", factorial_sym)

last: Optional[Any] = None
location: Optional[Any] = None
last: Optional[Any] = None
location: Optional[Any] = None

while not p.is_ended():
step_result = p.step()
if step_result is not None:
last = step_result
self.assertTrue("Failure" not in last)
while not p.is_ended():
step_result = p.step()
if step_result is not None:
last = step_result
assert "Failure" not in last

if "Operator-Location" in last:
location = last["Operator-Location"]
if "Operator-Location" in last:
location = last["Operator-Location"]

self.assertTrue(last is not None)
self.assertTrue(location is not None)
if last is not None and location is not None:
self.assertTrue("Final" in last)
self.assertEqual(int(last["Final"]), 120)
self.assertTrue(location.startswith("factorial"))
assert last is not None
assert location is not None
if last is not None and location is not None:
assert "Final" in last
assert int(last["Final"]) == 120
assert location.startswith("factorial")
67 changes: 38 additions & 29 deletions tests/clvm/test_program.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
from __future__ import annotations

from unittest import TestCase

import pytest
from clvm.EvalError import EvalError
from clvm.operators import KEYWORD_TO_ATOM
from clvm_tools.binutils import assemble, disassemble

from chia.types.blockchain_format.program import Program


class TestProgram(TestCase):
def test_at(self):
p = Program.to([10, 20, 30, [15, 17], 40, 50])
def test_at():
p = Program.to([10, 20, 30, [15, 17], 40, 50])

assert p.first() == p.at("f")
assert Program.to(10) == p.at("f")

assert p.rest() == p.at("r")
assert Program.to([20, 30, [15, 17], 40, 50]) == p.at("r")

assert p.rest().rest().rest().first().rest().first() == p.at("rrrfrf")
assert Program.to(17) == p.at("rrrfrf")

with pytest.raises(ValueError):
p.at("q")

with pytest.raises(EvalError):
p.at("ff")

self.assertEqual(p.first(), p.at("f"))
self.assertEqual(Program.to(10), p.at("f"))

self.assertEqual(p.rest(), p.at("r"))
self.assertEqual(Program.to([20, 30, [15, 17], 40, 50]), p.at("r"))
def test_replace():
p1 = Program.to([100, 200, 300])
assert p1.replace(f=105) == Program.to([105, 200, 300])
assert p1.replace(rrf=[301, 302]) == Program.to([100, 200, [301, 302]])
assert p1.replace(f=105, rrf=[301, 302]) == Program.to([105, 200, [301, 302]])
assert p1.replace(f=100, r=200) == Program.to((100, 200))

self.assertEqual(p.rest().rest().rest().first().rest().first(), p.at("rrrfrf"))
self.assertEqual(Program.to(17), p.at("rrrfrf"))

self.assertRaises(ValueError, lambda: p.at("q"))
self.assertRaises(EvalError, lambda: p.at("ff"))
def test_replace_conflicts():
p1 = Program.to([100, 200, 300])
with pytest.raises(ValueError):
p1.replace(rr=105, rrf=200)

def test_replace(self):
p1 = Program.to([100, 200, 300])
self.assertEqual(p1.replace(f=105), Program.to([105, 200, 300]))
self.assertEqual(p1.replace(rrf=[301, 302]), Program.to([100, 200, [301, 302]]))
self.assertEqual(p1.replace(f=105, rrf=[301, 302]), Program.to([105, 200, [301, 302]]))
self.assertEqual(p1.replace(f=100, r=200), Program.to((100, 200)))

def test_replace_conflicts(self):
p1 = Program.to([100, 200, 300])
self.assertRaises(ValueError, lambda: p1.replace(rr=105, rrf=200))
def test_replace_conflicting_paths():
p1 = Program.to([100, 200, 300])
with pytest.raises(ValueError):
p1.replace(ff=105)

def test_replace_conflicting_paths(self):
p1 = Program.to([100, 200, 300])
self.assertRaises(ValueError, lambda: p1.replace(ff=105))

def test_replace_bad_path(self):
p1 = Program.to([100, 200, 300])
self.assertRaises(ValueError, lambda: p1.replace(q=105))
self.assertRaises(ValueError, lambda: p1.replace(rq=105))
def test_replace_bad_path():
p1 = Program.to([100, 200, 300])
with pytest.raises(ValueError):
p1.replace(q=105)
with pytest.raises(ValueError):
p1.replace(rq=105)


def check_idempotency(f, *args):
Expand Down
Loading

0 comments on commit 4e81bfe

Please sign in to comment.