Browse Source

Add initial tests

master
Peter J. Jones 4 years ago
parent
commit
ffdbb7ae9c
4 changed files with 110 additions and 14 deletions
  1. 19
    0
      clockdown.cabal
  2. 9
    14
      src/Clockdown/Core/Digital/Indicator.hs
  3. 57
    0
      test/IndicatorTest.hs
  4. 25
    0
      test/Main.hs

+ 19
- 0
clockdown.cabal View File

@@ -41,3 +41,22 @@ library
, time >= 1.5 && < 1.6

-- , hscurses >= 1.4 && < 1.5

--------------------------------------------------------------------------------
test-suite test
type: exitcode-stdio-1.0
default-language: Haskell2010
hs-source-dirs: test
main-is: Main.hs
other-modules:
IndicatorTest

ghc-options: -Wall -fwarn-incomplete-uni-patterns

if flag(maintainer)
ghc-options: -Werror

build-depends: base
, clockdown
, tasty >= 0.10 && < 0.11
, tasty-hunit >= 0.9 && < 0.10

+ 9
- 14
src/Clockdown/Core/Digital/Indicator.hs View File

@@ -44,26 +44,21 @@ data Indicator = Indicator Word8 deriving (Show)
-- | |
-- -- D --
-- @
data Segment = A | B | C | D | E | F | G deriving (Eq, Enum)
data Segment = A | B | C | D | E | F | G deriving (Eq, Enum, Bounded, Show)

--------------------------------------------------------------------------------
-- | Create an indicator that represents the given integer. Only the numbers
-- 0 through 9 can be displayed. Numbers outside that range will be
-- drawn as an underscore.
indicator :: Int -> Indicator
indicator n =
Indicator $ case n of
0 -> 0x3f
1 -> 0x06
2 -> 0x5b
3 -> 0x4f
4 -> 0x66
5 -> 0x6d
6 -> 0x7d
7 -> 0x07
8 -> 0x7f
9 -> 0x6f
_ -> 0x10
indicator n = Indicator $
case n of
0 -> 0x3f; 1 -> 0x06
2 -> 0x5b; 3 -> 0x4f
4 -> 0x66; 5 -> 0x6d
6 -> 0x7d; 7 -> 0x07
8 -> 0x7f; 9 -> 0x6f
_ -> 0x08

--------------------------------------------------------------------------------
-- | Test to see if an indicator should be lit up.

+ 57
- 0
test/IndicatorTest.hs View File

@@ -0,0 +1,57 @@
{-

This file is part of the package clockdown. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/clockdown/LICENSE. No part of
the clockdown package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

--------------------------------------------------------------------------------
module IndicatorTest (tests) where

--------------------------------------------------------------------------------
import Control.Monad (forM_)
import Test.Tasty
import Test.Tasty.HUnit

--------------------------------------------------------------------------------
import Clockdown.Core.Digital.Indicator

--------------------------------------------------------------------------------
segmentsTest :: Assertion
segmentsTest =
forM_ testCases $ \(digit, segs) -> do
let table = zip segs (repeat ())
mapM_ (check digit table) [A .. G]

where
check :: Int -> [(Segment, ())] -> Segment -> Assertion
check digit table seg =
assertBool ("Segment: " ++ show seg ++ " from " ++ show digit) $
case lookup seg table of
Nothing -> not (indicator digit `testSegment` seg)
Just _ -> indicator digit `testSegment` seg

testCases :: [(Int, [Segment])]
testCases = [ (0, [A, B, C, D, E, F ])
, (1, [ B, C ])
, (2, [A, B, D, E, G])
, (3, [A, B, C, D, G])
, (4, [ B, C, F, G])
, (5, [A, C, D, F, G])
, (6, [A, C, D, E, F, G])
, (7, [A, B, C ])
, (8, [A, B, C, D, E, F, G])
, (9, [A, B, C, D, F, G])
, (42, [ D ])
]


--------------------------------------------------------------------------------
tests :: TestTree
tests = testGroup "IndicatorTest"
[ testCase "Correct segments light up" segmentsTest
]

+ 25
- 0
test/Main.hs View File

@@ -0,0 +1,25 @@
{-

This file is part of the package clockdown. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/clockdown/LICENSE. No part of
the clockdown package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

--------------------------------------------------------------------------------
module Main (main) where

--------------------------------------------------------------------------------
import qualified IndicatorTest as DI
import Test.Tasty

--------------------------------------------------------------------------------
tests :: TestTree
tests = testGroup "Tests" [ DI.tests ]

--------------------------------------------------------------------------------
main :: IO ()
main = defaultMain tests

Loading…
Cancel
Save