Пример #1
0
def test_set_col():
    # Check that setting a column and then getting the same column
    # returns a copy of the same column, but not the exact same column.
    for _ in range(NTESTS):
        for size in [2, 3]:
            rep = r.RubiksRep(size)
            # Generate a random face.
            face = random.choice('UDFBLR')
            # Generate a random column index.
            col_index = random.randrange(size)

            # Generate a random column.
            col_values = random.sample(string.ascii_letters, size)
            rep.set_col(face, col_index, col_values)
            retrieved = rep.get_col(face, col_index)
            assert retrieved == col_values
            # Make sure column returned is distinct from the column set.
            assert retrieved is not col_values

            # Generate a random column again.
            col_values = random.sample(string.ascii_letters, size)
            rep.set_col(face, col_index, col_values)
            # Check for aliasing.
            col_values[0] = '@'  # a non-alphabetic character.
            retrieved = rep.get_col(face, col_index)
            assert retrieved[0] != '@'
Пример #2
0
def test_set_row():
    # Check that setting a row and then getting the same row
    # returns a copy of the same row, but not the exact same row.
    for _ in range(NTESTS):
        for size in [2, 3]:
            rep = r.RubiksRep(size)
            # Generate a random face.
            face = random.choice('UDFBLR')
            # Generate a random row index.
            row_index = random.randrange(size)

            # Generate a random row.
            row_values = random.sample(string.ascii_letters, size)
            rep.set_row(face, row_index, row_values)
            retrieved = rep.get_row(face, row_index)
            assert retrieved == row_values
            # Make sure row returned is distinct from the row set.
            assert retrieved is not row_values

            # Generate a random row again.
            row_values = random.sample(string.ascii_letters, size)
            rep.set_row(face, row_index, row_values)
            # Check for aliasing.
            row_values[0] = '@'  # a non-alphabetic character.
            retrieved = rep.get_row(face, row_index)
            assert retrieved[0] != '@'
Пример #3
0
def test_get_col():
    # Check that getting a known column gets the right thing.
    # Check that changing the contents of a retrieved column doesn't affect
    # the original column.
    # Check that getting a known row gets the right thing.
    rep = r.RubiksRep(3)
    rep.test_faces()  # Use fake contents.
    r1 = rep.get_col('U', 0)
    assert r1 == ['a', 'd', 'g']
    r2 = rep.get_col('D', 2)
    assert r2 == ['l', 'o', 'r']
    r3 = rep.get_col('F', 1)
    assert r3 == ['t', 'w', 'z']
    r4 = rep.get_col('B', 1)
    assert r4 == ['C', 'F', 'I']
    r5 = rep.get_col('L', 0)
    assert r5 == ['K', 'N', 'Q']
    r6 = rep.get_col('R', 2)
    assert r6 == ['V', 'Y', '#']
    # Check that changing the contents of a retrieved column doesn't affect
    # the original column.
    r1 = rep.get_col('U', 0)
    r1[0] = '0'
    assert rep.get_col('U', 0) == ['a', 'd', 'g']

    # Check that getting a known row gets the right thing.
    rep = r.RubiksRep(2)
    rep.test_faces()  # Use fake contents.
    r1 = rep.get_col('U', 0)
    assert r1 == ['a', 'c']
    r2 = rep.get_col('D', 1)
    assert r2 == ['f', 'h']
    r3 = rep.get_col('F', 1)
    assert r3 == ['j', 'l']
    r4 = rep.get_col('B', 1)
    assert r4 == ['n', 'p']
    r5 = rep.get_col('L', 0)
    assert r5 == ['q', 's']
    r6 = rep.get_col('R', 1)
    assert r6 == ['v', 'x']
    # Check that changing the contents of a retrieved column doesn't affect
    # the original column.
    r1 = rep.get_col('U', 0)
    r1[0] = '0'
    assert rep.get_col('U', 0) == ['a', 'c']
Пример #4
0
def test_get_row():
    # Check that getting a known row gets the right thing.
    rep = r.RubiksRep(3)
    rep.test_faces()  # Use fake contents.
    r1 = rep.get_row('U', 0)
    assert r1 == ['a', 'b', 'c']
    r2 = rep.get_row('D', 2)
    assert r2 == ['p', 'q', 'r']
    r3 = rep.get_row('F', 1)
    assert r3 == ['v', 'w', 'x']
    r4 = rep.get_row('B', 1)
    assert r4 == ['E', 'F', 'G']
    r5 = rep.get_row('L', 0)
    assert r5 == ['K', 'L', 'M']
    r6 = rep.get_row('R', 2)
    assert r6 == ['Z', '@', '#']
    # Check that changing the contents of a retrieved row doesn't affect
    # the original row.
    r1 = rep.get_row('U', 0)
    r1[0] = '0'
    assert rep.get_row('U', 0) == ['a', 'b', 'c']

    # Check that getting a known row gets the right thing.
    rep = r.RubiksRep(2)
    rep.test_faces()  # Use fake contents.
    r1 = rep.get_row('U', 0)
    assert r1 == ['a', 'b']
    r2 = rep.get_row('D', 1)
    assert r2 == ['g', 'h']
    r3 = rep.get_row('F', 1)
    assert r3 == ['k', 'l']
    r4 = rep.get_row('B', 1)
    assert r4 == ['o', 'p']
    r5 = rep.get_row('L', 0)
    assert r5 == ['q', 'r']
    r6 = rep.get_row('R', 1)
    assert r6 == ['w', 'x']
    # Check that changing the contents of a retrieved row doesn't affect
    # the original row.
    r1 = rep.get_row('U', 0)
    r1[0] = '0'
    assert rep.get_row('U', 0) == ['a', 'b']
