|
| 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