From 120fe4c9e696ec3289736843f54657fdf2b8247d Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Tue, 3 Dec 2024 10:01:25 +0100 Subject: Day3 --- aoc/src/lib.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'aoc/src') diff --git a/aoc/src/lib.rs b/aoc/src/lib.rs index c4318e1..dcca651 100644 --- a/aoc/src/lib.rs +++ b/aoc/src/lib.rs @@ -2,6 +2,7 @@ use clap::Parser; use std::fs::File; use std::io; use std::io::{BufRead, BufReader, Read}; +use std::panic::catch_unwind; use std::time::Instant; #[derive(Parser, Debug)] @@ -16,6 +17,14 @@ struct Args { pub trait Day { fn example_input(&self) -> &'static str; + fn example_input_part_1(&self) -> &'static str { + self.example_input() + } + + fn example_input_part_2(&self) -> &'static str { + self.example_input() + } + fn example_result_part_1(&self) -> &'static str; fn example_result_part_2(&self) -> &'static str; @@ -61,19 +70,26 @@ pub fn main(day: &dyn Day) { println!("Time to run: {}s", now.elapsed().as_secs_f64()); } -pub fn test_day(day: &T) { +pub fn test_day(day: &T) { use std::io::Cursor; use std::io::{BufReader, Read}; - let example_input = day.example_input(); - // Test Part 1 - let input = BufReader::new(Box::new(Cursor::new(example_input)) as Box); - let output = day.part_1(input); - assert_eq!(output.trim(), day.example_result_part_1(), "Part 1 failed"); + let output = catch_unwind(|| { + let input = BufReader::new(Box::new(Cursor::new(day.example_input_part_1())) as Box); + day.part_1(input) + }); + if output.is_ok() { + assert_eq!(output.unwrap().trim(), day.example_result_part_1(), "Part 1 failed"); + } // Test Part 2 - let input = BufReader::new(Box::new(Cursor::new(example_input)) as Box); - let output = day.part_2(input); - assert_eq!(output.trim(), day.example_result_part_2(), "Part 2 failed"); + let output = catch_unwind(|| { + let input = BufReader::new(Box::new(Cursor::new(day.example_input_part_2())) as Box); + day.part_2(input) + }); + + if output.is_ok() { + assert_eq!(output.unwrap().trim(), day.example_result_part_2(), "Part 2 failed"); + } } \ No newline at end of file -- cgit v1.2.3