8684 add shortcut sort procs (#9174)
* Stdlib: Algorithm: Add shortcut versions of sort, sorted, and isSorted procs. * Add tests for sort, sorted, and isSorted procs from algorithm module. * Merge sort tests into tsortcall.nim, remove tsort.nim. * Stdlib: Algorithm: Add shortcut versions of sort, sorted, and isSorted procs. * Add tests for sort, sorted, and isSorted procs from algorithm module. * Merge sort tests into tsortcall.nim, remove tsort.nim.
This commit is contained in:
parent
10f5f67767
commit
fde4a086c5
2 changed files with 71 additions and 24 deletions
|
|
@ -1,5 +1,65 @@
|
|||
import algorithm
|
||||
|
||||
proc foosort(ships: var seq[int]) = sort(ships, system.cmp[int])
|
||||
import unittest
|
||||
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue