|
| 1 | +(ns recursion-test |
| 2 | + (:use recursion |
| 3 | + midje.sweet)) |
| 4 | + |
| 5 | +(facts "product" |
| 6 | + (product []) => 1 |
| 7 | + (product [1 2 3]) => 6 |
| 8 | + (product [1 2 3 4]) => 24 |
| 9 | + (product [0 1 2]) => 0 |
| 10 | + (product #{2 3 4}) => 24) |
| 11 | + |
| 12 | +(facts "last-element" |
| 13 | + (last-element []) => nil |
| 14 | + (last-element [1 2 3]) => 3 |
| 15 | + (last-element [2 5]) => 5) |
| 16 | + |
| 17 | +(facts "sequence-contains?" |
| 18 | + (sequence-contains? 3 [1 2 3]) => true |
| 19 | + (sequence-contains? 3 [4 7 9]) => false |
| 20 | + (sequence-contains? :pony []) => false) |
| 21 | + |
| 22 | +(facts "seq=" |
| 23 | + (seq= [1 2 4] '(1 2 4)) => true |
| 24 | + (seq= [1 2 3] [1 2 3 4]) => false |
| 25 | + (seq= [1 3 5] []) => false) |
| 26 | + |
| 27 | +(facts "power" |
| 28 | + (power 2 2) => 4 |
| 29 | + (power 5 3) => 125 |
| 30 | + (power 7 0) => 1 |
| 31 | + (power 0 10) => 0) |
| 32 | + |
| 33 | +(facts "fib" |
| 34 | + (fib 0) => 0 |
| 35 | + (fib 1) => 1 |
| 36 | + (fib 2) => 1 |
| 37 | + (fib 3) => 2 |
| 38 | + (fib 4) => 3 |
| 39 | + (fib 5) => 5 |
| 40 | + (fib 6) => 8 |
| 41 | + (fib 10) => 55) |
| 42 | + |
| 43 | +(facts "my-range" |
| 44 | + (my-range 0) => nil |
| 45 | + (my-range 1) => [0] |
| 46 | + (my-range 2) => [1 0] |
| 47 | + (my-range 3) => [2 1 0]) |
| 48 | + |
| 49 | +(facts "map-1" |
| 50 | + (map-1 identity []) => empty? |
| 51 | + (map-1 identity [1 2 3]) => [1 2 3] |
| 52 | + (map-1 count ["aaa" "bb" "cccc"]) => [3 2 4] |
| 53 | + (map-1 first [[1 2] [4] [7 12 28]]) => [1 4 7] |
| 54 | + (map-1 zero? [0 2 0 13 4 0]) |
| 55 | + => [true false true false false true]) |
| 56 | + |
| 57 | +(facts "tails" |
| 58 | + (tails [1 2 3 4]) => (just [[1 2 3 4] [2 3 4] [3 4] [4] empty?] :in-any-order) |
| 59 | + (tails []) => (just [empty?] :in-any-order) |
| 60 | + (tails [1]) => (just [[1] empty?] :in-any-order)) |
| 61 | + |
| 62 | +(facts "inits" |
| 63 | + (inits [1 2 3 4]) => (just [empty? [1] [1 2] [1 2 3] [1 2 3 4]] :in-any-order) |
| 64 | + (inits []) => (just [empty?] :in-any-order) |
| 65 | + (inits [1]) => (just [empty? [1]] :in-any-order)) |
| 66 | + |
| 67 | +(facts "split-into-monotonics" |
| 68 | + (split-into-monotonics [0 1 2 1 0]) => '((0 1 2) (1 0)) |
| 69 | + (split-into-monotonics [0 5 4 7 1 3]) => '((0 5) (4 7) (1 3))) |
| 70 | + |
| 71 | +(facts "rotations" |
| 72 | + (rotations []) => empty? |
| 73 | + (rotations [1 2 3]) => (just [[1 2 3] [2 3 1] [3 1 2]] :in-any-order) |
| 74 | + (rotations [:a :b]) => (just [[:a :b] [:b :a]] :in-any-order) |
| 75 | + (rotations [1 5 9 2]) => (just '(1 5 9 2) '(2 1 5 9) |
| 76 | + '(9 2 1 5) '(5 9 2 1) |
| 77 | + :in-any-order) |
| 78 | + (count (rotations [6 5 8 9 2])) => 5) |
| 79 | + |
| 80 | +(facts "my-frequencies" |
| 81 | + (my-frequencies []) => {} |
| 82 | + (my-frequencies [1 1 2 2 :D :D :D]) => {1 2, 2 2, :D 3} |
| 83 | + (my-frequencies [:a "moi" :a "moi" "moi" :a 1]) |
| 84 | + => {:a 3, "moi" 3, 1 1}) |
| 85 | + |
| 86 | +(facts "un-frequencies" |
| 87 | + (un-frequencies {:a 3 :b 2 "^_^" 1}) |
| 88 | + => (just [:a :a :a "^_^" :b :b] :in-any-order) |
| 89 | + (un-frequencies (my-frequencies [:a :b :c :a])) |
| 90 | + => (just [:a :a :b :c] :in-any-order) |
| 91 | + (my-frequencies (un-frequencies {:a 100 :b 10})) |
| 92 | + => {:a 100 :b 10}) |
| 93 | + |
| 94 | +(facts "seq-merge" |
| 95 | + (seq-merge [4] [1 2 6 7]) => '(1 2 4 6 7) |
| 96 | + (seq-merge [1 5 7 9] [2 2 8 10]) => '(1 2 2 5 7 8 9 10)) |
| 97 | + |
| 98 | +(facts "mergesort" |
| 99 | + (mergesort []) => empty? |
| 100 | + (mergesort [1 2 3]) => '(1 2 3) |
| 101 | + (mergesort [5 3 4 17 2 100 1]) => '(1 2 3 4 5 17 100)) |
| 102 | + |
| 103 | +(facts "permutations" |
| 104 | + (permutations []) => empty? |
| 105 | + (permutations [1 5 3]) |
| 106 | + => (just [[1 5 3] [5 1 3] [5 3 1] [1 3 5] [3 1 5] [3 5 1]] |
| 107 | + :in-any-order)) |
| 108 | + |
| 109 | +(facts "powerset" |
| 110 | + (powerset []) => '(()) |
| 111 | + (powerset [1 2 4]) => (just [empty? [4] [2] [2 4] [1] [1 4] [1 2] [1 2 4]] :in-any-order)) |
0 commit comments