diff options
author | Araq <rumpf_a@web.de> | 2011-03-03 02:01:22 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-03-03 02:01:22 +0100 |
commit | e424e13bd9d05ab3fbcbe3572e49c1bc611a1fc9 (patch) | |
tree | e0c60da1e1ddd60d040f02508deb554f687f23aa /tests/accept/compile | |
parent | f93ca8e42a7c987bf03606319d9dfcc0772684f5 (diff) | |
download | Nim-e424e13bd9d05ab3fbcbe3572e49c1bc611a1fc9.tar.gz |
various bugfixes for generics; added generic sort proc
Diffstat (limited to 'tests/accept/compile')
-rw-r--r-- | tests/accept/compile/tsortdev.nim | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/accept/compile/tsortdev.nim b/tests/accept/compile/tsortdev.nim new file mode 100644 index 000000000..488836cc7 --- /dev/null +++ b/tests/accept/compile/tsortdev.nim @@ -0,0 +1,69 @@ + +import math, algorithm + +proc sorted[T](a: openArray[T], order: TSortOrder): bool = + result = true + for i in 0 .. < a.high: + if cmp(a[i], a[i+1]) * order > 0: + echo "Out of order: ", a[i], " ", a[i+1] + result = false + +proc bubbleSort[T](a: var openArray[T], + cmp: proc (x, y: T): int = cmp, + order = TSortOrder.Ascending) = + while true: + var sorted = true + for i in 0 .. a.len-2: + if cmp(a[i], a[i+1]) * order > 0: + swap(a[i], a[i+1]) + sorted = false + if sorted: break + +when isMainModule: + proc main() = + const order = Ascending + var data: seq[string] = @[] + + for i in 0..10_000: + var L = random(59) + setLen(data, L) + for j in 0 .. L-1: + data[j] = $(math.random(90) - 10) + var copy = data + sort(data, cmp, order) + if not sorted(data, order): + #for x in items(data): echo x + break + else: + echo "SUCCESS!" + bubblesort(copy, cmp, order) + if copy.len != data.len: + quit "lengths differ!" + for i in 0 .. copy.high: + if copy[i] != data[i]: + quit "algorithms differ!" + + for i in 0..10_000: + var data: seq[int] = @[] + var L = random(59) + setLen(data, L) + for j in 0 .. L-1: + data[j] = (math.random(90) - 10) + var copy = data + sort(data, cmp[int, int], order) + if not sorted(data, order): + #for x in items(data): echo x + break + else: + echo "SUCCESS!" + bubblesort(copy) + if copy.len != data.len: + quit "lengths differ!" + for i in 0 .. copy.high: + if copy[i] != data[i]: + quit "algorithms differ!" + + main() + +echo "done" + |