From aec1216593ec109732842faf2845902435b3d458 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 27 Sep 2023 15:13:36 +0100 Subject: [PATCH] [new] Wire up the remaining integration tests for export We also fix the ycombinator test case --- tests/integration/conftest.py | 5 +-- tests/integration/test_arithmetic.py | 19 ++++++----- tests/integration/test_call.py | 9 +++--- tests/integration/test_functional.py | 16 ++++----- tests/integration/test_higher_order.py | 16 ++++----- tests/integration/test_if.py | 45 +++++++++++++------------- tests/integration/test_linear.py | 25 +++++++------- tests/integration/test_nested.py | 29 ++++++++--------- tests/integration/test_programs.py | 10 +++--- tests/integration/test_unused.py | 16 ++++----- tests/integration/test_while.py | 15 ++++----- 11 files changed, 100 insertions(+), 105 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 345fd6f5..af4f558c 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -12,10 +12,11 @@ def export_test_cases_dir(request): @pytest.fixture def validate(request, export_test_cases_dir): - def validate_impl(hugr): + def validate_impl(hugr,name=None): bs = hugr.serialize() util.validate_bytes(bs) if export_test_cases_dir: - export_file = export_test_cases_dir / f"{request.node.name}.msgpack" + file_name = f"{request.node.name}{f'_{name}' if name else ''}.msgpack" + export_file = export_test_cases_dir / file_name export_file.write_bytes(bs) return validate_impl diff --git a/tests/integration/test_arithmetic.py b/tests/integration/test_arithmetic.py index bf22f363..7ed1c6f1 100644 --- a/tests/integration/test_arithmetic.py +++ b/tests/integration/test_arithmetic.py @@ -1,8 +1,7 @@ from guppy.compiler import guppy -from tests.integration.util import validate -def test_arith_basic(): +def test_arith_basic(validate): @guppy def add(x: int, y: int) -> int: return x + y @@ -10,7 +9,7 @@ def add(x: int, y: int) -> int: validate(add) -def test_constant(): +def test_constant(validate): @guppy def const() -> float: return 42.0 @@ -18,7 +17,7 @@ def const() -> float: validate(const) -def test_ann_assign(): +def test_ann_assign(validate): @guppy def add(x: int) -> int: x += 1 @@ -27,7 +26,7 @@ def add(x: int) -> int: validate(add) -def test_float_coercion(): +def test_float_coercion(validate): @guppy def coerce(x: int, y: float) -> float: return x * y @@ -35,7 +34,7 @@ def coerce(x: int, y: float) -> float: validate(coerce) -def test_arith_big(): +def test_arith_big(validate): @guppy def arith(x: int, y: float, z: int) -> bool: a = x // y + 3 * z @@ -45,7 +44,7 @@ def arith(x: int, y: float, z: int) -> bool: validate(arith) -def test_shortcircuit_assign1(): +def test_shortcircuit_assign1(validate): @guppy def foo(x: bool, y: int) -> bool: if (z := x) and y > 0: @@ -55,7 +54,7 @@ def foo(x: bool, y: int) -> bool: validate(foo) -def test_shortcircuit_assign2(): +def test_shortcircuit_assign2(validate): @guppy def foo(x: bool, y: int) -> bool: if y > 0 and (z := x): @@ -65,7 +64,7 @@ def foo(x: bool, y: int) -> bool: validate(foo) -def test_shortcircuit_assign3(): +def test_shortcircuit_assign3(validate): @guppy def foo(x: bool, y: int) -> bool: if (z := x) or y > 0: @@ -75,7 +74,7 @@ def foo(x: bool, y: int) -> bool: validate(foo) -def test_shortcircuit_assign4(): +def test_shortcircuit_assign4(validate): @guppy def foo(x: bool, y: int) -> bool: if y > 0 or (z := x): diff --git a/tests/integration/test_call.py b/tests/integration/test_call.py index b6571f43..e37ca28c 100644 --- a/tests/integration/test_call.py +++ b/tests/integration/test_call.py @@ -1,8 +1,7 @@ from guppy.compiler import guppy, GuppyModule -from tests.integration.util import validate -def test_call(): +def test_call(validate): module = GuppyModule("module") @module @@ -16,7 +15,7 @@ def bar() -> int: validate(module.compile(exit_on_error=True)) -def test_call_back(tmp_path): +def test_call_back(validate): module = GuppyModule("module") @module @@ -30,7 +29,7 @@ def bar(x: int) -> int: validate(module.compile(exit_on_error=True)) -def test_recursion(): +def test_recursion(validate): @guppy def main(x: int) -> int: return main(x) @@ -38,7 +37,7 @@ def main(x: int) -> int: validate(main) -def test_mutual_recursion(): +def test_mutual_recursion(validate): module = GuppyModule("module") @module diff --git a/tests/integration/test_functional.py b/tests/integration/test_functional.py index 1f7f5981..38aa9340 100644 --- a/tests/integration/test_functional.py +++ b/tests/integration/test_functional.py @@ -1,11 +1,11 @@ import pytest from guppy.compiler import guppy -from tests.integration.util import validate, functional, _ +from tests.integration.util import functional, _ @pytest.mark.skip() -def test_if_no_else(): +def test_if_no_else(validate): @guppy def foo(x: bool, y: int) -> int: _@functional @@ -17,7 +17,7 @@ def foo(x: bool, y: int) -> int: @pytest.mark.skip() -def test_if_else(): +def test_if_else(validate): @guppy def foo(x: bool, y: int) -> int: _@functional @@ -31,7 +31,7 @@ def foo(x: bool, y: int) -> int: @pytest.mark.skip() -def test_if_elif(): +def test_if_elif(validate): @guppy def foo(x: bool, y: int) -> int: _@functional @@ -45,7 +45,7 @@ def foo(x: bool, y: int) -> int: @pytest.mark.skip() -def test_if_elif_else(): +def test_if_elif_else(validate): @guppy def foo(x: bool, y: int) -> int: _@functional @@ -61,7 +61,7 @@ def foo(x: bool, y: int) -> int: @pytest.mark.skip() -def test_infinite_loop(): +def test_infinite_loop(validate): @guppy def foo() -> int: while True: @@ -72,7 +72,7 @@ def foo() -> int: @pytest.mark.skip() -def test_counting_loop(): +def test_counting_loop(validate): @guppy def foo(i: int) -> int: while i > 0: @@ -83,7 +83,7 @@ def foo(i: int) -> int: @pytest.mark.skip() -def test_nested_loop(): +def test_nested_loop(validate): @guppy def foo(x: int, y: int) -> int: p = 0 diff --git a/tests/integration/test_higher_order.py b/tests/integration/test_higher_order.py index 96d5a7c6..fa3d5451 100644 --- a/tests/integration/test_higher_order.py +++ b/tests/integration/test_higher_order.py @@ -1,10 +1,9 @@ from typing import Callable from guppy.compiler import guppy, GuppyModule -from tests.integration.util import validate -def test_basic(): +def test_basic(validate): module = GuppyModule("test") @module @@ -18,7 +17,7 @@ def foo() -> Callable[[int], bool]: validate(module.compile()) -def test_call_1(): +def test_call_1(validate): module = GuppyModule("test") @module @@ -36,7 +35,7 @@ def baz() -> bool: validate(module.compile()) -def test_call_2(): +def test_call_2(validate): module = GuppyModule("test") @module @@ -54,7 +53,7 @@ def baz(y: int) -> None: validate(module.compile()) -def test_nested(): +def test_nested(validate): @guppy def foo(x: int) -> Callable[[int], bool]: def bar(y: int) -> bool: @@ -65,7 +64,7 @@ def bar(y: int) -> bool: validate(foo) -def test_curry(): +def test_curry(validate): module = GuppyModule("curry") @module @@ -78,7 +77,7 @@ def h(y: int) -> bool: @module def uncurry(f: Callable[[int], Callable[[int], bool]]) -> Callable[[int, int], bool]: - def g(x: int, y: int): + def g(x: int, y: int) -> bool: return f(x)(y) return g @@ -94,8 +93,9 @@ def main(x: int, y: int) -> None: uncurried(x, y) curry(uncurry(curry(gt)))(y)(x) + validate(module.compile()) -def test_y_combinator(): +def test_y_combinator(validate): module = GuppyModule("fib") @module diff --git a/tests/integration/test_if.py b/tests/integration/test_if.py index 25c65d3a..6ea6cafc 100644 --- a/tests/integration/test_if.py +++ b/tests/integration/test_if.py @@ -1,10 +1,9 @@ import pytest from guppy.compiler import guppy -from tests.integration.util import validate -def test_if_no_else(): +def test_if_no_else(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -14,7 +13,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_if_else(): +def test_if_else(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -26,7 +25,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_if_elif(): +def test_if_elif(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -38,7 +37,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_if_elif_else(): +def test_if_elif_else(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -52,7 +51,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_if_expr(): +def test_if_expr(validate): @guppy def foo(x: bool, y: int) -> int: return y+1 if x else 42 @@ -60,7 +59,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_if_expr_nested(): +def test_if_expr_nested(validate): @guppy def foo(x: bool, y: int) -> int: a = y + 1 if x else y * y if 0 < y <= 10 else 42 @@ -70,7 +69,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_if_expr_tuple(): +def test_if_expr_tuple(validate): @guppy def foo(x: bool, y: int) -> (int, int, int): t = (y + 1 if x else 42), 8, (y * 2 if x else 64) @@ -79,7 +78,7 @@ def foo(x: bool, y: int) -> (int, int, int): validate(foo) -def test_if_expr_assign_both(): +def test_if_expr_assign_both(validate): @guppy def foo(x: bool) -> int: (z := 5) if x else (z := 42) @@ -88,7 +87,7 @@ def foo(x: bool) -> int: validate(foo) -def test_if_expr_assign_cond(): +def test_if_expr_assign_cond(validate): @guppy def foo(x: bool) -> bool: return z if (z := x) else False @@ -96,7 +95,7 @@ def foo(x: bool) -> bool: validate(foo) -def test_if_expr_reassign(): +def test_if_expr_reassign(validate): @guppy def foo(x: bool) -> int: y = 5 @@ -108,7 +107,7 @@ def foo(x: bool) -> int: validate(foo) -def test_if_expr_reassign_cond(): +def test_if_expr_reassign_cond(validate): @guppy def foo(x: bool) -> int: y = 5 @@ -117,7 +116,7 @@ def foo(x: bool) -> int: validate(foo) -def test_if_expr_double_type_change(): +def test_if_expr_double_type_change(validate): @guppy def foo(x: bool) -> int: y = 4 @@ -127,7 +126,7 @@ def foo(x: bool) -> int: validate(foo) -def test_if_return(): +def test_if_return(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -138,7 +137,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_else_return(): +def test_else_return(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -151,7 +150,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_both_return(): +def test_both_return(validate): @guppy def foo(x: bool, y: int) -> int: if x: @@ -164,7 +163,7 @@ def foo(x: bool, y: int) -> int: validate(foo) -def test_nested_return(): +def test_nested_return(validate): @guppy def foo(x: int, y: int) -> int: if x > 5: @@ -177,7 +176,7 @@ def foo(x: int, y: int) -> int: validate(foo) -def test_return_defined1(): +def test_return_defined1(validate): @guppy def foo(x: int, y: int) -> int: if x > 5: @@ -189,7 +188,7 @@ def foo(x: int, y: int) -> int: validate(foo) -def test_return_defined2(): +def test_return_defined2(validate): @guppy def foo(x: int) -> int: if x > 5: @@ -201,7 +200,7 @@ def foo(x: int) -> int: validate(foo) -def test_break_different_types1(): +def test_break_different_types1(validate): @guppy def foo(x: int) -> int: z = 0 @@ -217,7 +216,7 @@ def foo(x: int) -> int: validate(foo) -def test_break_different_types2(): +def test_break_different_types2(validate): @guppy def foo(x: int) -> int: z = 0 @@ -234,7 +233,7 @@ def foo(x: int) -> int: @pytest.mark.skip("Known bug") -def test_continue_different_types1(): +def test_continue_different_types1(validate): @guppy def foo(x: int) -> int: z = 0 @@ -251,7 +250,7 @@ def foo(x: int) -> int: @pytest.mark.skip("Known bug") -def test_continue_different_types2(): +def test_continue_different_types2(validate): @guppy def foo(x: int) -> int: z = 0 diff --git a/tests/integration/test_linear.py b/tests/integration/test_linear.py index 6786de62..decc4f5c 100644 --- a/tests/integration/test_linear.py +++ b/tests/integration/test_linear.py @@ -1,12 +1,11 @@ from guppy.compiler import guppy, GuppyModule from guppy.prelude.quantum import Qubit -from tests.integration.util import validate import guppy.prelude.quantum as quantum from guppy.prelude.quantum import h, cx -def test_id(): +def test_id(validate): module = GuppyModule("test") module.load(quantum) @@ -17,7 +16,7 @@ def test(q: Qubit) -> Qubit: validate(module.compile(True)) -def test_assign(): +def test_assign(validate): module = GuppyModule("test") module.load(quantum) @@ -30,7 +29,7 @@ def test(q: Qubit) -> Qubit: validate(module.compile(True)) -def test_interleave(): +def test_interleave(validate): module = GuppyModule("test") module.load(quantum) @@ -52,7 +51,7 @@ def test(a: Qubit, b: Qubit, c: Qubit, d: Qubit) -> tuple[Qubit, Qubit, Qubit, Q validate(module.compile(True)) -def test_if(): +def test_if(validate): module = GuppyModule("test") module.load(quantum) @@ -72,7 +71,7 @@ def test(b: bool) -> Qubit: validate(module.compile(True)) -def test_if_return(): +def test_if_return(validate): module = GuppyModule("test") module.load(quantum) @@ -92,7 +91,7 @@ def test(b: bool) -> Qubit: validate(module.compile(True)) -def test_measure(): +def test_measure(validate): module = GuppyModule("test") module.load(quantum) @@ -108,7 +107,7 @@ def test(q: Qubit, x: int) -> int: validate(module.compile(True)) -def test_return_call(): +def test_return_call(validate): module = GuppyModule("test") module.load(quantum) @@ -123,7 +122,7 @@ def test(q: Qubit) -> Qubit: validate(module.compile(True)) -def test_while(): +def test_while(validate): module = GuppyModule("test") module.load(quantum) @@ -137,7 +136,7 @@ def test(q: Qubit, i: int) -> Qubit: validate(module.compile(True)) -def test_while_break(): +def test_while_break(validate): module = GuppyModule("test") module.load(quantum) @@ -153,7 +152,7 @@ def test(q: Qubit, i: int) -> Qubit: validate(module.compile(True)) -def test_while_continue(): +def test_while_continue(validate): module = GuppyModule("test") module.load(quantum) @@ -169,7 +168,7 @@ def test(q: Qubit, i: int) -> Qubit: validate(module.compile(True)) -def test_while_reset(): +def test_while_reset(validate): module = GuppyModule("test") module.load(quantum) @@ -193,7 +192,7 @@ def foo(i: bool) -> bool: return b -def test_rus(): +def test_rus(validate): module = GuppyModule("test") module.load(quantum) diff --git a/tests/integration/test_nested.py b/tests/integration/test_nested.py index 2277aab9..caac4e0d 100644 --- a/tests/integration/test_nested.py +++ b/tests/integration/test_nested.py @@ -1,8 +1,7 @@ from guppy.compiler import guppy -from tests.integration.util import validate -def test_basic(): +def test_basic(validate): @guppy def foo(x: int) -> int: def bar(y: int) -> int: @@ -12,7 +11,7 @@ def bar(y: int) -> int: validate(foo) -def test_call_twice(): +def test_call_twice(validate): @guppy def foo(x: int) -> int: def bar(y: int) -> int: @@ -25,7 +24,7 @@ def bar(y: int) -> int: validate(foo) -def test_redefine(): +def test_redefine(validate): @guppy def foo(x: int) -> int: def bar(y: int) -> int: @@ -42,7 +41,7 @@ def bar(y: int) -> int: validate(foo) -def test_define_twice(): +def test_define_twice(validate): @guppy def foo(x: int) -> int: if x == 0: @@ -56,7 +55,7 @@ def bar(y: int) -> int: validate(foo) -def test_nested_deep(): +def test_nested_deep(validate): @guppy def foo(x: int) -> int: def bar(y: int) -> int: @@ -68,7 +67,7 @@ def baz(z: int) -> int: validate(foo) -def test_recurse(): +def test_recurse(validate): @guppy def foo(x: int) -> int: def bar(y: int) -> int: @@ -80,7 +79,7 @@ def bar(y: int) -> int: validate(foo) -def test_capture_arg(): +def test_capture_arg(validate): @guppy def foo(x: int) -> int: def bar() -> int: @@ -90,7 +89,7 @@ def bar() -> int: validate(foo) -def test_capture_assigned(): +def test_capture_assigned(validate): @guppy def foo(x: int) -> int: y = x + 1 @@ -102,7 +101,7 @@ def bar() -> int: validate(foo) -def test_capture_multiple(): +def test_capture_multiple(validate): @guppy def foo(x: int) -> int: if x > 5: @@ -119,7 +118,7 @@ def bar() -> int: validate(foo) -def test_capture_cfg(): +def test_capture_cfg(validate): @guppy def foo(x: int) -> int: a = x + 4 @@ -134,7 +133,7 @@ def bar() -> int: validate(foo) -def test_capture_deep(): +def test_capture_deep(validate): @guppy def foo(x: int) -> int: a = x * 2 @@ -153,7 +152,7 @@ def baz(y: int) -> int: validate(foo) -def test_capture_recurse(): +def test_capture_recurse(validate): @guppy def foo(x: int) -> int: def bar(y: int, z: int) -> int: @@ -165,7 +164,7 @@ def bar(y: int, z: int) -> int: validate(foo) -def test_capture_recurse_nested(): +def test_capture_recurse_nested(validate): @guppy def foo(x: int) -> int: def bar(y: int, z: int) -> int: @@ -184,7 +183,7 @@ def baz() -> int: validate(foo) -def test_capture_while(): +def test_capture_while(validate): @guppy def foo(x: int) -> int: a = 0 diff --git a/tests/integration/test_programs.py b/tests/integration/test_programs.py index ba0e4be8..0d369ab2 100644 --- a/tests/integration/test_programs.py +++ b/tests/integration/test_programs.py @@ -2,7 +2,7 @@ from tests.integration.util import validate, functional, _ -def test_factorial(): +def test_factorial(validate): @guppy def factorial1(x: int) -> int: acc = 1 @@ -32,13 +32,13 @@ def factorial3(x: int, acc: int) -> int: # x -= 1 # return acc - validate(factorial1) - validate(factorial2) - validate(factorial3) + validate(factorial1, name="factorial1") + validate(factorial2, name="factorial2") + validate(factorial3, name="factorial3") # validate(factorial4) -def test_even_odd(): +def test_even_odd(validate): module = GuppyModule("module") @module diff --git a/tests/integration/test_unused.py b/tests/integration/test_unused.py index 4fb6d7f7..5f47717a 100644 --- a/tests/integration/test_unused.py +++ b/tests/integration/test_unused.py @@ -6,7 +6,7 @@ """ All sorts of weird stuff is allowed when variables are not used. """ -def test_not_always_defined1(): +def test_not_always_defined1(validate): @guppy def foo(x: bool) -> int: if x: @@ -16,7 +16,7 @@ def foo(x: bool) -> int: validate(foo) -def test_not_always_defined2(): +def test_not_always_defined2(validate): @guppy def foo(x: bool) -> int: if x: @@ -28,7 +28,7 @@ def foo(x: bool) -> int: validate(foo) -def test_not_always_defined3(): +def test_not_always_defined3(validate): @guppy def foo(x: bool) -> int: if x: @@ -40,7 +40,7 @@ def foo(x: bool) -> int: validate(foo) -def test_different_types1(): +def test_different_types1(validate): @guppy def foo(x: bool) -> int: if x: @@ -52,7 +52,7 @@ def foo(x: bool) -> int: validate(foo) -def test_different_types2(): +def test_different_types2(validate): @guppy def foo(x: bool) -> int: z = False @@ -65,7 +65,7 @@ def foo(x: bool) -> int: validate(foo) -def test_different_types3(): +def test_different_types3(validate): @guppy def foo(x: bool) -> int: z = False @@ -78,7 +78,7 @@ def foo(x: bool) -> int: validate(foo) -def test_while_change_type(): +def test_while_change_type(validate): @guppy def foo() -> None: x = 42 @@ -88,7 +88,7 @@ def foo() -> None: validate(foo) -def test_if_expr_different_types(): +def test_if_expr_different_types(validate): @guppy def foo(x: bool) -> None: 5 if x else False diff --git a/tests/integration/test_while.py b/tests/integration/test_while.py index e6c53601..1af3939f 100644 --- a/tests/integration/test_while.py +++ b/tests/integration/test_while.py @@ -1,8 +1,7 @@ from guppy.compiler import guppy -from tests.integration.util import validate -def test_infinite_loop(): +def test_infinite_loop(validate): @guppy def foo() -> int: while True: @@ -12,7 +11,7 @@ def foo() -> int: validate(foo) -def test_counting_loop(): +def test_counting_loop(validate): @guppy def foo(i: int) -> int: while i > 0: @@ -22,7 +21,7 @@ def foo(i: int) -> int: validate(foo) -def test_break(): +def test_break(validate): @guppy def foo(i: int) -> int: while True: @@ -34,7 +33,7 @@ def foo(i: int) -> int: validate(foo) -def test_continue(): +def test_continue(validate): @guppy def foo(i: int) -> int: x = 42 @@ -47,7 +46,7 @@ def foo(i: int) -> int: validate(foo) -def test_return_in_loop(): +def test_return_in_loop(validate): @guppy def foo(i: int) -> int: x = 42 @@ -61,7 +60,7 @@ def foo(i: int) -> int: validate(foo) -def test_nested_loop(): +def test_nested_loop(validate): @guppy def foo(x: int, y: int) -> int: p = 0 @@ -77,7 +76,7 @@ def foo(x: int, y: int) -> int: validate(foo) -def test_nested_loop_break_continue(): +def test_nested_loop_break_continue(validate): @guppy def foo(x: int, y: int) -> int: p = 0