-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathword-count.rs
More file actions
169 lines (154 loc) · 4.44 KB
/
word-count.rs
File metadata and controls
169 lines (154 loc) · 4.44 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::collections::HashMap;
fn check_word_count(mut output: HashMap<String, u32>, pairs: &[(&str, u32)]) {
// The reason for the awkward code in here is to ensure that the failure
// message for assert_eq! is as informative as possible. A simpler
// solution would simply check the length of the map, and then
// check for the presence and value of each key in the given pairs vector.
for &(k, v) in pairs.iter() {
assert_eq!((k, output.remove(&k.to_string()).unwrap_or(0)), (k, v));
}
// may fail with a message that clearly shows all extra pairs in the map
assert_eq!(output.iter().collect::<Vec<(&String, &u32)>>(), vec![]);
}
#[test]
fn count_one_word() {
let input = "word";
let output = word_count::word_count(input);
let expected = &[("word", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn count_one_of_each_word() {
let input = "one of each";
let output = word_count::word_count(input);
let expected = &[("one", 1), ("of", 1), ("each", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn multiple_occurrences_of_a_word() {
let input = "one fish two fish red fish blue fish";
let output = word_count::word_count(input);
let expected = &[("one", 1), ("fish", 4), ("two", 1), ("red", 1), ("blue", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn handles_cramped_lists() {
let input = "one,two,three";
let output = word_count::word_count(input);
let expected = &[("one", 1), ("two", 1), ("three", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn handles_expanded_lists() {
let input = "one,\ntwo,\nthree";
let output = word_count::word_count(input);
let expected = &[("one", 1), ("two", 1), ("three", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn ignore_punctuation() {
let input = "car: carpet as java: javascript!!&@$%^&";
let output = word_count::word_count(input);
let expected = &[
("car", 1),
("carpet", 1),
("as", 1),
("java", 1),
("javascript", 1),
];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn include_numbers() {
let input = "testing, 1, 2 testing";
let output = word_count::word_count(input);
let expected = &[("testing", 2), ("1", 1), ("2", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn normalize_case() {
let input = "go Go GO Stop stop";
let output = word_count::word_count(input);
let expected = &[("go", 3), ("stop", 2)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn with_apostrophes() {
let input = "'First: don't laugh. Then: don't cry. You're getting it.'";
let output = word_count::word_count(input);
let expected = &[
("first", 1),
("don't", 2),
("laugh", 1),
("then", 1),
("cry", 1),
("you're", 1),
("getting", 1),
("it", 1),
];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn with_quotations() {
let input = "Joe can't tell between 'large' and large.";
let output = word_count::word_count(input);
let expected = &[
("joe", 1),
("can't", 1),
("tell", 1),
("between", 1),
("large", 2),
("and", 1),
];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn substrings_from_the_beginning() {
let input = "Joe can't tell between app, apple and a.";
let output = word_count::word_count(input);
let expected = &[
("joe", 1),
("can't", 1),
("tell", 1),
("between", 1),
("app", 1),
("apple", 1),
("and", 1),
("a", 1),
];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn multiple_spaces_not_detected_as_a_word() {
let input = " multiple whitespaces";
let output = word_count::word_count(input);
let expected = &[("multiple", 1), ("whitespaces", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn alternating_word_separators_not_detected_as_a_word() {
let input = ",\n,one,\n ,two \n 'three'";
let output = word_count::word_count(input);
let expected = &[("one", 1), ("two", 1), ("three", 1)];
check_word_count(output, expected);
}
#[test]
#[ignore]
fn quotation_for_word_with_apostrophe() {
let input = "can, can't, 'can't'";
let output = word_count::word_count(input);
let expected = &[("can", 1), ("can't", 2)];
check_word_count(output, expected);
}