mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 21:30:31 +01:00
Use macros for tests
This commit is contained in:
@@ -47,21 +47,19 @@ impl Solution for Day01 {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::test_implementation;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const SAMPLE: &[u8] = include_bytes!("../samples/01.txt");
|
const SAMPLE: &[u8] = include_bytes!("../samples/01.txt");
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sample_part1() {
|
fn sample_part1() {
|
||||||
let mut day = Day01::default();
|
test_implementation!(Day01, 1, SAMPLE, 514579);
|
||||||
let result = day.part1(&mut SAMPLE.as_ref());
|
|
||||||
assert_eq!("514579", &result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sample_part2() {
|
fn sample_part2() {
|
||||||
let mut day = Day01::default();
|
test_implementation!(Day01, 2, SAMPLE, 241861950);
|
||||||
let result = day.part2(&mut SAMPLE.as_ref());
|
|
||||||
assert_eq!("241861950", &result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,23 +67,19 @@ impl Solution for Day02 {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::test_implementation;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const SAMPLE: &[u8] = include_bytes!("../samples/02.txt");
|
const SAMPLE: &[u8] = include_bytes!("../samples/02.txt");
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sample_part1() {
|
fn sample_part1() {
|
||||||
let mut implementation = Day02;
|
test_implementation!(Day02, 1, SAMPLE, 2);
|
||||||
|
|
||||||
let result = implementation.part1(&mut SAMPLE.as_ref());
|
|
||||||
assert_eq!("2", &result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sample_part2() {
|
fn sample_part2() {
|
||||||
let mut implementation = Day02;
|
test_implementation!(Day02, 2, SAMPLE, 1);
|
||||||
|
|
||||||
let result = implementation.part2(&mut SAMPLE.as_ref());
|
|
||||||
assert_eq!("1", &result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,3 +19,20 @@ pub fn get_implementation(day: usize) -> Box<dyn Solution> {
|
|||||||
_ => panic!("Unsupported day {}", day),
|
_ => panic!("Unsupported day {}", day),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! test_implementation {
|
||||||
|
($impl:ident, 1, $source:ident, $output:expr) => {
|
||||||
|
let mut implementation = $impl::default();
|
||||||
|
|
||||||
|
let result = implementation.part1(&mut $source.as_ref());
|
||||||
|
assert_eq!($output.to_string(), result);
|
||||||
|
};
|
||||||
|
|
||||||
|
($impl:ident, 2, $source:ident, $output:expr) => {
|
||||||
|
let mut implementation = $impl::default();
|
||||||
|
|
||||||
|
let result = implementation.part2(&mut $source.as_ref());
|
||||||
|
assert_eq!($output.to_string(), result);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user