|
1 | 1 | module Test.Main where
|
2 | 2 |
|
3 | 3 | import Prelude
|
| 4 | + |
4 | 5 | import Control.Monad.Eff (Eff)
|
5 |
| -import Control.Monad.Eff.Console (CONSOLE, logShow) |
| 6 | +import Control.Monad.Eff.Console (CONSOLE, log, logShow) |
| 7 | +import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), cardinality, fromEnum, pred, succ, toEnum) |
6 | 8 | import Data.Generic.Rep as G
|
| 9 | +import Data.Generic.Rep.Bounded as GBounded |
| 10 | +import Data.Generic.Rep.Enum as GEnum |
7 | 11 | import Data.Generic.Rep.Eq as GEq
|
8 | 12 | import Data.Generic.Rep.Ord as GOrd
|
9 | 13 | import Data.Generic.Rep.Show as GShow
|
10 |
| -import Data.Generic.Rep.Bounded as GBounded |
| 14 | +import Data.Maybe (Maybe(..)) |
| 15 | +import Test.Assert (ASSERT, assert) |
11 | 16 |
|
12 | 17 | data List a = Nil | Cons { head :: a, tail :: List a }
|
13 | 18 |
|
@@ -36,16 +41,98 @@ instance showSimpleBounded :: Show SimpleBounded where
|
36 | 41 | instance boundedSimpleBounded :: Bounded SimpleBounded where
|
37 | 42 | bottom = GBounded.genericBottom
|
38 | 43 | top = GBounded.genericTop
|
| 44 | +instance enumSimpleBounded :: Enum SimpleBounded where |
| 45 | + pred = GEnum.genericPred |
| 46 | + succ = GEnum.genericSucc |
| 47 | +instance boundedEnumSimpleBounded :: BoundedEnum SimpleBounded where |
| 48 | + cardinality = GEnum.genericCardinality |
| 49 | + toEnum = GEnum.genericToEnum |
| 50 | + fromEnum = GEnum.genericFromEnum |
| 51 | + |
| 52 | +data Option a = None | Some a |
| 53 | +derive instance genericOption :: G.Generic (Option a) _ |
| 54 | +instance eqOption :: Eq a => Eq (Option a) where |
| 55 | + eq x y = GEq.genericEq x y |
| 56 | +instance ordOption :: Ord a => Ord (Option a) where |
| 57 | + compare x y = GOrd.genericCompare x y |
| 58 | +instance showOption :: Show a => Show (Option a) where |
| 59 | + show x = GShow.genericShow x |
| 60 | +instance boundedOption :: Bounded a => Bounded (Option a) where |
| 61 | + bottom = GBounded.genericBottom |
| 62 | + top = GBounded.genericTop |
| 63 | +instance enumOption :: (Bounded a, Enum a) => Enum (Option a) where |
| 64 | + pred = GEnum.genericPred |
| 65 | + succ = GEnum.genericSucc |
| 66 | +instance boundedEnumOption :: BoundedEnum a => BoundedEnum (Option a) where |
| 67 | + cardinality = GEnum.genericCardinality |
| 68 | + toEnum = GEnum.genericToEnum |
| 69 | + fromEnum = GEnum.genericFromEnum |
39 | 70 |
|
40 |
| -main :: Eff (console :: CONSOLE) Unit |
| 71 | +main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit |
41 | 72 | main = do
|
42 | 73 | logShow (cons 1 (cons 2 Nil))
|
43 | 74 |
|
44 |
| - logShow (cons 1 (cons 2 Nil) == cons 1 (cons 2 Nil)) |
45 |
| - logShow (cons 1 (cons 2 Nil) == cons 1 Nil) |
| 75 | + log "Checking equality" |
| 76 | + assert $ cons 1 (cons 2 Nil) == cons 1 (cons 2 Nil) |
| 77 | + |
| 78 | + log "Checking inequality" |
| 79 | + assert $ cons 1 (cons 2 Nil) /= cons 1 Nil |
| 80 | + |
| 81 | + log "Checking comparison EQ" |
| 82 | + assert $ (cons 1 (cons 2 Nil) `compare` cons 1 (cons 2 Nil)) == EQ |
| 83 | + |
| 84 | + log "Checking comparison GT" |
| 85 | + assert $ (cons 1 (cons 2 Nil) `compare` cons 1 Nil) == GT |
| 86 | + |
| 87 | + log "Checking comparison LT" |
| 88 | + assert $ (cons 1 Nil `compare` cons 1 (cons 2 Nil)) == LT |
| 89 | + |
| 90 | + log "Checking simple bottom" |
| 91 | + assert $ bottom == A |
| 92 | + |
| 93 | + log "Checking simple top" |
| 94 | + assert $ top == D |
| 95 | + |
| 96 | + log "Checking composite bottom" |
| 97 | + assert $ bottom == None :: Option SimpleBounded |
| 98 | + |
| 99 | + log "Checking composite top" |
| 100 | + assert $ top == Some D |
| 101 | + |
| 102 | + log "Checking simple pred bottom" |
| 103 | + assert $ pred (bottom :: SimpleBounded) == Nothing |
| 104 | + |
| 105 | + log "Checking simple (pred =<< succ bottom)" |
| 106 | + assert $ (pred =<< succ bottom) == Just A |
| 107 | + |
| 108 | + log "Checking simple succ top" |
| 109 | + assert $ succ (top :: SimpleBounded) == Nothing |
| 110 | + |
| 111 | + log "Checking simple (succ =<< pred top)" |
| 112 | + assert $ (succ =<< pred top) == Just D |
| 113 | + |
| 114 | + log "Checking composite pred bottom" |
| 115 | + assert $ pred (bottom :: Option SimpleBounded) == Nothing |
| 116 | + |
| 117 | + log "Checking composite (pred =<< succ bottom)" |
| 118 | + assert $ (pred =<< succ (bottom :: Option SimpleBounded)) == Just None |
| 119 | + |
| 120 | + log "Checking composite succ top" |
| 121 | + assert $ succ (top :: Option SimpleBounded) == Nothing |
| 122 | + |
| 123 | + log "Checking composite (succ =<< pred top)" |
| 124 | + assert $ (succ =<< pred top) == Just (Some D) |
| 125 | + |
| 126 | + log "Checking simple cardinality" |
| 127 | + assert $ (cardinality :: Cardinality SimpleBounded) == Cardinality 4 |
| 128 | + |
| 129 | + log "Checking composite cardinality" |
| 130 | + assert $ (cardinality :: Cardinality (Option SimpleBounded)) == Cardinality 5 |
46 | 131 |
|
47 |
| - logShow (cons 1 (cons 2 Nil) `compare` cons 1 (cons 2 Nil)) |
48 |
| - logShow (cons 1 (cons 2 Nil) `compare` cons 1 Nil) |
| 132 | + log "Checking simple toEnum/fromEnum roundtrip" |
| 133 | + assert $ toEnum (fromEnum A) == Just A |
| 134 | + assert $ toEnum (fromEnum B) == Just B |
49 | 135 |
|
50 |
| - logShow (bottom :: SimpleBounded) |
51 |
| - logShow (top :: SimpleBounded) |
| 136 | + log "Checking composite toEnum/fromEnum roundtrip" |
| 137 | + assert $ toEnum (fromEnum (None :: Option SimpleBounded)) == Just (None :: Option SimpleBounded) |
| 138 | + assert $ toEnum (fromEnum (Some A)) == Just (Some A) |
0 commit comments