-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_cube.py
More file actions
575 lines (442 loc) · 13.8 KB
/
test_cube.py
File metadata and controls
575 lines (442 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import os
import random
from unittest import mock
import pytest
import numpy as np
from magiccube import Cube
from magiccube.cube import CubeException
from magiccube.cube_base import Face, PieceType
from magiccube.cube_move import CubeMove, CubeMoveType
from magiccube.cube_piece import Color, CubePiece
from magiccube.solver.basic.basic_solver import BasicSolver, SolverException
def test_cube_size_1():
with pytest.raises(CubeException):
Cube(1)
def test_reset():
c = Cube(3)
c.check_consistency()
c.rotate("R' D'")
c.check_consistency()
c.reset()
assert c.is_done()
def test_12_moves():
c = Cube(3)
c.check_consistency()
for _ in range(12):
c.rotate("R' D' R D")
c.check_consistency()
assert c.is_done()
def test_12_moves_2d():
c = Cube(2)
c.check_consistency()
for _ in range(12):
c.rotate("R' D' R D")
c.check_consistency()
assert c.is_done()
def test_all_rotations():
c = Cube(3)
c.rotate("R R' L L' B B' F F' U U' D D'")
c.check_consistency()
assert c.is_done()
def test_rotations_4x():
c = Cube(4)
c.rotate("R R'")
c.check_consistency()
assert c.is_done()
c.rotate("2R")
c.check_consistency()
assert c.get_face_flat(Face.F) == [
Color.G, Color.G, Color.Y, Color.G,
Color.G, Color.G, Color.Y, Color.G,
Color.G, Color.G, Color.Y, Color.G,
Color.G, Color.G, Color.Y, Color.G,
]
c.reset()
c.rotate("Uw")
assert c.get_face_flat(Face.F) == [
Color.R, Color.R, Color.R, Color.R,
Color.R, Color.R, Color.R, Color.R,
Color.G, Color.G, Color.G, Color.G,
Color.G, Color.G, Color.G, Color.G,
]
c.check_consistency()
def test_all_rotations_2d():
c = Cube(2)
c.rotate("R R' L L' B B' F F' U U' D D'")
c.check_consistency()
assert c.is_done()
def test_is_done():
c = Cube(3)
assert c.is_done()
c.rotate("R")
assert not c.is_done()
c.reset()
assert c.is_done()
def test_pattern1():
c = Cube(3)
c.rotate("R R L' L' B B F' F' U U D' D'")
c.check_consistency()
def test_pattern2():
c = Cube(3)
c.rotate("R' L U D' F B' R' L")
c.check_consistency()
def test_history_str():
c = Cube(3)
moves = "R' L U D' F B' R' L"
c.rotate(moves)
assert c.history(to_str=True) == moves
def test_history():
c = Cube(5)
moves = [CubeMove(CubeMoveType.R, False), CubeMove(
CubeMoveType.F, True), CubeMove(CubeMoveType.D, False, wide=True, layer=2)]
c.rotate(moves)
assert c.history() == moves
def test_reverse():
c = Cube(3)
moves = "R' L U D' F B' R' L"
c.rotate(moves)
assert c.reverse_history(to_str=True) == "L' R B F' D U' L' R"
c.rotate(c.reverse_history())
assert c.is_done()
def test_reverse_multiplicative_moves():
c = Cube(3)
moves = "R' L2"
c.rotate(moves)
assert c.reverse_history(to_str=True) == "L'2 R"
c.rotate(c.reverse_history())
assert c.is_done()
def test_scramble_3x3():
c = Cube(3)
c.scramble(num_steps=50)
assert not c.is_done()
def test_scramble_2x2():
c = Cube(2)
c.scramble(num_steps=50)
assert not c.is_done()
def test_scramble_4x4():
c = Cube(4)
c.scramble(num_steps=50)
assert not c.is_done()
def test_get_piece():
c = Cube(3)
piece = c.get_piece((2, 0, 0))
assert piece.get_piece_color(Face.R.get_axis()) == Color.R
assert piece.get_piece_color(Face.D.get_axis()) == Color.Y
assert piece.get_piece_color(Face.B.get_axis()) == Color.B
piece = c.get_piece((0, 2, 2))
assert piece.get_piece_color(Face.L.get_axis()) == Color.O
assert piece.get_piece_color(Face.U.get_axis()) == Color.W
assert piece.get_piece_color(Face.F.get_axis()) == Color.G
def test_get_all_faces():
c = Cube(3)
faces = c.get_all_faces()
assert np.all(np.array(faces[Face.F]).flatten() == Color.G)
assert np.all(np.array(faces[Face.B]).flatten() == Color.B)
assert np.all(np.array(faces[Face.L]).flatten() == Color.O)
assert np.all(np.array(faces[Face.R]).flatten() == Color.R)
assert np.all(np.array(faces[Face.U]).flatten() == Color.W)
assert np.all(np.array(faces[Face.D]).flatten() == Color.Y)
def test_get_all_pieces():
c = Cube(3)
pieces = c.get_all_pieces()
assert len(pieces) == 26
c = Cube(4)
pieces = c.get_all_pieces()
assert len(pieces) == 56
def test_move():
c = Cube(3)
c.rotate("L")
assert c.get_piece((1, 0, 0)).get_piece_colors() == (
None, Color.Y, Color.B)
assert c.get_piece((0, 0, 0)).get_piece_colors() == (
Color.O, Color.G, Color.Y)
assert c.get_piece((0, 0, 2)).get_piece_colors() == (
Color.O, Color.G, Color.W)
def test_move_special_rot():
c = Cube(3)
c.rotate("X'")
assert c.get_piece((2, 2, 2)).get_piece_colors() == (
Color.R, Color.B, Color.W)
c.reset()
c.rotate("Y")
assert c.get_piece((2, 2, 2)).get_piece_colors() == (
Color.B, Color.W, Color.R)
c.reset()
c.rotate("Z")
assert c.get_piece((2, 2, 2)).get_piece_colors() == (
Color.W, Color.O, Color.G)
def test_move_special_mid():
c = Cube(3)
c.rotate("M")
assert c.get_piece((1, 2, 2)).get_piece_colors() == (
None, Color.B, Color.W)
c.rotate("E'")
assert c.get_piece((2, 1, 2)).get_piece_colors() == (
Color.B, None, Color.R)
c.rotate("S")
assert c.get_piece((2, 2, 1)).get_piece_colors() == (
Color.W, Color.O, None)
def test_move_noloc():
c = Cube(3)
c.rotate("L")
assert c.get_piece((1, 0, 0)).get_piece_colors(
no_loc=True) == (Color.B, Color.Y)
assert c.get_piece((0, 0, 0)).get_piece_colors(
no_loc=True) == (Color.G, Color.O, Color.Y)
assert c.get_piece((0, 0, 2)).get_piece_colors(
no_loc=True) == (Color.G, Color.O, Color.W)
def test_move_outcome():
c = Cube(3)
c.rotate("B'")
assert c.get_piece((0, 2, 0)).get_piece_colors_str() == 'YOB'
assert c.get_piece((0, 0, 0)).get_piece_colors_str() == 'YRB'
c = Cube(3)
c.rotate("F")
assert c.get_piece((2, 0, 2)).get_piece_colors_str() == 'WRG'
assert c.get_piece((0, 0, 2)).get_piece_colors_str() == 'YRG'
c = Cube(3)
c.rotate("R")
assert c.get_piece((2, 0, 0)).get_piece_colors_str() == 'RBW'
assert c.get_piece((2, 0, 2)).get_piece_colors_str() == 'RBY'
c = Cube(3)
c.rotate("L")
assert c.get_piece((0, 0, 1)).get_piece_colors_str() == 'OG'
assert c.get_piece((0, 0, 0)).get_piece_colors_str() == 'OGY'
assert c.get_piece((0, 0, 2)).get_piece_colors_str() == 'OGW'
c = Cube(3)
c.rotate("D")
assert c.get_piece((2, 0, 0)).get_piece_colors_str(
) == 'GYR', c.get_piece((2, 0, 0)).get_piece_colors_str()
assert c.get_piece((2, 0, 2)).get_piece_colors_str() == 'GYO'
c = Cube(3)
c.rotate("U")
assert c.get_piece((0, 2, 0)).get_piece_colors_str() == 'GWO'
assert c.get_piece((0, 2, 2)).get_piece_colors_str() == 'GWR'
def test_find():
c = Cube(3)
c.rotate("R")
coord, _ = c.find_piece("GRW")
assert coord == (2, 2, 0)
coord, _ = c.find_piece("BOY")
assert coord == (0, 0, 0)
@mock.patch.dict(os.environ, {"TERM": "none"})
def test_print_simple_move():
c = Cube(3)
c.rotate("F")
assert str(c) == \
" W W W \n" + \
" W W W \n" + \
" O O O \n" + \
" O O Y G G G W R R B B B \n" + \
" O O Y G G G W R R B B B \n" + \
" O O Y G G G W R R B B B \n" + \
" R R R \n" + \
" Y Y Y \n" + \
" Y Y Y \n"
def test_wide_move():
c = Cube(3)
c.reset()
c.rotate("Fw")
c.rotate(c.reverse_history())
assert c.is_done()
c.reset()
c.rotate("1Fw")
c.rotate(c.reverse_history())
assert c.is_done()
c.reset()
c.rotate("2Fw")
c.rotate(c.reverse_history())
assert c.is_done()
def test_scramble_wide_move():
c = Cube(6)
random.seed(42)
c.scramble(num_steps=500, wide=True)
c.rotate(c.reverse_history())
assert c.is_done()
def test_get_piece_type():
c = Cube(3)
assert c.get_piece(coordinates=(0, 0, 2)
).get_piece_type() == PieceType.CORNER
assert c.get_piece(coordinates=(1, 0, 1)
).get_piece_type() == PieceType.CENTER
assert c.get_piece(coordinates=(2, 1, 2)
).get_piece_type() == PieceType.EDGE
inner = c.get_piece(coordinates=(1, 1, 1))
assert inner is None or inner.get_piece_type() == PieceType.INNER
def test_set_cube():
c = Cube(3)
c.set("YYYYYYYYYRRRRRRRRRGGGGGGGGGOOOOOOOOOBBBBBBBBBWWWWWWWWW")
assert c.is_done()
def test_set_cube_invalid():
c = Cube(3)
with pytest.raises(CubeException):
c.set("YYYYYYYYYRRRRRRRRRGGGGGGGGGOOOOOOOOOBBBBBBBBBWWWWWWWWWYYYYYYYYYYYY")
def test_set_cube_4x():
c = Cube(4)
c.set("YYYYYYYYYYYYYYYYRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGOOOOOOOOOOOOOOOOBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWW")
assert c.is_done()
def test_set_cube_initial_state():
c = Cube(3, state="YYYYYYGGGGGWRRRRRROOOGGWGGWYBBOOOOOORRRYBBYBBWWBWWBWWB")
c.rotate("U' R'")
assert c.is_done()
def test_set_cube_initial_state_4X():
c = Cube(4, state="""
YYYYYYYYYYYYGGGG
GGGWRRRRRRRRRRRR
OOOOGGGWGGGWGGGW
YBBBOOOOOOOOOOOO
RRRRYBBBYBBBYBBB
WWWBWWWBWWWBWWWB
""")
c.rotate("U' R'")
assert c.is_done()
def test_set_cube_not_done():
c = Cube(3)
c.set("""
RBB BYO GGO
YRO YRW WWW
YYB GGW BRW
YYR OOW GGW
YYG BBR OOG
RGO RWO RBB
""")
c.rotate("D' B' R' F' L' U'")
assert c.is_done()
def test_set_cube_bad_cube():
c = Cube(3)
c.set("""
RBB BYO OGG
YRO YRW WWW
YYB GGW BRW
YYR OOW GGW
YYG BBR OOG
RGO RWO RBB
""")
solver = BasicSolver(c)
with pytest.raises(SolverException):
solver.solve()
def test_get_cube():
c = Cube(3)
assert c.get() == "WWWWWWWWWOOOOOOOOOGGGGGGGGGRRRRRRRRRBBBBBBBBBYYYYYYYYY"
c.rotate("U")
assert c.get() == "WWWWWWWWWGGGOOOOOORRRGGGGGGBBBRRRRRROOOBBBBBBYYYYYYYYY"
state = "YYYYYYYYYRRRRRRRRRGGGGGGGGGOOOOOOOOOBBBBBBBBBWWWWWWWWW"
c = Cube(3)
c.set(state)
assert c.get() == state
def test_inconsistent_cube():
c = Cube(3)
# pylint: disable=protected-access
c._cube[0, 0, 0] = CubePiece(colors=[None, None, None])
with pytest.raises(CubeException):
c.check_consistency()
def test_bad_direction():
c = Cube(3)
with pytest.raises(CubeException):
# pylint: disable=protected-access
c._get_direction(CubeMove(None)) # type: ignore
def test_mes_move_even_cube():
c = Cube(4)
with pytest.raises(CubeException):
c.rotate("M")
def test_invalid_slice():
c = Cube(4)
with pytest.raises(CubeException):
# pylint: disable=protected-access
c._move_to_slice(CubeMove(CubeMoveType.L, layer=-1))
with pytest.raises(CubeException):
# pylint: disable=protected-access
c._move_to_slice(CubeMove(CubeMoveType.L, layer=5))
def test_rotate_twice():
c = Cube(3)
c.rotate("U2")
assert c.get_face_flat(Face.F) == [
Color.B, Color.B, Color.B,
Color.G, Color.G, Color.G,
Color.G, Color.G, Color.G,
]
c.rotate("U'2")
assert c.is_done()
c.rotate("D2")
assert c.get_face_flat(Face.F) == [
Color.G, Color.G, Color.G,
Color.G, Color.G, Color.G,
Color.B, Color.B, Color.B,
]
c.rotate("D'2")
assert c.is_done()
c.rotate("L2")
assert c.get_face_flat(Face.F) == [
Color.B, Color.G, Color.G,
Color.B, Color.G, Color.G,
Color.B, Color.G, Color.G,
]
c.rotate("L'2")
assert c.is_done()
c.rotate("R2")
assert c.get_face_flat(Face.F) == [
Color.G, Color.G, Color.B,
Color.G, Color.G, Color.B,
Color.G, Color.G, Color.B,
]
c.rotate("R'2")
assert c.is_done()
c.rotate("F2")
assert c.get_face_flat(Face.U) == [
Color.W, Color.W, Color.W,
Color.W, Color.W, Color.W,
Color.Y, Color.Y, Color.Y,
]
c.rotate("F'2")
assert c.is_done()
c.rotate("B2")
assert c.get_face_flat(Face.U) == [
Color.Y, Color.Y, Color.Y,
Color.W, Color.W, Color.W,
Color.W, Color.W, Color.W,
]
c.rotate("B'2")
assert c.is_done()
def test_get_kociemba_facelet_positions():
c = Cube(3)
assert c.get_kociemba_facelet_positions(
) == 'UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB'
moves = 'R F U'
c.rotate(moves)
assert c.get_kociemba_facelet_positions(
) == 'LUULUULFFUBBURRFRRURRFFFDDDRRRDDBDDBFFFLLDLLBLLDUBBUBB'
def test_get_kociemba_facelet_colors():
c = Cube(3)
assert c.get_kociemba_facelet_colors(
) == 'WWWWWWWWWRRRRRRRRRGGGGGGGGGYYYYYYYYYOOOOOOOOOBBBBBBBBB'
moves = 'R F U'
c.rotate(moves)
assert c.get_kociemba_facelet_colors(
) == 'OWWOWWOGGWBBWRRGRRWRRGGGYYYRRRYYBYYBGGGOOYOOBOOYWBBWBB'
def test_undo():
c = Cube(3)
c.rotate("U F B")
assert not c.is_done()
assert c.history(to_str=True) == "U F B"
c.undo(2)
assert c.history(to_str=True) == "U"
c.undo()
assert c.history(to_str=True) == ""
assert c.is_done()
def test_undo_fail_history_disabled():
c = Cube(3, hist=False)
c.rotate("U F B")
with pytest.raises(CubeException):
c.undo(1)
def test_undo_fail_too_many_undos():
c = Cube(3)
c.rotate("U F B")
with pytest.raises(CubeException):
c.undo(4)
def test_history_disabled():
c = Cube(3, hist=False)
c.rotate("U F B")
assert c.history() == []
assert c.reverse_history() == []
if __name__ == "__main__":
pytest.main()