feat: setup tests and implement them for each day

This commit is contained in:
Victor Martinez 2025-03-13 18:41:24 +01:00
parent 7503f592b5
commit 796eb829c5
8 changed files with 71 additions and 23 deletions

View file

@ -44,6 +44,9 @@ test-suite aoc2024-test
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Spec.hs main-is: Spec.hs
other-modules: other-modules:
Day1Spec
Day2Spec
Day3Spec
Paths_aoc2024 Paths_aoc2024
autogen-modules: autogen-modules:
Paths_aoc2024 Paths_aoc2024
@ -51,8 +54,7 @@ test-suite aoc2024-test
test test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends: build-depends:
HUnit >=1.6.2.0 && <1.7 QuickCheck >=2.14.3 && <2.15
, QuickCheck >=2.14.3 && <2.15
, aoc2024 , aoc2024
, base >=4.7 && <5 , base >=4.7 && <5
, hspec >=2.0.0 , hspec >=2.0.0

View file

@ -48,5 +48,4 @@ tests:
dependencies: dependencies:
- aoc2024 - aoc2024
- QuickCheck ^>= 2.14.3 - QuickCheck ^>= 2.14.3
- HUnit ^>= 1.6.2.0
- hspec >=2.0.0 - hspec >=2.0.0

View file

@ -1,6 +1,7 @@
module Day1 module Day1
( partOne, ( partOne,
partTwo, partTwo,
Error,
) )
where where
@ -16,10 +17,10 @@ type LocationID = Int
data ParseError data ParseError
= ParseLocationError String String = ParseLocationError String String
| ParseLineError String | ParseLineError String
deriving (Show) deriving (Show, Eq)
newtype Error = ParseInputError ParseError newtype Error = ParseInputError ParseError
deriving (Show) deriving (Show, Eq)
partOne :: String -> Either Error Int partOne :: String -> Either Error Int
partOne contents = calculateScore <$> BF.first ParseInputError (parseInput contents) partOne contents = calculateScore <$> BF.first ParseInputError (parseInput contents)

View file

@ -1,10 +1,10 @@
module Day2 (partOne, partTwo) where module Day2 (partOne, partTwo, Error) where
import qualified Data.Bifunctor as BF import qualified Data.Bifunctor as BF
import Text.Read (readEither) import Text.Read (readEither)
newtype Error = ParseError String newtype Error = ParseError String
deriving (Show) deriving (Show, Eq)
type Level = Int type Level = Int

23
test/Day1Spec.hs Normal file
View file

@ -0,0 +1,23 @@
module Day1Spec (spec) where
import Day1 (partOne, partTwo)
import Test.Hspec
spec :: Spec
spec = do
describe "PartOne" $ do
it "Given two valid lists it returns the total distance" $ do
let expectedResult = 11
partOne testInput `shouldBe` Right expectedResult
describe "PartTwo" $ do
it "Given two valid lists it returns their similarity score" $ do
partTwo testInput `shouldBe` Right 31
where
testInput =
"3 4\n\
\4 3\n\
\2 5\n\
\1 3\n\
\3 9\n\
\3 3"

22
test/Day2Spec.hs Normal file
View file

@ -0,0 +1,22 @@
module Day2Spec (spec) where
import Day2 (partOne, partTwo)
import Test.Hspec
spec :: Spec
spec = do
describe "PartOne" $ do
it "Given a list of reports, it returns how many are valid" $ do
partOne testInput `shouldBe` Right 2
describe "PartTwo" $ do
it "Given a list of reports, it returns how many are valid" $ do
partTwo testInput `shouldBe` Right 4
where
testInput =
"7 6 4 2 1\n\
\1 2 7 8 9\n\
\9 7 6 2 1\n\
\1 3 2 4 5\n\
\8 6 4 4 1\n\
\1 3 6 7 9"

16
test/Day3Spec.hs Normal file
View file

@ -0,0 +1,16 @@
module Day3Spec (spec) where
import Day3 (partOne, partTwo)
import Test.Hspec
spec :: Spec
spec = do
describe "PartOne" $ do
it "Given a corrupted program, it extracts the valid operations and runs them" $ do
let input = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
partOne input `shouldBe` (2 * 4 + 5 * 5 + 11 * 8 + 8 * 5)
describe "PartTwo" $ do
it "Given a corrupted program, it extracts the valid operations and runs them" $ do
let input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
partTwo input `shouldBe` (2 * 4 + 8 * 5)

View file

@ -1,16 +1 @@
import Control.Exception (evaluate) {-# OPTIONS_GHC -F -pgmF hspec-discover #-}
import Test.Hspec
import Test.QuickCheck
main :: IO ()
main = hspec $ do
describe "Prelude.head" $ do
it "returns the first element of a list" $ do
head [23 ..] `shouldBe` (23 :: Int)
it "returns the first element of an *arbitrary* list" $
property $
\x xs -> head (x : xs) == (x :: Int)
it "throws an exception if used with an empty list" $ do
evaluate (head []) `shouldThrow` anyException