From 1ee14b228c6656bb6245b5dba0e23edff28cc848 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Thu, 14 Nov 2019 19:32:36 +0100 Subject: [PATCH] 2018: add tests --- 2018/aoc01/src/main.rs | 118 ++++++++++++++++++++++++++++++++++++++++- 2018/aoc02/src/main.rs | 26 +++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) diff --git a/2018/aoc01/src/main.rs b/2018/aoc01/src/main.rs index 5af08f6..7bfbf91 100644 --- a/2018/aoc01/src/main.rs +++ b/2018/aoc01/src/main.rs @@ -33,13 +33,129 @@ fn part2(input: &str) -> Result { let mut freq = 0; loop { for line in input.lines() { - freq += line.parse::()?; if freqs.contains(&freq) { println!("{}", freq); return Ok(freq); } else { freqs.insert(freq); } + freq += line.parse::()?; } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_provided1() { + let input = "+1 +-2 ++3 ++1 +"; + + assert_eq!(part1(input).unwrap(), 3); + } + + #[test] + fn part1_provided2() { + let input = "+1 ++1 ++1 +"; + + assert_eq!(part1(input).unwrap(), 3); + } + + #[test] + fn part1_provided3() { + let input = "+1 ++1 +-2 +"; + + assert_eq!(part1(input).unwrap(), 0); + } + + #[test] + fn part1_provided4() { + let input = "-1 +-2 +-3 +"; + + assert_eq!(part1(input).unwrap(), -6); + } + + #[test] + fn part1_real() { + let input = include_str!("../input/input.txt"); + + assert_eq!(part1(input).unwrap(), 427); + } + + #[test] + fn part2_provided1() { + let input = "+1 +-2 ++3 ++1 +"; + + assert_eq!(part2(input).unwrap(), 2); + } + + #[test] + fn part2_provided2() { + let input = "+1 +-1 +"; + + assert_eq!(part2(input).unwrap(), 0); + } + + #[test] + fn part2_provided3() { + let input = "+3 ++3 ++4 +-2 +-4 +"; + + assert_eq!(part2(input).unwrap(), 10); + } + + #[test] + fn part2_provided4() { + let input = "-6 ++3 ++8 ++5 +-6 +"; + + assert_eq!(part2(input).unwrap(), 5); + } + + #[test] + fn part2_provided5() { + let input = "+7 ++7 +-2 +-7 +-4 +"; + + assert_eq!(part2(input).unwrap(), 14); + } + + #[test] + fn part2_real() { + let input = include_str!("../input/input.txt"); + + assert_eq!(part2(input).unwrap(), 341); + } +} diff --git a/2018/aoc02/src/main.rs b/2018/aoc02/src/main.rs index 2d97726..92e565f 100644 --- a/2018/aoc02/src/main.rs +++ b/2018/aoc02/src/main.rs @@ -40,3 +40,29 @@ fn part1(input: &str) -> Result { Ok(twice * thrice) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_provided() { + let input = "abcdef +bababc +abbcde +abcccd +aabcdd +abcdee +ababab +"; + + assert_eq!(part1(input).unwrap(), 12); + } + + #[test] + fn part1_real() { + let input = include_str!("../input/input.txt"); + + assert_eq!(part1(input).unwrap(), 5750); + } +}