Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and fix enumFromTo, closes #15 #16

Merged
merged 1 commit into from
May 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ install:
- bower install
script:
- npm run -s build
- npm test
after_success:
- >-
test $TRAVIS_TAG &&
Expand Down
4 changes: 4 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
"purescript-either": "^1.0.0-rc.1",
"purescript-strings": "^1.0.0-rc.1",
"purescript-unfoldable": "^1.0.0-rc.1"
},
"devDependencies": {
"purescript-assert": "^1.0.0-rc.1",
"purescript-console": "^1.0.0-rc.1"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build --censor-lib --strict"
"build": "pulp build --censor-lib --strict",
"test": "pulp test"
},
"devDependencies": {
"pulp": "^8.2.0",
Expand Down
15 changes: 10 additions & 5 deletions src/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Data.Enum

import Prelude

import Control.MonadPlus (guard)

import Data.Char (fromCharCode, toCharCode)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..), maybe, fromJust)
Expand Down Expand Up @@ -93,12 +95,15 @@ defaultSucc toEnum' fromEnum' a = toEnum' (fromEnum' a + 1)
defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a
defaultPred toEnum' fromEnum' a = toEnum' (fromEnum' a - 1)

-- | Property: ```fromEnum a = a', fromEnum b = b' => forall e', a' <= e' <= b': Exists e: toEnum e' = Just e```
-- |
-- | Following from the propery of `intFromTo`, we are sure all elements in `intFromTo (fromEnum a) (fromEnum b)` are `Just`s.
-- TODO need to update the doc comment above
-- | Returns a successive sequence of elements from the lower bound to
-- | the upper bound (inclusive).
enumFromTo :: forall a u. (Enum a, Unfoldable u) => a -> a -> u a
enumFromTo from to = unfoldr (\x -> succ x >>= \x' -> if x <= to then pure $ Tuple x x' else Nothing) from
enumFromTo from to = unfoldr go (Just from)
where
go mx = do
x <- mx
guard (x <= to)
pure $ Tuple x (succ x)

-- | `[a,b..c]`
enumFromThenTo :: forall a. BoundedEnum a => a -> a -> a -> Array a
Expand Down
12 changes: 12 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Test.Main where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE)

import Test.Assert (ASSERT)
import Test.Data.Enum (testEnum)

main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
main = testEnum
65 changes: 65 additions & 0 deletions test/Test/Data/Enum.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Test.Data.Enum (testEnum) where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Enum (class Enum, class BoundedEnum, defaultToEnum, defaultFromEnum,
defaultCardinality, enumFromTo, enumFromThenTo, upFrom,
downFrom)
import Data.Maybe (Maybe(..))

import Test.Assert (ASSERT, assert)

data T = A | B | C | D | E

derive instance eqT :: Eq T
derive instance ordT :: Ord T

instance enumT :: Enum T where
succ A = Just B
succ B = Just C
succ C = Just D
succ D = Just E
succ E = Nothing

pred A = Nothing
pred B = Just A
pred C = Just B
pred D = Just C
pred E = Just D

instance boundedT :: Bounded T where
bottom = A
top = E

instance boundedEnumT :: BoundedEnum T where
cardinality = defaultCardinality
toEnum = defaultToEnum
fromEnum = defaultFromEnum

testEnum :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
testEnum = do
log "enumFromTo"
assert $ enumFromTo A A == [A]
assert $ enumFromTo B A == []
assert $ enumFromTo A C == [A, B, C]
assert $ enumFromTo A E == [A, B, C, D, E]

log "enumFromThenTo"
assert $ enumFromThenTo A B E == [A, B, C, D, E]
assert $ enumFromThenTo A C E == [A, C, E]
assert $ enumFromThenTo A E E == [A, E]
assert $ enumFromThenTo A C C == [A, C ]
assert $ enumFromThenTo A C D == [A, C ]

log "upFrom"
assert $ upFrom B == [C, D, E]
assert $ upFrom D == [ E]
assert $ upFrom E == [ ]

log "downFrom"
assert $ downFrom D == [C, B, A]
assert $ downFrom B == [ A]
assert $ downFrom A == [ ]