2017-11-29 17:28:21 +03:00
|
|
|
(load "Test.carp")
|
2020-05-18 23:59:41 +03:00
|
|
|
(use-all Array Test Result)
|
2017-12-19 14:32:09 +03:00
|
|
|
|
|
|
|
(defn nested []
|
|
|
|
[[1 2 3]
|
|
|
|
[4 5 6]
|
|
|
|
[7 8 9]])
|
2017-11-29 17:28:21 +03:00
|
|
|
|
2019-05-27 19:05:44 +03:00
|
|
|
(defn excl [x] (String.append &x "!"))
|
|
|
|
(defn excl-ref [x] (String.append x "!"))
|
2017-11-29 17:28:21 +03:00
|
|
|
|
2018-01-02 20:13:52 +03:00
|
|
|
(defn inc-ref [x] (+ @x 1))
|
|
|
|
|
2018-01-24 16:07:43 +03:00
|
|
|
(defn make-zero [] 0)
|
2018-01-24 19:08:20 +03:00
|
|
|
(defn make-idx [i] i)
|
2018-01-24 16:07:43 +03:00
|
|
|
|
2020-05-24 13:26:18 +03:00
|
|
|
(def a (range-or-default 0 9 1))
|
2018-11-07 18:11:38 +03:00
|
|
|
(def b (Array.replicate 5 "Hi"))
|
|
|
|
|
|
|
|
(deftest test
|
|
|
|
(assert-true test
|
|
|
|
(= &[1 2 3] &[1 2 3])
|
|
|
|
"= works as expected I")
|
|
|
|
(assert-false test
|
|
|
|
(= &[1 2 3] &[1 2 3 4])
|
|
|
|
"= works as expected II")
|
|
|
|
(assert-false test
|
|
|
|
(= &[1 2 3] &[1 2 34])
|
|
|
|
"= works as expected III")
|
|
|
|
(assert-true test
|
|
|
|
(/= &[1 2 3] &[1 2 3 4])
|
|
|
|
"/= works as expected I")
|
|
|
|
(assert-true test
|
|
|
|
(/= &[1 2 4] &[1 2 3])
|
|
|
|
"/= works as expected II")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
[0 0 0 0]
|
|
|
|
(replicate 4 &0)
|
|
|
|
"replicate works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[0 0 0 0]
|
|
|
|
(repeat 4 &make-zero)
|
|
|
|
"repeat works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[0 1 2 3]
|
|
|
|
(repeat-indexed 4 make-idx)
|
|
|
|
"repeat-indexed works as expected")
|
2018-11-07 18:11:38 +03:00
|
|
|
(assert-equal test
|
|
|
|
1
|
2020-11-27 12:19:06 +03:00
|
|
|
@(unsafe-first &[1 2 3])
|
2019-02-15 12:18:23 +03:00
|
|
|
"unsafe-first works as expected")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 1)
|
|
|
|
(first &[1 2 3])
|
|
|
|
"first works as expected")
|
|
|
|
(assert-nothing test
|
|
|
|
&(first &(the (Array Int) []))
|
|
|
|
"first works as expected on empty array")
|
2018-11-07 18:11:38 +03:00
|
|
|
(assert-equal test
|
|
|
|
\c
|
2020-11-27 12:19:06 +03:00
|
|
|
@(unsafe-last &[\a \b \c])
|
2019-02-15 12:18:23 +03:00
|
|
|
"unsafe-last works as expected")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just \c)
|
|
|
|
(last &[\a \b \c])
|
|
|
|
"last works as expected")
|
|
|
|
(assert-nothing test
|
|
|
|
&(last &(the (Array Int) []))
|
|
|
|
"last works as expected on empty array")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[3 2 1]
|
|
|
|
(reverse [1 2 3])
|
|
|
|
"reverse works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 10)
|
2020-05-18 23:59:41 +03:00
|
|
|
(maximum &(unsafe-from-success (range 1 10 1)))
|
2020-05-12 15:53:42 +03:00
|
|
|
"maximum works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 1)
|
2020-05-24 13:26:18 +03:00
|
|
|
(minimum &(range-or-default 1 10 1))
|
2020-05-12 15:53:42 +03:00
|
|
|
"minimum works as expected")
|
|
|
|
(assert-ref-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-ref-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")
|
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 3)
|
|
|
|
(index-of &[1 2 3 4] &4)
|
|
|
|
"index-of works as expected when element is in the array")
|
|
|
|
(assert-nothing test
|
|
|
|
&(index-of &[1 2 3 4] &7)
|
|
|
|
"index-of works as expected when element is not in the array")
|
2018-11-07 18:11:38 +03:00
|
|
|
(assert-equal test
|
|
|
|
55
|
2020-05-24 13:26:18 +03:00
|
|
|
(sum &(range-or-default 1 10 1))
|
2018-11-07 18:11:38 +03:00
|
|
|
"sum works as expected")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
[2 3]
|
2020-05-24 13:26:18 +03:00
|
|
|
(slice &(range-or-default 1 10 1) 1 3)
|
2020-05-12 15:53:42 +03:00
|
|
|
"slice works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2 3]
|
2020-05-24 13:26:18 +03:00
|
|
|
(prefix &(range-or-default 1 10 1) 3)
|
2020-05-12 15:53:42 +03:00
|
|
|
"prefix works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[8 9 10]
|
2020-05-24 13:26:18 +03:00
|
|
|
(suffix &(range-or-default 1 10 1) 7)
|
2020-05-12 15:53:42 +03:00
|
|
|
"suffix works as expected")
|
|
|
|
(assert-nothing test
|
|
|
|
&(nth &a 100)
|
|
|
|
"nth works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 0)
|
|
|
|
(nth &a 0)
|
|
|
|
"nth works as expected")
|
2018-11-07 18:11:38 +03:00
|
|
|
(assert-equal test
|
|
|
|
5
|
2019-10-31 12:23:23 +03:00
|
|
|
@(unsafe-nth &a 5)
|
|
|
|
"unsafe-nth works as expected")
|
2018-11-07 18:11:38 +03:00
|
|
|
(assert-equal test
|
|
|
|
&[1 2 3]
|
2019-10-31 12:23:23 +03:00
|
|
|
(unsafe-nth &(nested) 0)
|
|
|
|
"unsafe-nth works as expected")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
2020-05-18 23:59:41 +03:00
|
|
|
(Success [10 11 12 13 14 15])
|
2020-05-12 15:53:42 +03:00
|
|
|
(range 10 15 1)
|
|
|
|
"range works as expected")
|
|
|
|
(assert-ref-equal test
|
2020-05-18 23:59:41 +03:00
|
|
|
(Success [10.0 10.5 11.0 11.5 12.0])
|
2020-05-12 15:53:42 +03:00
|
|
|
(range 10.0 12.0 0.5)
|
|
|
|
"range works as expected on non-integers")
|
|
|
|
(assert-ref-equal test
|
2020-05-18 23:59:41 +03:00
|
|
|
(Success [10 9 8 7 6 5 4 3 2 1 0])
|
2020-05-12 15:53:42 +03:00
|
|
|
(range 10 0 -1)
|
|
|
|
"range backwards works as expected")
|
|
|
|
(assert-ref-equal test
|
2020-05-18 23:59:41 +03:00
|
|
|
(Success [1 3 5 7 9])
|
2020-05-12 15:53:42 +03:00
|
|
|
(range 1 10 2)
|
|
|
|
"range works as expected if we dont exactly hit")
|
|
|
|
(assert-ref-equal test
|
2020-05-18 23:59:41 +03:00
|
|
|
(Success [10 8 6 4 2])
|
2020-05-12 15:53:42 +03:00
|
|
|
(range 10 1 -2)
|
|
|
|
"range backwards works as expected if we dont exactly hit")
|
2020-05-21 16:23:29 +03:00
|
|
|
(assert-error test
|
|
|
|
&(range 10 1 2)
|
|
|
|
"range backwards works as expected if we go forwards instead")
|
|
|
|
(assert-error test
|
|
|
|
&(range 1 10 -2)
|
|
|
|
"range forwards works as expected if we go backwarss instead")
|
|
|
|
(assert-error test
|
|
|
|
&(range 10 1 0)
|
|
|
|
"range works as expected if we use a step size of 0")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
[@"Hi!" @"Hi!" @"Hi!" @"Hi!" @"Hi!"]
|
|
|
|
(copy-map &excl-ref &b)
|
|
|
|
"copy-map works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[@"Hi!" @"Hi!" @"Hi!" @"Hi!" @"Hi!"]
|
|
|
|
(endo-map &excl @&b)
|
|
|
|
"endo-map works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2]
|
|
|
|
(swap [2 1] 0 1)
|
|
|
|
"swap works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[2 1]
|
|
|
|
(let-do [arr [1 2]]
|
|
|
|
(Array.swap! &arr 0 1)
|
|
|
|
arr)
|
|
|
|
"swap! works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 3]
|
2021-05-25 13:11:31 +03:00
|
|
|
(aupdate [1 2] 1 &inc)
|
2020-05-12 15:53:42 +03:00
|
|
|
"aupdate works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 3]
|
|
|
|
(let-do [arr [1 2]]
|
2021-05-25 13:11:31 +03:00
|
|
|
(aupdate! &arr 1 &inc)
|
2020-05-12 15:53:42 +03:00
|
|
|
arr)
|
|
|
|
"aupdate! works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2 3 4 5 6 7 8]
|
|
|
|
(concat &[[1] [2 3] [4 5 6] [7 8]])
|
|
|
|
"concat works as expected")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[11 22 33]
|
|
|
|
(zip &add-ref &[1 2 3 4 5 6 7] &[10 20 30])
|
|
|
|
"zip works as expected")
|
2018-11-07 18:11:38 +03:00
|
|
|
(assert-equal test
|
|
|
|
"[(Pair 0 @\"a\") (Pair 1 @\"b\") (Pair 2 @\"c\")]"
|
|
|
|
&(str &(Array.enumerated &[@"a" @"b" @"c"]))
|
|
|
|
"enumerated works as expected")
|
2020-05-11 18:37:07 +03:00
|
|
|
(assert-equal test
|
2020-05-11 21:50:40 +03:00
|
|
|
&[]
|
|
|
|
&(Array.enumerated &(the (Array Int) []))
|
2020-05-11 18:37:07 +03:00
|
|
|
"enumerated works with empty arrays")
|
2018-11-07 18:11:38 +03:00
|
|
|
(let-do [arr [1 2 3 4 5 6]
|
|
|
|
exp [1 2 3 4 5 6 7]
|
|
|
|
new (Array.push-back arr 7)]
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
exp
|
|
|
|
new
|
|
|
|
"Array.push-back works as expected"))
|
2018-11-07 18:11:38 +03:00
|
|
|
(let-do [arr [1 2 3]
|
|
|
|
exp [1 2 3 4 5 6 7 8 9 10 11 12 13 14]]
|
2020-05-12 15:53:42 +03:00
|
|
|
(Array.push-back! &arr 4)
|
|
|
|
(Array.push-back! &arr 5)
|
|
|
|
(Array.push-back! &arr 6)
|
|
|
|
(Array.push-back! &arr 7)
|
|
|
|
(Array.push-back! &arr 8)
|
|
|
|
(Array.push-back! &arr 9)
|
|
|
|
(Array.push-back! &arr 10)
|
|
|
|
(Array.push-back! &arr 11)
|
|
|
|
(Array.push-back! &arr 12)
|
|
|
|
(Array.push-back! &arr 13)
|
|
|
|
(Array.push-back! &arr 14)
|
|
|
|
(assert-ref-equal test
|
|
|
|
exp
|
|
|
|
arr
|
|
|
|
"Array.push-back! works as expected"))
|
2018-11-07 18:11:38 +03:00
|
|
|
(let-do [a [1 2 3 4 5 6]
|
|
|
|
b (Array.pop-back a)
|
|
|
|
c (Array.pop-back b)
|
|
|
|
d (Array.pop-back c)
|
|
|
|
exp [1 2 3]]
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
exp
|
|
|
|
d
|
|
|
|
"Array.pop-back works as expected"))
|
2018-11-07 18:11:38 +03:00
|
|
|
(let-do [arr [1 2 3 4 5 6]
|
|
|
|
exp [1 2 3]
|
|
|
|
six (Array.pop-back! &arr)
|
|
|
|
five (Array.pop-back! &arr)
|
|
|
|
four (Array.pop-back! &arr)]
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-true test
|
2021-06-20 22:44:04 +03:00
|
|
|
(and (= &exp &arr)
|
2020-05-12 15:53:42 +03:00
|
|
|
(= six 6)
|
|
|
|
(= five 5)
|
|
|
|
(= four 4))
|
|
|
|
"Array.pop-back! works as expected"))
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2 3 4 5 6 7 8 9]
|
2020-05-24 13:26:18 +03:00
|
|
|
(sort (range-or-default 9 1 -1))
|
2020-05-12 15:53:42 +03:00
|
|
|
"sort works as expected")
|
2018-11-07 18:11:38 +03:00
|
|
|
(let-do [arr [3 2 5]
|
|
|
|
exp [2 3 5]]
|
2020-05-12 15:53:42 +03:00
|
|
|
(sort! &arr)
|
|
|
|
(assert-ref-equal test
|
|
|
|
exp
|
|
|
|
arr
|
|
|
|
"sort! works as expected"))
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2 3 4 5 6 7 8 9]
|
|
|
|
(sorted &[9 2 1 3 7 8 6 5 4])
|
|
|
|
"sorted works as expected")
|
|
|
|
(assert-true test
|
|
|
|
(empty? &(the (Array ()) []))
|
|
|
|
"empty? works as expected I")
|
|
|
|
(assert-false test
|
2018-11-08 21:31:54 +03:00
|
|
|
(empty? &[1])
|
|
|
|
"empty? works as expected II")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-true test
|
2020-05-24 13:26:18 +03:00
|
|
|
(any? &(fn [x] (= 0 @x)) &(range-or-default 0 10 1))
|
2020-05-12 15:53:42 +03:00
|
|
|
"any? works as expected I")
|
|
|
|
(assert-false test
|
2020-05-24 13:26:18 +03:00
|
|
|
(any? &(fn [x] (= 0 @x)) &(range-or-default 1 10 1))
|
2018-11-08 21:24:21 +03:00
|
|
|
"any? works as expected II")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-true test
|
2020-05-24 13:26:18 +03:00
|
|
|
(all? &(fn [x] (< 0 @x)) &(range-or-default 1 10 1))
|
2020-05-12 15:53:42 +03:00
|
|
|
"all? works as expected I")
|
|
|
|
(assert-false test
|
2020-05-24 13:26:18 +03:00
|
|
|
(all? &(fn [x] (= 0 @x)) &(range-or-default 10 1 -1))
|
2018-11-08 21:24:21 +03:00
|
|
|
"all? works as expected II")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 3)
|
2020-05-24 13:26:18 +03:00
|
|
|
(find &(fn [x] (= 3 @x)) &(range-or-default 1 10 1))
|
2020-05-12 15:53:42 +03:00
|
|
|
"find works as expected I")
|
|
|
|
(assert-nothing test
|
2020-05-24 13:26:18 +03:00
|
|
|
&(find &(fn [x] (= 0 @x)) &(range-or-default 1 10 1))
|
2020-05-12 15:53:42 +03:00
|
|
|
"find works as expected II")
|
|
|
|
(assert-nothing test
|
|
|
|
&(find-index &(fn [i] (Int.even? @i)) &[1 3 5])
|
|
|
|
"find-index works I")
|
|
|
|
(assert-ref-equal test
|
|
|
|
(Maybe.Just 1)
|
|
|
|
(find-index &(fn [i] (Int.even? @i)) &[1 8 5])
|
|
|
|
"find-index works II")
|
2020-05-03 19:02:28 +03:00
|
|
|
(assert-equal test
|
|
|
|
2
|
|
|
|
(element-count &[1 4 3 4] &4)
|
|
|
|
"element-count works as expected")
|
2018-12-18 17:30:09 +03:00
|
|
|
(assert-equal test
|
|
|
|
2
|
|
|
|
(predicate-count &[1 8 5 10 3] &(fn [i] (Int.even? @i)))
|
|
|
|
"predicate-count works")
|
2020-01-18 19:11:09 +03:00
|
|
|
(assert-equal test
|
|
|
|
&1
|
|
|
|
(Pointer.to-ref (unsafe-raw &[1 2 3]))
|
|
|
|
"unsafe-raw works")
|
|
|
|
(assert-equal test
|
|
|
|
&2
|
|
|
|
(Pointer.to-ref (Pointer.inc (unsafe-raw &[1 2 3])))
|
2020-01-18 20:27:41 +03:00
|
|
|
"unsafe-raw works II")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
[1 3 5]
|
|
|
|
(remove &2 [2 1 3 2 5])
|
|
|
|
"remove works")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 3]
|
|
|
|
(remove &2 [1 3])
|
|
|
|
"remove works when element is not found")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 3]
|
|
|
|
(remove-nth 1 [1 2 3])
|
|
|
|
"remove-nth works")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1.0 1.5 2.0 2.5]
|
|
|
|
(unreduce 1.0 &(fn [x] (< x 3.0)) &(fn [x] (+ x 0.5)))
|
|
|
|
"unreduce works")
|
2020-05-03 19:27:58 +03:00
|
|
|
(assert-true test
|
|
|
|
(contains? &[0 1 2] &1)
|
|
|
|
"contains? works as expected I")
|
|
|
|
(assert-false test
|
|
|
|
(contains? &[0 1 2] &100)
|
|
|
|
"contains? works as expected II")
|
2020-05-12 15:53:42 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2 3]
|
|
|
|
(from-static $[1 2 3])
|
2021-04-19 17:45:15 +03:00
|
|
|
"from-static works")
|
|
|
|
(assert-ref-equal test
|
|
|
|
(Pair.init 6 [2 4 6])
|
|
|
|
(map-reduce &(fn [acc x] (Pair.init (+ @x @acc) (* @x 2))) 0 &[1 2 3])
|
|
|
|
"map-reduce works")
|
2021-10-22 07:59:40 +03:00
|
|
|
(assert-ref-equal test
|
|
|
|
[0 1 2 3 4 5]
|
|
|
|
(scan &(fn [x y] (+ @x @y)) 0 &[1 1 1 1 1])
|
|
|
|
"scan works")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[@"" @"a" @"ab" @"abc"]
|
|
|
|
(Array.scan &(fn [a b] (String.append a b)) @"" &[@"a" @"b" @"c"])
|
|
|
|
"scan works on managed type")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[1 2 3 4 5]
|
|
|
|
(endo-scan &(fn [x y] (+ @x @y)) [1 1 1 1 1])
|
|
|
|
"endo-scan works")
|
|
|
|
(assert-ref-equal test
|
|
|
|
[@"a" @"ab" @"abc"]
|
|
|
|
(Array.endo-scan &String.append [@"a" @"b" @"c"])
|
|
|
|
"endo-scan works on managed type")
|
2021-04-19 17:45:15 +03:00
|
|
|
)
|