def helper(input_filename, expected_filename, R, M_threshold, B_threshold, max_num_steps, expected_num_relocations): ''' Do one simulation with the specified parameters (R, threshold, max_num_steps) starting from the specified input file. Match actual grid generated with the expected grid and match expected steps and actual steps. Inputs: input_filename: (string) name of the input grid file expected_filename: (string) name of the expected grid file. R: (int) radius for the neighborhood M_threshold: lower bound for similarity score for maroon homeowners B_threshold: lower bound for similarity score for blue homeowners max_steps: (int) maximum number of steps to do expected_num_relocations: (int) expected number of relocations performed during the simulation ''' input_filename = os.path.join(BASE_DIR, input_filename) actual_grid = utility.read_grid(input_filename) expected_num_homeowners = count_homeowners(actual_grid) opens = utility.find_opens(actual_grid) actual_num_relocations = do_simulation(actual_grid, R, M_threshold, B_threshold, max_num_steps, opens) actual_num_homeowners = count_homeowners(actual_grid) expected_filename = os.path.join(BASE_DIR, expected_filename) expected_grid = utility.read_grid(expected_filename) if actual_num_relocations != expected_num_relocations: s = ("actual and expected values number of relocations do not match\n" " got {:d}, expected {:d}") s = s.format(actual_num_relocations, expected_num_relocations) pytest.fail(s) if actual_num_homeowners != expected_num_homeowners: if actual_num_homeowners <= expected_num_homeowners: s = "Homeowners are fleeing the city!\n" else: s = ("The city is gaining homeowners.\n") s += (" Actual number of homeowners: {:d}\n" " Expected number of homeowners: {:d}\n") s = s.format(actual_num_homeowners, expected_num_homeowners) pytest.fail(s) mismatch = utility.find_mismatch(actual_grid, expected_grid) if mismatch: (i, j) = mismatch s = ("actual and expected grid values do not match " "at location ({:d}, {:d})\n") s = s.format(i, j) s = s + " got {}, expected {}".format(actual_grid[i][j], expected_grid[i][j]) pytest.fail(s)
def helper_check_steps(input_filename, R, threshold, max_num_steps, expected_num_steps): ''' Do one simulation with the specified parameters (R, threshold, max_num_steps) starting from the specified input file. Compare the actual number of steps taken to the expected number of steps. ''' grid = utility.read_grid(TEST_DIR + input_filename) actual = do_simulation(grid, R, threshold, max_num_steps) assert(actual == expected_num_steps)
def helper_check_relocations(input_filename, expected_filename, R, threshold, max_num_steps): ''' Do one simulation with the specified parameters (R, threshold, max_num_steps) starting from the specified input file. Match actual grid generated with the expected grid. ''' grid = utility.read_grid(TEST_DIR + input_filename) do_simulation(grid, R, threshold, max_num_steps) expected_grid = utility.read_grid(TEST_DIR + expected_filename) # compare the actual state of the city against the expected state for i in range(len(grid)): for j in range(len(grid[0])): expected = expected_grid[i][j] # strip unsatisfied indicator from the expected file, # if necessary expected = expected if "U" not in expected else expected[1] if grid[i][j] != expected: s = "actual and expected values do not match at location ({:d}, {:d})\n".format(i, j) s = s + "got {}, expected {}".format(grid[i][j], expected) pytest.fail(s)
def helper(input_filename, expected_filename, R, threshold, max_num_steps, expected_num_steps): ''' Do one simulation with the specified parameters (R, threshold, max_num_steps) starting from the specified input file. Match actual grid generated with the expected grid and match expected steps and actual steps. ''' input_filename = os.path.join(BASE_DIR, input_filename) actual_grid = utility.read_grid(input_filename) actual_num_steps = do_simulation(actual_grid, R, threshold, max_num_steps) actual_num_homeowners = count_homeowners(actual_grid) expected_filename = os.path.join(BASE_DIR, expected_filename) expected_grid = utility.read_grid(expected_filename) expected_num_homeowners = count_homeowners(expected_grid) if actual_num_homeowners != expected_num_homeowners: if actual_num_homeowners <= expected_num_homeowners: s = "Homeowners are fleeing the city!\n" else: s = "City is gaining homeowners.\n" s += " Actual number of homeowners: {:d}\n".format(actual_num_homeowners) s += " Expected number of homeowners: {:d}\n".format(expected_num_homeowners) pytest.fail(s) mismatch = utility.find_mismatch(actual_grid, expected_grid) if mismatch: s = "actual and expected grid values do not match at location ({:d}, {:d})\n" s = s.format(mismatch[0], mismatch[1]) actual = actual_grid[mismatch[0]][mismatch[1]] expected = expected_grid[mismatch[0]][mismatch[1]] s += " got {}, expected {}".format(actual, expected) pytest.fail(s) # check steps only when expected steps >= 0 if expected_num_steps >= 0 and actual_num_steps != expected_num_steps: s = "actual and expected values number of steps do not match\n" s = s + " got {:d}, expected {:d}".format(actual_num_steps, expected_num_steps) pytest.fail(s)