More test macros

This commit is contained in:
kageru 2020-12-10 16:41:54 +01:00
parent 6bf1811339
commit 59f534307d
9 changed files with 43 additions and 114 deletions

View File

@ -43,6 +43,6 @@ mod tests {
use paste::paste;
use test::black_box;
bench!(part1 == 731731);
bench!(part2 == 116115990);
bench!(part1() == 731731);
bench!(part2() == 116115990);
}

View File

@ -101,37 +101,13 @@ mod tests {
trees
}
test!(count_all_paths == 336);
bench!(count_all_paths == 4723283400);
test!(count_trees(STEP_RIGHT[1], STEP_DOWN[1]) == 7);
test!(count_trees_imperative(STEP_RIGHT[1], STEP_DOWN[1]) == 7);
test!(count_all_paths() == 336);
bench!(count_all_paths() == 4723283400);
bench!(count_trees(STEP_RIGHT[1], STEP_DOWN[1]) == 187);
bench!(count_trees_imperative(STEP_RIGHT[1], STEP_DOWN[1]) == 187);
bench_input!(len == 323);
#[test]
fn part1_test_functional() {
let forest = parse_input(TEST_INPUT);
assert_eq!(count_trees(&forest, STEP_RIGHT[1], STEP_DOWN[1]), 7);
}
#[test]
fn part1_test_imperative() {
let forest = parse_input(TEST_INPUT);
assert_eq!(count_trees_imperative(&forest, STEP_RIGHT[1], STEP_DOWN[1]), 7);
}
#[bench]
fn bench_part_1_functional(b: &mut test::Bencher) {
let forest = parse_input(&read_input());
b.iter(|| assert_eq!(count_trees(black_box(&forest), STEP_RIGHT[1], STEP_DOWN[1]), 187));
}
#[bench]
fn bench_part_1_imperative(b: &mut test::Bencher) {
let forest = parse_input(&read_input());
b.iter(|| assert_eq!(count_trees_imperative(black_box(&forest), STEP_RIGHT[1], STEP_DOWN[1]), 187));
}
#[bench]
fn bench_part_2(b: &mut test::Bencher) {
let forest = parse_input(&read_input());
b.iter(|| assert_eq!(count_all_paths(black_box(&forest)), 4723283400));
}
}

View File

@ -135,5 +135,5 @@ iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719";
}
bench_input!(len == 235);
bench!(part2 == 194);
bench!(part2() == 194);
}

View File

@ -76,7 +76,7 @@ ldhc";
}
bench_input!(len == 490);
test!(part1 == 11);
bench!(part1 == 6735);
bench!(part2 == 3221);
test!(part1() == 11);
bench!(part1() == 6735);
bench!(part2() == 3221);
}

View File

@ -80,6 +80,7 @@ mod tests {
use super::*;
use aoc2020::*;
use test::black_box;
use paste::paste;
const TEST_INPUT: &str = "light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
@ -91,12 +92,6 @@ vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.";
#[test]
fn part1_test() {
let input = parse_input(TEST_INPUT);
assert_eq!(part1(&input, COLOR, &mut HashSet::new()).len(), 4);
}
const TEST_INPUT_2: &str = "shiny gold bags contain 2 dark red bags.
dark red bags contain 2 dark orange bags.
dark orange bags contain 2 dark yellow bags.
@ -105,6 +100,12 @@ dark green bags contain 2 dark blue bags.
dark blue bags contain 2 dark violet bags.
dark violet bags contain no other bags.";
#[test]
fn part1_test() {
let input = parse_input(TEST_INPUT);
assert_eq!(part1(&input, COLOR, &mut HashSet::new()).len(), 4);
}
#[test]
fn part2_test() {
let input = parse_input(TEST_INPUT);
@ -121,9 +122,5 @@ dark violet bags contain no other bags.";
b.iter(|| assert_eq!(part1(black_box(&bags), COLOR, &mut HashSet::new()).len(), 226))
}
#[bench]
fn bench_part2(b: &mut test::Bencher) {
let bags = parse_input(&read_input());
b.iter(|| assert_eq!(part2(black_box(&bags), COLOR), 9569))
}
bench!(part2(COLOR) == 9569);
}

