From fde4a086c5be2140b3b39014b302f681c87cb33c Mon Sep 17 00:00:00 2001 From: Konstantin Molchanov Date: Fri, 12 Oct 2018 00:51:23 +0400 Subject: 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. --- tests/stdlib/tsortcall.nim | 62 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) (limited to 'tests/stdlib/tsortcall.nim') 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) -- cgit 1.4.1-2-gfad0