Macro utils::examples

source ·
macro_rules! examples {
    ($day:ident$(<$lifetime:lifetime>)? -> ($p1:ty, $p2:ty) [$($($tail:tt,)+)?]) => { ... };
    (@item {input: $str:literal, part1: $p1:literal, part2: $p2:expr $(,)?}) => { ... };
    (@item {input: $str:literal, part1: $p1:literal $(,)?}) => { ... };
    (@item {input: $str:literal, part2: $p2:expr $(,)?}) => { ... };
    (@item {file: $file:literal, part1: $p1:literal, part2: $p2:expr $(,)?}) => { ... };
    (@item {file: $file:literal, part1: $p1:literal $(,)?}) => { ... };
    (@item {file: $file:literal, part2: $p2:expr $(,)?}) => { ... };
    (@ignore $($tail:tt)*) => { ... };
}
Expand description

Macro to generate a list of examples, implement PuzzleExamples and add example tests.

The provided types for part1 and part2 don’t have to match the types returned by the day’s functions, but they must be comparable with PartialEq. For functions returning String &'static str should be used.

If no examples are provided, tests aren’t generated.

§Examples

Adding examples to a Day01 puzzle where part1 returns u32 and part2 returns u64. The first example has correct answers defined for both parts. The second and third examples are only applicable to part1 and part2 of the puzzle respectively.

examples!(Day01 -> (u32, u64) [
    {input: "ABCDEF", part1: 30, part2: 342},
    {input: "AAAAAA", part1: 21},
    {input: "ABC123", part2: 853},
]);

Example inputs can also be included from the crate’s examples directory by using file instead of input:

examples!(Day01 -> (u32, u64) [
    {input: "Short example", part1: 27},
    {file: "day01_example.txt", part2: 483},
]);