-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Sum value equality. Add unit tests (#1484)
This was supposed to be part of #1481, but pushed it to the branch that depended on it instead 🤦 - Adds the string/repr unit tests suggested by #1481 (review) - Tests—and fixes—equality comparation between Sum values.
- Loading branch information
Showing
4 changed files
with
103 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
from __future__ import annotations | ||
|
||
from hugr.tys import Bool, Qubit, Sum, Tuple, UnitSum | ||
import pytest | ||
|
||
from hugr.tys import Bool, Either, Option, Qubit, Sum, Tuple, Type, UnitSum | ||
|
||
|
||
def test_sums(): | ||
assert Sum([[Bool, Qubit]]) == Tuple(Bool, Qubit) | ||
assert Tuple(Bool, Qubit) == Sum([[Bool, Qubit]]) | ||
assert Sum([[Bool, Qubit]]).as_tuple() == Sum([[Bool, Qubit]]) | ||
|
||
assert Sum([[Bool, Qubit], []]) == Option(Bool, Qubit) | ||
assert Sum([[Bool, Qubit], []]) == Either([Bool, Qubit], []) | ||
assert Option(Bool, Qubit) == Either([Bool, Qubit], []) | ||
assert Sum([[Qubit], [Bool]]) == Either([Qubit], [Bool]) | ||
|
||
assert Tuple() == Sum([[]]) | ||
assert UnitSum(0) == Sum([]) | ||
assert UnitSum(1) == Tuple() | ||
assert UnitSum(4) == Sum([[], [], [], []]) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("ty", "string", "repr_str"), | ||
[ | ||
( | ||
Sum([[Bool], [Qubit], [Qubit, Bool]]), | ||
"Sum([[Bool], [Qubit], [Qubit, Bool]])", | ||
"Sum([[Bool], [Qubit], [Qubit, Bool]])", | ||
), | ||
(UnitSum(1), "Unit", "Unit"), | ||
(UnitSum(2), "Bool", "Bool"), | ||
(UnitSum(3), "UnitSum(3)", "UnitSum(3)"), | ||
(Tuple(Bool, Qubit), "Tuple(Bool, Qubit)", "Tuple(Bool, Qubit)"), | ||
(Option(Bool, Qubit), "Option(Bool, Qubit)", "Option(Bool, Qubit)"), | ||
( | ||
Either([Bool, Qubit], [Bool]), | ||
"Either((Bool, Qubit), Bool)", | ||
"Either(left=[Bool, Qubit], right=[Bool])", | ||
), | ||
], | ||
) | ||
def test_tys_sum_str(ty: Type, string: str, repr_str: str): | ||
assert str(ty) == string | ||
assert repr(ty) == repr_str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from hugr import tys | ||
from hugr.val import FALSE, TRUE, Left, None_, Right, Some, Sum, Tuple, UnitSum, Value | ||
|
||
|
||
def test_sums(): | ||
assert Sum(0, tys.Tuple(), []) == Tuple() | ||
assert Sum(0, tys.Tuple(tys.Bool, tys.Bool), [TRUE, FALSE]) == Tuple(TRUE, FALSE) | ||
|
||
ty = tys.Sum([[tys.Bool, tys.Bool], []]) | ||
assert Sum(0, ty, [TRUE, FALSE]) == Some(TRUE, FALSE) | ||
assert Sum(0, ty, [TRUE, FALSE]) == Left([TRUE, FALSE], []) | ||
assert Sum(1, ty, []) == None_(tys.Bool, tys.Bool) | ||
assert Sum(1, ty, []) == Right([tys.Bool, tys.Bool], []) | ||
|
||
ty = tys.Sum([[tys.Bool], [tys.Bool]]) | ||
assert Sum(0, ty, [TRUE]) == Left([TRUE], [tys.Bool]) | ||
assert Sum(1, ty, [FALSE]) == Right([tys.Bool], [FALSE]) | ||
|
||
assert Tuple() == Sum(0, tys.Tuple(), []) | ||
assert UnitSum(0, size=1) == Tuple() | ||
assert UnitSum(2, size=4) == Sum(2, tys.UnitSum(size=4), []) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("value", "string", "repr_str"), | ||
[ | ||
( | ||
Sum(0, tys.Sum([[tys.Bool], [tys.Qubit]]), [TRUE, FALSE]), | ||
"Sum(tag=0, typ=Sum([[Bool], [Qubit]]), vals=[TRUE, FALSE])", | ||
"Sum(tag=0, typ=Sum([[Bool], [Qubit]]), vals=[TRUE, FALSE])", | ||
), | ||
(UnitSum(0, size=1), "Unit", "Unit"), | ||
(UnitSum(0, size=2), "FALSE", "FALSE"), | ||
(UnitSum(1, size=2), "TRUE", "TRUE"), | ||
(UnitSum(2, size=5), "UnitSum(2, 5)", "UnitSum(2, 5)"), | ||
(Tuple(TRUE, FALSE), "Tuple(TRUE, FALSE)", "Tuple(TRUE, FALSE)"), | ||
(Some(TRUE, FALSE), "Some(TRUE, FALSE)", "Some(TRUE, FALSE)"), | ||
(None_(tys.Bool, tys.Bool), "None", "None(Bool, Bool)"), | ||
( | ||
Left([TRUE, FALSE], [tys.Bool]), | ||
"Left(TRUE, FALSE)", | ||
"Left(vals=[TRUE, FALSE], right_typ=[Bool])", | ||
), | ||
( | ||
Right([tys.Bool, tys.Bool], [FALSE]), | ||
"Right(FALSE)", | ||
"Right(left_typ=[Bool, Bool], vals=[FALSE])", | ||
), | ||
], | ||
) | ||
def test_val_sum_str(value: Value, string: str, repr_str: str): | ||
assert str(value) == string | ||
assert repr(value) == repr_str |