Spaces:
Sleeping
Sleeping
add unit tests
Browse files- tests/unit/test_action.py +80 -0
tests/unit/test_action.py
CHANGED
|
@@ -8,6 +8,10 @@ from rubik.action import (
|
|
| 8 |
POS_SHIFTS,
|
| 9 |
FACE_ROTATIONS,
|
| 10 |
build_actions_tensor,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
|
|
@@ -97,3 +101,79 @@ def test_build_actions_tensor_shape(size: int):
|
|
| 97 |
assert expected == observed, (
|
| 98 |
f"'build_actions_tensor' output has incorrect shape: expected shape '{expected}', got '{observed}' instead"
|
| 99 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
POS_SHIFTS,
|
| 9 |
FACE_ROTATIONS,
|
| 10 |
build_actions_tensor,
|
| 11 |
+
build_action_tensor,
|
| 12 |
+
parse_action_str,
|
| 13 |
+
parse_actions_str,
|
| 14 |
+
sample_actions_str,
|
| 15 |
)
|
| 16 |
|
| 17 |
|
|
|
|
| 101 |
assert expected == observed, (
|
| 102 |
f"'build_actions_tensor' output has incorrect shape: expected shape '{expected}', got '{observed}' instead"
|
| 103 |
)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@pytest.mark.parametrize(
|
| 107 |
+
"size, axis, slice, inverse",
|
| 108 |
+
[
|
| 109 |
+
(2, 2, 1, 0),
|
| 110 |
+
(3, 0, 1, 1),
|
| 111 |
+
(5, 1, 4, 0),
|
| 112 |
+
],
|
| 113 |
+
)
|
| 114 |
+
def test_build_action_tensor_shape(size: int, axis: int, slice: int, inverse: int):
|
| 115 |
+
"""
|
| 116 |
+
Test that "build_actions_tensor" output has expected shape.
|
| 117 |
+
"""
|
| 118 |
+
expected = (3, size, 2, 6 * (size**2), 6 * (size**2))
|
| 119 |
+
observed = build_action_tensor(size, axis, slice, inverse).shape
|
| 120 |
+
assert expected == observed, (
|
| 121 |
+
f"'build_action_tensor' output has incorrect shape: expected shape '{expected}', got '{observed}' instead"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
@pytest.mark.parametrize(
|
| 126 |
+
"move, expected",
|
| 127 |
+
[
|
| 128 |
+
["X1", (0, 1, 0)],
|
| 129 |
+
["X25i", (0, 25, 1)],
|
| 130 |
+
["Y0", (1, 0, 0)],
|
| 131 |
+
["Y5i", (1, 5, 1)],
|
| 132 |
+
["Z30", (2, 30, 0)],
|
| 133 |
+
["Z512ijk", (2, 512, 1)],
|
| 134 |
+
],
|
| 135 |
+
)
|
| 136 |
+
def test_parse_action_str(move: str, expected: tuple[int, int, int]):
|
| 137 |
+
"""
|
| 138 |
+
Test that "parse_action_str" behaves as expected.
|
| 139 |
+
"""
|
| 140 |
+
observed = parse_action_str(move)
|
| 141 |
+
assert expected == observed, (
|
| 142 |
+
f"'parse_action_str' output is incorrect: expected '{expected}', got '{observed}' instead"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@pytest.mark.parametrize(
|
| 147 |
+
"moves, expected",
|
| 148 |
+
[
|
| 149 |
+
[" X1 Y0 X25i Z512ijk Z30 Y5i ", [(0, 1, 0), (1, 0, 0), (0, 25, 1), (2, 512, 1), (2, 30, 0), (1, 5, 1)]],
|
| 150 |
+
],
|
| 151 |
+
)
|
| 152 |
+
def test_parse_actions_str(moves: str, expected: tuple[int, int, int]):
|
| 153 |
+
"""
|
| 154 |
+
Test that "parse_action_str" behaves as expected.
|
| 155 |
+
"""
|
| 156 |
+
observed = parse_actions_str(moves)
|
| 157 |
+
assert expected == observed, (
|
| 158 |
+
f"'parse_actions_str' output is incorrect: expected '{expected}', got '{observed}' instead"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
@pytest.mark.parametrize(
|
| 163 |
+
"num_moves, size, seed",
|
| 164 |
+
[
|
| 165 |
+
[1, 3, 0],
|
| 166 |
+
[1, 20, 42],
|
| 167 |
+
[256, 5, 21],
|
| 168 |
+
],
|
| 169 |
+
)
|
| 170 |
+
def test_sample_actions_str(num_moves: int, size: int, seed: int):
|
| 171 |
+
"""
|
| 172 |
+
Test that "sample_actions_str" is deterministic and outputs parsable content.
|
| 173 |
+
"""
|
| 174 |
+
moves_1 = sample_actions_str(num_moves, size, seed)
|
| 175 |
+
moves_2 = sample_actions_str(num_moves, size, seed)
|
| 176 |
+
assert moves_1 == moves_2, f"'sample_actions_str' is non-deterministic: {moves_1} != {moves_2}"
|
| 177 |
+
|
| 178 |
+
parsed = parse_actions_str(moves_1)
|
| 179 |
+
assert len(parsed) == len(moves_1.split()), "'sample_actions_str' output cannot be parsed correctly"
|