| 1 |
use crate::Counter; |
| 2 |
use maplit::hashmap; |
| 3 |
use std::collections::HashMap; |
| 4 |
#[test] |
| 5 |
fn test_creation() { |
| 6 |
let _: Counter<usize> = Counter::new(); |
| 7 |
|
| 8 |
let counter = Counter::from_iter(&[1]); |
| 9 |
|
| 10 |
let mut expected = HashMap::new(); |
| 11 |
static ONE: usize = 1; |
| 12 |
expected.insert(&ONE, 1); |
| 13 |
assert!(counter.map == expected); |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_creation_with_capacity() { |
| 18 |
let counter: Counter<usize, usize> = Counter::with_capacity(3); |
| 19 |
assert_eq!(counter.map.capacity(), 3); |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn test_update() { |
| 24 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 25 |
let expected = hashmap! { |
| 26 |
'a' => 1, |
| 27 |
'b' => 2, |
| 28 |
'c' => 3, |
| 29 |
}; |
| 30 |
assert!(counter.map == expected); |
| 31 |
|
| 32 |
counter.update("aeeeee".chars()); |
| 33 |
let expected = hashmap! { |
| 34 |
'a' => 2, |
| 35 |
'b' => 2, |
| 36 |
'c' => 3, |
| 37 |
'e' => 5, |
| 38 |
}; |
| 39 |
assert!(counter.map == expected); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_add_update_iterable() { |
| 44 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 45 |
let expected = hashmap! { |
| 46 |
'a' => 1, |
| 47 |
'b' => 2, |
| 48 |
'c' => 3, |
| 49 |
}; |
| 50 |
assert!(counter.map == expected); |
| 51 |
|
| 52 |
counter += "aeeeee".chars(); |
| 53 |
let expected = hashmap! { |
| 54 |
'a' => 2, |
| 55 |
'b' => 2, |
| 56 |
'c' => 3, |
| 57 |
'e' => 5, |
| 58 |
}; |
| 59 |
assert!(counter.map == expected); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_add_update_counter() { |
| 64 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 65 |
let expected = hashmap! { |
| 66 |
'a' => 1, |
| 67 |
'b' => 2, |
| 68 |
'c' => 3, |
| 69 |
}; |
| 70 |
assert!(counter.map == expected); |
| 71 |
|
| 72 |
let other = "aeeeee".chars().collect::<Counter<_>>(); |
| 73 |
counter += other; |
| 74 |
let expected = hashmap! { |
| 75 |
'a' => 2, |
| 76 |
'b' => 2, |
| 77 |
'c' => 3, |
| 78 |
'e' => 5, |
| 79 |
}; |
| 80 |
assert!(counter.map == expected); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn test_subtract() { |
| 85 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 86 |
counter.subtract("bbccddd".chars()); |
| 87 |
let expected = hashmap! { |
| 88 |
'a' => 1, |
| 89 |
'c' => 1, |
| 90 |
}; |
| 91 |
assert!(counter.map == expected); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn test_sub_update_iterable() { |
| 96 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 97 |
counter -= "bbccddd".chars(); |
| 98 |
let expected = hashmap! { |
| 99 |
'a' => 1, |
| 100 |
'c' => 1, |
| 101 |
}; |
| 102 |
assert!(counter.map == expected); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn test_sub_update_counter() { |
| 107 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 108 |
let other = "bbccddd".chars().collect::<Counter<_>>(); |
| 109 |
counter -= other; |
| 110 |
let expected = hashmap! { |
| 111 |
'a' => 1, |
| 112 |
'c' => 1, |
| 113 |
}; |
| 114 |
assert!(counter.map == expected); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn test_from_iter_simple() { |
| 119 |
let counter = "abbccc".chars().collect::<Counter<_>>(); |
| 120 |
let expected = hashmap! { |
| 121 |
'a' => 1, |
| 122 |
'b' => 2, |
| 123 |
'c' => 3, |
| 124 |
}; |
| 125 |
assert!(counter.map == expected); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_from_iter_tuple() { |
| 130 |
let items = [('a', 1), ('b', 2), ('c', 3)]; |
| 131 |
let counter = items.iter().cloned().collect::<Counter<_>>(); |
| 132 |
let expected: HashMap<char, usize> = items.iter().cloned().collect(); |
| 133 |
assert_eq!(counter.map, expected); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn test_from_iter_tuple_with_duplicates() { |
| 138 |
let items = [('a', 1), ('b', 2), ('c', 3)]; |
| 139 |
let counter = items |
| 140 |
.iter() |
| 141 |
.cycle() |
| 142 |
.take(items.len() * 2) |
| 143 |
.cloned() |
| 144 |
.collect::<Counter<_>>(); |
| 145 |
let expected: HashMap<char, usize> = items.iter().map(|(c, n)| (*c, n * 2)).collect(); |
| 146 |
assert_eq!(counter.map, expected); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn test_extend_simple() { |
| 151 |
let mut counter = "abbccc".chars().collect::<Counter<_>>(); |
| 152 |
counter.extend("bccddd".chars()); |
| 153 |
let expected = hashmap! { |
| 154 |
'a' => 1, |
| 155 |
'b' => 3, |
| 156 |
'c' => 5, |
| 157 |
'd' => 3, |
| 158 |
}; |
| 159 |
assert!(counter.map == expected); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn test_extend_tuple() { |
| 164 |
let mut counter = "bccddd".chars().collect::<Counter<_>>(); |
| 165 |
let items = [('a', 1), ('b', 2), ('c', 3)]; |
| 166 |
counter.extend(items.iter().cloned()); |
| 167 |
let expected = hashmap! { |
| 168 |
'a' => 1, |
| 169 |
'b' => 3, |
| 170 |
'c' => 5, |
| 171 |
'd' => 3, |
| 172 |
}; |
| 173 |
assert_eq!(counter.map, expected); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn test_extend_tuple_with_duplicates() { |
| 178 |
let mut counter = "ccc".chars().collect::<Counter<_>>(); |
| 179 |
let items = [('a', 1), ('b', 2), ('c', 3)]; |
| 180 |
counter.extend(items.iter().cycle().take(items.len() * 2 - 1).cloned()); |
| 181 |
let expected: HashMap<char, usize> = items.iter().map(|(c, n)| (*c, n * 2)).collect(); |
| 182 |
assert_eq!(counter.map, expected); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn test_count_minimal_type() { |
| 187 |
#[derive(Debug, Hash, PartialEq, Eq)] |
| 188 |
struct Inty { |
| 189 |
i: usize, |
| 190 |
} |
| 191 |
|
| 192 |
impl Inty { |
| 193 |
pub fn new(i: usize) -> Inty { |
| 194 |
Inty { i } |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
let intys = vec![ |
| 200 |
Inty::new(8), |
| 201 |
Inty::new(0), |
| 202 |
Inty::new(0), |
| 203 |
Inty::new(8), |
| 204 |
Inty::new(6), |
| 205 |
Inty::new(7), |
| 206 |
Inty::new(5), |
| 207 |
Inty::new(3), |
| 208 |
Inty::new(0), |
| 209 |
Inty::new(9), |
| 210 |
]; |
| 211 |
|
| 212 |
let inty_counts = intys.into_iter().collect::<Counter<_>>(); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
assert!(inty_counts.map.get(&Inty { i: 8 }) == Some(&2)); |
| 217 |
assert!(inty_counts.map.get(&Inty { i: 0 }) == Some(&3)); |
| 218 |
assert!(inty_counts.map.get(&Inty { i: 6 }) == Some(&1)); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn test_collect() { |
| 223 |
let counter: Counter<_> = "abbccc".chars().collect(); |
| 224 |
let expected = hashmap! { |
| 225 |
'a' => 1, |
| 226 |
'b' => 2, |
| 227 |
'c' => 3, |
| 228 |
}; |
| 229 |
assert!(counter.map == expected); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_non_usize_count() { |
| 234 |
let counter: Counter<_, i8> = "abbccc".chars().collect(); |
| 235 |
let expected = hashmap! { |
| 236 |
'a' => 1, |
| 237 |
'b' => 2, |
| 238 |
'c' => 3, |
| 239 |
}; |
| 240 |
assert!(counter.map == expected); |
| 241 |
} |