summaryrefslogtreecommitdiffstats
path: root/aoc/src
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/src')
-rw-r--r--aoc/src/lib.rs34
1 files changed, 25 insertions, 9 deletions
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;
2use std::fs::File; 2use std::fs::File;
3use std::io; 3use std::io;
4use std::io::{BufRead, BufReader, Read}; 4use std::io::{BufRead, BufReader, Read};
5use std::panic::catch_unwind;
5use std::time::Instant; 6use std::time::Instant;
6 7
7#[derive(Parser, Debug)] 8#[derive(Parser, Debug)]
@@ -16,6 +17,14 @@ struct Args {
16 17
17pub trait Day { 18pub trait Day {
18 fn example_input(&self) -> &'static str; 19 fn example_input(&self) -> &'static str;
20 fn example_input_part_1(&self) -> &'static str {
21 self.example_input()
22 }
23
24 fn example_input_part_2(&self) -> &'static str {
25 self.example_input()
26 }
27
19 fn example_result_part_1(&self) -> &'static str; 28 fn example_result_part_1(&self) -> &'static str;
20 fn example_result_part_2(&self) -> &'static str; 29 fn example_result_part_2(&self) -> &'static str;
21 30
@@ -61,19 +70,26 @@ pub fn main(day: &dyn Day) {
61 println!("Time to run: {}s", now.elapsed().as_secs_f64()); 70 println!("Time to run: {}s", now.elapsed().as_secs_f64());
62} 71}
63 72
64pub fn test_day<T: Day>(day: &T) { 73pub fn test_day<T: Day + std::panic::RefUnwindSafe>(day: &T) {
65 use std::io::Cursor; 74 use std::io::Cursor;
66 use std::io::{BufReader, Read}; 75 use std::io::{BufReader, Read};
67 76
68 let example_input = day.example_input();
69
70 // Test Part 1 77 // Test Part 1
71 let input = BufReader::new(Box::new(Cursor::new(example_input)) as Box<dyn Read>); 78 let output = catch_unwind(|| {
72 let output = day.part_1(input); 79 let input = BufReader::new(Box::new(Cursor::new(day.example_input_part_1())) as Box<dyn Read>);
73 assert_eq!(output.trim(), day.example_result_part_1(), "Part 1 failed"); 80 day.part_1(input)
81 });
82 if output.is_ok() {
83 assert_eq!(output.unwrap().trim(), day.example_result_part_1(), "Part 1 failed");
84 }
74 85
75 // Test Part 2 86 // Test Part 2
76 let input = BufReader::new(Box::new(Cursor::new(example_input)) as Box<dyn Read>); 87 let output = catch_unwind(|| {
77 let output = day.part_2(input); 88 let input = BufReader::new(Box::new(Cursor::new(day.example_input_part_2())) as Box<dyn Read>);
78 assert_eq!(output.trim(), day.example_result_part_2(), "Part 2 failed"); 89 day.part_2(input)
90 });
91
92 if output.is_ok() {
93 assert_eq!(output.unwrap().trim(), day.example_result_part_2(), "Part 2 failed");
94 }
79} \ No newline at end of file 95} \ No newline at end of file