Пример #5
0
def test_get_face():
    # Check that face has the appropriate properties
    # (list, size*size).
    # Check that it is equivalent but not identical to the internal face.
    for size in [2, 3]:
        rep = r.RubiksRep(size)
        for face in 'UDFBLR':
            contents = rep.get_face(face)
            assert contents == rep.contents[face]
            assert contents is not rep.contents[face]
            assert type(contents) is list
            assert len(contents) == size
            for i in range(size):
                row = contents[i]
                assert type(row) is list
                assert len(row) == size
Пример #6
0
def test_init():
    face_colors = {'U': 'w', 'D': 'y', 'F': 'r', 'B': 'o', 'L': 'g', 'R': 'b'}

    for size in [2, 3, 4, 5, 6, 7]:
        rep = r.RubiksRep(size)
        assert rep.size == size
        assert type(rep.contents) is dict
        assert set(rep.contents.keys()) == {'U', 'D', 'F', 'B', 'L', 'R'}
        contents = rep.contents

        assert check(lambda: validate_cube(contents, size))

        # Check that each face only has one color.
        for face in contents:
            vals = contents[face]
            assert check(lambda: uniform_list_of_lists(vals, size))

            # Check that it's the right color.
            ok_color = face_colors[face]
            assert check_equal(
                vals[0][0], ok_color,
                f'face: {vals} should only have color {ok_color}')
Пример #7
0
 def __init__(self, size):
     '''Initialize the cube representation.'''
     # Cube representation.
     self.rep = r.RubiksRep(size)
     # Number of moves, quarter-turn metric.
     self.count = 0
