Esempio n. 1
0
    def test_first_step(self):
        goals = [
            'DFR',
            'DFL',
            'BDL',
            'BDR',
            'BRU',
            'BLU',
            'FLU',
        ]

        for goal in goals:
            c = Cube()
            solver = WhiteFaceSolver(c)
            # Muahaha at that range
            for i in range(1 if goal == 'DFR' else 0, 3):
                c.cubies[goal].facings[goal[i % 3]] = 'W'
                c.cubies[goal].facings[goal[(i + 1) % 3]] = 'Y'
                c.cubies[goal].facings[goal[(i + 2) % 3]] = 'O'

                steps = WhiteFaceSolver.first_step(goal, goal[i % 3])

                for s in steps:
                    c.move(Move(s))

                self.assertIn('W', c.cubies['FRU'].colors,
                              "%s --> %d" % (goal, i))
                self.assertIn('Y', c.cubies['FRU'].colors,
                              "%s --> %d" % (goal, i))
                self.assertIn('O', c.cubies['FRU'].colors,
                              "%s --> %d" % (goal, i))
Esempio n. 2
0
    def test_main(self):
        stdout, stderr = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = StringIO(), StringIO()

        with self.assertRaises(SystemExit):
            utils.main([])

        with self.assertRaises(SystemExit):
            utils.main(['-c'])

        with self.assertRaises(SystemExit):
            utils.main(['-h'])

        with self.assertRaises(SystemExit):
            utils.main(['-i'])

        # Discard stdout
        sys.stdout = StringIO()
        for method in self.solve_methods:
            for i in range(10):
                c = Cube()
                ref_solution = method(c).solution()
                utils.main(['--cube', c.to_naive_cube().get_cube()])
                utils.main(['-i', c.to_naive_cube().get_cube()])
        # Restore stdout and stderr
        sys.stdout, sys.stderr = stdout, stderr
Esempio n. 3
0
 def test_timeout(self):
     c = Cube()
     nc = NaiveCube()
     nc.set_cube("orgyyybbbwgobbbyrywowwrwrwyrorogboogwygyorrwobrggwgbgy")
     c.from_naive_cube(nc)
     with self.assertRaises(Kociemba.Search.SolverTimeoutError):
         solver = Kociemba.KociembaSolver(c)
         solver.solution(timeOut = 1)
Esempio n. 4
0
    def test_pprint(self):
        c = Cube()
        with self.assertRaises(ValueError):
            utils.pprint(None)

        with self.assertRaises(ValueError):
            utils.pprint(1)
        # Just call it and wait not to fail
        utils.pprint(Cube())
Esempio n. 5
0
    def _check_solution(self, c, solution):
        cr = Cube()
        for s in solution:
            c.move(s)
        # Align faces
        while cr.cubies['F'].facings['F'] != c.cubies['F'].facings['F']:
            c.move(Move('Y'))

        for cubie in cr.cubies:
            for facing in cr.cubies[cubie].facings:
                self.assertEqual(cr.cubies[cubie].facings[facing], c.cubies[cubie].facings[facing])
Esempio n. 6
0
    def test_steps(self):
        for orientation, steps in OLLSolver.STEPS.items():
            c = Cube()
            for i, cubie in enumerate(
                ['BLU', 'BU', 'BRU', 'LU', 'U', 'RU', 'FLU', 'FU', 'FRU']):
                for facing in cubie:
                    if facing == orientation[i]:
                        c.cubies[cubie].facings[facing].color = 'Y'
                    else:
                        c.cubies[cubie].facings[facing].color = 'W'

            for s in steps:
                c.move(Move(s))

            for i, cubie in enumerate(
                ['BLU', 'BU', 'BRU', 'LU', 'U', 'RU', 'FLU', 'FU', 'FRU']):
                self.assertEqual(
                    c.cubies[cubie].facings['U'].color,
                    'Y',
                    msg='%s != %s -> Fail OLL with %s orientation on cubie %s'
                    % (c.cubies[cubie].facings['U'], 'Y', orientation, cubie))
Esempio n. 7
0
    def test_steps(self):
        cubies = ['BLU', 'BU', 'BRU', 'LU', 'U', 'RU', 'FLU', 'FU', 'FRU']
        orientations = {
            2: {
                '1': 'BU',
                '3': 'LU',
                '5': 'RU',
                '7': 'FU'
            },
            3: {
                '0': 'BLU',
                '2': 'RBU',
                '6': 'LFU',
                '8': 'FRU'
            }
        }
        cr = Cube()
        for orientation, steps in PLLSolver.STEPS.items():
            c = Cube()
            for i, oriented in enumerate(orientation):
                if i != 4:  # The center cubie doesn't need to be relocated
                    cubie_or = cubies[i]
                    cubie_dest = cubies[int(oriented)]
                    orient_or = orientations[len(cubie_or)][str(i)]
                    orient_dest = orientations[len(cubie_or)][oriented]
                    kwargs = {}
                    for j, orient in enumerate(orient_or):
                        kwargs[orient_dest[j]] = cr.cubies[cubie_or].facings[
                            orient].color
                    c.cubies[cubie_dest] = Cubie(**kwargs)

            for s in steps:
                c.move(Move(s))

            while cr.cubies['F'].facings['F'] != c.cubies['F'].facings['F']:
                c.move(Move('Y'))

            for cubie in cr.cubies:
                for facing in cr.cubies[cubie].facings:
                    self.assertEqual(
                        cr.cubies[cubie].facings[facing],
                        c.cubies[cubie].facings[facing],
                        msg=
                        '%s != %s -> Fail PLL with %s orientation on cubie %s'
                        % (cr.cubies[cubie].facings[facing].color,
                           c.cubies[cubie].facings[facing].color, orientation,
                           cubie))
Esempio n. 8
0
 def _check_solution(self, c, solution):
     cr = Cube()
     for s in solution:
         c.move(s)
     # Align faces
     while cr.cubies['F'].facings['F'] != c.cubies['F'].facings['F']:
         c.move(Move('Y'))
     for cubie in cr.cubies:
         for facing in cr.cubies[cubie].facings:
             self.assertEqual(
                 cr.cubies[cubie].facings[facing],
                 c.cubies[cubie].facings[facing],
                 msg='Invalid solution at cubie %s --> %s != %s' %
                 (cubie, cr.cubies[cubie].facings[facing],
                  c.cubies[cubie].facings[facing]))
Esempio n. 9
0
    def test_solution(self):
        for i in range(1):
            c = Cube()
            cr = Cube()
            c.shuffle(i)
            cross_steps = WhiteCrossSolver(c).solution()
            solution = self._test_solution(c)
            # Align faces
            while cr.cubies['F'].facings['F'] != c.cubies['F'].facings['F']:
                c.move(Move('Y'))

            for cubie in cr.cubies:
                for facing in cr.cubies[cubie].facings:
                    # Upper cubies aren't positioned
                    if 'U' not in cubie:
                        self.assertEqual(cr.cubies[cubie].facings[facing],
                                         c.cubies[cubie].facings[facing])
Esempio n. 10
0
    def test_corner_is_placed(self):
        c = Cube()
        solver = YellowFaceSolver(c)
        for _ in range(4):
            c.move(Move('U'))
            for corner in ['FRU', 'FLU', 'BRU', 'BLU']:
                self.assertTrue(solver.corner_is_placed(corner))
            self.assertTrue(solver.placed_corners())

        for corner in ['FRU', 'FLU', 'BRU', 'BLU']:
            c = Cube()
            solver = YellowFaceSolver(c)
            c.cubies[corner].facings[corner[0]] = 'W'
            self.assertFalse(solver.corner_is_placed(corner))
Esempio n. 11
0
    def test_solution(self):
        cases = [
            ('D', 'F'),
            ('D', 'B'),
            ('D', 'R'),
            ('D', 'L'),
            ('F', 'U'),
            ('F', 'D'),
            ('F', 'R'),
            ('F', 'L'),
            ('B', 'U'),
            ('B', 'D'),
            ('B', 'R'),
            ('B', 'L'),
            ('R', 'U'),
            ('R', 'D'),
            ('R', 'F'),
            ('R', 'B'),
            ('L', 'U'),
            ('L', 'D'),
            ('L', 'F'),
            ('L', 'B'),
        ]

        for c0, c1 in cases:
            c = Cube()
            # Use an "imposible" edge to move, so it is not duped in the cube
            c.cubies[c._t_key(c0 + c1)].facings[c0].color = 'W'
            c.cubies[c._t_key(c0 + c1)].facings[c1].color = 'Y'

            steps = WhiteCrossSolver.first_step(c0, c1)
            for step in steps:
                c.move(Move(step))

            place = c.search_by_colors('W', 'Y')
            self.assertEqual(c.cubies[place].facings['U'], 'W')
            # Weird, but works
            self.assertEqual(c.cubies[place].facings[place.replace('U', '')],
                             'Y')
