Skip to content

Commit 51f2067

Browse files
committed
Add more sort-by tests
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent 60d90fa commit 51f2067

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/glojure/test_glojure/sort.glj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
(is (= '(1 2 3 4 5) (sort [3 1 4 2 5])))
1212
(is (= '(1 1 3 4 5) (sort [3 1 4 1 5]))) ; duplicates preserved
1313
(is (= '(1.0 2.5 3 4.7) (sort [4.7 1.0 3 2.5]))) ; mixed numeric types
14+
(is (= '(-5 0 1.5 2 3.14 10) (sort [3.14 2 1.5 10 -5 0])))
1415

1516
;; Strings
1617
(is (= '("apple" "banana" "cherry") (sort ["cherry" "apple" "banana"])))
@@ -251,5 +252,40 @@
251252
(is (= -1 (compare 'ns/a 'ns/b)))
252253
(is (= 1 (compare 'ns/b 'ns/a)))))
253254

255+
(deftest sort-by-clojuredocs-examples
256+
(testing "Examples from ClojureDocs sort-by documentation"
257+
258+
(let [words ["banana" "apple" "cherry" "date"]]
259+
(is (= '("date" "apple" "banana" "cherry")
260+
(sort-by count words))))
261+
262+
(let [words ["banana" "apple" "cherry" "date"]]
263+
(is (= (sort-by count > words)
264+
'("banana" "cherry" "apple" "date"))))
265+
266+
(let [people [{:name "Alice" :age 30 :city "NYC"}
267+
{:name "Bob" :age 25 :city "LA"}
268+
{:name "Charlie" :age 35 :city "NYC"}
269+
{:name "David" :age 25 :city "LA"}]]
270+
(is (= (sort-by (juxt :city :age) people)
271+
'({:name "Bob" :age 25 :city "LA"}
272+
{:name "David" :age 25 :city "LA"}
273+
{:name "Alice" :age 30 :city "NYC"}
274+
{:name "Charlie" :age 35 :city "NYC"}))))
275+
276+
(let [numbers [3 1 4 1 5 9 2 6]]
277+
(is (= '(3 9 6 1 4 1 5 2)
278+
(sort-by #(mod % 3) numbers))))
279+
280+
(let [items [nil "hello" 42 :keyword]]
281+
(is (= (sort-by str items)
282+
'(nil 42 :keyword "hello"))))
283+
284+
(is (= (sort-by identity []) '()))
285+
(is (= (sort-by count []) '()))
286+
287+
(is (= (sort-by identity [42]) '(42)))
288+
(is (= (sort-by count ["hello"]) '("hello")))))
289+
254290
;; Run tests
255291
(run-tests)

0 commit comments

Comments
 (0)