Spaces:
Sleeping
Sleeping
add unit tests
Browse files- tests/unit/test_display.py +42 -0
tests/unit/test_display.py
CHANGED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import torch
|
4 |
+
|
5 |
+
from rubik.cube import Cube
|
6 |
+
from rubik.display import stringify, pad_colors
|
7 |
+
|
8 |
+
|
9 |
+
@pytest.mark.parametrize(
|
10 |
+
"colors, size",
|
11 |
+
[
|
12 |
+
[["U", "L", "C", "R", "B", "D"], 3],
|
13 |
+
[["Up", "Left", "Center", "Right", "Back", "Down"], 5],
|
14 |
+
[["A", "BB", "CCC", "DDDD", "EEEEE", "FFFFFF"], 10],
|
15 |
+
],
|
16 |
+
)
|
17 |
+
def test_stringify(colors: list[str], size: int):
|
18 |
+
"""
|
19 |
+
Test that stringify behaves as expected.
|
20 |
+
"""
|
21 |
+
cube = Cube(colors=colors, size=size)
|
22 |
+
state = cube.state.argmax(dim=-1).to(device="cpu", dtype=torch.int16)
|
23 |
+
repr = stringify(state, colors, size)
|
24 |
+
lens = {len(line) for line in repr.split("\n")}
|
25 |
+
assert len(lens) == 1, f"'stringify' lines have variable length: {lens}"
|
26 |
+
|
27 |
+
|
28 |
+
@pytest.mark.parametrize(
|
29 |
+
"colors",
|
30 |
+
[
|
31 |
+
["U", "L", "C", "R", "B", "D"],
|
32 |
+
["Up", "Left", "Center", "Right", "Back", "Down"],
|
33 |
+
["A", "BB", "CCC", "DDDD", "EEEEE", "FFFFFF"],
|
34 |
+
],
|
35 |
+
)
|
36 |
+
def test_pad_colors(colors: list[str]):
|
37 |
+
"""
|
38 |
+
Test that pad_colors behaves as expected.
|
39 |
+
"""
|
40 |
+
padded = pad_colors(colors)
|
41 |
+
lengths = {len(color) for color in padded}
|
42 |
+
assert len(lengths) == 1, f"'pad_colors' generates non-unique lengths: {lengths}"
|