Skip to content

Commit a19fd88

Browse files
committed
Finish to read ch10
1 parent 5928530 commit a19fd88

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

ch10/wcount/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,37 @@ fn word_count_works2() {
9696

9797
assert_eq!(count(Cursor::new("aa cc dd"), CountOption::Word), exp);
9898
}
99+
#[cfg(test)]
100+
mod test {
101+
use super::*;
102+
use std::io::Cursor;
103+
104+
macro_rules! assert_map {
105+
($expr: expr, {$($key: expr => $value:expr),*}) => {
106+
$(assert_eq!($expr[$key], $value));*
107+
};
108+
}
109+
110+
#[test]
111+
fn word_count_works3() {
112+
let freqs = count(Cursor::new("aa cc dd"), CountOption::Word);
113+
114+
assert_eq!(freqs.len(), 3);
115+
assert_map!(freqs, {"aa" => 1, "cc" => 1, "dd" => 1});
116+
}
117+
}
118+
119+
#[test]
120+
#[should_panic]
121+
fn word_count_do_not_contain_unknown_words() {
122+
use std::io::Cursor;
123+
124+
count(
125+
Cursor::new([
126+
b'a', // a
127+
0xf0, 0x90, 0x80, // でたらめなバイト列
128+
0xe3, 0x81, 0x82, // あ
129+
]),
130+
CountOption::Word,
131+
);
132+
}

0 commit comments

Comments
 (0)