#lang racket (require rackunit "main.rkt") (define test-grid (grid (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 1 1) 0) (line (point 1 0) (point 1 1) 1) (line (point 0 0) (point 1 0) 0) (line (point 1 1) (point 2 1) 0) (line (point 2 0) (point 2 1) 1) (line (point 2 0) (point 3 0) 0) (line (point 3 3) (point 3 4) 0) (line (point 3 3) (point 4 3) 0) (line (point 4 3) (point 4 4) 0) (line (point 3 4) (point 4 4) 0)) 6 6)) (test-case "total-moves" (check-equal? (total-moves (grid '() 2 2)) 4) (check-equal? (total-moves (grid '() 4 5)) 31) (check-equal? (total-moves (grid '() 2 6)) 16)) (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 "boxes-for" (check-equal? (boxes-for (line (point 0 0) (point 0 1) 1) test-grid) (list (point 0 0))) (check-equal? (boxes-for (line (point 2 1) (point 2 2) 1) test-grid) (list (point 2 1) (point 1 1))) (check-equal? (boxes-for (line (point 0 0) (point 1 0) 1) test-grid) (list (point 0 0))) (check-equal? (boxes-for (line (point 1 1) (point 2 1) 1) test-grid) (list (point 1 1) (point 1 0))) (check-equal? (boxes-for (line (point 0 5) (point 1 5) 1) test-grid) (list (point 0 4))) (check-equal? (boxes-for (line (point 5 0) (point 5 1) 0) test-grid) (list (point 4 0)))) (test-case "vertical?" (check-false (vertical? (line (point 2 0) (point 3 0) 0))) (check-true (vertical? (line (point 0 5) (point 0 6) 0)))) (test-case "horizontal?" (check-true (horizontal? (line (point 2 0) (point 3 0) 0))) (check-false (horizontal? (line (point 0 5) (point 0 6) 0)))) (test-case "box-in-grid?" (check-true (box-in-grid? test-grid (point 0 0))) (check-true (box-in-grid? test-grid (point 2 4))) (check-false (box-in-grid? test-grid (point 0 5))) (check-false (box-in-grid? test-grid (point 5 0))) (check-false (box-in-grid? test-grid (point -1 3)))) (test-case "completes-boxes" (define test-grid (grid (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 1 1) 0) (line (point 1 0) (point 1 1) 1) (line (point 0 2) (point 0 3) 1) (line (point 0 3) (point 1 3) 1) (line (point 0 2) (point 1 2) 1) (line (point 0 4) (point 0 5) 0) (line (point 0 4) (point 1 4) 1) (line (point 1 4) (point 1 5) 0) (line (point 3 0) (point 4 0) 0) (line (point 3 0) (point 3 1) 1) (line (point 4 0) (point 4 1) 0) (line (point 3 5) (point 4 5) 1) (line (point 3 4) (point 3 5) 0) (line (point 3 4) (point 4 4) 1) (line (point 4 5) (point 5 5) 1) (line (point 4 4) (point 5 4) 0) (line (point 5 4) (point 5 5) 1)) 6 6)) ; all possible unfinished boxes (plus an unfinished double) (check-equal? (completes-boxes test-grid (line (point 0 0) (point 1 0) 0)) 1) (check-equal? (completes-boxes test-grid (line (point 1 2) (point 1 3) 0)) 1) (check-equal? (completes-boxes test-grid (line (point 3 1) (point 4 1) 0)) 1) (check-equal? (completes-boxes test-grid (line (point 0 5) (point 1 5) 0)) 1) (check-equal? (completes-boxes test-grid (line (point 4 4) (point 4 5) 0)) 2) (check-equal? (completes-boxes test-grid (line (point 1 0) (point 2 0) 1)) 0)) (test-case "valid-move?" (define test-grid (grid (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)) 6 6)) (check-false (valid-move? (line (point 0 0) (point 0 1) 1) test-grid)) (check-true (valid-move? (line (point 0 2) (point 0 3) 0) test-grid)) ; not overwriting existing moves (check-false (valid-move? (line (point 0 2) (point 0 1) 1) test-grid)) ; moving forward (check-false (valid-move? (line (point 0 0) (point 0 2) 1) test-grid)) ; valid length check (check-false (valid-move? (line (point (+ (grid-width test-grid) 1) 0) (point 0 2) 1) test-grid)) (check-false (valid-move? (line (point (+ (grid-width test-grid) 1) 0) (point 0 2) 1) test-grid))) ; out of bounds (test-case "out-of-bounds?" (check-false (out-of-bounds? (point 0 0) test-grid)) (check-false (out-of-bounds? (point 0 (- (grid-width test-grid) 1)) test-grid)) (check-true (out-of-bounds? (point (grid-width test-grid) 0) test-grid)) (check-true (out-of-bounds? (point -2 0) test-grid)) (check-true (out-of-bounds? (point 0 -2) test-grid))) (test-case "forwards?" (check-true (forwards? (line (point 0 0) (point 0 1) 1))) (check-true (forwards? (line (point 0 1) (point 1 1) 1))) (check-false (forwards? (line (point 1 1) (point 0 0) 0)))) (test-case "point+" (check-equal? (point+ (point 0 0) (point 1 2)) (point 1 2)) (check-equal? (point+ (point 2 4) (point 5 0)) (point 7 4)) (check-equal? (point+ (point 0 0) (point 0 0)) (point 0 0))) (test-case "count-square" (check-equal? (count-square (point 0 0) test-grid) 4) (check-equal? (count-square (point 1 0) test-grid) 3) (check-equal? (count-square (point 2 0) test-grid) 2) (check-equal? (count-square (point 3 2) test-grid) 1) (check-equal? (count-square (point 0 4) test-grid) 0)) (test-case "append-move" (check-equal? (append-move test-grid (line (point 2 2) (point 2 3) 0)) (grid (append (grid-lines test-grid) (list (line (point 2 2) (point 2 3) 0))) (grid-width test-grid) (grid-height test-grid))) (check-equal? (append-move test-grid (line (point 0 0) (point 0 1) 0)) test-grid)) (test-case "valid-moves" (check-equal? (valid-moves test-grid) (list (line (point 0 1) (point 0 2) 1) (line (point 0 2) (point 1 2) 1) (line (point 0 2) (point 0 3) 1) (line (point 0 3) (point 1 3) 1) (line (point 0 3) (point 0 4) 1) (line (point 0 4) (point 1 4) 1) (line (point 0 4) (point 0 5) 1) (line (point 0 5) (point 1 5) 1) (line (point 1 0) (point 2 0) 1) (line (point 1 1) (point 1 2) 1) (line (point 1 2) (point 2 2) 1) (line (point 1 2) (point 1 3) 1) (line (point 1 3) (point 2 3) 1) (line (point 1 3) (point 1 4) 1) (line (point 1 4) (point 2 4) 1) (line (point 1 4) (point 1 5) 1) (line (point 1 5) (point 2 5) 1) (line (point 2 1) (point 3 1) 1) (line (point 2 1) (point 2 2) 1) (line (point 2 2) (point 3 2) 1) (line (point 2 2) (point 2 3) 1) (line (point 2 3) (point 3 3) 1) (line (point 2 3) (point 2 4) 1) (line (point 2 4) (point 3 4) 1) (line (point 2 4) (point 2 5) 1) (line (point 2 5) (point 3 5) 1) (line (point 3 0) (point 4 0) 1) (line (point 3 0) (point 3 1) 1) (line (point 3 1) (point 4 1) 1) (line (point 3 1) (point 3 2) 1) (line (point 3 2) (point 4 2) 1) (line (point 3 2) (point 3 3) 1) (line (point 3 4) (point 3 5) 1) (line (point 3 5) (point 4 5) 1) (line (point 4 0) (point 5 0) 1) (line (point 4 0) (point 4 1) 1) (line (point 4 1) (point 5 1) 1) (line (point 4 1) (point 4 2) 1) (line (point 4 2) (point 5 2) 1) (line (point 4 2) (point 4 3) 1) (line (point 4 3) (point 5 3) 1) (line (point 4 4) (point 5 4) 1) (line (point 4 4) (point 4 5) 1) (line (point 4 5) (point 5 5) 1) (line (point 5 0) (point 5 1) 1) (line (point 5 1) (point 5 2) 1) (line (point 5 2) (point 5 3) 1) (line (point 5 3) (point 5 4) 1) (line (point 5 4) (point 5 5) 1))))