Skip to content

Commit 0037450

Browse files
committed
Add tests and fix enumFromTo, closes #15
1 parent 9332412 commit 0037450

File tree

6 files changed

+94
-6
lines changed

6 files changed

+94
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ install:
1414
- bower install
1515
script:
1616
- npm run -s build
17+
- npm test
1718
after_success:
1819
- >-
1920
test $TRAVIS_TAG &&

bower.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
"purescript-either": "^1.0.0-rc.1",
2222
"purescript-strings": "^1.0.0-rc.1",
2323
"purescript-unfoldable": "^1.0.0-rc.1"
24+
},
25+
"devDependencies": {
26+
"purescript-assert": "^1.0.0-rc.1",
27+
"purescript-console": "^1.0.0-rc.1"
2428
}
2529
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"private": true,
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
5-
"build": "pulp build --censor-lib --strict"
5+
"build": "pulp build --censor-lib --strict",
6+
"test": "pulp test"
67
},
78
"devDependencies": {
89
"pulp": "^8.2.0",

src/Data/Enum.purs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Data.Enum
1515

1616
import Prelude
1717

18+
import Control.MonadPlus (guard)
19+
1820
import Data.Char (fromCharCode, toCharCode)
1921
import Data.Either (Either(..))
2022
import Data.Maybe (Maybe(..), maybe, fromJust)
@@ -93,12 +95,15 @@ defaultSucc toEnum' fromEnum' a = toEnum' (fromEnum' a + 1)
9395
defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a
9496
defaultPred toEnum' fromEnum' a = toEnum' (fromEnum' a - 1)
9597

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

103108
-- | `[a,b..c]`
104109
enumFromThenTo :: forall a. BoundedEnum a => a -> a -> a -> Array a

test/Main.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Test.Main where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Console (CONSOLE)
7+
8+
import Test.Assert (ASSERT)
9+
import Test.Data.Enum (testEnum)
10+
11+
main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
12+
main = testEnum

test/Test/Data/Enum.purs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Test.Data.Enum (testEnum) where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Console (CONSOLE, log)
7+
8+
import Data.Enum (class Enum, class BoundedEnum, defaultToEnum, defaultFromEnum,
9+
defaultCardinality, enumFromTo, enumFromThenTo, upFrom,
10+
downFrom)
11+
import Data.Maybe (Maybe(..))
12+
13+
import Test.Assert (ASSERT, assert)
14+
15+
data T = A | B | C | D | E
16+
17+
derive instance eqT :: Eq T
18+
derive instance ordT :: Ord T
19+
20+
instance enumT :: Enum T where
21+
succ A = Just B
22+
succ B = Just C
23+
succ C = Just D
24+
succ D = Just E
25+
succ E = Nothing
26+
27+
pred A = Nothing
28+
pred B = Just A
29+
pred C = Just B
30+
pred D = Just C
31+
pred E = Just D
32+
33+
instance boundedT :: Bounded T where
34+
bottom = A
35+
top = E
36+
37+
instance boundedEnumT :: BoundedEnum T where
38+
cardinality = defaultCardinality
39+
toEnum = defaultToEnum
40+
fromEnum = defaultFromEnum
41+
42+
testEnum :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
43+
testEnum = do
44+
log "enumFromTo"
45+
assert $ enumFromTo A A == [A]
46+
assert $ enumFromTo B A == []
47+
assert $ enumFromTo A C == [A, B, C]
48+
assert $ enumFromTo A E == [A, B, C, D, E]
49+
50+
log "enumFromThenTo"
51+
assert $ enumFromThenTo A B E == [A, B, C, D, E]
52+
assert $ enumFromThenTo A C E == [A, C, E]
53+
assert $ enumFromThenTo A E E == [A, E]
54+
assert $ enumFromThenTo A C C == [A, C ]
55+
assert $ enumFromThenTo A C D == [A, C ]
56+
57+
log "upFrom"
58+
assert $ upFrom B == [C, D, E]
59+
assert $ upFrom D == [ E]
60+
assert $ upFrom E == [ ]
61+
62+
log "downFrom"
63+
assert $ downFrom D == [C, B, A]
64+
assert $ downFrom B == [ A]
65+
assert $ downFrom A == [ ]

0 commit comments

Comments
 (0)