#!/usr/bin/python
import unittest
import rubik

# For these tests to work, you must have a file named rubik.py
# which contains the functions rubik_bfs(moves, level) and
# shortest_path(start_position, end_position)

class TestRubik(unittest.TestCase):
    def setUp(self):
        global quarter_twists_permutations
        quarter_twists_permutations = {}
        quarter_twists_permutations['F'] = rubik.F
        quarter_twists_permutations['Fi'] = rubik.Fi
        quarter_twists_permutations['L'] = rubik.L
        quarter_twists_permutations['Li'] = rubik.Li
        quarter_twists_permutations['U'] = rubik.U
        quarter_twists_permutations['Ui'] = rubik.Ui
        pass

    def testLevel1(self):
        ans = rubik.rubik_bfs(rubik.quarter_twists, 1)
        self.assertEqual(ans, 6)

    def testLevel4(self):
        ans = rubik.rubik_bfs(rubik.quarter_twists, 4)
        self.assertEqual(ans, 534)

#    def test10(self):
#        ans = rubik.rubik_bfs(rubik.quarter_twists, 10)
#        self.assertEqual(ans, 930588)

    def testShortestPath0(self):
        start = rubik.I
        end = rubik.I
        ans = rubik.shortest_path(start, end)
        self.assertEqual(len(ans), 0)

    def testShortestPath1(self):
        start = rubik.I
        end = rubik.perm_apply(rubik.F, start)
        ans = rubik.shortest_path(start, end)
        self.assertEqual(len(ans), 1)
        self.assertEqual(ans, ['F'])

    def testShortestPath14(self):
        start = (6, 7, 8, 20, 18, 19, 3, 4, 5, 16, 17, 15, 0, 1, 2, 14, 12, 13, 10, 11, 9, 21, 22, 23)
        end = rubik.I
        ans = rubik.shortest_path(start, end)
        self.assertEqual(len(ans), 14)
        for twist in ans:
            start = rubik.perm_apply(quarter_twists_permutations[twist], start)
        self.assertEqual(start, end)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestRubik)
    unittest.TextTestRunner(verbosity=2).run(suite)