View File

@ -92,6 +92,8 @@ fn part2(commands: &Vec<Command>, seen: &mut Vec<bool>, mut index: i32, mut acc:
mod tests {
use super::*;
use test::black_box;
use aoc2020::*;
use paste::paste;
const TEST_INPUT: &str = "nop +0
acc +1
@ -103,38 +105,9 @@ acc +1
jmp -4
acc +6";
#[test]
fn part1_test() {
let commands = parse_input(TEST_INPUT);
assert_eq!(part1(&commands), 5);
}
#[test]
fn part2_test() {
let commands = parse_input(TEST_INPUT);
assert_eq!(part2(&commands, &mut vec![false; commands.len()], 0, 0, false), Some(8));
}
#[bench]
fn bench_part1(b: &mut test::Bencher) {
let commands = parse_input(&read_input());
b.iter(|| assert_eq!(part1(black_box(&commands)), 1317));
}
#[bench]
fn bench_part2(b: &mut test::Bencher) {
let commands = parse_input(&read_input());
b.iter(|| {
assert_eq!(
part2(black_box(&commands), &mut vec![false; commands.len()], 0, 0, false),
Some(1033)
)
});
}
#[bench]
fn bench_input_parsing(b: &mut test::Bencher) {
let raw = read_input();
b.iter(|| assert_eq!(parse_input(black_box(&raw)).len(), 626))
}
test!(part1() == 5);
test!(part2(&mut vec![false; 9], 0, 0, false) == Some(8));
bench!(part1() == 1317);
bench!(part2(&mut vec![false; 626], 0, 0, false) == Some(1033));
bench_input!(len == 626);
}

View File

@ -54,6 +54,8 @@ fn main() {
mod tests {
use super::*;
use test::black_box;
use aoc2020::*;
use paste::paste;
const TEST_INPUT: &str = "35
20
@ -76,27 +78,8 @@ mod tests {
309
576";
#[test]
fn part1_test() {
let input = parse_input(TEST_INPUT);
assert_eq!(part1(&input, 5), 127);
}
#[test]
fn part2_test() {
let input = parse_input(TEST_INPUT);
assert_eq!(part2(&input, 127), 62);
}
#[bench]
fn bench_part1(b: &mut test::Bencher) {
let input = parse_input(&read_input());
b.iter(|| assert_eq!(part1(black_box(&input), 25), 393911906));
}
#[bench]
fn bench_part2(b: &mut test::Bencher) {
let input = parse_input(&read_input());
b.iter(|| assert_eq!(part2(black_box(&input), 393911906), 59341885));
}
test!(part1(5) == 127);
test!(part2(127) == 62);
bench!(part1(25) == 393911906);
bench!(part2(393911906) == 59341885);
}

View File

@ -86,9 +86,9 @@ mod tests {
10
3";
test!(part1 == (22, 10));
test!(part2 == 19208);
bench!(part1 == (69, 24));
bench!(part2 == 56693912375296);
test!(part1() == (22, 10));
test!(part2() == 19208);
bench!(part1() == (69, 24));
bench!(part2() == 56693912375296);
bench_input!(len == 93);
}

View File

@ -1,11 +1,11 @@
#[macro_export]
macro_rules! bench {
($part: ident == $expected:expr) => {
($part: ident ($($param: expr),*) == $expected:expr) => {
paste! {
#[bench]
fn [<$part _bench>](b: &mut test::Bencher) {
let input = parse_input(&read_input());
b.iter(|| assert_eq!($part(black_box(&input)), $expected));
b.iter(|| assert_eq!($part(black_box(&input)$(, $param)*), $expected));
}
}
};
@ -24,12 +24,12 @@ macro_rules! bench_input {
#[macro_export]
macro_rules! test {
($part: ident == $expected:expr) => {
($part: ident ($($param: expr),*) == $expected:expr) => {
paste! {
#[test]
fn [<$part _test>]() {
let input = parse_input(TEST_INPUT);
assert_eq!($part(&input), $expected);
assert_eq!($part(&input$(, $param)*), $expected);
}
}
};