actual_output_str = call(base_url, step['method'], input) try: actual_output = json.loads(actual_output_str) except Exception, e: fail(filename, str(i + 1), step['method'], input, step['output'], actual_output_str) raise e try: new_vars, replaced_output = var_replace(step['output'], actual_output) except Exception, e: fail(filename, str(i + 1), step['method'], input, step['output'], actual_output) raise e vars = dict(vars.items() + new_vars.items()) try: diff = data_diff(replaced_output, actual_output) if diff: fail(filename, str(i + 1), step['method'], input, replaced_output, actual_output) exit() except Exception, e: fail(filename, str(i + 1), step['method'], input, replaced_output, actual_output) raise e if __name__ == "__main__": import os import sys dir = 'tests/' start_time = time.time() if len(sys.argv) == 2: run_test(BASE_URL, dir + sys.argv[1] + '.json') else:
def solve_from_file(filename): f = open(filename) lines = f.readlines() #convert into a list of lists and remove whitespace grid = [] width = 0 for line in lines: line = line.rstrip() if line: row = string.split(line, "\t") width = max(width, len(row)) grid.append(row) height = len(grid) #convert into integers and normalize row width y = 0 for row in grid: new_row = [] for x in xrange(width): try: i = int(row[x]) except IndexError: i = None except ValueError: if row[x] == 'T': i = True elif row[x] == 'F': i = False else: i = None new_row.append(i) grid[y] = new_row y += 1 #measure height and width of inner grid x = width - 1 y = height - 1 while x >= 0: if type(grid[y][x]) == types.IntType: break x -= 1 inner_width = width - x - 1 x = width - 1 y = height - 1 while y >= 0: if type(grid[y][x]) == types.IntType: break y -= 1 inner_height = len(grid) - y - 1 print "board size: %dx%d" % (inner_width, inner_height) #ensure inner grid is valid for x in xrange(width - inner_width, width): for y in xrange(height - inner_height, height): if type(grid[y][x]) != types.NoneType and type(grid[y][x]) != types.BooleanType: print 'invalid board' exit() #ensure upper left is empty for x in xrange(width - inner_width): for y in xrange(height - inner_height): if grid[y][x] != None: print 'invalid board' exit() counts_width = width - inner_width counts_height = height - inner_height #populate row counts row_counts = [] for y in xrange(counts_height, height): counts = [] for x in xrange(counts_width): count = grid[y][x] if count: counts.append(count) row_counts.append(counts) #populate column counts col_counts = [] for x in xrange(counts_width, width): counts = [] for y in xrange(counts_height): count = grid[y][x] if count: counts.append(count) col_counts.append(counts) #redo grid width = inner_width height = inner_height inner_grid = [] for y in xrange(height): inner_grid.append([]) for x in xrange(width): inner_grid[y].append(grid[y+counts_height][x+counts_width]) grid = solve(row_counts, col_counts, inner_grid) complete = True for row in grid: for item in row: if item == None: complete = False if complete: l = check_solution(grid) if data_diff(l[0], row_counts) or data_diff(l[1], col_counts): print 'FAIL!' exit() for y in xrange(counts_height): for x in xrange(counts_width): sys.stdout.write("\t") for counts in col_counts: try: sys.stdout.write(str(counts[-counts_height+y])) except: pass sys.stdout.write("\t") print y = 0 for row in grid: for x in xrange(counts_width): try: sys.stdout.write(str(row_counts[y][-counts_width+x])) except: pass sys.stdout.write("\t") for square in row: if square == True: sys.stdout.write('T') elif square == False: sys.stdout.write('F') sys.stdout.write("\t") print y += 1