|
1 | 1 | import LeanTest |
| 2 | +import Std |
2 | 3 | import WordCount |
3 | 4 |
|
4 | 5 | open LeanTest |
5 | 6 |
|
6 | | -def sort (list : List (String × Nat)) : List (String × Nat) := |
7 | | - list.mergeSort (fun x y => x.fst <= y.fst) |
8 | | - |
9 | | -def sortedResult (sentence : String) : List (String × Nat) := |
10 | | - WordCount.countWords sentence |> Std.HashMap.toList |> sort |
11 | | - |
12 | 7 | def wordCountTests : TestSuite := |
13 | 8 | (TestSuite.empty "WordCount") |
14 | 9 | |>.addTest "count one word" (do |
15 | | - return assertEqual (sort [("word", 1)]) (sortedResult "word")) |
| 10 | + return assertEqual [ |
| 11 | + ("word", 1) |
| 12 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "word")) |
16 | 13 | |>.addTest "count one of each word" (do |
17 | | - return assertEqual (sort [("one", 1), ("of", 1), ("each", 1)]) (sortedResult "one of each")) |
| 14 | + return assertEqual [ |
| 15 | + ("each", 1), |
| 16 | + ("of", 1), |
| 17 | + ("one", 1) |
| 18 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "one of each")) |
18 | 19 | |>.addTest "multiple occurrences of a word" (do |
19 | | - return assertEqual (sort [("one", 1), ("fish", 4), ("two", 1), ("red", 1), ("blue", 1)]) (sortedResult "one fish two fish red fish blue fish")) |
| 20 | + return assertEqual [ |
| 21 | + ("blue", 1), |
| 22 | + ("fish", 4), |
| 23 | + ("one", 1), |
| 24 | + ("red", 1), |
| 25 | + ("two", 1) |
| 26 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "one fish two fish red fish blue fish")) |
20 | 27 | |>.addTest "handles cramped lists" (do |
21 | | - return assertEqual (sort [("one", 1), ("two", 1), ("three", 1)]) (sortedResult "one,two,three")) |
| 28 | + return assertEqual [ |
| 29 | + ("one", 1), |
| 30 | + ("three", 1), |
| 31 | + ("two", 1) |
| 32 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "one,two,three")) |
22 | 33 | |>.addTest "handles expanded lists" (do |
23 | | - return assertEqual (sort [("one", 1), ("two", 1), ("three", 1)]) (sortedResult "one,\ntwo,\nthree")) |
| 34 | + return assertEqual [ |
| 35 | + ("one", 1), |
| 36 | + ("three", 1), |
| 37 | + ("two", 1) |
| 38 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "one,\ntwo,\nthree")) |
24 | 39 | |>.addTest "ignore punctuation" (do |
25 | | - return assertEqual (sort [("car", 1), ("carpet", 1), ("as", 1), ("java", 1), ("javascript", 1)]) (sortedResult "car: carpet as java: javascript!!&@$%^&")) |
| 40 | + return assertEqual [ |
| 41 | + ("as", 1), |
| 42 | + ("car", 1), |
| 43 | + ("carpet", 1), |
| 44 | + ("java", 1), |
| 45 | + ("javascript", 1) |
| 46 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "car: carpet as java: javascript!!&@$%^&")) |
26 | 47 | |>.addTest "include numbers" (do |
27 | | - return assertEqual (sort [("testing", 2), ("1", 1), ("2", 1)]) (sortedResult "testing, 1, 2 testing")) |
| 48 | + return assertEqual [ |
| 49 | + ("1", 1), |
| 50 | + ("2", 1), |
| 51 | + ("testing", 2) |
| 52 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "testing, 1, 2 testing")) |
28 | 53 | |>.addTest "normalize case" (do |
29 | | - return assertEqual (sort [("go", 3), ("stop", 2)]) (sortedResult "go Go GO Stop stop")) |
| 54 | + return assertEqual [ |
| 55 | + ("go", 3), |
| 56 | + ("stop", 2) |
| 57 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "go Go GO Stop stop")) |
30 | 58 | |>.addTest "with apostrophes" (do |
31 | | - return assertEqual (sort [("first", 1), ("don't", 2), ("laugh", 1), ("then", 1), ("cry", 1), ("you're", 1), ("getting", 1), ("it", 1)] ) (sortedResult "'First: don't laugh. Then: don't cry. You're getting it.'")) |
| 59 | + return assertEqual [ |
| 60 | + ("cry", 1), |
| 61 | + ("don't", 2), |
| 62 | + ("first", 1), |
| 63 | + ("getting", 1), |
| 64 | + ("it", 1), |
| 65 | + ("laugh", 1), |
| 66 | + ("then", 1), |
| 67 | + ("you're", 1) |
| 68 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "'First: don't laugh. Then: don't cry. You're getting it.'")) |
32 | 69 | |>.addTest "with quotations" (do |
33 | | - return assertEqual (sort [("joe", 1), ("can't", 1), ("tell", 1), ("between", 1), ("large", 2), ("and", 1)]) (sortedResult "Joe can't tell between 'large' and large.")) |
| 70 | + return assertEqual [ |
| 71 | + ("and", 1), |
| 72 | + ("between", 1), |
| 73 | + ("can't", 1), |
| 74 | + ("joe", 1), |
| 75 | + ("large", 2), |
| 76 | + ("tell", 1) |
| 77 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "Joe can't tell between 'large' and large.")) |
34 | 78 | |>.addTest "substrings from the beginning" (do |
35 | | - return assertEqual (sort [("joe", 1), ("can't", 1), ("tell", 1), ("between", 1), ("app", 1), ("apple", 1), ("and", 1), ("a", 1)]) (sortedResult "Joe can't tell between app, apple and a.")) |
| 79 | + return assertEqual [ |
| 80 | + ("a", 1), |
| 81 | + ("and", 1), |
| 82 | + ("app", 1), |
| 83 | + ("apple", 1), |
| 84 | + ("between", 1), |
| 85 | + ("can't", 1), |
| 86 | + ("joe", 1), |
| 87 | + ("tell", 1) |
| 88 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "Joe can't tell between app, apple and a.")) |
36 | 89 | |>.addTest "multiple spaces not detected as a word" (do |
37 | | - return assertEqual (sort [("multiple", 1), ("whitespaces", 1)]) (sortedResult " multiple whitespaces")) |
| 90 | + return assertEqual [ |
| 91 | + ("multiple", 1), |
| 92 | + ("whitespaces", 1) |
| 93 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords " multiple whitespaces")) |
38 | 94 | |>.addTest "alternating word separators not detected as a word" (do |
39 | | - return assertEqual (sort [("one", 1), ("two", 1), ("three", 1)]) (sortedResult ",\n,one,\n ,two \n 'three'")) |
| 95 | + return assertEqual [ |
| 96 | + ("one", 1), |
| 97 | + ("three", 1), |
| 98 | + ("two", 1) |
| 99 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords ",\n,one,\n ,two \n 'three'")) |
40 | 100 | |>.addTest "quotation for word with apostrophe" (do |
41 | | - return assertEqual (sort [("can", 1), ("can't", 2)]) (sortedResult "can, can't, 'can't'")) |
| 101 | + return assertEqual [ |
| 102 | + ("can", 1), |
| 103 | + ("can't", 2) |
| 104 | + ] ((·.mergeSort (λ (k1, _) (k2, _) => k1 ≤ k2)) <| Std.HashMap.toList <| WordCount.countWords "can, can't, 'can't'")) |
42 | 105 |
|
43 | 106 | def main : IO UInt32 := do |
44 | 107 | runTestSuitesWithExitCode [wordCountTests] |
0 commit comments