modes:single:tests: use zip_cartesian and Card::into_iter

This commit is contained in:
2026-04-14 18:08:19 +01:00
committed by oreodave
parent 51f15c4b0a
commit 501410f725

View File

@@ -54,7 +54,7 @@ mod tests {
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use super::*; use super::*;
use crate::modes::tests::test_footstool; use crate::{modes::tests::test_footstool, zipcartesian::ZipCatersianExt};
#[test] #[test]
fn new() { fn new() {
@@ -121,11 +121,7 @@ mod tests {
// with the results of the first footstooling the second. // with the results of the first footstooling the second.
let exhaustive_singles_footstool = singles let exhaustive_singles_footstool = singles
.iter() .iter()
.flat_map(|single| { .zip_cartesian(singles.iter())
singles
.iter()
.map(move |other_single| (single, other_single))
})
.map(|(single, other_single)| { .map(|(single, other_single)| {
// TEST: Expected generic pattern for footstooling of hands - // TEST: Expected generic pattern for footstooling of hands -
// see mod::tests::test_footstool for details. // see mod::tests::test_footstool for details.
@@ -157,23 +153,36 @@ mod tests {
// TEST: For any Single there is only 1 other Single that it // TEST: For any Single there is only 1 other Single that it
// half footstools. // half footstools.
counter.iter().for_each(|(c1, counter)| { for (c1, counter) in counter.iter() {
assert_eq!( assert_eq!(
counter.len(), counter.len(),
1, 1,
"Expected {c1} to only have 1 card that it half footstools" "Expected {c1} to only have 1 card that it half footstools"
); );
}); }
// TEST: For any Single, the Single that it half footstools // TEST: For any Single, the Single that it half footstools
// is unique to it. // is unique to it.
{ {
let mut unique_half_footstools = HashSet::<Single>::new(); let mut unique_half_footstools = HashSet::<Card>::new();
counter.iter().for_each(|(c1, counter)| { for (Single(c1), counter) in counter.iter() {
let c2 = counter[0]; let Single(c2) = counter[0];
assert_eq!(unique_half_footstools.get(&c2), None, "Expected {c2} to be unique to the half footstools of {c1}"); assert_eq!(
unique_half_footstools.get(&c2),
None,
"Expected {c2} to be only a half footstool of {c1}"
);
unique_half_footstools.insert(c2); unique_half_footstools.insert(c2);
})
// So c2 was half footstooled by c1.
// TEST: c1 is the direct next card after c2.
let c3 = c2.into_iter().next().unwrap();
assert_eq!(
*c1, c3,
"Expected next(c2) = c1 as c2 half footstools c1"
);
}
} }
// TEST: The only Single that doesn't have a half footstool is 3[D] // TEST: The only Single that doesn't have a half footstool is 3[D]
@@ -232,6 +241,7 @@ mod tests {
*/ */
const N_DECKS: usize = 10; const N_DECKS: usize = 10;
const N_DECKS_SQR: usize = N_DECKS * N_DECKS;
// Function which maps a Footstool to a usize for use in array indexing. // Function which maps a Footstool to a usize for use in array indexing.
fn footstool_to_numeral(f: Footstool) -> usize { fn footstool_to_numeral(f: Footstool) -> usize {
@@ -248,14 +258,12 @@ mod tests {
let mut counter = [0; 3]; let mut counter = [0; 3];
// Iterator over Cards from N_DECKS number of decks. // Iterator over Cards from N_DECKS number of decks.
let cards = (0..((N_DECKS as i64) * 52)).map(Card::from); let cards = (0..((N_DECKS as i64) * 52)).map(Card::from);
cards
.clone() for (c1, c2) in cards.clone().zip_cartesian(cards.clone()) {
.flat_map(move |c1| cards.clone().map(move |c2| (c1, c2))) let res = test_footstool(&Single(c1), &Single(c2)).0;
.for_each(|(c1, c2)| { let res = footstool_to_numeral(res);
let res = test_footstool(&Single(c1), &Single(c2)).0; counter[res] += 1;
let res = footstool_to_numeral(res); }
counter[res] += 1;
});
counter counter
}; };
@@ -263,12 +271,12 @@ mod tests {
let [_, half, full] = counter; let [_, half, full] = counter;
// TEST: There are 103n^2 footstool instances. // TEST: There are 103n^2 footstool instances.
assert_eq!(103 * N_DECKS * N_DECKS, half + full); assert_eq!(103 * N_DECKS_SQR, half + full);
// TEST: There are 51n^2 half footstool instances. // TEST: There are 51n^2 half footstool instances.
assert_eq!(51 * N_DECKS * N_DECKS, half); assert_eq!(51 * N_DECKS_SQR, half);
// TEST: There are 52n^2 full footstool instances. // TEST: There are 52n^2 full footstool instances.
assert_eq!(52 * N_DECKS * N_DECKS, full); assert_eq!(52 * N_DECKS_SQR, full);
} }
} }