Add macros for tests and benches
This commit is contained in:
parent
5cc46fbd60
commit
728122032d
@ -14,3 +14,4 @@ text_io = "0.1.8"
|
||||
# for the day 4 meme solution
|
||||
serde = { version = "1.0.117", features = ["derive"] }
|
||||
serde_yaml = "0.8.14"
|
||||
paste = "1.0.3"
|
||||
|
@ -37,37 +37,15 @@ fn main() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2020::*;
|
||||
use paste::paste;
|
||||
use test::black_box;
|
||||
|
||||
const TEST_INPUT: &str = "";
|
||||
|
||||
#[test]
|
||||
fn part1_test() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!(part1(&input), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_test() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!(part2(&input), 0);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| parse_input(black_box(&raw)))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part1(b: &mut test::Bencher) {
|
||||
let input = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part1(black_box(&input)), 0));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut test::Bencher) {
|
||||
let input = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part2(black_box(&input)), 0));
|
||||
}
|
||||
test!(part1 == 0);
|
||||
test!(part2 == 0);
|
||||
bench!(part1 == 0);
|
||||
bench!(part2 == 0);
|
||||
bench_input!(len == 0);
|
||||
}' > src/bin/day$today.rs
|
||||
|
@ -3,7 +3,7 @@ extern crate test;
|
||||
use itertools::Itertools;
|
||||
use std::env;
|
||||
|
||||
fn read_input() -> Vec<usize> {
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
@ -11,9 +11,10 @@ fn read_input() -> Vec<usize> {
|
||||
.unwrap_or_else(|| String::from("inputs/day01")),
|
||||
)
|
||||
.unwrap()
|
||||
.lines()
|
||||
.filter_map(|l| l.parse().ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> Vec<usize> {
|
||||
input.lines().filter_map(|l| l.parse().ok()).collect()
|
||||
}
|
||||
|
||||
fn part1(input: &[usize]) -> usize {
|
||||
@ -36,7 +37,7 @@ fn part2(input: &[usize]) -> usize {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = read_input();
|
||||
let input = parse_input(&read_input());
|
||||
println!("Part 1: {}", part1(&input));
|
||||
println!("Part 2: {}", part2(&input));
|
||||
}
|
||||
@ -44,17 +45,10 @@ fn main() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use test::{black_box, Bencher};
|
||||
use aoc2020::*;
|
||||
use paste::paste;
|
||||
use test::black_box;
|
||||
|
||||
#[bench]
|
||||
fn bench_part1(b: &mut Bencher) {
|
||||
let input = read_input();
|
||||
b.iter(|| black_box(part1(black_box(&input))));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut Bencher) {
|
||||
let input = read_input();
|
||||
b.iter(|| black_box(part2(black_box(&input))));
|
||||
}
|
||||
bench!(part1 == 731731);
|
||||
bench!(part2 == 116115990);
|
||||
}
|
||||
|
@ -75,9 +75,11 @@ fn main() {
|
||||
#[allow(unused)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2020::*;
|
||||
use paste::paste;
|
||||
use test::{self, black_box};
|
||||
|
||||
const INPUT: &str = "..##.......
|
||||
const TEST_INPUT: &str = "..##.......
|
||||
#...#...#..
|
||||
.#....#..#.
|
||||
..#.#...#.#
|
||||
@ -104,33 +106,22 @@ mod tests {
|
||||
trees
|
||||
}
|
||||
|
||||
test!(count_all_paths == 336);
|
||||
bench!(count_all_paths == 4723283400);
|
||||
bench_input!(len == 323);
|
||||
|
||||
#[test]
|
||||
fn part1_test_functional() {
|
||||
let forest = parse_input(INPUT);
|
||||
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(INPUT);
|
||||
let forest = parse_input(TEST_INPUT);
|
||||
assert_eq!(count_trees_imperative(&forest, STEP_RIGHT[1], STEP_DOWN[1]), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_test() {
|
||||
let forest = parse_input(INPUT);
|
||||
assert_eq!(count_all_paths(&forest), 336);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| {
|
||||
let forest = parse_input(black_box(&raw));
|
||||
assert_eq!(forest.len(), 323);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part_1_functional(b: &mut test::Bencher) {
|
||||
let forest = parse_input(&read_input());
|
||||
|
@ -81,6 +81,8 @@ fn main() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2020::*;
|
||||
use paste::paste;
|
||||
use test::black_box;
|
||||
|
||||
const TEST_INPUT: &str = "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
|
||||
@ -138,15 +140,6 @@ iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719";
|
||||
assert_eq!(part2(&invalid), 0);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| assert_eq!(parse_input(black_box(&raw)).len(), 235))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut test::Bencher) {
|
||||
let parsed = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(black_box(part2(&parsed)), 194))
|
||||
}
|
||||
bench_input!(len == 235);
|
||||
bench!(part2 == 194);
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ fn part2(answers: &[Vec<HashSet<char>>]) -> usize {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2020::*;
|
||||
use paste::paste;
|
||||
use test::black_box;
|
||||
|
||||
const TEST_INPUT: &str = "abc
|
||||
@ -68,11 +70,6 @@ hdcl
|
||||
hldc
|
||||
ldhc";
|
||||
|
||||
#[test]
|
||||
fn part1_test() {
|
||||
assert_eq!(part1(&parse_input(TEST_INPUT)), 11);
|
||||
}
|
||||
|
||||
fn part2_help(answers: &[Vec<HashSet<char>>]) -> Vec<usize> {
|
||||
answers.iter().map(|v| v.iter().fold(v[0].clone(), |a, b| &a & b).len()).collect()
|
||||
}
|
||||
@ -83,21 +80,8 @@ ldhc";
|
||||
assert_eq!(part2(&parse_input(TEST_INPUT)), 6);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let s = read_input();
|
||||
b.iter(|| assert_eq!(parse_input(black_box(&s)).len(), 490))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut test::Bencher) {
|
||||
let i = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part2(black_box(&i)), 3221))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part1(b: &mut test::Bencher) {
|
||||
let i = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part1(black_box(&i)), 6735))
|
||||
}
|
||||
bench_input!(len == 490);
|
||||
test!(part1 == 11);
|
||||
bench!(part1 == 6735);
|
||||
bench!(part2 == 3221);
|
||||
}
|
||||
|
@ -83,6 +83,7 @@ fn parse_input(s: &str) -> Vec<Bag> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2020::*;
|
||||
use test::black_box;
|
||||
|
||||
const TEST_INPUT: &str = "light red bags contain 1 bright white bag, 2 muted yellow bags.
|
||||
@ -117,11 +118,7 @@ dark violet bags contain no other bags.";
|
||||
assert_eq!(part2(&input, COLOR), 126);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| assert_eq!(parse_input(black_box(&raw)).len(), 594))
|
||||
}
|
||||
bench_input!(len == 594);
|
||||
|
||||
#[bench]
|
||||
fn bench_part1(b: &mut test::Bencher) {
|
||||
|
@ -56,6 +56,8 @@ fn main() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2020::*;
|
||||
use paste::paste;
|
||||
use test::black_box;
|
||||
|
||||
const TEST_INPUT: &str = "28
|
||||
@ -90,33 +92,9 @@ mod tests {
|
||||
10
|
||||
3";
|
||||
|
||||
#[test]
|
||||
fn part1_test() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!(part1(&input), (22, 10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_test() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!(part2(&input), 19208);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| parse_input(black_box(&raw)))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part1(b: &mut test::Bencher) {
|
||||
let input = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part1(black_box(&input)), (69, 24)));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut test::Bencher) {
|
||||
let input = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part2(black_box(&input)), 56693912375296));
|
||||
}
|
||||
test!(part1 == (22, 10));
|
||||
test!(part2 == 19208);
|
||||
bench!(part1 == (69, 24));
|
||||
bench!(part2 == 56693912375296);
|
||||
bench_input!(len == 93);
|
||||
}
|
||||
|
1
2020/src/lib.rs
Normal file
1
2020/src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod teststuff;
|
@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
println!("Please use cargo run dayXX to run any of the days.");
|
||||
}
|
36
2020/src/teststuff.rs
Normal file
36
2020/src/teststuff.rs
Normal file
@ -0,0 +1,36 @@
|
||||
#[macro_export]
|
||||
macro_rules! bench {
|
||||
($part: ident == $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));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! bench_input {
|
||||
($fn:ident == $expected_len:expr) => {
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| assert_eq!(parse_input(black_box(&raw)).$fn(), $expected_len));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! test {
|
||||
($part: ident == $expected:expr) => {
|
||||
paste! {
|
||||
#[test]
|
||||
fn [<$part _test>]() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!($part(&input), $expected);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user