Skip to content

Commit 6ea43b8

Browse files
committed
Added into_group_map-based checks and _by and _by_key variant quicktests
1 parent 8622626 commit 6ea43b8

File tree

1 file changed

+187
-1
lines changed

1 file changed

+187
-1
lines changed

tests/quick.rs

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::default::Default;
88
use std::num::Wrapping;
99
use std::ops::Range;
1010
use std::cmp::{max, min, Ordering};
11-
use std::collections::HashSet;
11+
use std::collections::{HashMap, HashSet};
1212
use itertools::Itertools;
1313
use itertools::{
1414
multizip,
@@ -1213,6 +1213,23 @@ quickcheck! {
12131213
Some(acc.unwrap_or(0) + val)
12141214
}
12151215
});
1216+
1217+
let group_map_lookup = a.iter()
1218+
.map(|&b| b as u64)
1219+
.map(|i| (i % modulo, i))
1220+
.into_group_map()
1221+
.into_iter()
1222+
.filter_map(|(key, vals)| {
1223+
vals.into_iter().fold(None, |acc, val| {
1224+
if val % (modulo - 1) == 0 {
1225+
None
1226+
} else {
1227+
Some(acc.unwrap_or(0) + val)
1228+
}
1229+
}).map(|new_val| (key, new_val))
1230+
})
1231+
.collect::<HashMap<_,_>>();
1232+
assert_eq!(lookup, group_map_lookup);
12161233

12171234
for m in 0..modulo {
12181235
assert_eq!(
@@ -1240,6 +1257,15 @@ quickcheck! {
12401257
acc + val
12411258
});
12421259

1260+
let group_map_lookup = a.iter()
1261+
.map(|&b| b as u64)
1262+
.map(|i| (i % modulo, i))
1263+
.into_group_map()
1264+
.into_iter()
1265+
.map(|(key, vals)| (key, vals.into_iter().fold(0u64, |acc, val| acc + val)))
1266+
.collect::<HashMap<_,_>>();
1267+
assert_eq!(lookup, group_map_lookup);
1268+
12431269
for (&key, &sum) in lookup.iter() {
12441270
assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
12451271
}
@@ -1254,6 +1280,16 @@ quickcheck! {
12541280
acc + val
12551281
});
12561282

1283+
// TODO: Swap `fold1` with stdlib's `fold_first` when it's stabilized
1284+
let group_map_lookup = a.iter()
1285+
.map(|&b| b as u64)
1286+
.map(|i| (i % modulo, i))
1287+
.into_group_map()
1288+
.into_iter()
1289+
.map(|(key, vals)| (key, vals.into_iter().fold1(|acc, val| acc + val).unwrap()))
1290+
.collect::<HashMap<_,_>>();
1291+
assert_eq!(lookup, group_map_lookup);
1292+
12571293
for (&key, &sum) in lookup.iter() {
12581294
assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
12591295
}
@@ -1272,6 +1308,14 @@ quickcheck! {
12721308
let count = a.len();
12731309
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).count();
12741310

1311+
let group_map_lookup = a.iter().copied()
1312+
.map(|i| (i % modulo, i))
1313+
.into_group_map()
1314+
.into_iter()
1315+
.map(|(key, vals)| (key, vals.len()))
1316+
.collect::<HashMap<_,_>>();
1317+
assert_eq!(lookup, group_map_lookup);
1318+
12751319
assert_eq!(lookup.values().sum::<usize>(), count);
12761320

12771321
for (&key, &count) in lookup.iter() {
@@ -1283,35 +1327,169 @@ quickcheck! {
12831327
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
12841328
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).max();
12851329

1330+
let group_map_lookup = a.iter().copied()
1331+
.map(|i| (i % modulo, i))
1332+
.into_group_map()
1333+
.into_iter()
1334+
.map(|(key, vals)| (key, vals.into_iter().max().unwrap()))
1335+
.collect::<HashMap<_,_>>();
1336+
assert_eq!(lookup, group_map_lookup);
1337+
12861338
for (&key, &max) in lookup.iter() {
12871339
assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max());
12881340
}
12891341
}
1342+
1343+
fn correct_grouping_map_by_max_by_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1344+
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1345+
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).max_by(|_, v1, v2| v1.cmp(v2));
1346+
1347+
let group_map_lookup = a.iter().copied()
1348+
.map(|i| (i % modulo, i))
1349+
.into_group_map()
1350+
.into_iter()
1351+
.map(|(key, vals)| (key, vals.into_iter().max_by(|v1, v2| v1.cmp(v2)).unwrap()))
1352+
.collect::<HashMap<_,_>>();
1353+
assert_eq!(lookup, group_map_lookup);
1354+
1355+
for (&key, &max) in lookup.iter() {
1356+
assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max_by(|v1, v2| v1.cmp(v2)));
1357+
}
1358+
}
1359+
1360+
fn correct_grouping_map_by_max_by_key_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1361+
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1362+
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).max_by_key(|_, &val| val);
1363+
1364+
let group_map_lookup = a.iter().copied()
1365+
.map(|i| (i % modulo, i))
1366+
.into_group_map()
1367+
.into_iter()
1368+
.map(|(key, vals)| (key, vals.into_iter().max_by_key(|&val| val).unwrap()))
1369+
.collect::<HashMap<_,_>>();
1370+
assert_eq!(lookup, group_map_lookup);
1371+
1372+
for (&key, &max) in lookup.iter() {
1373+
assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max_by_key(|&val| val));
1374+
}
1375+
}
12901376

