33from queen_attack import board , can_attack
44
55
6+ # Tests adapted from `problem-specifications//canonical-data.json` @ v2.0.0
7+
68class QueenAttackTest (unittest .TestCase ):
7- def test_board1 (self ):
8- ans = ['________' ,
9- '________' ,
10- '___W____' ,
11- '________' ,
12- '________' ,
13- '______B_' ,
14- '________' ,
15- '________' ]
16- self .assertEqual (board ((2 , 3 ), (5 , 6 )), ans )
179
18- def test_board2 (self ):
19- ans = ['______W_' ,
20- '_______B' ,
21- '________' ,
22- '________' ,
23- '________' ,
24- '________' ,
25- '________' ,
26- '________' ]
27- self .assertEqual (board ((0 , 6 ), (1 , 7 )), ans )
10+ def test_queen_valid_position (self ):
11+ try :
12+ board ((1 , 1 ), (2 , 2 ))
13+ except ValueError :
14+ self .fail ("Unexpected Exception" )
2815
29- def test_attack_true1 (self ):
30- self .assertIs (can_attack ((2 , 3 ), (5 , 6 )), True )
16+ def test_queen_negative_row (self ):
17+ with self .assertRaises (ValueError ):
18+ board ((1 , 1 ), (- 2 , 2 ))
19+
20+ def test_queen_invalid_row (self ):
21+ with self .assertRaises (ValueError ):
22+ board ((1 , 1 ), (8 , 4 ))
3123
32- def test_attack_true2 (self ):
33- self .assertIs (can_attack ((2 , 6 ), (5 , 3 )), True )
24+ def test_queen_negative_column (self ):
25+ with self .assertRaises (ValueError ):
26+ board ((1 , 1 ), (2 , - 2 ))
27+
28+ def test_queen_invalid_column (self ):
29+ with self .assertRaises (ValueError ):
30+ board ((1 , 1 ), (4 , 8 ))
3431
35- def test_attack_true3 (self ):
36- self .assertIs (can_attack ((2 , 4 ), (2 , 7 )), True )
32+ def test_attack_false (self ):
33+ self .assertIs (can_attack ((2 , 4 ), (6 , 6 )), False )
3734
38- def test_attack_true4 (self ):
39- self .assertIs (can_attack ((5 , 4 ), (2 , 4 )), True )
35+ def test_attack_same_row (self ):
36+ self .assertIs (can_attack ((2 , 4 ), (2 , 6 )), True )
4037
41- def test_attack_true5 (self ):
42- self .assertIs (can_attack ((1 , 1 ), (6 , 6 )), True )
38+ def test_attack_same_column (self ):
39+ self .assertIs (can_attack ((4 , 5 ), (2 , 5 )), True )
4340
44- def test_attack_true6 (self ):
45- self .assertIs (can_attack ((0 , 6 ), (1 , 7 )), True )
41+ def test_attack_diagonal1 (self ):
42+ self .assertIs (can_attack ((2 , 2 ), (0 , 4 )), True )
4643
47- def test_attack_false1 (self ):
48- self .assertIs (can_attack ((4 , 2 ), (0 , 5 )), False )
44+ def test_attack_diagonal2 (self ):
45+ self .assertIs (can_attack ((2 , 2 ), (3 , 1 )), True )
4946
50- def test_attack_false2 (self ):
51- self .assertIs (can_attack ((2 , 3 ), (4 , 7 )), False )
47+ def test_attack_diagonal3 (self ):
48+ self .assertIs (can_attack ((2 , 2 ), (1 , 1 )), True )
49+
50+ def test_attack_diagonal4 (self ):
51+ self .assertIs (can_attack ((2 , 2 ), (5 , 5 )), True )
52+
53+ # Tests beyond this point are not part of the canonical data.
5254
5355 # If either board or can_attack are called with an invalid board position
5456 # they should raise a ValueError with a meaningful error message.
55- def test_invalid_position_board (self ):
56- with self .assertRaises (ValueError ):
57- board ((0 , 0 ), (7 , 8 ))
58-
5957 def test_invalid_position_can_attack (self ):
6058 with self .assertRaises (ValueError ):
6159 can_attack ((0 , 0 ), (7 , 8 ))
@@ -68,6 +66,28 @@ def test_queens_same_position_can_attack(self):
6866 with self .assertRaises (ValueError ):
6967 can_attack ((2 , 2 ), (2 , 2 ))
7068
69+ def test_board1 (self ):
70+ ans = ['________' ,
71+ '________' ,
72+ '___W____' ,
73+ '________' ,
74+ '________' ,
75+ '______B_' ,
76+ '________' ,
77+ '________' ]
78+ self .assertEqual (board ((2 , 3 ), (5 , 6 )), ans )
79+
80+ def test_board2 (self ):
81+ ans = ['______W_' ,
82+ '_______B' ,
83+ '________' ,
84+ '________' ,
85+ '________' ,
86+ '________' ,
87+ '________' ,
88+ '________' ]
89+ self .assertEqual (board ((0 , 6 ), (1 , 7 )), ans )
90+
7191
7292if __name__ == '__main__' :
7393 unittest .main ()
0 commit comments