-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.rs
137 lines (122 loc) · 4.54 KB
/
integration.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
util::tests! {
2015: {
1: [138, 1771],
2: [1598415, 3812909],
3: [2565, 2639],
4: [346386, 9958218],
5: [236, 51],
6: [569999, 17836115],
7: [16076, 2797],
8: [1371, 2117],
9: [251, 898],
10: [360154, 5103798],
11: ["vzbxxyzz", "vzcaabcc"],
12: [111754, 65402],
13: [709, 668],
14: [2696, 1084],
15: [222870, 117936],
16: [213, 323],
17: [654, 57],
18: [821, 886],
19: [518, 200],
20: [786240, 831600],
21: [78, 148],
22: [900, 1216],
23: [170, 247],
24: [10439961859u64, 72050269],
25: [9132360]
},
2016: {
1: [241, 116],
2: [78985, "57DD8"],
3: [1050, 1921],
4: [137896, 501],
5: ["801b56a7", "424a0197"],
6: ["tzstqsua", "myregdnr"],
7: [115, 231],
8: [115, "EFEYKFRFIJ"],
9: [102239, #[ignore = "slow implementation"] 10780403063u64]
},
2024: {
1: [2000468, 18567089],
2: [442],
3: [179834255, 80570939],
4: [2454, 1858]
}
}
mod util {
//! Utilities for integration testing.
/// Helper macro for more easily writing advent of code integration tests.
macro_rules! tests {
{$($year:literal: {$($day:literal: [$($(#[$attrs:meta])* $part:literal),+]),*}),+} => {
::paste::paste!{
$(
mod [<_ $year>] {
$(
crate::util::tests_impl!(year: $year, day: $day, parts: [$($(#[$attrs])* $part),+]);
)*
}
)+
}
};
}
/// Implementation detail of the `tests` macro to handle cases where there's either 1 or 2 solved parts of a day.
#[doc(hidden)]
macro_rules! tests_impl {
// Construct a module named `d{$day}` that contains tests for both parts of a given day
(year: $year:literal, day: $day:literal, parts: [$(#[$attrs1:meta])* $part1:literal, $(#[$attrs2:meta])* $part2:literal]) => {
::paste::paste!{
mod [<day $day>] {
crate::util::tests_impl!(year: $year, day: $day, part: 1, solution: $part1, meta: $($attrs1)*);
crate::util::tests_impl!(year: $year, day: $day, part: 2, solution: $part2, meta: $($attrs2)*);
}
}
};
// Construct a module named `d{$day}` that contains a test for part 1 of a given day
(year: $year:literal, day: $day:literal, parts: [$(#[$attrs:meta])* $part1:literal]) => {
::paste::paste!{
mod [<day $day>] {
crate::util::tests_impl!(year: $year, day: $day, part: 1, solution: $part1, meta: $($attrs)*);
}
}
};
// Construct a test named `p{$part}` that checks for the given solution
(year: $year:literal, day: $day:literal, part: $part:literal, solution: $solution:literal, meta: $($attrs:meta)*) => {
::paste::paste!{
$(#[$attrs])*
#[test]
fn [<part $part>]() {
let two_digit_day = if $day < 10 {
::std::concat!("0", ::std::stringify!($day))
} else {
::std::stringify!($day)
};
// Read input from ENV in CI, or from disk locally.
let input = {
let input_file = {
let mut crate_dir: ::std::path::PathBuf =
::std::env!("CARGO_MANIFEST_DIR").parse().unwrap();
crate_dir.push("tests");
crate_dir.push("inputs");
crate_dir.push(stringify!($year));
crate_dir.push(two_digit_day);
crate_dir
};
::std::fs::read_to_string(input_file).unwrap()
};
let year = <::advent_of_code::meta::Year as ::std::convert::TryFrom<u16>>::try_from($year)
.unwrap();
let day =
<::advent_of_code::meta::Day as ::std::convert::TryFrom<u8>>::try_from($day).unwrap();
let part =
<::advent_of_code::meta::Part as ::std::convert::TryFrom<u8>>::try_from($part).unwrap();
let output = ::advent_of_code::AOC[year][day][part].solve(input.trim()).unwrap();
::pretty_assertions::assert_eq!(output, $solution.to_string());
}
}
};
}
pub(crate) use tests;
#[doc(hidden)]
pub(crate) use tests_impl;
}