| 1 |
#[cfg(test)] |
| 2 |
mod tests { |
| 3 |
use counter::Counter; |
| 4 |
use rand::Rng; |
| 5 |
|
| 6 |
#[test] |
| 7 |
fn test_composite_add_sub() { |
| 8 |
let mut counts = "able babble table babble rabble table able fable scrabble" |
| 9 |
.split_whitespace() |
| 10 |
.collect::<Counter<_>>(); |
| 11 |
|
| 12 |
counts += "cain and abel fable table cable".split_whitespace(); |
| 13 |
|
| 14 |
let other_counts = "scrabble cabbie fable babble" |
| 15 |
.split_whitespace() |
| 16 |
.collect::<Counter<_>>(); |
| 17 |
let _diff = counts - other_counts; |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn test_most_common() { |
| 22 |
let counter = "abbccc".chars().collect::<Counter<_>>(); |
| 23 |
let by_common = counter.most_common(); |
| 24 |
let expected = vec![('c', 3), ('b', 2), ('a', 1)]; |
| 25 |
assert!(by_common == expected); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_most_common_tiebreaker() { |
| 30 |
let counter = "eaddbbccc".chars().collect::<Counter<_>>(); |
| 31 |
let by_common = counter.most_common_tiebreaker(|&a, &b| a.cmp(&b)); |
| 32 |
let expected = vec![('c', 3), ('b', 2), ('d', 2), ('a', 1), ('e', 1)]; |
| 33 |
assert!(by_common == expected); |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
fn test_most_common_tiebreaker_reversed() { |
| 38 |
let counter = "eaddbbccc".chars().collect::<Counter<_>>(); |
| 39 |
let by_common = counter.most_common_tiebreaker(|&a, &b| b.cmp(&a)); |
| 40 |
let expected = vec![('c', 3), ('d', 2), ('b', 2), ('e', 1), ('a', 1)]; |
| 41 |
assert!(by_common == expected); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[test] |
| 47 |
fn test_most_common_tiebreaker_fn_mut() { |
| 48 |
let counter: Counter<_> = "abracadabra".chars().collect::<Counter<_>>(); |
| 49 |
|
| 50 |
let mut num_ties = 0; |
| 51 |
let sorted = counter.most_common_tiebreaker(|a, b| { |
| 52 |
num_ties += 1; |
| 53 |
a.cmp(b) |
| 54 |
}); |
| 55 |
let expected = vec![('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]; |
| 56 |
assert_eq!(sorted, expected); |
| 57 |
|
| 58 |
|
| 59 |
assert_eq!(num_ties, 2); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_most_common_ordered() { |
| 64 |
let counter = "eaddbbccc".chars().collect::<Counter<_>>(); |
| 65 |
let by_common = counter.most_common_ordered(); |
| 66 |
let expected = vec![('c', 3), ('b', 2), ('d', 2), ('a', 1), ('e', 1)]; |
| 67 |
assert!(by_common == expected); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_k_most_common_ordered() { |
| 72 |
let counter: Counter<_> = "abracadabra".chars().collect(); |
| 73 |
let all = counter.most_common_ordered(); |
| 74 |
for k in 0..=counter.len() { |
| 75 |
let topk = counter.k_most_common_ordered(k); |
| 76 |
assert_eq!(&topk, &all[..k]); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[test] |
| 84 |
fn test_k_most_common_ordered_heavy() { |
| 85 |
let mut rng = rand::thread_rng(); |
| 86 |
|
| 87 |
for container_size in [5, 10, 25, 100, 256] { |
| 88 |
for max_value_factor in [0.25, 0.5, 1.0, 1.25, 2.0, 10.0, 100.0] { |
| 89 |
let max_value = ((container_size as f64) * max_value_factor) as u32; |
| 90 |
let mut values = vec![0; container_size]; |
| 91 |
for value in values.iter_mut() { |
| 92 |
*value = rng.gen_range(0..=max_value); |
| 93 |
} |
| 94 |
|
| 95 |
let counter: Counter<_> = values.into_iter().collect(); |
| 96 |
let all = counter.most_common_ordered(); |
| 97 |
for k in 0..=counter.len() { |
| 98 |
let topk = counter.k_most_common_ordered(k); |
| 99 |
assert_eq!(&topk, &all[..k]); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn test_total() { |
| 107 |
let counter = "".chars().collect::<Counter<_>>(); |
| 108 |
let total: usize = counter.total(); |
| 109 |
assert_eq!(total, 0); |
| 110 |
|
| 111 |
let counter = "eaddbbccc".chars().collect::<Counter<_>>(); |
| 112 |
let total: usize = counter.total(); |
| 113 |
assert_eq!(total, 9); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_add() { |
| 118 |
let d = "abbccc".chars().collect::<Counter<_>>(); |
| 119 |
let e = "bccddd".chars().collect::<Counter<_>>(); |
| 120 |
|
| 121 |
let out = d + e; |
| 122 |
let expected = "abbbcccccddd".chars().collect::<Counter<_>>(); |
| 123 |
assert!(out == expected); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_sub() { |
| 128 |
let d = "abbccc".chars().collect::<Counter<_>>(); |
| 129 |
let e = "bccddd".chars().collect::<Counter<_>>(); |
| 130 |
|
| 131 |
let out = d - e; |
| 132 |
let expected = "abc".chars().collect::<Counter<_>>(); |
| 133 |
assert!(out == expected); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn test_intersection() { |
| 138 |
let d = "abbccc".chars().collect::<Counter<_>>(); |
| 139 |
let e = "bccddd".chars().collect::<Counter<_>>(); |
| 140 |
|
| 141 |
let out = d & e; |
| 142 |
let expected = "bcc".chars().collect::<Counter<_>>(); |
| 143 |
assert!(out == expected); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_union() { |
| 148 |
let d = "abbccc".chars().collect::<Counter<_>>(); |
| 149 |
let e = "bccddd".chars().collect::<Counter<_>>(); |
| 150 |
|
| 151 |
let out = d | e; |
| 152 |
let expected = "abbcccddd".chars().collect::<Counter<_>>(); |
| 153 |
assert!(out == expected); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn test_delete_key_from_backing_map() { |
| 158 |
let mut counter = "aa-bb-cc".chars().collect::<Counter<_>>(); |
| 159 |
counter.remove(&'-'); |
| 160 |
assert!(counter == "aabbcc".chars().collect::<Counter<_>>()); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn test_superset_non_usize_count() { |
| 165 |
let mut a: Counter<_, i8> = "abbcccc".chars().collect(); |
| 166 |
let mut b: Counter<_, i8> = "abb".chars().collect(); |
| 167 |
assert!(a.is_superset(&b)); |
| 168 |
|
| 169 |
a[&'e'] = -1; |
| 170 |
assert!(!a.is_superset(&b)); |
| 171 |
|
| 172 |
b[&'e'] = -2; |
| 173 |
assert!(a.is_superset(&b)); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn test_subset_non_usize_count() { |
| 178 |
let mut a: Counter<_, i8> = "abb".chars().collect(); |
| 179 |
let mut b: Counter<_, i8> = "abbcccc".chars().collect(); |
| 180 |
assert!(a.is_subset(&b)); |
| 181 |
|
| 182 |
b[&'e'] = -1; |
| 183 |
assert!(!a.is_subset(&b)); |
| 184 |
|
| 185 |
a[&'e'] = -2; |
| 186 |
assert!(a.is_subset(&b)); |
| 187 |
} |
| 188 |
|
| 189 |
#[cfg(feature = "serde")] |
| 190 |
#[test] |
| 191 |
fn test_serialize_deserialize() { |
| 192 |
let a = "abbccc".chars().collect::<Counter<_>>(); |
| 193 |
let serialized = serde_json::to_string(&a).unwrap(); |
| 194 |
let b: Counter<char> = serde_json::from_str(&serialized).unwrap(); |
| 195 |
assert!(a == b) |
| 196 |
} |
| 197 |
} |