|
1 | 1 | ;; Tests for sort function |
2 | | -;; Ensures Glojure behavior matches Clojure |
| 2 | +;; Ensures Glojure behavior matches Clojure as closely as possible |
3 | 3 |
|
4 | 4 | (ns glojure.test-glojure.sort |
5 | 5 | (:require [glojure.test :refer :all] |
|
51 | 51 |
|
52 | 52 | ;; Sort by string length |
53 | 53 | (is (= '("a" "bb" "ccc" "dddd") |
54 | | - (sort (fn [a b] (compare (count a) (count b))) |
55 | | - ["ccc" "a" "dddd" "bb"]))) |
| 54 | + (sort-by count ["ccc" "a" "dddd" "bb"]))) |
56 | 55 |
|
57 | 56 | ;; Sort maps by a specific key |
58 | 57 | (let [data [{:name "John" :age 30} |
|
202 | 201 | ;; Lists are not comparable |
203 | 202 | (is (thrown? go/any (compare '(1 2) '(1 2)))) |
204 | 203 | (is (thrown? go/any (sort ['(1 2) '(3 4)]))) |
205 | | - |
| 204 | + |
206 | 205 | ;; Maps are not comparable |
207 | 206 | (is (thrown? go/any (compare {:a 1} {:b 2}))) |
208 | 207 | (is (thrown? go/any (sort [{:a 1} {:b 2}]))) |
209 | | - |
| 208 | + |
210 | 209 | ;; Sets are not comparable |
211 | 210 | (is (thrown? go/any (compare #{1 2} #{3 4}))) |
212 | 211 | (is (thrown? go/any (sort [#{1 2} #{3 4}]))) |
213 | | - |
| 212 | + |
214 | 213 | ;; Mixed incompatible types |
215 | 214 | (is (thrown? go/any (compare 1 :a))) |
216 | 215 | (is (thrown? go/any (compare "string" 'symbol))) |
|
222 | 221 | (is (= -1 (compare [1 2] [1 3]))) |
223 | 222 | (is (= 1 (compare [1 3] [1 2]))) |
224 | 223 | (is (= 0 (compare [1 2 3] [1 2 3]))) |
225 | | - |
| 224 | + |
226 | 225 | ;; Shorter vectors are less than longer vectors with same prefix |
227 | 226 | (is (= -1 (compare [1 2] [1 2 3]))) |
228 | 227 | (is (= 1 (compare [1 2 3] [1 2]))) |
229 | | - |
| 228 | + |
230 | 229 | ;; Nested vectors |
231 | 230 | (is (= -1 (compare [[1 2] [3 4]] [[1 2] [3 5]]))) |
232 | 231 | (is (= 0 (compare [[1 2] [3 4]] [[1 2] [3 4]]))) |
233 | | - |
| 232 | + |
234 | 233 | ;; SubVectors behave like vectors |
235 | 234 | (let [v [1 2 3 4 5] |
236 | 235 | sv1 (subvec v 1 3) ; [2 3] |
|
243 | 242 | ;; No namespace < with namespace |
244 | 243 | (is (= -1 (compare 'x 'a/x))) |
245 | 244 | (is (= 1 (compare 'a/x 'x))) |
246 | | - |
| 245 | + |
247 | 246 | ;; Different namespaces |
248 | 247 | (is (= -1 (compare 'a/x 'b/x))) |
249 | 248 | (is (= 1 (compare 'b/x 'a/x))) |
250 | | - |
| 249 | + |
251 | 250 | ;; Same namespace, different names |
252 | 251 | (is (= -1 (compare 'ns/a 'ns/b))) |
253 | 252 | (is (= 1 (compare 'ns/b 'ns/a))))) |
|
0 commit comments