forked from pyav/Sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sudoku.py
69 lines (59 loc) · 2.12 KB
/
test_sudoku.py
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
#!/usr/bin/env python3
'''
This program reads an input testcase file and constructs an input string
which it then sends to the Sudoku solver program. It then gets the return value
which tells whether a solution is possible for the Sudoku problem given in the
input file having testcases.
__author__ = pyav
'''
import os
import sys
import time
import subprocess
from subprocess import PIPE, STDOUT, Popen
def is_solution_possible(byte_code_name, final_input_val):
'''
Run the Java program which solves the given Sudoku problem and send the
input string having the values in the matrix. The first value contains the
size of the square 2D matrix.
'''
cmd_run = ['java', byte_code_name, final_input_val]
p = Popen(cmd_run, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
stdout, stderr = p.communicate(final_input_val.encode())
return stdout
def run_code(byte_code_name, testcase_file):
count_test = 1
f = open(testcase_file, "r")
# Get the number of testcases
num_tests = int(f.readline())
# Iterate for all the testcases
for i in range(0, num_tests):
f.readline()
size = int(f.readline())
final_input_val = str()
for i in range(0, size):
final_input_val += f.readline().rstrip('\n') + " "
final_input_val = str(size) + " " + final_input_val[:-1]
# Send final_input_val to the java program and get the result.
ret = is_solution_possible(byte_code_name, final_input_val)
print("Test " + str(count_test) + ": " + str(ret))
count_test += 1
f.close()
def compile_code(code_file):
cmd_compile = ['javac', code_file]
subprocess.check_call(cmd_compile)
byte_code_name = os.path.splitext(code_file)[0]
count = 0
if os.path.exists(byte_code_name + '.class'):
return byte_code_name
print('Not found ' + byte_code_name + '.class')
return None
def main():
code_file = 'SudokuSolver.java'
byte_code_name = compile_code(code_file)
if byte_code_name == None:
sys.exit(1)
testcase_file = './testcases_sudoku.txt'
run_code(byte_code_name, testcase_file)
main()
# __eof__