|
1 | 1 | #lang racket
|
2 | 2 |
|
3 |
| -; Tests adapted from `problem-specifications/canonical-data.json v1.4.0 |
4 | 3 | (require "word-count.rkt")
|
5 | 4 |
|
6 | 5 | (module+ test
|
7 | 6 | (require rackunit rackunit/text-ui)
|
8 | 7 |
|
9 |
| - (define-binary-check (hashes-equal? actual expected) |
10 |
| - (equal? (sort (hash->list actual) string<? #:key car) |
11 |
| - (sort (hash->list expected) string<? #:key car))) |
12 |
| - |
13 | 8 | (define suite
|
14 | 9 | (test-suite
|
15 |
| - "For each word in the input, count the number of times it appears in the entire sentence." |
16 |
| - |
17 |
| - (hashes-equal? (word-count "word") |
18 |
| - '#hash(("word" . 1)) |
19 |
| - "count one word") |
20 |
| - |
21 |
| - (hashes-equal? (word-count "one of each") |
22 |
| - '#hash(("one" . 1) ("of" . 1) ("each" . 1)) |
23 |
| - "count one of each word") |
24 |
| - |
25 |
| - (hashes-equal? (word-count "one fish two fish red fish blue fish") |
26 |
| - '#hash(("one" . 1) ("fish" . 4) ("two" . 1) ("red" . 1) ("blue" . 1)) |
27 |
| - "multiple occurrences of a word") |
28 |
| - |
29 |
| - (hashes-equal? (word-count "one,two,three") |
30 |
| - '#hash(("one" . 1) ("two" . 1) ("three" . 1)) |
31 |
| - "handles cramped lists") |
32 |
| - |
33 |
| - (hashes-equal? (word-count "one,\ntwo,\nthree") |
34 |
| - '#hash(("one" . 1) ("two" . 1) ("three" . 1)) |
35 |
| - "handles expanded lists") |
36 |
| - |
37 |
| - (hashes-equal? (word-count "car : carpet as java : javascript!!&@$%^&") |
38 |
| - '#hash(("car" . 1) ("carpet" . 1) ("as" . 1) ("java" . 1) ("javascript" . 1)) |
39 |
| - "ignore punctuation") |
40 |
| - |
41 |
| - (hashes-equal? (word-count "testing, 1, 2, testing") |
42 |
| - '#hash(("testing" . 2) ("1" . 1) ("2" . 1)) |
43 |
| - "include numbers") |
44 |
| - |
45 |
| - (hashes-equal? (word-count "go Go GO Stop stop") |
46 |
| - '#hash(("go" . 3) ("stop" . 2)) |
47 |
| - "normalize case") |
48 |
| - |
49 |
| - (hashes-equal? (word-count "Joe can't tell between 'large' and large.") |
50 |
| - '#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("large" . 2) |
51 |
| - ("and" . 1)) |
52 |
| - "with quotations") |
53 |
| - |
54 |
| - (hashes-equal? (word-count "Joe can't tell between app, apple and a.") |
55 |
| - '#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("app" . 1) |
56 |
| - ("apple" . 1) ("and" . 1) ("a" . 1)) |
57 |
| - "substrings from the beginning") |
58 |
| - |
59 |
| - |
60 |
| - (hashes-equal? (word-count " multiple whitespaces") |
61 |
| - '#hash(("multiple" . 1) ("whitespaces" . 1)) |
62 |
| - "multiple spaces not detected as a word") |
63 |
| - |
64 |
| - (hashes-equal? (word-count ",\n,one,\n ,two \n 'three'") |
65 |
| - '#hash(("one" . 1) ("two" . 1) ("three" . 1)) |
66 |
| - "alternating word separators not detected as a word"))) |
| 10 | + "word-count tests" |
| 11 | + |
| 12 | + (test-equal? "count one word" |
| 13 | + (word-count "word") |
| 14 | + '#hash(("word" . 1))) |
| 15 | + |
| 16 | + (test-equal? "count one of each word" |
| 17 | + (word-count "one of each") |
| 18 | + '#hash(("one" . 1) ("of" . 1) ("each" . 1))) |
| 19 | + |
| 20 | + (test-equal? "multiple occurrences of a word" |
| 21 | + (word-count "one fish two fish red fish blue fish") |
| 22 | + '#hash(("one" . 1) ("fish" . 4) ("two" . 1) ("red" . 1) ("blue" . 1))) |
| 23 | + |
| 24 | + (test-equal? "handles cramped lists" |
| 25 | + (word-count "one,two,three") |
| 26 | + '#hash(("one" . 1) ("two" . 1) ("three" . 1))) |
| 27 | + |
| 28 | + (test-equal? "handles expanded lists" |
| 29 | + (word-count "one,\ntwo,\nthree") |
| 30 | + '#hash(("one" . 1) ("two" . 1) ("three" . 1))) |
| 31 | + |
| 32 | + (test-equal? "ignore punctuation" |
| 33 | + (word-count "car : carpet as java : javascript!!&@$%^&") |
| 34 | + '#hash(("car" . 1) ("carpet" . 1) ("as" . 1) ("java" . 1) ("javascript" . 1))) |
| 35 | + |
| 36 | + (test-equal? "include numbers" |
| 37 | + (word-count "testing, 1, 2, testing") |
| 38 | + '#hash(("testing" . 2) ("1" . 1) ("2" . 1))) |
| 39 | + |
| 40 | + (test-equal? "normalize case" |
| 41 | + (word-count "go Go GO Stop stop") |
| 42 | + '#hash(("go" . 3) ("stop" . 2))) |
| 43 | + |
| 44 | + (test-equal? "with apostrophes" |
| 45 | + (word-count "'First: don't laugh. Then: don't cry. You're getting it.'") |
| 46 | + '#hash(("first" . 1) ("don't" . 2) ("laugh" . 1) ("then" . 1) ("cry" . 1) |
| 47 | + ("you're" . 1) ("getting" . 1) ("it" . 1))) |
| 48 | + |
| 49 | + (test-equal? "with quotations" |
| 50 | + (word-count "Joe can't tell between 'large' and large.") |
| 51 | + '#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("large" . 2) |
| 52 | + ("and" . 1))) |
| 53 | + |
| 54 | + (test-equal? "substrings from the beginning" |
| 55 | + (word-count "Joe can't tell between app, apple and a.") |
| 56 | + '#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("app" . 1) |
| 57 | + ("apple" . 1) ("and" . 1) ("a" . 1))) |
| 58 | + |
| 59 | + (test-equal? "multiple spaces not detected as a word" |
| 60 | + (word-count " multiple whitespaces") |
| 61 | + '#hash(("multiple" . 1) ("whitespaces" . 1))) |
| 62 | + |
| 63 | + (test-equal? "alternating word separators not detected as a word" |
| 64 | + (word-count ",\n,one,\n ,two \n 'three'") |
| 65 | + '#hash(("one" . 1) ("two" . 1) ("three" . 1))) |
| 66 | + |
| 67 | + (test-equal? "quotation for word with apostrophe" |
| 68 | + (word-count "can, can't, 'can't'") |
| 69 | + '#hash(("can" . 1) ("can't" . 2))))) |
67 | 70 |
|
68 | 71 | (run-tests suite))
|
0 commit comments