Skip to content

Commit

Permalink
Added Array.hs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bsl committed Aug 10, 2009
1 parent 28321d4 commit 78788da
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
13 changes: 11 additions & 2 deletions Setup.hs
@@ -1,3 +1,12 @@
import Distribution.Simple module Main (main) where


main = defaultMain import Distribution.Simple (defaultMainWithHooks, runTests, simpleUserHooks)
import System.Cmd (system)

main :: IO ()
main =
defaultMainWithHooks $ simpleUserHooks { runTests = runTests' }
where
runTests' _ _ _ _ = do
system "runhaskell -i./src tests/Array.hs"
return ()
2 changes: 1 addition & 1 deletion event.cabal
Expand Up @@ -10,7 +10,7 @@ author: Bryan O'Sullivan <bos@serpentine.com>
maintainer: Johan Tibell <johan.tibell@gmail.com> maintainer: Johan Tibell <johan.tibell@gmail.com>
category: System category: System


build-type: Simple build-type: Custom
cabal-version: >= 1.6 cabal-version: >= 1.6


library library
Expand Down
67 changes: 67 additions & 0 deletions tests/Array.hs
@@ -0,0 +1,67 @@
import Control.Monad (when)
import System.Exit (exitFailure)

import System.Event.Array (Array)
import qualified System.Event.Array as A

import Test.HUnit (Counts(..), Test(..), (@=?), runTestTT)

main :: IO ()
main = do
counts <- runTestTT tests
when (errors counts + failures counts > 0) exitFailure
where
tests = TestList
[ TestLabel "empty" testEmpty
, TestLabel "new 0" (testNew 0 0)
, TestLabel "new 1" (testNew 1 1)
, TestLabel "new 50" (testNew 50 64)
, TestLabel "new 100" (testNew 100 128)
, TestLabel "new 128" (testNew 128 128)
, TestLabel "ensureCapacity 0 on empty" (testEnsureCapacity A.empty 0 0)
, TestLabel "ensureCapacity 0 on cap 4" (testEnsureCapacity (A.new 4) 0 4)
, TestLabel "ensureCapacity 4 on cap 4" (testEnsureCapacity (A.new 4) 4 4)
, TestLabel "ensureCapacity 5 on cap 4" (testEnsureCapacity (A.new 4) 5 8)
, TestLabel "1 snoc on cap 0" (testSnoc 0 1 1)
, TestLabel "5 snocs on cap 4" (testSnoc 4 5 8)
]

testEmpty :: Test
testEmpty = TestCase $ do
arr <- A.empty
lenA <- A.length arr
capA <- A.capacity arr
0 @=? lenA
0 @=? capA

testNew :: Int -> Int -> Test
testNew cI cC = TestCase $ do
arr <- A.new cI :: IO (Array Int)
lenA <- A.length arr
capA <- A.capacity arr
0 @=? lenA
cC @=? capA

testEnsureCapacity :: IO (Array Int) -> Int -> Int -> Test
testEnsureCapacity ioarr cI cC = TestCase $ do
arr <- ioarr
A.ensureCapacity arr cI
lenA <- A.length arr
capA <- A.capacity arr
0 @=? lenA
cC @=? capA

testSnoc :: Int -> Int -> Int -> Test
testSnoc cI h capC = TestCase $ do
arr <- A.new cI :: IO (Array Int)
mapM_ (A.snoc arr) esC
lenA <- A.length arr
capA <- A.capacity arr
esA <- mapM (A.unsafeRead arr) is
lenC @=? lenA
capC @=? capA
esC @=? esA
where
lenC = h
esC = [1..h]
is = [0..pred h]

0 comments on commit 78788da

Please sign in to comment.