Пример #8
0
def test_rotate_cube_Z():
    #
    # Size = 2
    #

    rep = r.RubiksRep(2)
    rep.test_faces()
    face_U_r0 = [['a', 'b'], ['c', 'd']]
    face_D_r0 = [['e', 'f'], ['g', 'h']]
    face_F_r0 = [['i', 'j'], ['k', 'l']]
    face_B_r0 = [['m', 'n'], ['o', 'p']]
    face_L_r0 = [['q', 'r'], ['s', 't']]
    face_R_r0 = [['u', 'v'], ['w', 'x']]

    face_U_r1 = [['s', 'q'], ['t', 'r']]
    face_D_r1 = [['w', 'u'], ['x', 'v']]
    face_F_r1 = [['k', 'i'], ['l', 'j']]
    face_B_r1 = [['n', 'p'], ['m', 'o']]
    face_L_r1 = [['g', 'e'], ['h', 'f']]
    face_R_r1 = [['c', 'a'], ['d', 'b']]

    face_U_0 = rep.get_face('U')
    face_D_0 = rep.get_face('D')
    face_F_0 = rep.get_face('F')
    face_B_0 = rep.get_face('B')
    face_L_0 = rep.get_face('L')
    face_R_0 = rep.get_face('R')
    assert check_equal(face_U_r0, face_U_0, 'invalid U face')
    assert check_equal(face_D_r0, face_D_0, 'invalid D face')
    assert check_equal(face_F_r0, face_F_0, 'invalid F face')
    assert check_equal(face_B_r0, face_B_0, 'invalid B face')
    assert check_equal(face_L_r0, face_L_0, 'invalid L face')
    assert check_equal(face_R_r0, face_R_0, 'invalid R face')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_cube_Z()
    face_U_1 = rep.get_face('U')
    face_D_1 = rep.get_face('D')
    face_F_1 = rep.get_face('F')
    face_B_1 = rep.get_face('B')
    face_L_1 = rep.get_face('L')
    face_R_1 = rep.get_face('R')
    assert check_equal(face_U_r1, face_U_1, 'invalid U face')
    assert check_equal(face_D_r1, face_D_1, 'invalid D face')
    assert check_equal(face_F_r1, face_F_1, 'invalid F face')
    assert check_equal(face_B_r1, face_B_1, 'invalid B face')
    assert check_equal(face_L_r1, face_L_1, 'invalid L face')
    assert check_equal(face_R_r1, face_R_1, 'invalid R face')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    #
    # Size = 3
    #

    rep = r.RubiksRep(3)
    rep.test_faces()
    face_U_r0 = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
    face_D_r0 = [['j', 'k', 'l'], ['m', 'n', 'o'], ['p', 'q', 'r']]
    face_F_r0 = [['s', 't', 'u'], ['v', 'w', 'x'], ['y', 'z', 'A']]
    face_B_r0 = [['B', 'C', 'D'], ['E', 'F', 'G'], ['H', 'I', 'J']]
    face_L_r0 = [['K', 'L', 'M'], ['N', 'O', 'P'], ['Q', 'R', 'S']]
    face_R_r0 = [['T', 'U', 'V'], ['W', 'X', 'Y'], ['Z', '@', '#']]

    face_U_r1 = [['Q', 'N', 'K'], ['R', 'O', 'L'], ['S', 'P', 'M']]
    face_D_r1 = [['Z', 'W', 'T'], ['@', 'X', 'U'], ['#', 'Y', 'V']]
    face_F_r1 = [['y', 'v', 's'], ['z', 'w', 't'], ['A', 'x', 'u']]
    face_B_r1 = [['D', 'G', 'J'], ['C', 'F', 'I'], ['B', 'E', 'H']]
    face_L_r1 = [['p', 'm', 'j'], ['q', 'n', 'k'], ['r', 'o', 'l']]
    face_R_r1 = [['g', 'd', 'a'], ['h', 'e', 'b'], ['i', 'f', 'c']]

    face_U_0 = rep.get_face('U')
    face_D_0 = rep.get_face('D')
    face_F_0 = rep.get_face('F')
    face_B_0 = rep.get_face('B')
    face_L_0 = rep.get_face('L')
    face_R_0 = rep.get_face('R')
    assert check_equal(face_U_r0, face_U_0, 'invalid U face')
    assert check_equal(face_D_r0, face_D_0, 'invalid D face')
    assert check_equal(face_F_r0, face_F_0, 'invalid F face')
    assert check_equal(face_B_r0, face_B_0, 'invalid B face')
    assert check_equal(face_L_r0, face_L_0, 'invalid L face')
    assert check_equal(face_R_r0, face_R_0, 'invalid R face')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_cube_Z()
    face_U_1 = rep.get_face('U')
    face_D_1 = rep.get_face('D')
    face_F_1 = rep.get_face('F')
    face_B_1 = rep.get_face('B')
    face_L_1 = rep.get_face('L')
    face_R_1 = rep.get_face('R')
    assert check_equal(face_U_r1, face_U_1, 'invalid U face')
    assert check_equal(face_D_r1, face_D_1, 'invalid D face')
    assert check_equal(face_F_r1, face_F_1, 'invalid F face')
    assert check_equal(face_B_r1, face_B_1, 'invalid B face')
    assert check_equal(face_L_r1, face_L_1, 'invalid L face')
    assert check_equal(face_R_r1, face_R_1, 'invalid R face')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))
Пример #9
0
def test_rotate_face():
    '''
    Test the rotate_face_cw() and rotate_face_ccw() methods.
    '''

    #
    # Size = 2
    #

    rep = r.RubiksRep(2)
    rep.test_faces()
    face_F_0 = [['i', 'j'], ['k', 'l']]
    face_F_1 = [['k', 'i'], ['l', 'j']]
    face_F_2 = [['l', 'k'], ['j', 'i']]
    face_F_3 = [['j', 'l'], ['i', 'k']]

    face_F = rep.get_face('F')
    face_U_0 = rep.get_face('U')
    assert check_equal(face_F, face_F_0, 'invalid F face')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_1, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_2, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_3, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_0, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_ccw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_3, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_ccw('F')
    rep.rotate_face_ccw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_1, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    #
    # Size = 3
    #

    rep = r.RubiksRep(3)
    rep.test_faces()
    face_F_0 = [['s', 't', 'u'], ['v', 'w', 'x'], ['y', 'z', 'A']]
    face_F_1 = [['y', 'v', 's'], ['z', 'w', 't'], ['A', 'x', 'u']]
    face_F_2 = [['A', 'z', 'y'], ['x', 'w', 'v'], ['u', 't', 's']]
    face_F_3 = [['u', 'x', 'A'], ['t', 'w', 'z'], ['s', 'v', 'y']]

    face_F = rep.get_face('F')
    face_U_0 = rep.get_face('U')
    assert check_equal(face_F, face_F_0, 'invalid F face')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_1, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_2, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_3, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_cw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_0, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_ccw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_3, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))

    rep.rotate_face_ccw('F')
    rep.rotate_face_ccw('F')
    face_F = rep.get_face('F')
    face_U = rep.get_face('U')
    assert check_equal(face_F, face_F_1, 'invalid F face')
    assert check_equal(face_U, face_U_0, 'U face should not have changed')
    assert check(lambda: validate_cube_lite(rep.contents, rep.size))