Skip to content

Add skeleton tests #1

Description

@glyn

Sorry this isn't a PR, but I'm not sure how to map this code to your codegen scheme. Hopefully, that would be straightforward for you to do.

I use a standard stack project at the start of each day, so the following are structured to match that. The tests actually reflect day 1, part 1 of this year's AoC, but should be generalised to fit your codegen scheme as appropriate.

As you will notice, my preferred style is to package unit tests with the solution code. This allows testing intermediate functions on the way to a full solution should that be necessary, without having to export those functions. If you don't like this, feel free to change it (or anything else actually - the goal is to encourage testing).

test/Spec.hs:

import Test.HUnit
import Solution

main :: IO ()
main = do
         runTestTTAndExit tests

tests :: Test
tests = TestList[TestLabel "t0" t0,
                 TestLabel "unit tests" unitTests
                ]

t0 :: Test
t0 = TestCase ( assertEqual "t0" expected (solution input))
         where expected = 142
               input = ["1abc2",
                        "pqr3stu8vwx",
                        "a1b2c3d4e5f",
                        "treb7uchet"]

src/Solution.hs:

module Solution (solution, unitTests) where

import qualified Data.Set as S
import Data.Char
import Test.HUnit

solution :: [String] -> Int
solution file = let cvs = map cv file in
                  sum cvs

cv :: String -> Int
cv s = undefined // trying to reduce the spoiler effect of this issue

unitTests :: Test
unitTests = TestList[TestLabel "t0" t0,
                     TestLabel "t1" t1,
                     TestLabel "t2" t2,
                     TestLabel "t3" t3
                ]

t0 :: Test
t0 = TestCase ( assertEqual "t0" expected (cv input))
         where expected = 12
               input = "1abc2"

t1 :: Test
t1 = TestCase ( assertEqual "t1" expected (cv input))
         where expected = 38
               input = "pqr3stu8vwx"

t2 :: Test
t2 = TestCase ( assertEqual "t2" expected (cv input))
         where expected = 15
               input = "a1b2c3d4e5f"

t3 :: Test
t3 = TestCase ( assertEqual "t3" expected (cv input))
         where expected = 77
               input = "treb7uchet"

app/Main.hs:

module Main (main) where

import Solution

main :: IO ()
main = do
           contents <- readFile "day1.data"
           print . solution $ lines contents

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions