1
exercism fetch nim queen-attack

queen_attack_test.nim

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
70
71
72
73
74
75
76
77
78
79
import unittest

import queen_attack

const answer1 = @["________",
                  "________",
                  "___W____",
                  "________",
                  "________",
                  "______B_",
                  "________",
                  "________"]

test "board1":
  check board((2, 3), (5, 6)) == answer1

const answer2 = @["______W_",
                  "_______B",
                  "________",
                  "________",
                  "________",
                  "________",
                  "________",
                  "________"]

test "board2":
  check board((0, 6), (1, 7)) == answer2

test "true attacks":
  var positions: seq[tuple[white, black: tuple[rank, file: int]]] = @[((2, 3), (5, 6)),
                                                                      ((2, 6), (5, 3)),
                                                                      ((2, 4), (2, 7)),
                                                                      ((5, 4), (2, 4)),
                                                                      ((1, 1), (6, 6)),
                                                                      ((0, 6), (1, 7))]
  for position in positions:
    check canAttack(position.white, position.black)

test "false attacks":
  var positions: seq[tuple[white, black: tuple[rank, file: int]]] = @[((4, 2), (0, 5)),
                                                                      ((2, 3), (4, 7))]
  for position in positions:
    check (not canAttack(position.white, position.black))

test "invalid position on board":
  expect(ValueError):
    discard board((0, 0), (7, 8))

test "invalid position in canAttack":
  expect(ValueError):
    discard canAttack((0, 0), (7, 8))

test "true attacks":
  var positions: seq[tuple[white, black: tuple[rank, file: int]]] = @[((2, 3), (5, 6)),
                                                                      ((2, 6), (5, 3)),
                                                                      ((2, 4), (2, 7)),
                                                                      ((5, 4), (2, 4)),
                                                                      ((1, 1), (6, 6)),
                                                                      ((0, 6), (1, 7))]
  for position in positions:
    check canAttack(position.white, position.black)

test "false attacks":
  var positions: seq[tuple[white, black: tuple[rank, file: int]]] = @[((4, 2), (0, 5)),
                                                                      ((2, 3), (4, 7))]
  for position in positions:
    check (not canAttack(position.white, position.black))

test "invalid position on board":
  expect(ValueError):
    discard board((0, 0), (7, 8))

test "queens in same position on board":
  expect(ValueError):
    discard board((2, 2), (2, 2))

test "queens in same position in canAttack":
  expect(ValueError):
    discard canAttack((2, 2), (2, 2))