12911377
fn correct_grouping_map_by_min_modulo_key(a: Vec<u8>, modulo: u8) -> () {
12921378
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
12931379
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min();
12941380

1381+
let group_map_lookup = a.iter().copied()
1382+
.map(|i| (i % modulo, i))
1383+
.into_group_map()
1384+
.into_iter()
1385+
.map(|(key, vals)| (key, vals.into_iter().min().unwrap()))
1386+
.collect::<HashMap<_,_>>();
1387+
assert_eq!(lookup, group_map_lookup);
1388+
12951389
for (&key, &min) in lookup.iter() {
12961390
assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min());
12971391
}
12981392
}
1393+
1394+
fn correct_grouping_map_by_min_by_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1395+
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1396+
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min_by(|_, v1, v2| v1.cmp(v2));
1397+
1398+
let group_map_lookup = a.iter().copied()
1399+
.map(|i| (i % modulo, i))
1400+
.into_group_map()
1401+
.into_iter()
1402+
.map(|(key, vals)| (key, vals.into_iter().min_by(|v1, v2| v1.cmp(v2)).unwrap()))
1403+
.collect::<HashMap<_,_>>();
1404+
assert_eq!(lookup, group_map_lookup);
1405+
1406+
for (&key, &min) in lookup.iter() {
1407+
assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min_by(|v1, v2| v1.cmp(v2)));
1408+
}
1409+
}
1410+
1411+
fn correct_grouping_map_by_min_by_key_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1412+
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1413+
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min_by_key(|_, &val| val);
1414+
1415+
let group_map_lookup = a.iter().copied()
1416+
.map(|i| (i % modulo, i))
1417+
.into_group_map()
1418+
.into_iter()
1419+
.map(|(key, vals)| (key, vals.into_iter().min_by_key(|&val| val).unwrap()))
1420+
.collect::<HashMap<_,_>>();
1421+
assert_eq!(lookup, group_map_lookup);
1422+
1423+
for (&key, &min) in lookup.iter() {
1424+
assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min_by_key(|&val| val));
1425+
}
1426+
}
12991427

13001428
fn correct_grouping_map_by_minmax_modulo_key(a: Vec<u8>, modulo: u8) -> () {
13011429
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
13021430
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax();
13031431

1432+
let group_map_lookup = a.iter().copied()
1433+
.map(|i| (i % modulo, i))
1434+
.into_group_map()
1435+
.into_iter()
1436+
.map(|(key, vals)| (key, vals.into_iter().minmax()))
1437+
.collect::<HashMap<_,_>>();
1438+
assert_eq!(lookup, group_map_lookup);
1439+
13041440
for (&key, &minmax) in lookup.iter() {
13051441
assert_eq!(minmax, a.iter().copied().filter(|&val| val % modulo == key).minmax());
13061442
}
13071443
}
13081444

1445+
fn correct_grouping_map_by_minmax_by_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1446+
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1447+
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax_by(|_, v1, v2| v1.cmp(v2));
1448+
1449+
let group_map_lookup = a.iter().copied()
1450+
.map(|i| (i % modulo, i))
1451+
.into_group_map()
1452+
.into_iter()
1453+
.map(|(key, vals)| (key, vals.into_iter().minmax_by(|v1, v2| v1.cmp(v2))))
1454+
.collect::<HashMap<_,_>>();
1455+
assert_eq!(lookup, group_map_lookup);
1456+
1457+
for (&key, &minmax) in lookup.iter() {
1458+
assert_eq!(minmax, a.iter().copied().filter(|&val| val % modulo == key).minmax_by(|v1, v2| v1.cmp(v2)));
1459+
}
1460+
}
1461+
1462+
fn correct_grouping_map_by_minmax_by_key_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1463+
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1464+
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax_by_key(|_, &val| val);
1465+
1466+
let group_map_lookup = a.iter().copied()
1467+
.map(|i| (i % modulo, i))
1468+
.into_group_map()
1469+
.into_iter()
1470+
.map(|(key, vals)| (key, vals.into_iter().minmax_by_key(|&val| val)))
1471+
.collect::<HashMap<_,_>>();
1472+
assert_eq!(lookup, group_map_lookup);
1473+
1474+
for (&key, &minmax) in lookup.iter() {
1475+
assert_eq!(minmax, a.iter().copied().filter(|&val| val % modulo == key).minmax_by_key(|&val| val));
1476+
}
1477+
}
1478+
13091479
fn correct_grouping_map_by_sum_modulo_key(a: Vec<u8>, modulo: u8) -> () {
13101480
let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
13111481
let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
13121482
.into_grouping_map_by(|i| i % modulo)
13131483
.sum();
13141484

1485+
let group_map_lookup = a.iter().map(|&b| b as u64)
1486+
.map(|i| (i % modulo, i))
1487+
.into_group_map()
1488+
.into_iter()
1489+
.map(|(key, vals)| (key, vals.into_iter().sum()))
1490+
.collect::<HashMap<_,_>>();
1491+
assert_eq!(lookup, group_map_lookup);
1492+
13151493
for (&key, &sum) in lookup.iter() {
13161494
assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
13171495
}
@@ -1323,6 +1501,14 @@ quickcheck! {
13231501
.into_grouping_map_by(|i| i % modulo)
13241502
.product();
13251503

1504+
let group_map_lookup = a.iter().map(|&b| Wrapping(b as u64))
1505+
.map(|i| (i % modulo, i))
1506+
.into_group_map()
1507+
.into_iter()
1508+
.map(|(key, vals)| (key, vals.into_iter().product::<Wrapping<u64>>()))
1509+
.collect::<HashMap<_,_>>();
1510+
assert_eq!(lookup, group_map_lookup);
1511+
13261512
for (&key, &prod) in lookup.iter() {
13271513
assert_eq!(
13281514
prod,

0 commit comments

Comments
 (0)