move tests to seperate file

This commit is contained in:
Nico 2022-03-30 20:12:43 +01:00
parent 9093548776
commit af9d9436f5
2 changed files with 51 additions and 37 deletions

47
main-test.rkt Normal file
View File

@ -0,0 +1,47 @@
#lang racket
(require rackunit "main.rkt")
(test-case
"same-position?"
(check-true (same-position? (line (point 0 0) (point 0 1) 1)
(line (point 0 0) (point 0 1) 0)))
(check-true (same-position? (line (point 0 0) (point 0 1) 1)
(line (point 0 1) (point 0 0) 0)))
(check-true (same-position? (line (point 0 2) (point 0 1) 0)
(line (point 0 2) (point 0 1) 0)))
(check-false (same-position? (line (point 0 0) (point 0 1) 1)
(line (point 2 4) (point 0 3) 0))))
(test-case
"valid-length?"
(check-true (valid-length? (line (point 0 0) (point 0 1) 1)))
(check-true (valid-length? (line (point 0 1) (point 0 0) 1)))
(check-false (valid-length? (line (point 0 0) (point 1 1) 0)))
(check-false (valid-length? (line (point 0 0) (point 0 2) 1)))
(check-false (valid-length? (line (point 0 0) (point 0 2) 1))))
(test-case
"out-of-bounds?"
(check-false (out-of-bounds? (point 0 0)))
(check-false (out-of-bounds? (point 0 (- GRID-WIDTH 1))))
(check-true (out-of-bounds? (point GRID-WIDTH 0)))
(check-true (out-of-bounds? (point -2 0)))
(check-true (out-of-bounds? (point 0 -2))))
(test-case
"valid-move?"
(define board (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 0 2) 0) (line (point 3 2) (point 3 3) 0)))
(check-false (valid-move? (line (point 0 0) (point 0 1) 1) board))
(check-true (valid-move? (line (point 0 2) (point 0 3) 0) board)) ; not overwriting existing moves
(check-false (valid-move? (line (point 0 0) (point 0 2) 1) board)) ; valid length check
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) board))
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) board))) ; out of bounds
(test-case
"out-of-bounds?"
(check-false (out-of-bounds? (point 0 0)))
(check-false (out-of-bounds? (point 0 (- GRID-WIDTH 1))))
(check-true (out-of-bounds? (point GRID-WIDTH 0)))
(check-true (out-of-bounds? (point -2 0)))
(check-true (out-of-bounds? (point 0 -2))))

View File

@ -1,6 +1,7 @@
#lang racket
(require rackunit)
(provide (all-defined-out)) ; for testing module
; a Board is a list of lines.
@ -29,17 +30,7 @@
(equal? (line-to l1) (line-from l2))
(equal? (line-from l1) (line-to l2)))))
(test-case
"same-position?"
(check-true (same-position? (line (point 0 0) (point 0 1) 1)
(line (point 0 0) (point 0 1) 0)))
(check-true (same-position? (line (point 0 0) (point 0 1) 1)
(line (point 0 1) (point 0 0) 0)))
(check-true (same-position? (line (point 0 2) (point 0 1) 0)
(line (point 0 2) (point 0 1) 0)))
(check-false (same-position? (line (point 0 0) (point 0 1) 1)
(line (point 2 4) (point 0 3) 0))))
; Line -> Bool
@ -49,14 +40,6 @@
(+ (abs (- (point-x (line-from line)) (point-x (line-to line))))
(abs (- (point-y (line-from line)) (point-y (line-to line)))))))
(test-case
"valid-length?"
(check-true (valid-length? (line (point 0 0) (point 0 1) 1)))
(check-true (valid-length? (line (point 0 1) (point 0 0) 1)))
(check-false (valid-length? (line (point 0 0) (point 1 1) 0)))
(check-false (valid-length? (line (point 0 0) (point 0 2) 1)))
(check-false (valid-length? (line (point 0 0) (point 0 2) 1))))
; Point -> Bool
; returns #t if a point is out of bounds (off the edge of the grid)
(define (out-of-bounds? p)
@ -66,13 +49,7 @@
(< (- GRID-WIDTH 1) (point-x p))
(< (- GRID-WIDTH 1) (point-y p))))
(test-case
"out-of-bounds?"
(check-false (out-of-bounds? (point 0 0)))
(check-false (out-of-bounds? (point 0 (- GRID-WIDTH 1))))
(check-true (out-of-bounds? (point GRID-WIDTH 0)))
(check-true (out-of-bounds? (point -2 0)))
(check-true (out-of-bounds? (point 0 -2))))
; Line Board -> Bool
; returns #t if adding the given move to the board is valid.
@ -83,13 +60,3 @@
(and
(not (out-of-bounds? (line-from line)))
(not (out-of-bounds? (line-to line))))))
(test-case
"valid-move?"
(define board (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 0 2) 0) (line (point 3 2) (point 3 3) 0)))
(check-false (valid-move? (line (point 0 0) (point 0 1) 1) board))
(check-true (valid-move? (line (point 0 2) (point 0 3) 0) board)) ; not overwriting existing moves
(check-false (valid-move? (line (point 0 0) (point 0 2) 1) board)) ; valid length check
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) board))
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) board)) ; out of bounds
)