summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/algorithm.nim31
-rw-r--r--tests/stdlib/tsortcall.nim62
2 files changed, 70 insertions, 23 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim
index 7f3d5e936..a5b18ae58 100644
--- a/lib/pure/algorithm.nim
+++ b/lib/pure/algorithm.nim
@@ -297,15 +297,10 @@ func sort*[T](a: var openArray[T],
       dec(m, s*2)
     s = s*2
 
-func sort*[T](a: var openArray[T], order = SortOrder.Ascending) =
-  ## sorts an openarray in-place with a default lexicographical ordering.
-  runnableExamples:
-    var s = @[1,3,2,5,4]
-    s.sort
-    doAssert s == @[1,2,3,4,5]
-  sort(a, system.cmp, order)
+proc sort*[T](a: var openArray[T], order = SortOrder.Ascending) = sort[T](a, system.cmp[T], order)
+  ## Shortcut version of ``sort`` that uses ``system.cmp[T]`` as the comparison function.
 
-func sorted*[T](a: openArray[T], cmp: proc(x, y: T): int {.closure.},
+proc sorted*[T](a: openArray[T], cmp: proc(x, y: T): int {.closure.},
                 order = SortOrder.Ascending): seq[T] =
   ## returns ``a`` sorted by ``cmp`` in the specified ``order``.
   runnableExamples:
@@ -320,14 +315,9 @@ func sorted*[T](a: openArray[T], cmp: proc(x, y: T): int {.closure.},
     result[i] = a[i]
   sort(result, cmp, order)
 
-func sorted*[T](a: openArray[T], order = SortOrder.Ascending): seq[T] =
-  ## returns ``a`` sorted with default lexicographical ordering.
-  runnableExamples:
-    let orig = @[2,3,1,2]
-    let copy = orig.sorted()
-    doAssert orig == @[2,3,1,2]
-    doAssert copy == @[1,2,2,3]
-  return sorted(a, system.cmp, order)
+proc sorted*[T](a: openArray[T], order = SortOrder.Ascending): seq[T] =
+  ## Shortcut version of ``sorted`` that uses ``system.cmp[T]`` as the comparison function.
+  sorted[T](a, system.cmp[T], order)
 
 template sortedByIt*(seq1, op: untyped): untyped =
   ## Convenience template around the ``sorted`` proc to reduce typing.
@@ -373,12 +363,9 @@ func isSorted*[T](a: openArray[T],
     if cmp(a[i],a[i+1]) * order > 0:
       return false
 
-func isSorted*[T](a: openArray[T], order = SortOrder.Ascending): bool =
-  ## checks whether ``a`` is sorted with a default lexicographical ordering.
-  runnableExamples:
-    let test = @[1,1,2,3,5,8]
-    doAssert test.isSorted()
-  result = isSorted(a, system.cmp, order)
+proc isSorted*[T](a: openarray[T], order = SortOrder.Ascending): bool =
+  ## Shortcut version of ``isSorted`` that uses ``system.cmp[T]`` as the comparison function.
+  isSorted(a, system.cmp[T], order)
 
 proc product*[T](x: openArray[seq[T]]): seq[seq[T]] =
   ## produces the Cartesian product of the array. Warning: complexity
diff --git a/tests/stdlib/tsortcall.nim b/tests/stdlib/tsortcall.nim
index efe1d0b8b..45b98805f 100644
--- a/tests/stdlib/tsortcall.nim
+++ b/tests/stdlib/tsortcall.nim
@@ -1,5 +1,65 @@
 import algorithm
+import unittest
 
-proc foosort(ships: var seq[int]) = sort(ships, system.cmp[int])
 
+suite "test sort, sorted, and isSorted procs":
+  proc foosort(ships: var seq[int]) = sort(ships, system.cmp[int])
 
+  type
+    User = object
+      name: string
+      age: int
+
+  func newUser(name: string, age: int): User =
+    result.name = name
+    result.age = age
+
+  proc compareUsers(x, y: User): int =
+    if x.age == y.age: return 0
+    if x.age < y.age: return -1
+    return 1
+
+  setup:
+    var
+      unSortedIntSeq = @[1, 4, 3, 5, -1]
+      unSortedUserSeq = @[newUser("Andreas", 34), newUser("Alice", 12), newUser("Bob", 23)]
+
+    let
+      sortedIntSeq = @[-1, 1, 3, 4, 5]
+      sortedUserSeq = @[newUser("Alice", 12), newUser("Bob", 23), newUser("Andreas", 34)]
+
+  test "test the shortcut versions of sort, sorted, and isSorted":
+    check(not unSortedIntSeq.isSorted)
+    check sorted(unSortedIntSeq) == sortedIntSeq
+    check sorted(unSortedIntSeq).isSorted
+
+    unSortedIntSeq.sort()
+    check unSortedIntSeq == sortedIntSeq
+    check unSortedIntSeq.isSorted
+
+  test "test the shortcut versions with descending sort order":
+    check(not unSortedIntSeq.isSorted(SortOrder.Descending))
+    check sorted(unSortedIntSeq, SortOrder.Descending) == reversed sortedIntSeq
+    check sorted(unSortedIntSeq).isSorted(SortOrder.Descending)
+
+    unSortedIntSeq.sort(SortOrder.Descending)
+    check unSortedIntSeq == reversed sortedIntSeq
+    check unSortedIntSeq.isSorted(SortOrder.Descending)
+
+  test "test the versions that accept a custom compareUsers function":
+    check(not unSortedUserSeq.isSorted(compareUsers))
+    check sorted(unSortedUserSeq, compareUsers) == sortedUserSeq
+    check sorted(unSortedUserSeq, compareUsers).isSorted(compareUsers)
+
+    unSortedUserSeq.sort(compareUsers)
+    check unSortedUserSeq == sortedUserSeq
+    check unSortedUserSeq.isSorted(compareUsers)
+
+  test "test the long versions with descending sort order":
+    check(not unSortedUserSeq.isSorted(compareUsers, SortOrder.Descending))
+    check sorted(unSortedUserSeq, compareUsers, SortOrder.Descending) == reversed sortedUserSeq
+    check sorted(unSortedUserSeq, compareUsers,
+                  SortOrder.Descending).isSorted(compareUsers, SortOrder.Descending)
+    unSortedUserSeq.sort(compareUsers, SortOrder.Descending)
+    check unSortedUserSeq == reversed sortedUserSeq
+    check unSortedUserSeq.isSorted(compareUsers, SortOrder.Descending)