Reimplement test macro as a generic function.

This commit is contained in:
2020-12-25 09:31:44 +01:00
parent 4c3640e994
commit 40a9cb63ff
21 changed files with 47 additions and 54 deletions

View File

@@ -55,11 +55,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day01, 1, SAMPLE, 514579); test_implementation(Day01, 1, SAMPLE, 514579);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day01, 2, SAMPLE, 241861950); test_implementation(Day01, 2, SAMPLE, 241861950);
} }
} }

View File

@@ -65,11 +65,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day02, 1, SAMPLE, 2); test_implementation(Day02, 1, SAMPLE, 2);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day02, 2, SAMPLE, 1); test_implementation(Day02, 2, SAMPLE, 1);
} }
} }

View File

@@ -50,11 +50,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day03, 1, SAMPLE, 7); test_implementation(Day03, 1, SAMPLE, 7);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day03, 2, SAMPLE, 336); test_implementation(Day03, 2, SAMPLE, 336);
} }
} }

View File

@@ -57,11 +57,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day06, 1, SAMPLE, 11); test_implementation(Day06, 1, SAMPLE, 11);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day06, 2, SAMPLE, 6); test_implementation(Day06, 2, SAMPLE, 6);
} }
} }

View File

@@ -126,12 +126,12 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day07, 1, SAMPLE, 4); test_implementation(Day07, 1, SAMPLE, 4);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day07, 2, SAMPLE, 32); test_implementation(Day07, 2, SAMPLE, 32);
test_implementation!(Day07, 2, SAMPLE2, 126); test_implementation(Day07, 2, SAMPLE2, 126);
} }
} }

View File

@@ -113,11 +113,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day08, 1, SAMPLE, 5); test_implementation(Day08, 1, SAMPLE, 5);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day08, 2, SAMPLE, 8); test_implementation(Day08, 2, SAMPLE, 8);
} }
} }

View File

@@ -59,13 +59,13 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day10, 1, SAMPLE, 35); test_implementation(Day10, 1, SAMPLE, 35);
test_implementation!(Day10, 1, SAMPLE2, 220); test_implementation(Day10, 1, SAMPLE2, 220);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day10, 2, SAMPLE, 8); test_implementation(Day10, 2, SAMPLE, 8);
test_implementation!(Day10, 2, SAMPLE2, 19208); test_implementation(Day10, 2, SAMPLE2, 19208);
} }
} }

View File

@@ -157,11 +157,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day11, 1, SAMPLE, 37); test_implementation(Day11, 1, SAMPLE, 37);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day11, 2, SAMPLE, 26); test_implementation(Day11, 2, SAMPLE, 26);
} }
} }

View File

@@ -97,11 +97,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day12, 1, SAMPLE, 25); test_implementation(Day12, 1, SAMPLE, 25);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day12, 2, SAMPLE, 286); test_implementation(Day12, 2, SAMPLE, 286);
} }
} }

View File

@@ -59,11 +59,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day13, 1, SAMPLE, 295); test_implementation(Day13, 1, SAMPLE, 295);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day13, 2, SAMPLE, 1068781); test_implementation(Day13, 2, SAMPLE, 1068781);
} }
} }

View File

@@ -143,12 +143,12 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day14, 1, SAMPLE, 165); test_implementation(Day14, 1, SAMPLE, 165);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day14, 2, SAMPLE2, 208); test_implementation(Day14, 2, SAMPLE2, 208);
} }
#[test] #[test]

View File

@@ -165,6 +165,6 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day16, 1, SAMPLE, 71); test_implementation(Day16, 1, SAMPLE, 71);
} }
} }

View File

@@ -130,11 +130,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day17, 1, SAMPLE, 112); test_implementation(Day17, 1, SAMPLE, 112);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day17, 2, SAMPLE, 848); test_implementation(Day17, 2, SAMPLE, 848);
} }
} }

View File

@@ -125,12 +125,12 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day19, 1, SAMPLE, 2); test_implementation(Day19, 1, SAMPLE, 2);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day19, 1, SAMPLE2, 3); test_implementation(Day19, 1, SAMPLE2, 3);
test_implementation!(Day19, 2, SAMPLE2, 12); test_implementation(Day19, 2, SAMPLE2, 12);
} }
} }

View File

@@ -413,12 +413,12 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day20, 1, SAMPLE, 20899048083289u64); test_implementation(Day20, 1, SAMPLE, 20899048083289u64);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day20, 2, SAMPLE, 273); test_implementation(Day20, 2, SAMPLE, 273);
} }
#[test] #[test]

View File

@@ -123,11 +123,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day21, 1, SAMPLE, 5); test_implementation(Day21, 1, SAMPLE, 5);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day21, 2, SAMPLE, "mxmxvkd,sqjhc,fvjkl"); test_implementation(Day21, 2, SAMPLE, "mxmxvkd,sqjhc,fvjkl");
} }
} }

View File

@@ -120,11 +120,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day22, 1, SAMPLE, 306); test_implementation(Day22, 1, SAMPLE, 306);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day22, 2, SAMPLE, 291); test_implementation(Day22, 2, SAMPLE, 291);
} }
} }

View File

@@ -149,6 +149,6 @@ mod tests {
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day23, 2, SAMPLE, 149245887792u64); test_implementation(Day23, 2, SAMPLE, 149245887792u64);
} }
} }

View File

@@ -129,11 +129,11 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day24, 1, SAMPLE, 10); test_implementation(Day24, 1, SAMPLE, 10);
} }
#[test] #[test]
fn sample_part2() { fn sample_part2() {
test_implementation!(Day24, 2, SAMPLE, 2208); test_implementation(Day24, 2, SAMPLE, 2208);
} }
} }

View File

@@ -92,6 +92,6 @@ mod tests {
#[test] #[test]
fn sample_part1() { fn sample_part1() {
test_implementation!(Day25, 1, SAMPLE, 14897079); test_implementation(Day25, 1, SAMPLE, 14897079);
} }
} }

View File

@@ -67,19 +67,12 @@ pub fn get_implementation(day: usize) -> Box<dyn Solution> {
} }
#[cfg(test)] #[cfg(test)]
#[macro_export] fn test_implementation(mut day: impl Solution, part: u8, mut input: &[u8], answer: impl ToString) {
macro_rules! test_implementation { let result = match part {
($impl:ident, 1, $source:ident, $output:expr) => { 1 => day.part1(&mut input),
let mut implementation = $impl::default(); 2 => day.part2(&mut input),
_ => panic!("Invalid part: {}", part),
let result = implementation.part1(&mut $source.clone());
assert_eq!($output.to_string(), result);
}; };
($impl:ident, 2, $source:ident, $output:expr) => { assert_eq!(answer.to_string(), result);
let mut implementation = $impl::default();
let result = implementation.part2(&mut $source.clone());
assert_eq!($output.to_string(), result);
};
} }