-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
65 lines (48 loc) · 1.53 KB
/
sort.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "sort.h"
#ifdef TEST
#include "test.h"
typedef struct sort_test_struct
{
int a;
int b;
} sort_test_struct;
void sort_test()
{
int array1[3] = {3, 2, 1};
sort(array1, {return a < b;});
test_assert(array1[0], 1, 'd', TEST_EQUAL);
test_assert(array1[1], 2, 'd', TEST_EQUAL);
test_assert(array1[2], 3, 'd', TEST_EQUAL);
sort(array1, {return a > b;});
test_assert(array1[0], 3, 'd', TEST_EQUAL);
test_assert(array1[1], 2, 'd', TEST_EQUAL);
test_assert(array1[2], 1, 'd', TEST_EQUAL);
char *array2[] = {"c", "b", "a"};
sort(array2, {return strcmp(a, b) < 0;});
test_assert(array2[0], "a", 's', TEST_EQUAL);
test_assert(array2[1], "b", 's', TEST_EQUAL);
test_assert(array2[2], "c", 's', TEST_EQUAL);
sort(array2, {return strcmp(a, b) > 0;});
test_assert(array2[0], "c", 's', TEST_EQUAL);
test_assert(array2[1], "b", 's', TEST_EQUAL);
test_assert(array2[2], "a", 's', TEST_EQUAL);
sort_test_struct *array3[3];
int i;
for(i = 0; i < 3; i++)
{
array3[i] = calloc(1, sizeof(sort_test_struct));
array3[i]->a = 2 - i;
array3[i]->b = i;
}
sort(array3, {return a->a < b->a;});
test_assert(array3[0]->a, 0, 'd', TEST_EQUAL);
test_assert(array3[1]->a, 1, 'd', TEST_EQUAL);
test_assert(array3[2]->a, 2, 'd', TEST_EQUAL);
sort(array3, {return a->b < b->b;});
test_assert(array3[0]->b, 0, 'd', TEST_EQUAL);
test_assert(array3[1]->b, 1, 'd', TEST_EQUAL);
test_assert(array3[2]->b, 2, 'd', TEST_EQUAL);
for(i = 0; i < 3; i++)
free(array3[i]);
}
#endif