def test_with_both_as_positional(self): numbered_cell = NumberedCell(4, 3, 2, 12, 46) expected = "NumberedCell(seq=4,r=3,c=2,a=12,d=46)" actual = str(numbered_cell) self.assertEqual(expected, actual) self.assertEqual(12, numbered_cell.a) self.assertEqual(46, numbered_cell.d)
def test_just_r_and_c(self): numbered_cell = NumberedCell(1, 3, 2) expected = "NumberedCell(seq=1,r=3,c=2)" actual = str(numbered_cell) self.assertEqual(expected, actual) self.assertEqual(0, numbered_cell.a) self.assertEqual(0, numbered_cell.d)
def test_with_d(self): numbered_cell = NumberedCell(45, 3, 2, d=46) expected = "NumberedCell(seq=45,r=3,c=2,d=46)" actual = str(numbered_cell) self.assertEqual(expected, actual) self.assertEqual(0, numbered_cell.a) self.assertEqual(46, numbered_cell.d)
def test_with_both(self): numbered_cell = NumberedCell(22, 3, 2, d=46, a=12) expected = "NumberedCell(seq=22,r=3,c=2,a=12,d=46)" actual = str(numbered_cell) self.assertEqual(expected, actual) self.assertEqual(12, numbered_cell.a) self.assertEqual(46, numbered_cell.d)
def test_with_a(self): numbered_cell = NumberedCell(1, 3, 2, a=4) expected = "NumberedCell(seq=1,r=3,c=2,a=4)" actual = str(numbered_cell) self.assertEqual(expected, actual) self.assertEqual(4, numbered_cell.a) self.assertEqual(0, numbered_cell.d)
def test_from_json(self): jsonstr = '{"seq": 17, "r": 3, "c": 2, "a": 4}' nc = NumberedCell.from_json(jsonstr) self.assertEqual(17, nc.seq) self.assertEqual(3, nc.r) self.assertEqual(2, nc.c) self.assertEqual(4, nc.a) self.assertEqual(0, nc.d)
def get_numbered_cells(self): """ Finds list of all cells that start a word """ # If already calculated, return that (lazy instantiation) if self.numbered_cells: return self.numbered_cells # Otherwise calculate and store n = self.n nclist = [] for r in range(1, n + 1): for c in range(1, n + 1): # Ignore black cells if self.is_black_cell(r, c): continue # See if this is the start of an "across" word a = 0 if c == 1 or self.is_black_cell(r, c - 1): # This is the beginning of an "across" word # Find the (r, c) of the stopping point, which is either # the next black cell, or the edge of the puzzle for cprime in range(c + 1, n + 1): if self.is_black_cell(r, cprime): a = cprime - c break if cprime == n: a = cprime + 1 - c break if a < 2: a = 0 # Same for "down" word d = 0 if r == 1 or self.is_black_cell(r - 1, c): # This is the beginning of a "down" word # Find the (r, c) of the stopping point, which is either # the next black cell, or the edge of the puzzle for rprime in range(r + 1, n + 1): if self.is_black_cell(rprime, c): d = rprime - r break if rprime == n: d = rprime + 1 - r break if d < 2: d = 0 if a or d: seq = 1 + len(nclist) numbered_cell = NumberedCell(seq, r, c, a=a, d=d) nclist.append(numbered_cell) self.numbered_cells = nclist return nclist
def test_new_grid(self): puzzle = TestPuzzle.create_solved_atlantic_puzzle() oldjson = puzzle.to_json() grid = Grid.from_json(puzzle.to_json()) grid.add_black_cell(4, 4) puzzle.replace_grid(grid) newjson = puzzle.to_json() import json old = json.loads(oldjson) new = json.loads(newjson) # Compare black cells self.assertIn([4, 4], new['black_cells']) self.assertIn([6, 6], new['black_cells']) new['black_cells'].remove([4, 4]) new['black_cells'].remove([6, 6]) self.assertListEqual(old['black_cells'], new['black_cells']) # Compare numbered cells expected = [ NumberedCell(seq=1, r=1, c=1, a=3, d=4), NumberedCell(seq=2, r=1, c=2, a=0, d=4), NumberedCell(seq=3, r=1, c=3, a=0, d=9), NumberedCell(seq=4, r=1, c=6, a=4, d=5), NumberedCell(seq=5, r=1, c=7, a=0, d=9), NumberedCell(seq=6, r=1, c=8, a=0, d=4), NumberedCell(seq=7, r=1, c=9, a=0, d=3), NumberedCell(seq=8, r=2, c=1, a=4, d=0), NumberedCell(seq=9, r=2, c=4, a=0, d=2), NumberedCell(seq=10, r=2, c=6, a=4, d=0), NumberedCell(seq=11, r=3, c=1, a=9, d=0), NumberedCell(seq=12, r=3, c=5, a=0, d=5), NumberedCell(seq=13, r=4, c=1, a=3, d=0), NumberedCell(seq=14, r=4, c=5, a=4, d=0), NumberedCell(seq=15, r=5, c=3, a=5, d=0), NumberedCell(seq=16, r=5, c=4, a=0, d=5), NumberedCell(seq=17, r=6, c=2, a=4, d=4), NumberedCell(seq=18, r=6, c=7, a=3, d=0), NumberedCell(seq=19, r=6, c=8, a=0, d=4), NumberedCell(seq=20, r=6, c=9, a=0, d=4), NumberedCell(seq=21, r=7, c=1, a=9, d=3), NumberedCell(seq=22, r=7, c=6, a=0, d=2), NumberedCell(seq=23, r=8, c=1, a=4, d=0), NumberedCell(seq=24, r=8, c=6, a=4, d=0), NumberedCell(seq=25, r=9, c=1, a=4, d=0), NumberedCell(seq=26, r=9, c=7, a=3, d=0), ] actual = [] for x in new['numbered_cells']: jsonstr = json.dumps(x) nc = NumberedCell.from_json(jsonstr) actual.append(nc) self.assertListEqual(expected, actual) # Compare clues oldclues = { x['text']: x['clue'] for x in old['across_words'] + old['down_words'] } newclues = { x['text']: x['clue'] for x in new['across_words'] + new['down_words'] } for k, v in newclues.items(): if k in oldclues: oldclue = oldclues[k] self.assertEqual(oldclue, newclues[k])
def notest_print_json(self): nc = NumberedCell(17, 3, 2, 4, 5) print(f"DEBUG: json={nc.to_json()}")
def test_json_round_trip(self): nc1 = NumberedCell(17, 3, 2, 4, 5) jsonstr = nc1.to_json() nc2 = NumberedCell.from_json(jsonstr) self.assertEqual(nc1, nc2)
def test_to_json(self): nc = NumberedCell(17, 3, 2, 4, 5) expected = '{"seq": 17, "r": 3, "c": 2, "a": 4, "d": 5}' actual = nc.to_json() self.assertEqual(expected, actual)
def test_hash(self): nc1 = NumberedCell(4, 3, 2, 12, 46) self.assertIsNotNone(hash(nc1))
def test_equals_when_equal(self): nca = NumberedCell(17, 3, 2, 4, 5) ncb = NumberedCell(17, 3, 2, 3 + 1, 4 + 1) self.assertEqual(nca, ncb)
def test_both(self): self.assertIn(NumberedCell(15, 6, 2, 8, 4), self.nclist)
def test_down_only(self): self.assertIn(NumberedCell(5, 1, 7, 0, 9), self.nclist)
def test_across_only(self): self.assertIn(NumberedCell(8, 2, 1, 4, 0), self.nclist) self.assertIn(NumberedCell(22, 9, 7, 3, 0), self.nclist)