|
1 | 1 | (ns glojure.test-glojure.sort
|
2 | 2 | (:use glojure.test))
|
3 | 3 |
|
4 |
| -(deftest basic-sort |
5 |
| - (testing "Basic sorting with default comparator" |
| 4 | +(deftest numeric-sorting |
| 5 | + (testing "Numeric sorting with different types" |
| 6 | + (is (= (sort [3.14 2 1.5 10 -5 0]) '(-5 0 1.5 2 3.14 10))) |
| 7 | + (is (= (sort [-10 -5 -20 -1 -100]) '(-100 -20 -10 -5 -1))))) |
| 8 | + |
| 9 | +(deftest collection-types |
| 10 | + (testing "Sorting different collection types" |
| 11 | + (is (= (sort [5 2 8 1 9]) '(1 2 5 8 9))) |
| 12 | + (is (= (sort '(5 2 8 1 9)) '(1 2 5 8 9))) |
| 13 | + (is (= (sort ["zebra" "apple" "banana" "cat"]) '("apple" "banana" "cat" "zebra"))))) |
| 14 | + |
| 15 | +(deftest edge-cases |
| 16 | + (testing "Edge cases for sorting" |
| 17 | + (is (= (sort []) '())) |
| 18 | + (is (= (sort '()) '())) |
| 19 | + (is (= (sort [42]) '(42))) |
| 20 | + (is (= (sort [3 nil 1 4 nil 2]) '(nil nil 1 2 3 4))) |
| 21 | + (is (= (sort [3 "hello" 1 :keyword 4]) '(1 3 4 :keyword "hello"))))) |
| 22 | + |
| 23 | +(deftest custom-comparator-examples |
| 24 | + (testing "Custom comparator examples" |
| 25 | + (let [even-odd-compare (fn [a b] |
| 26 | + (let [a-even (even? a) |
| 27 | + b-even (even? b)] |
| 28 | + (cond |
| 29 | + (and a-even b-even) (compare a b) |
| 30 | + (and (not a-even) (not b-even)) (compare a b) |
| 31 | + a-even -1 |
| 32 | + :else 1)))] |
| 33 | + (is (= (sort even-odd-compare [1 2 3 4 5 6 7 8 9 10]) |
| 34 | + '(2 4 6 8 10 1 3 5 7 9)))))) |
| 35 | + |
| 36 | +(deftest complex-examples |
| 37 | + (testing "Complex sorting examples" |
| 38 | + (let [students [{:name "Alice" :grade 85 :age 20} |
| 39 | + {:name "Bob" :grade 92 :age 19} |
| 40 | + {:name "Charlie" :grade 85 :age 21} |
| 41 | + {:name "David" :grade 78 :age 20}]] |
| 42 | + |
| 43 | + (is (= (sort-by (fn [s] [(:age s) (:grade s)]) students) |
| 44 | + '({:name "Bob" :grade 92 :age 19} |
| 45 | + {:name "David" :grade 78 :age 20} |
| 46 | + {:name "Alice" :grade 85 :age 20} |
| 47 | + {:name "Charlie" :grade 85 :age 21}))) |
| 48 | + |
| 49 | + (let [grade-desc-compare (fn [a b] (compare (:grade b) (:grade a)))] |
| 50 | + (is (= (sort grade-desc-compare students) |
| 51 | + '({:name "Bob" :grade 92 :age 19} |
| 52 | + {:name "Alice" :grade 85 :age 20} |
| 53 | + {:name "Charlie" :grade 85 :age 21} |
| 54 | + {:name "David" :grade 78 :age 20}))))))) |
| 55 | + |
| 56 | +(deftest sort-clojuredocs-examples |
| 57 | + (testing "Examples from ClojureDocs sort documentation" |
| 58 | + |
| 59 | + ;; Basic sort with default comparator |
6 | 60 | (is (= (sort [3 1 4 1 5 9 2 6]) '(1 1 2 3 4 5 6 9)))
|
7 |
| - (is (= (sort ["banana" "apple" "cherry" "date"]) '("apple" "banana" "cherry" "date"))))) |
8 | 61 |
|
9 |
| -(deftest custom-comparators |
10 |
| - (testing "Sort with custom comparators" |
| 62 | + ;; Sort with custom comparator |
11 | 63 | (is (= (sort > [3 1 4 1 5 9 2 6]) '(9 6 5 4 3 2 1 1)))
|
12 | 64 |
|
| 65 | + ;; Sort with custom comparator function |
| 66 | + (let [reverse-compare (fn [a b] (compare b a))] |
| 67 | + (is (= (sort reverse-compare [3 1 4 1 5 9 2 6]) '(9 6 5 4 3 2 1 1)))) |
| 68 | + |
| 69 | + ;; Sort with length-based comparator |
13 | 70 | (let [length-compare (fn [a b] (compare (count (str a)) (count (str b))))]
|
14 |
| - (is (= (sort length-compare ["banana" "apple" "cherry" "date"]) '("date" "apple" "cherry" "banana")))) |
| 71 | + (is (= (sort length-compare ["banana" "apple" "cherry" "date"]) |
| 72 | + '("date" "apple" "cherry" "banana")))) |
15 | 73 |
|
16 |
| - (let [reverse-compare (fn [a b] (compare b a))] |
17 |
| - (is (= (sort reverse-compare [3 1 4 1 5 9 2 6]) '(9 6 5 4 3 2 1 1)))))) |
| 74 | + ;; Sort with even-odd comparator |
| 75 | + (let [even-odd-compare (fn [a b] |
| 76 | + (let [a-even (even? a) |
| 77 | + b-even (even? b)] |
| 78 | + (cond |
| 79 | + (and a-even b-even) (compare a b) |
| 80 | + (and (not a-even) (not b-even)) (compare a b) |
| 81 | + a-even -1 |
| 82 | + :else 1)))] |
| 83 | + (is (= (sort even-odd-compare [1 2 3 4 5 6 7 8 9 10]) |
| 84 | + '(2 4 6 8 10 1 3 5 7 9)))) |
18 | 85 |
|
19 |
| -(deftest sort-by-tests |
20 |
| - (testing "Sort-by functionality" |
21 |
| - (let [people [{:name "Alice" :age 30} |
22 |
| - {:name "Bob" :age 25} |
23 |
| - {:name "Charlie" :age 35}]] |
| 86 | + ;; Sort with grade-based comparator |
| 87 | + (let [students [{:name "Alice" :grade 85 :age 20} |
| 88 | + {:name "Bob" :grade 92 :age 19} |
| 89 | + {:name "Charlie" :grade 85 :age 21} |
| 90 | + {:name "David" :grade 78 :age 20}] |
| 91 | + grade-desc-compare (fn [a b] (compare (:grade b) (:grade a)))] |
| 92 | + (is (= (sort grade-desc-compare students) |
| 93 | + '({:name "Bob" :grade 92 :age 19} |
| 94 | + {:name "Alice" :grade 85 :age 20} |
| 95 | + {:name "Charlie" :grade 85 :age 21} |
| 96 | + {:name "David" :grade 78 :age 20})))) |
| 97 | + |
| 98 | + ;; Note: Array modification test is commented out because to-array behavior |
| 99 | + ;; may differ in Glojure compared to Clojure's Java arrays |
| 100 | + ;; The original example shows that Java arrays are modified in place, |
| 101 | + ;; but Glojure's to-array implementation may not have the same behavior |
| 102 | + ;; (let [arr (to-array [3 1 4 1 5 9 2 6])] |
| 103 | + ;; (sort arr) |
| 104 | + ;; (is (= (seq arr) '(1 1 2 3 4 5 6 9)))) |
| 105 | + |
| 106 | + ;; Sort with nil values |
| 107 | + (is (= (sort [3 nil 1 4 nil 2]) '(nil nil 1 2 3 4))) |
| 108 | + |
| 109 | + ;; Sort with mixed types |
| 110 | + (is (= (sort [3 "hello" 1 :keyword 4]) '(1 3 4 :keyword "hello"))) |
24 | 111 |
|
25 |
| - (is (= (sort-by :age people) |
26 |
| - '({:name "Bob" :age 25} {:name "Alice" :age 30} {:name "Charlie" :age 35}))) |
| 112 | + ;; Sort with floating point numbers |
| 113 | + (is (= (sort [3.14 2 1.5 10 -5 0]) '(-5 0 1.5 2 3.14 10))) |
27 | 114 |
|
28 |
| - (is (= (sort-by :name people) |
29 |
| - '({:name "Alice" :age 30} {:name "Bob" :age 25} {:name "Charlie" :age 35}))) |
| 115 | + ;; Sort with negative numbers |
| 116 | + (is (= (sort [-10 -5 -20 -1 -100]) '(-100 -20 -10 -5 -1))) |
30 | 117 |
|
31 |
| - (let [age-then-name (fn [person] [(:age person) (:name person)])] |
32 |
| - (is (= (sort-by age-then-name people) |
33 |
| - '({:name "Bob" :age 25} {:name "Alice" :age 30} {:name "Charlie" :age 35}))))))) |
| 118 | + ;; Sort with strings |
| 119 | + (is (= (sort ["zebra" "apple" "banana" "cat"]) '("apple" "banana" "cat" "zebra"))) |
| 120 | + |
| 121 | + ;; Sort with lists |
| 122 | + (is (= (sort '(5 2 8 1 9)) '(1 2 5 8 9))) |
| 123 | + |
| 124 | + ;; Sort with empty collections |
| 125 | + (is (= (sort []) '())) |
| 126 | + (is (= (sort '()) '())) |
| 127 | + (is (= (sort [42]) '(42))))) |
34 | 128 |
|
35 | 129 | (deftest sort-by-clojuredocs-examples
|
36 | 130 | (testing "Examples from ClojureDocs sort-by documentation"
|
|
94 | 188 | (is (= (sort-by identity [42]) '(42)))
|
95 | 189 | (is (= (sort-by count ["hello"]) '("hello")))))
|
96 | 190 |
|
97 |
| -(deftest edge-cases |
98 |
| - (testing "Edge cases for sorting" |
99 |
| - (is (= (sort []) '())) |
100 |
| - (is (= (sort '()) '())) |
101 |
| - (is (= (sort [42]) '(42))) |
102 |
| - (is (= (sort [3 nil 1 4 nil 2]) '(nil nil 1 2 3 4))) |
103 |
| - (is (= (sort [3 "hello" 1 :keyword 4]) '(1 3 4 :keyword "hello"))))) |
104 |
| - |
105 |
| -(deftest collection-types |
106 |
| - (testing "Sorting different collection types" |
107 |
| - (is (= (sort [5 2 8 1 9]) '(1 2 5 8 9))) |
108 |
| - (is (= (sort '(5 2 8 1 9)) '(1 2 5 8 9))) |
109 |
| - (is (= (sort ["zebra" "apple" "banana" "cat"]) '("apple" "banana" "cat" "zebra"))))) |
110 |
| - |
111 |
| -(deftest numeric-sorting |
112 |
| - (testing "Numeric sorting with different types" |
113 |
| - (is (= (sort [3.14 2 1.5 10 -5 0]) '(-5 0 1.5 2 3.14 10))) |
114 |
| - (is (= (sort [-10 -5 -20 -1 -100]) '(-100 -20 -10 -5 -1))))) |
115 |
| - |
116 |
| -(deftest custom-comparator-examples |
117 |
| - (testing "Custom comparator examples" |
118 |
| - (let [even-odd-compare (fn [a b] |
119 |
| - (let [a-even (even? a) |
120 |
| - b-even (even? b)] |
121 |
| - (cond |
122 |
| - (and a-even b-even) (compare a b) |
123 |
| - (and (not a-even) (not b-even)) (compare a b) |
124 |
| - a-even -1 |
125 |
| - :else 1)))] |
126 |
| - (is (= (sort even-odd-compare [1 2 3 4 5 6 7 8 9 10]) |
127 |
| - '(2 4 6 8 10 1 3 5 7 9)))))) |
128 |
| - |
129 |
| -(deftest complex-examples |
130 |
| - (testing "Complex sorting examples" |
131 |
| - (let [students [{:name "Alice" :grade 85 :age 20} |
132 |
| - {:name "Bob" :grade 92 :age 19} |
133 |
| - {:name "Charlie" :grade 85 :age 21} |
134 |
| - {:name "David" :grade 78 :age 20}]] |
135 |
| - |
136 |
| - (is (= (sort-by (fn [s] [(:age s) (:grade s)]) students) |
137 |
| - '({:name "Bob" :grade 92 :age 19} |
138 |
| - {:name "David" :grade 78 :age 20} |
139 |
| - {:name "Alice" :grade 85 :age 20} |
140 |
| - {:name "Charlie" :grade 85 :age 21}))) |
141 |
| - |
142 |
| - (let [grade-desc-compare (fn [a b] (compare (:grade b) (:grade a)))] |
143 |
| - (is (= (sort grade-desc-compare students) |
144 |
| - '({:name "Bob" :grade 92 :age 19} |
145 |
| - {:name "Alice" :grade 85 :age 20} |
146 |
| - {:name "Charlie" :grade 85 :age 21} |
147 |
| - {:name "David" :grade 78 :age 20}))))))) |
148 |
| - |
149 | 191 | (run-tests)
|
0 commit comments