2019-02-11 13:39:03 +03:00
|
|
|
(load "Test.carp")
|
|
|
|
(use-all Maybe Test)
|
|
|
|
|
|
|
|
(deftest test
|
|
|
|
(assert-true test
|
2019-02-13 09:43:34 +03:00
|
|
|
(nothing? &(the (Maybe Int) (Nothing)))
|
2019-02-15 12:18:23 +03:00
|
|
|
"nothing? works on Nothing"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-false test
|
2019-02-13 09:43:34 +03:00
|
|
|
(nothing? &(Just 1))
|
2019-02-15 12:18:23 +03:00
|
|
|
"nothing? works on Just"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-true test
|
2019-02-13 09:43:34 +03:00
|
|
|
(just? &(Just 1))
|
2019-02-15 12:18:23 +03:00
|
|
|
"just? works on Just"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-false test
|
2019-02-13 09:43:34 +03:00
|
|
|
(just? &(the (Maybe Int) (Nothing)))
|
2019-02-15 12:18:23 +03:00
|
|
|
"just? works on Nothing"
|
|
|
|
)
|
2019-09-11 01:01:36 +03:00
|
|
|
(assert-equal test
|
|
|
|
0
|
|
|
|
(get-tag &(Just 1))
|
|
|
|
"tag works on Just"
|
|
|
|
)
|
|
|
|
(assert-equal test
|
|
|
|
1
|
|
|
|
(get-tag &(the (Maybe Int) (Nothing)))
|
|
|
|
"tag works on Nothing"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-equal test
|
|
|
|
1
|
|
|
|
(from (Just 1) 0)
|
2019-02-15 12:18:23 +03:00
|
|
|
"from works on Just"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-equal test
|
|
|
|
0
|
2019-02-11 22:24:17 +03:00
|
|
|
(from (the (Maybe Int) (Nothing)) 0)
|
2019-02-15 12:18:23 +03:00
|
|
|
"from works on Nothing"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-equal test
|
|
|
|
1
|
|
|
|
(unsafe-from (Just 1))
|
2019-02-15 12:18:23 +03:00
|
|
|
"unsafe-from works on Just"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-equal test
|
|
|
|
2
|
2020-01-25 16:08:37 +03:00
|
|
|
(from (apply (Just 1) &Int.inc) 0)
|
2019-02-15 12:18:23 +03:00
|
|
|
"apply works on Just"
|
|
|
|
)
|
2019-02-11 13:39:03 +03:00
|
|
|
(assert-equal test
|
|
|
|
0
|
2020-01-25 16:08:37 +03:00
|
|
|
(from (apply (the (Maybe Int) (Nothing)) &Int.inc) 0)
|
2019-02-15 12:18:23 +03:00
|
|
|
"apply works on Nothing"
|
|
|
|
)
|
2019-06-12 09:52:54 +03:00
|
|
|
(assert-equal test
|
|
|
|
10
|
|
|
|
(or-zero (Just 10))
|
|
|
|
"or-zero works on Just"
|
|
|
|
)
|
|
|
|
(assert-equal test
|
|
|
|
0
|
|
|
|
(or-zero (the (Maybe Int) (Nothing)))
|
|
|
|
"or-zero works on Nothing"
|
|
|
|
)
|
2019-06-06 22:29:36 +03:00
|
|
|
(assert-equal test
|
|
|
|
&(Nothing)
|
|
|
|
&(the (Maybe Int) (zero))
|
|
|
|
"zero works"
|
|
|
|
)
|
2019-03-26 16:30:00 +03:00
|
|
|
(assert-equal test
|
|
|
|
&(Result.Success 1)
|
|
|
|
&(to-result (Just 1) @"error")
|
|
|
|
"to-result works on Just"
|
|
|
|
)
|
|
|
|
(assert-equal test
|
|
|
|
&(the (Result Int String) (Result.Error @"error"))
|
|
|
|
&(to-result (Nothing) @"error")
|
|
|
|
"to-result works on Nothing"
|
|
|
|
)
|
2019-05-03 14:49:33 +03:00
|
|
|
(assert-equal test
|
|
|
|
1
|
2019-10-31 11:38:47 +03:00
|
|
|
@(Pointer.to-ref (unsafe-ptr &(Just 1)))
|
|
|
|
"unsafe-ptr works on Just"
|
2019-05-03 14:49:33 +03:00
|
|
|
)
|
|
|
|
(assert-true test
|
2019-10-31 11:38:47 +03:00
|
|
|
(null? (unsafe-ptr &(the (Maybe Int) (Nothing))))
|
|
|
|
"unsafe-ptr works on Nothing"
|
2019-05-03 14:49:33 +03:00
|
|
|
)
|
|
|
|
(assert-equal test
|
|
|
|
&(Just 0)
|
2021-05-27 23:04:46 +03:00
|
|
|
&(from-ptr (Pointer.address &(zero)))
|
2019-05-03 14:49:33 +03:00
|
|
|
"from-ptr works on Ptr/Just"
|
|
|
|
)
|
|
|
|
(assert-equal test
|
|
|
|
&(Nothing)
|
|
|
|
&(the (Maybe Int) (from-ptr NULL))
|
|
|
|
"from-ptr works on NULL/Nothing"
|
2020-04-29 14:55:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
123
|
|
|
|
(match (the (Maybe (Maybe Int)) (Just (Just 123)))
|
|
|
|
Nothing 0
|
|
|
|
(Just (Nothing)) 0
|
|
|
|
(Just (Just x)) x)
|
|
|
|
"Matching on nested Maybes."
|
|
|
|
)
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
123
|
|
|
|
(match (the (Maybe (Maybe (Maybe Int))) (Just (Just (Nothing))))
|
|
|
|
Nothing 0
|
|
|
|
(Just (Nothing)) 0
|
|
|
|
(Just (Just (Just _))) 0
|
|
|
|
(Just (Just (Nothing))) 123
|
|
|
|
)
|
|
|
|
"Matching on nested Maybes."
|
|
|
|
)
|
|
|
|
|
2019-02-11 13:39:03 +03:00
|
|
|
)
|