2020-04-29 11:39:15 +03:00
|
|
|
(load "Test.carp")
|
|
|
|
(use Test)
|
2020-05-02 15:18:14 +03:00
|
|
|
(use StaticArray)
|
2020-04-29 11:39:15 +03:00
|
|
|
|
|
|
|
(deftest test
|
|
|
|
|
2020-05-03 17:35:33 +03:00
|
|
|
(assert-equal test
|
|
|
|
$[2 4 8]
|
|
|
|
(let-do [arr $[1 2 4]]
|
|
|
|
(map!
|
|
|
|
arr
|
|
|
|
&(fn [val] (* @val 2)))
|
|
|
|
arr)
|
|
|
|
"map! works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
$[3 2 1]
|
|
|
|
(let-do [arr $[1 2 3]]
|
|
|
|
(reverse! arr)
|
|
|
|
arr)
|
|
|
|
"reverse! works as expected")
|
|
|
|
|
2020-04-29 11:39:15 +03:00
|
|
|
(assert-true test
|
|
|
|
(= $[1 2 3] $[1 2 3])
|
|
|
|
"= works as expected I")
|
|
|
|
|
|
|
|
(assert-false test
|
|
|
|
(= $[1 2 3] $[1 2 3 4 5])
|
|
|
|
"= works as expected II")
|
|
|
|
|
|
|
|
(assert-false test
|
|
|
|
(= $[1 1 1] $[0 0 0])
|
|
|
|
"= works as expected III")
|
|
|
|
|
2020-04-29 11:54:41 +03:00
|
|
|
(assert-equal test
|
|
|
|
5
|
|
|
|
(let [a $[0 1 2 3 4 5 6 7 8 9]]
|
|
|
|
@(unsafe-nth a 5))
|
|
|
|
"unsafe-nth works as expected")
|
|
|
|
|
|
|
|
;; TODO: FIX! THIS ONE IS PROBLEMATIC.
|
|
|
|
;; (assert-equal test
|
|
|
|
;; &[4 5 6]
|
|
|
|
;; (let [nested $[[1 2 3]
|
|
|
|
;; [4 5 6]
|
|
|
|
;; [7 8 9]]]
|
|
|
|
;; &@(unsafe-nth nested 1))
|
|
|
|
;; "unsafe-nth works as expected")
|
|
|
|
|
2020-05-02 15:18:14 +03:00
|
|
|
(assert-equal test
|
|
|
|
10
|
|
|
|
(let [arr $[2 2 2 2]]
|
|
|
|
(reduce
|
|
|
|
&(fn [acc val] (+ acc @val))
|
|
|
|
2
|
|
|
|
arr))
|
|
|
|
"reduce works as expected")
|
|
|
|
|
2020-05-03 16:18:41 +03:00
|
|
|
(assert-equal test
|
|
|
|
true
|
|
|
|
(empty? (the (Ref (StaticArray Int)) $[]))
|
|
|
|
"empty? works as expected I")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
false
|
|
|
|
(empty? $[1])
|
|
|
|
"empty? works as expected II")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
true
|
|
|
|
(any? &(fn [x] (= 0 @x)) $[0 1 2 3])
|
|
|
|
"any? works as expected I")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
false
|
|
|
|
(any? &(fn [x] (= 0 @x)) $[1 2 3 4])
|
|
|
|
"any? works as expected II")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
true
|
|
|
|
(all? &(fn [x] (< 0 @x)) $[1 2 3])
|
|
|
|
"all? works as expected I")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
false
|
|
|
|
(all? &(fn [x] (< 0 @x)) $[0 1 2])
|
2020-05-03 16:35:30 +03:00
|
|
|
"all? works as expected II")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just 3)
|
|
|
|
&(find &(fn [x] (= 3 @x)) $[0 1 2 3])
|
|
|
|
"find works as expected I")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Nothing)
|
|
|
|
&(find &(fn [x] (= 4 @x)) $[0 1 2 3])
|
|
|
|
"find works as expected II")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Nothing)
|
|
|
|
&(find-index &(fn [i] (Int.even? @i)) $[1 3 5])
|
|
|
|
"find-index works I")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just 1)
|
|
|
|
&(find-index &(fn [i] (Int.even? @i)) $[1 8 5])
|
2020-05-03 17:00:15 +03:00
|
|
|
"find-index works II")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
1
|
|
|
|
(unsafe-first $[1 2 3])
|
|
|
|
"unsafe-first works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just 1)
|
|
|
|
&(first $[1 2 3])
|
|
|
|
"first works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(the (Maybe Int) (Maybe.Nothing))
|
|
|
|
&(first $[])
|
|
|
|
"first works as expected on empty array")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
\c
|
|
|
|
(unsafe-last $[\a \b \c])
|
|
|
|
"unsafe-last works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just \c)
|
|
|
|
&(last $[\a \b \c])
|
|
|
|
"last works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(the (Maybe Char) (Maybe.Nothing))
|
|
|
|
&(last $[])
|
2020-05-03 17:15:29 +03:00
|
|
|
"last works as expected on empty array")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just 10)
|
|
|
|
&(maximum $[8 10 9])
|
|
|
|
"maximum works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just (Pair.init 2 1))
|
|
|
|
&(maximum $[(Pair.init 1 3) (Pair.init 2 1) (Pair.init 2 0)])
|
|
|
|
"maximum works on pairs")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just 1)
|
|
|
|
&(minimum $[3 1 2])
|
|
|
|
"minimum works as expected")
|
|
|
|
|
|
|
|
(assert-equal test
|
|
|
|
&(Maybe.Just (Pair.init 1 3))
|
|
|
|
&(minimum $[(Pair.init 1 3) (Pair.init 2 1) (Pair.init 2 0)])
|
|
|
|
"minimum works on pairs"))
|
2020-05-03 17:00:15 +03:00
|
|
|
|