Carp/test/array.carp
2019-05-14 06:48:26 -03:00

289 lines
9.7 KiB
Plaintext

(use Array)
(load "Test.carp")
(use Test)
(defn nested []
[[1 2 3]
[4 5 6]
[7 8 9]])
(defn excl [x] (StringCopy.append x @"!"))
(defn excl-ref [x] (StringCopy.append @x @"!"))
(defn inc-ref [x] (+ @x 1))
(defn make-zero [] 0)
(defn make-idx [i] i)
(def a (range 0 9 1))
(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")
(assert-equal test
&[0 0 0 0]
&(replicate 4 &0)
"replicate works as expected")
(assert-equal test
&[0 0 0 0]
&(repeat 4 &make-zero)
"repeat works as expected")
(assert-equal test
&[0 1 2 3]
&(repeat-indexed 4 make-idx)
"repeat-indexed works as expected")
(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 &[])
"last works as expected on empty array")
(assert-equal test
&[3 2 1]
&(reverse [1 2 3])
"reverse works as expected")
(assert-equal test
&(Maybe.Just 10)
&(maximum &(range 1 10 1))
"maximum works as expected")
(assert-equal test
&(Maybe.Just 1)
&(minimum &(range 1 10 1))
"minimum 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 (Pair.init 1 3))
&(minimum &[(Pair.init 1 3) (Pair.init 2 1) (Pair.init 2 0)])
"minimum works on pairs")
(assert-equal test
&(Maybe.Just 3)
&(index-of &[1 2 3 4] &4)
"index-of works as expected when element is in the array")
(assert-equal test
&(Maybe.Nothing)
&(index-of &[1 2 3 4] &7)
"index-of works as expected when element is not in the array")
(assert-equal test
55
(sum &(range 1 10 1))
"sum works as expected")
(assert-equal test
&[2 3]
&(subarray &(range 1 10 1) 1 3)
"subarray works as expected")
(assert-equal test
&[1 2 3]
&(prefix-array &(range 1 10 1) 3)
"prefix-array works as expected")
(assert-equal test
&[8 9 10]
&(suffix-array &(range 1 10 1) 7)
"suffix-array works as expected")
(assert-equal test
5
@(nth &a 5)
"nth works as expected")
(assert-equal test
&[1 2 3]
(nth &(nested) 0)
"nth works as expected")
(assert-equal test
&[10 11 12 13 14 15]
&(range 10 15 1)
"range works as expected")
(assert-equal test
&[10.0 10.5 11.0 11.5 12.0]
&(range 10.0 12.0 0.5)
"range works as expected on non-integers")
(assert-equal test
&[10 9 8 7 6 5 4 3 2 1 0]
&(range 10 0 -1)
"range backwards works as expected")
(assert-equal test
&[1 3 5 7 9]
&(range 1 10 2)
"range works as expected if we dont exactly hit")
(assert-equal test
&[10 8 6 4 2]
&(range 10 1 -2)
"range backwards works as expected if we dont exactly hit")
(assert-equal test
&[@"Hi!" @"Hi!" @"Hi!" @"Hi!" @"Hi!"]
&(copy-map &excl-ref &b)
"copy-map works as expected")
(assert-equal test
&[@"Hi!" @"Hi!" @"Hi!" @"Hi!" @"Hi!"]
&(endo-map &excl @&b)
"endo-map works as expected")
(assert-equal test
&[1 2]
&(swap [2 1] 0 1)
"swap works as expected")
(assert-equal test
&[1 3]
&(aupdate [1 2] 1 &inc-ref)
"aupdate works as expected")
(assert-equal test
&[1 2 3 4 5 6 7 8]
&(concat &[[1] [2 3] [4 5 6] [7 8]])
"concat works as expected")
(assert-equal test
&[11 22 33]
&(zip &add-ref &[1 2 3 4 5 6 7] &[10 20 30])
"zip works as expected")
(assert-equal test
"[(Pair 0 @\"a\") (Pair 1 @\"b\") (Pair 2 @\"c\")]"
&(str &(Array.enumerated &[@"a" @"b" @"c"]))
"enumerated works as expected")
(let-do [arr [1 2 3 4 5 6]
exp [1 2 3 4 5 6 7]
new (Array.push-back arr 7)]
(assert-equal test
&exp
&new
"Array.push-back works as expected"))
(let-do [arr [1 2 3]
exp [1 2 3 4 5 6 7 8 9 10 11 12 13 14]]
(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-equal test
&exp
&arr
"Array.push-back! works as expected"))
(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]]
(assert-equal test
&exp
&d
"Array.pop-back works as expected"))
(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)]
(assert-true test
(and* (= &exp &arr)
(= six 6)
(= five 5)
(= four 4))
"Array.pop-back! works as expected"))
(assert-equal test
&[1 2 3 4 5 6 7 8 9]
&(sort (range 9 1 -1))
"sort works as expected")
(let-do [arr [3 2 5]
exp [2 3 5]]
(sort! &arr)
(assert-equal test
&exp
&arr
"sort! works as expected"))
(assert-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-equal test
true
(empty? &(the (Array ()) []))
"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)) &(range 0 10 1))
"any? works as expected I")
(assert-equal test
false
(any? &(fn [x] (= 0 @x)) &(range 1 10 1))
"any? works as expected II")
(assert-equal test
true
(all? &(fn [x] (< 0 @x)) &(range 1 10 1))
"all? works as expected I")
(assert-equal test
false
(all? &(fn [x] (= 0 @x)) &(range 10 1 -1))
"all? works as expected II")
(assert-equal test
&(Maybe.Just 3)
&(find &(fn [x] (= 3 @x)) &(range 1 10 1))
"find works as expected I")
(assert-equal test
&(Maybe.Nothing)
&(find &(fn [x] (= 0 @x)) &(range 1 10 1))
"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])
"find-index works II")
(assert-equal test
2
(predicate-count &[1 8 5 10 3] &(fn [i] (Int.even? @i)))
"predicate-count works")
(assert-equal test
&[1 3 5]
&(remove &2 [2 1 3 2 5])
"remove works")
(assert-equal test
&[1 3]
&(remove &2 [1 3])
"remove works when element is not found")
(assert-equal test
&[1 3]
&(remove-nth 1 [1 2 3])
"remove-nth works"))