Skip to content

Commit

Permalink
Some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jani Rahkola committed Nov 25, 2012
1 parent 88adb97 commit 889bed0
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions test/recursion_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,67 @@
(product #{2 3 4}) => 24)

(facts "ex 3 singleton?"
(singleton? [1]) => true
(singleton? #{2}) => true
(singleton? []) => false
(singleton? [1 2 3]) => false)
(singleton? [1]) => true
(singleton? #{2}) => true
(singleton? []) => false
(singleton? [1 2 3]) => false)

(facts "ex 4 my-last"
(my-last []) => nil
(my-last [1 2 3]) => 3
(my-last [2 5]) => 5)

(facts "ex 5 max-element"
(max-element [2 4 1 4]) => 4
(max-element [2]) => 2
(max-element []) => nil)
(max-element [2 4 1 4]) => 4
(max-element [2]) => 2
(max-element []) => nil)

(facts "ex 6 seq-max"
(seq-max [1] [1 2]) => [1 2]
(seq-max [1 2] [3 4]) => [3 4])
(seq-max [1] [1 2]) => [1 2]
(seq-max [1 2 3] [:a :b]) => [1 2 3]
(seq-max [1 2] [3 4]) => [3 4])

(facts "ex 7 longest-sequence"
(longest-sequence [[1 2] [] [1 2 3]]) => [1 2 3]
(longest-sequence [[1 2]]) => [1 2]
(longest-sequence []) => nil)
(longest-sequence [[1 2] [] [1 2 3]]) => [1 2 3]
(longest-sequence [[1 2]]) => [1 2]
(longest-sequence []) => nil)

(facts "ex 8 my-filter"
(my-filter odd? [1 2 3 4]) => '(1 3)
(my-filter (fn [x] (> x 9000)) [12 49 90 9001]) => '(9001)
(my-filter even? [1 3 5 7]) => empty?)
(my-filter odd? [1 2 3 4]) => (just '(1 3))
(my-filter false? [1 2 3]) => empty?
(my-filter nil? [1 nil 2]) => (just [nil])
(my-filter (fn [x] (> x 9000)) [12 49 90 9001]) => (just '(9001))
(my-filter even? [1 3 5 7]) => empty?)

(facts "ex 9 sequence-contains?"
(sequence-contains? 3 [1 2 3]) => true
(sequence-contains? 3 [4 7 9]) => false
(sequence-contains? :pony []) => false)

(facts "ex 10 my-take-while"
(my-take-while odd? [1 2 3 4]) => '(1)
(my-take-while odd? [1 3 4 5]) => '(1 3)
(my-take-while even? [1 3 4 5]) => empty?
(my-take-while odd? []) => empty?)
(my-take-while odd? [1 2 3 4]) => '(1)
(my-take-while odd? [1 3 4 5]) => '(1 3)
(my-take-while even? [1 3 4 5]) => empty?
(my-take-while odd? []) => empty?)

(facts "ex 11 my-drop-while"
(my-drop-while odd? [1 2 3 4]) => '(2 3 4)
(my-drop-while odd? [1 3 4 5]) => '(4 5)
(my-drop-while even? [1 3 4 5]) => '(1 3 4 5)
(my-drop-while odd? []) => empty?)
(my-drop-while odd? [1 2 3 4]) => '(2 3 4)
(my-drop-while odd? [1 3 4 5]) => '(4 5)
(my-drop-while even? [1 3 4 5]) => '(1 3 4 5)
(my-drop-while odd? []) => empty?)

(facts "ex 12 seq="
(seq= [1 2 4] '(1 2 4)) => true
(seq= [] []) => true
(seq= [1 2 nil] [1 2]) => false
(seq= [1 4 2] [1 2 4]) => false
(seq= [1 2 3] [1 2 3 4]) => false
(seq= [1 3 5] []) => false)

(facts "ex 13 my-map"
(my-map + [1 2 3] [4 4 4]) => '(5 6 7)
(my-map + [1 2 3 4] [0 0 0]) => '(1 2 3)
(my-map + [1 2 3] []) => empty?)
(my-map + [1 2 3] [4 4 4]) => '(5 6 7)
(my-map + [1 2 3 4] [0 0 0]) => '(1 2 3)
(my-map + [1 2 3] []) => empty?)

(facts "ex 14 power"
(power 2 2) => 4
Expand All @@ -87,9 +93,9 @@
(fib 10) => 55)

(facts "ex 16 my-repeat"
(my-repeat 2 :a) => '(:a :a)
(my-repeat 3 "lol") => '("lol" "lol" "lol")
(my-repeat -1 :a) => empty?)
(my-repeat 2 :a) => '(:a :a)
(my-repeat 3 "lol") => '("lol" "lol" "lol")
(my-repeat -1 :a) => empty?)

(facts "ex 17 my-range"
(my-range 0) => empty?
Expand All @@ -108,7 +114,7 @@
(inits [1]) => (just [[] [1]] :in-any-order))

(facts "ex 19 rotations"
(rotations []) => '(())
(rotations []) => (just '(()))
(rotations [1 2 3]) => (just [[1 2 3] [2 3 1] [3 1 2]] :in-any-order)
(rotations [:a :b]) => (just [[:a :b] [:b :a]] :in-any-order)
(rotations [1 5 9 2]) => (just '(1 5 9 2) '(2 1 5 9)
Expand All @@ -131,17 +137,17 @@
=> {:a 100 :b 10})

(facts "ex 22 my-take"
(my-take 2 [1 2 3 4]) => '(1 2)
(my-take 4 [:a :b]) => '(:a :b))
(my-take 2 [1 2 3 4]) => '(1 2)
(my-take 4 [:a :b]) => '(:a :b))

(facts "ex 23 my-drop"
(my-drop 2 [1 2 3 4]) => '(3 4)
(my-drop 4 [:a :b]) => empty?)
(my-drop 2 [1 2 3 4]) => '(3 4)
(my-drop 4 [:a :b]) => empty?)

(facts "ex 24 halve"
(halve [1 2 3 4]) => ['(1 2) '(3 4)]
(halve [1 2 3 4 5]) => ['(1 2) '(3 4 5)]
(halve [1]) => (just empty? '(1)))
(halve [1 2 3 4]) => ['(1 2) '(3 4)]
(halve [1 2 3 4 5]) => ['(1 2) '(3 4 5)]
(halve [1]) => (just empty? '(1)))

(facts "ex 25 seq-merge"
(seq-merge [4] [1 2 6 7]) => '(1 2 4 6 7)
Expand All @@ -166,13 +172,13 @@
(facts "ex 29 powerset"
(powerset []) => (some-checker (just empty?) (just #{#{}}))
(powerset [1 2 4]) => (some-checker
(just empty?
(just 4 :in-any-order)
(just 2 :in-any-order)
(just 2 4 :in-any-order)
(just 1 :in-any-order)
(just 1 4 :in-any-order)
(just 1 2 :in-any-order)
(just 1 2 4 :in-any-order) :in-any-order)
(just #{#{} #{4} #{2} #{2 4} #{1} #{1 4} #{1 2} #{1 2 4}}))
(just empty?
(just 4 :in-any-order)
(just 2 :in-any-order)
(just 2 4 :in-any-order)
(just 1 :in-any-order)
(just 1 4 :in-any-order)
(just 1 2 :in-any-order)
(just 1 2 4 :in-any-order) :in-any-order)
(just #{#{} #{4} #{2} #{2 4} #{1} #{1 4} #{1 2} #{1 2 4}}))
(count (powerset (range 10))) => 1024)

0 comments on commit 889bed0

Please sign in to comment.