Carp/test/map.carp

264 lines
8.0 KiB
Plaintext
Raw Normal View History

2018-06-11 21:50:54 +03:00
(use Map)
(load "Test.carp")
(use Test)
2018-11-07 18:11:38 +03:00
(deftest test
(assert-equal test
"2"
&(Map.get &(Map.put (Map.create) "1" "2") "1")
"basic put and get works"
)
2018-12-03 21:45:23 +03:00
(assert-equal test
"3"
&(Map.get &(Map.put (Map.put (Map.create) "1" "2") "1" "3") "1")
"put, update and get"
)
(assert-equal test
"2"
&(Map.get &(Map.put (Map.create) &-7 "2") &-7)
"basic put and get works with negative keys"
)
(assert-equal test
true
(Map.empty? &(Map.update (Map.create) "x" Int.inc))
"update works with empty map"
)
(assert-equal test
2
(Map.get &(Map.update {@"x" 1} "x" Int.inc) "x")
"update works"
)
(assert-equal test
8
(Map.get &(Map.update-with-default (Map.create) "x" Int.inc 7) "x")
"update-with-default works with empty map"
)
(assert-equal test
2
(Map.get &(Map.update-with-default {@"x" 1} "x" Int.inc 7) "x")
"update-with-default works"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
1
(Map.length &(Map.put (Map.create) "1" "2"))
"length works"
)
(assert-equal test
0
(Map.length &(the (Map.Map Int Int) (Map.create)))
"length works on empty map"
)
(assert-equal test
false
(Map.contains? &(the (Map.Map String Int) (Map.create)) "1")
"contains? works on empty map"
)
(assert-equal test
true
(Map.contains? &(Map.put (Map.create) "1" "2") "1")
"contains? works"
)
(assert-equal test
true
(Map.contains? &(Map.put (Map.create) &-7 "2") &-7)
"contains? works with negative keys"
)
(assert-equal test
false
(Map.contains? &(Map.put (Map.create) &1 "2") &-7)
"contains? works with negative keys"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
true
(Map.empty? &(the (Map Int Int) (Map.create)))
"empty? works on empty map"
)
(assert-equal test
false
(Map.empty? &(Map.put (Map.create) "1" "2"))
"empty? works"
)
(assert-equal test
true
(Map.empty? &(Map.remove (Map.put (Map.create) "1" "2") "1"))
"remove works"
)
(assert-equal test
2
(Map.length &(Map.from-array &[(Pair.init 1 2)
(Pair.init 3 4)]))
"creating a map from an array works"
)
(assert-equal test
"{ 1 2 }"
&(str &(Map.from-array &[(Pair.init 1 2)]))
"stringification works I"
)
(assert-equal test
"{ @\"hi\" @\"bye\" }"
&(str &(Map.from-array &[(Pair.init @"hi" @"bye")]))
"stringification works II"
)
(assert-equal test
&[(Pair.init 1 2)]
&(Map.to-array &(Map.put (Map.create) &1 &2))
"Map.to-array works 1"
)
(assert-equal test
2
(Array.length &(Map.to-array &(Map.from-array &[(Pair.init 1 2)
(Pair.init 3 4)])))
"Map.to-array works 2"
)
2018-12-07 14:22:09 +03:00
(assert-equal test
"{ 1 12 3 34 }"
&(str &(Map.endo-map (fn [k v] (+ @v (* 10 @k)))
{1 2 3 4}))
"endo-map works"
)
(assert-equal test
641
(Map.kv-reduce (fn [sum k v] (+ sum (+ (* 100 @k) (* 10 @v))))
1
&{1 1 2 1 3 2})
"kv-reduce works"
)
(assert-equal test
&[1 2 3]
&(Map.keys &{1 1 2 1 3 2})
"keys works"
)
(assert-equal test
&[1 1 2]
&(Map.vals &{1 1 2 1 3 2})
"vals works"
)
(assert-equal test
3
(Map.get &{(Pair.init 1 2) 3} &(Pair.init 1 2))
"Pairs work as keys"
)
(assert-equal test
1
(Set.length &(Set.put (Set.create) "1"))
"length works"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
2018-12-03 21:45:23 +03:00
2
(Set.length &(Set.put (Set.put (Set.create) "1") "2"))
2018-11-07 18:11:38 +03:00
"length works"
)
2018-12-03 21:45:23 +03:00
(assert-equal test
1
(Set.length &(Set.put (Set.put (Set.create) "1") "1"))
"putting the same element twice doesn't increase size"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
0
(Set.length &(the (Set.Set Int) (Set.create)))
"length works on empty map"
)
(assert-equal test
false
(Set.contains? &(the (Set.Set String) (Set.create)) "1")
"contains? works on empty map"
)
(assert-equal test
true
(Set.contains? &(Set.put (Set.create) "1") "1")
"contains? works"
)
(assert-equal test
true
(Set.contains? &(Set.put (Set.create) &-7) &-7)
"contains? works with negative keys"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
true
(Set.empty? &(the (Set Int) (Set.create)))
"empty? works on empty map"
)
(assert-equal test
false
(Set.empty? &(Set.put (Set.create) "1"))
"empty? works"
)
(assert-equal test
true
(Set.empty? &(Set.remove (Set.put (Set.create) "1") "1"))
"remove works"
)
(assert-equal test
true
(Set.subset? &(Set.from-array &[1 2]) &(Set.from-array &[1 2 3]))
"subset? works"
)
(assert-equal test
false
(Set.subset? &(Set.from-array &[1 2 3]) &(Set.from-array &[1 2]))
"subset? works II"
)
(assert-equal test
true
(Set.= &(Set.from-array &[1 3 5]) &(Set.from-array &[1 3 5]))
"Set.= works"
)
(assert-equal test
false
(Set.= &(Set.from-array &[1 3]) &(Set.from-array &[1 3 5]))
"Set.= works II"
)
(assert-equal test
false
(Set.= &(Set.from-array &[1 3 5]) &(Set.from-array &[1 3]))
"Set.= works III"
)
(assert-equal test
false
(Set.= &(Set.from-array &[1 3 5]) &(Set.from-array &[1 4 5]))
"Set.= works IV"
)
2018-12-07 14:15:23 +03:00
(assert-equal test
61
(Set.reduce (fn [state i] (+ state (* 10 @i)))
1
&(Set.from-array &[1 2 3]))
"reduce works"
)
(assert-equal test
&(Set.from-array &[3 5])
&(Set.intersection &(Set.from-array &[1 3 5]) &(Set.from-array &[3 5 7]))
"intersection works"
)
(assert-equal test
&(Set.from-array &[1 3 5 7])
&(Set.union &(Set.from-array &[1 3 5]) &(Set.from-array &[3 5 7]))
"union works"
)
(assert-equal test
&(Set.from-array &[1])
&(Set.difference &(Set.from-array &[1 3 5]) &(Set.from-array &[3 5 7]))
"difference works"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
"{ @\"hi\" @\"bye\" }"
&(str &(Set.from-array &[@"hi" @"bye"]))
"stringification works"
)
2018-12-12 09:21:16 +03:00
(assert-equal test
"{ 2 }"
&(str &(Set.from-array &[2]))
"stringification with ints"
)
(assert-equal test
&[1]
&(Set.to-array &(Set.put (Set.create) &1))
"Set.to-array works 1"
)
(assert-equal test
2
(Array.length &(Set.to-array &(Set.from-array &[1 2])))
"Set.to-array works 2"
2018-11-07 18:11:38 +03:00
))