Skip to content

Commit dc6bbed

Browse files
authored
Merge pull request #19 from LiamGoodacre/fix/stack-overflow
Fix stack overflow in BoundedEnum (Either _ _)
2 parents 52e61de + 8110a90 commit dc6bbed

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Data/Enum.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ instance boundedEnumEither :: (BoundedEnum a, BoundedEnum b) => BoundedEnum (Eit
203203
Cardinality
204204
$ unwrap (cardinality :: Cardinality a)
205205
+ unwrap (cardinality :: Cardinality b)
206-
toEnum = to cardinality cardinality
206+
toEnum n = to cardinality cardinality
207207
where
208-
to :: Cardinality a -> Cardinality (Either a b) -> Int -> Maybe (Either a b)
209-
to (Cardinality ca) (Cardinality cab) n
208+
to :: Cardinality a -> Cardinality (Either a b) -> Maybe (Either a b)
209+
to (Cardinality ca) (Cardinality cab)
210210
| n >= 0 && n < ca = Left <$> toEnum n
211211
| n >= ca && n < cab = Right <$> toEnum (n - ca)
212212
| otherwise = Nothing

test/Test/Data/Enum.purs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8+
import Data.Newtype (unwrap)
89
import Data.Enum (class Enum, class BoundedEnum, defaultToEnum, defaultFromEnum,
910
defaultCardinality, enumFromTo, enumFromThenTo, upFrom,
10-
downFrom, toEnum)
11+
downFrom, toEnum, fromEnum, Cardinality, cardinality)
1112
import Data.Maybe (Maybe(..))
13+
import Data.Either (Either(..))
1214

1315
import Test.Assert (ASSERT, assert)
1416

@@ -70,3 +72,13 @@ testEnum = do
7072
assert $ toEnum 1 == Just (Just false) :: Maybe (Maybe Boolean)
7173
assert $ toEnum 2 == Just (Just true) :: Maybe (Maybe Boolean)
7274
assert $ toEnum 3 == Nothing :: Maybe (Maybe Boolean)
75+
76+
log "BoundedEnum (Either _ _)"
77+
assert $ unwrap (cardinality :: Cardinality (Either Boolean Boolean)) == 4
78+
assert $ toEnum 0 == Just (Left false :: Either Boolean T)
79+
assert $ toEnum 1 == Just (Left true :: Either Boolean T)
80+
assert $ toEnum 3 == Just (Right B :: Either Boolean T)
81+
assert $ fromEnum (Left false :: Either Boolean T) == 0
82+
assert $ fromEnum (Left true :: Either Boolean T) == 1
83+
assert $ fromEnum (Right B :: Either Boolean T) == 3
84+

0 commit comments

Comments
 (0)