File: sorttest.5c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (42 lines) | stat: -rw-r--r-- 858 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Make sure sorting works */

autoimport PRNG;
autoimport Sort;

bool ordered(real[] values)
{
    int d = dim(values);
    if (d == 0)
	return true;
    real a = values[0];
    for (int i = 1; i < d; i++) {
	if (a > values[i])
	    return false;
	a = values[i];
    }
    return true;
}

void check(&real[] values, string sort)
{
    if (ordered(values))
	return;
    abort("check failed %v not ordered by %s", values, sort);
}

bool int_gt(int a, int b) = a > b;

for (int i = 0; i < 25; i++) {
    int len = randint(1000);
    printf("test %d len %d\n", i, len);
    real[*] v = Sort::randomints(len, 1000000);
    real[*] qv = v;
    real[*] mv = v;
    real[*] gv = v;
    Sort::qsort(&qv, int_gt);
    check(&qv, "qsort");
    Sort::mergesort(&mv, int_gt);
    check(&mv, "mergesort");
    Sort::gnomesort(&gv, int_gt);
    check(&gv, "gnomesort");
}