Esempio n. 12
0
    def test_solve(self):
        c = Cube()
        with self.assertRaises(TypeError):
            utils.solve(c, None)

        with self.assertRaises(ValueError):
            utils.solve(None, "INVALID SOLVER")

        with self.assertRaises(ValueError):
            utils.solve(None, MockSolver)

        with self.assertRaises(ValueError):
            utils.solve(None, self.solve_methods[0])

        with self.assertRaises(ValueError):
            utils.solve(1, self.solve_methods[0])

        for method in self.solve_methods:
            for i in range(10):
                c = Cube()
                ref_solution = method(c).solution()
                s1 = utils.solve(c, method)
                self.assertEqual(ref_solution,
                                 s1,
                                 msg="Failed with Cubie.Cube and method %s" %
                                 method.__class__.__name__)
                # Test with NaiveCube
                s2 = utils.solve(c.to_naive_cube(), method)
                self.assertEqual(ref_solution,
                                 s2,
                                 msg="Failed with Cubie.Cube and method %s" %
                                 method.__class__.__name__)

                # Test with string representation
                s3 = utils.solve(c.to_naive_cube().get_cube(), method)
                self.assertEqual(ref_solution,
                                 s3,
                                 msg="Failed with Cubie.Cube and method %s" %
                                 method.__class__.__name__)
Esempio n. 13
0
    def test_steps(self):
        for corner in F2LSolver.STEPS:
            for edge, steps in F2LSolver.STEPS[corner].items():
                c = Cube()
                tmp = c.cubies[Cube._t_key(corner)]
                orig_corner = ''.join([
                    tmp.facings[corner[0]].color, tmp.facings[corner[1]].color,
                    tmp.facings[corner[2]].color
                ])
                tmp = c.cubies[Cube._t_key(edge)]
                orig_edge = ''.join([
                    tmp.facings[edge[0]].color,
                    tmp.facings[edge[1]].color,
                ])

                for s in steps:
                    c.move(Move(s))

                dest_corner = ''.join([
                    c.cubies['DFR'].facings['F'].color,
                    c.cubies['DFR'].facings['R'].color,
                    c.cubies['DFR'].facings['D'].color,
                ])

                dest_edge = ''.join([
                    c.cubies['FR'].facings['F'].color,
                    c.cubies['FR'].facings['R'].color,
                ])

                self.assertEqual(
                    dest_corner,
                    orig_corner,
                    msg='Failed F2L corner check corner:%s edge:%s (%s != %s)'
                    % (corner, edge, dest_corner, orig_corner))
                self.assertEqual(
                    dest_edge,
                    orig_edge,
                    msg='Failed F2L edge check corner:%s edge:%s (%s != %s)' %
                    (corner, edge, dest_edge, orig_edge))
Esempio n. 14
0
 def test_edges_are_placed(self):
     c = Cube()
     solver = YellowFaceSolver(c)
     for _ in range(4):
         c.move(Move('U'))
         self.assertTrue(solver.edges_are_placed())
Esempio n. 15
0
 def test_solution(self):
     for i in range(10):
         c = Cube()
         c.shuffle(i)
         solution = self._test_solution(c)
         self._check_solution(c, solution)
Esempio n. 16
0
 def test_solved_solution(self):
     '''Try to solve an already solved cube'''
     c = Cube()
     solution = self._test_solution(c)
     self._check_solution(c, solution)
Esempio n. 17
0
    def test_second_step(self):
        # Case 1
        c = Cube()
        c.cubies['FRU'].facings['F'] = 'W'
        c.cubies['FRU'].facings['R'] = 'Y'
        c.cubies['FRU'].facings['U'] = 'O'
        steps = WhiteFaceSolver.second_step('F')
        for s in steps:
            c.move(Move(s))

        self.assertEqual(c.cubies['DFR'].facings['D'], 'W')
        self.assertEqual(c.cubies['DFR'].facings['F'], 'O')
        self.assertEqual(c.cubies['DFR'].facings['R'], 'Y')
        # Case 2
        c = Cube()
        c.cubies['FRU'].facings['F'] = 'O'
        c.cubies['FRU'].facings['R'] = 'W'
        c.cubies['FRU'].facings['U'] = 'Y'
        steps = WhiteFaceSolver.second_step('R')
        for s in steps:
            c.move(Move(s))

        self.assertEqual(c.cubies['DFR'].facings['D'], 'W')
        self.assertEqual(c.cubies['DFR'].facings['F'], 'O')
        self.assertEqual(c.cubies['DFR'].facings['R'], 'Y')
        # Case 3
        c = Cube()
        c.cubies['FRU'].facings['F'] = 'O'
        c.cubies['FRU'].facings['R'] = 'Y'
        c.cubies['FRU'].facings['U'] = 'W'
        steps = WhiteFaceSolver.second_step('U')
        for s in steps:
            c.move(Move(s))

        self.assertEqual(c.cubies['DFR'].facings['D'], 'W')
        self.assertEqual(c.cubies['DFR'].facings['F'], 'Y')
        self.assertEqual(c.cubies['DFR'].facings['R'], 'O')
Esempio n. 18
0
 def test_is_solved(self):
     c = Cube()
     solver = SecondLayerSolver(c)
     self.assertTrue(solver.is_solved())