Skip to content

Commit

Permalink
Hotfix/console script (#2)
Browse files Browse the repository at this point in the history
* Add requirements into setup.py to avoid reading requirements.txt file

* Fix setup.py requirements

* Update CHANGESET

* Update MANIFEST.in

* Fix solution print in console_script and add time

* Add already solved cube tests

* Improve tests on utils.main()
  • Loading branch information
Wiston999 authored May 7, 2017
1 parent 4559e5d commit f4a7a60
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 42 deletions.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include requirements.txt rubik_solver/CoordCube/*.csv
include requirements.txt
recursive-include rubik_solver/CoordCube *.csv
9 changes: 6 additions & 3 deletions rubik_solver/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
import argparse
import time
from past.builtins import basestring
from .Solver import Solver
from .Solver import Beginner
Expand Down Expand Up @@ -60,15 +61,17 @@ def pprint(cube, color = True):
printer = TtyPrinter(cube, color)
printer.pprint()

def main():
def main(argv = None):
arg_parser = argparse.ArgumentParser(description = 'rubik_solver command line tool')
arg_parser.add_argument('-i', '--cube', dest = 'cube', required = True, help = 'Cube definition string')
arg_parser.add_argument('-c', '--color', dest = 'color', default = True, action = 'store_false', help = 'Disable use of colors with TtyPrinter')
arg_parser.add_argument('-s', '--solver', dest = 'solver', default = 'Beginner', choices = METHODS.keys(), help = 'Solver method to use')
args = arg_parser.parse_args()
args = arg_parser.parse_args(argv)

cube = args.cube.lower()
print ("Read cube", cube)
pprint(cube, args.color)

print ("Solution", ', '.join(solve(cube, METHODS[args.solver])))
start = time.time()
print ("Solution", ', '.join(map(str, solve(cube, METHODS[args.solver]))))
print ("Solved in", time.time() - start, "seconds")
41 changes: 25 additions & 16 deletions tests/test_BeginnerSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_first_step(self):
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))

Expand All @@ -87,7 +87,7 @@ def test_second_step(self):
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')
Expand All @@ -99,7 +99,7 @@ def test_second_step(self):
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')
Expand All @@ -111,7 +111,7 @@ def test_second_step(self):
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')
Expand All @@ -121,7 +121,7 @@ def test_is_solved(self):
c = Cube()
solver = SecondLayerSolver(c)
self.assertTrue(solver.is_solved())

# Dunno how to test the solution function

class TestYellowFaceSolver(unittest.TestCase):
Expand Down Expand Up @@ -153,18 +153,27 @@ def _test_solution(self, c):
solver = Beginner.BeginnerSolver(c)
return solver.solution()

def test_solved_solution(self):
'''Try to solve an already solved cube'''
c = Cube()
solution = self._test_solution(c)
self._check_solution(c, solution)

def test_solution(self):
for i in range(100):
c = Cube()
cr = Cube()
c.shuffle(i)
solution = self._test_solution(c)
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])
self._check_solution(c, solution)

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])
35 changes: 22 additions & 13 deletions tests/test_CFOPSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,30 @@ def _test_solution(self, c):
solver = CFOP.CFOPSolver(c)
return solver.solution()

def test_solved_solution(self):
'''Try to solve an already solved cube'''
c = Cube()
solution = self._test_solution(c)
self._check_solution(c, solution)

def test_solution(self):
for i in range(100):
c = Cube()
cr = Cube()
c.shuffle(i)
solution = self._test_solution(c)
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])
)
self._check_solution(c, solution)

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])
)
27 changes: 18 additions & 9 deletions tests/test_KociembaSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@ def _test_solution(self, c):
solver = Kociemba.KociembaSolver(c)
return solver.solution()

def test_solved_solution(self):
'''Try to solve an already solved cube'''
c = Cube()
solution = self._test_solution(c)
self._check_solution(c, solution)

def test_solution(self):
for i in range(10):
c = Cube()
cr = Cube()
c.shuffle(i)
solution = self._test_solution(c)
for s in solution:
c.move(s)
# Align faces
while cr.cubies['F'].facings['F'] != c.cubies['F'].facings['F']:
c.move(Move('Y'))
self._check_solution(c, solution)

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])
for cubie in cr.cubies:
for facing in cr.cubies[cubie].facings:
self.assertEqual(cr.cubies[cubie].facings[facing], c.cubies[cubie].facings[facing])

def test_timeout(self):
c = Cube()
Expand Down
29 changes: 29 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys
import StringIO
from rubik_solver.Cubie import Cube
from rubik_solver import utils
import timeout_decorator
Expand Down Expand Up @@ -60,3 +62,30 @@ def test_pprint(self):
utils.pprint(1)
# Just call it and wait not to fail
utils.pprint(Cube())

def test_main(self):
stdout, stderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = StringIO.StringIO(), 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.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

0 comments on commit f4a7a60

Please sign in to comment.