-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathsort_test.c
158 lines (126 loc) · 4.03 KB
/
sort_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <test.h>
#include <cf3.defs.h>
#include <sort.h>
#include <rlist.h>
#include <item_lib.h>
/*
* Those testcases only perform smoke testing of sorting functionality.
*/
void test_sort_item_list_names(void)
{
Item *head = xcalloc(1, sizeof(Item));
head->name = xstrdup("c");
head->next = xcalloc(1, sizeof(Item));
head->next->name = xstrdup("b");
head->next->next = xcalloc(1, sizeof(Item));
head->next->next->name = xstrdup("a");
Item *sorted = SortItemListNames(head);
assert_string_equal(sorted->name, "a");
assert_string_equal(sorted->next->name, "b");
assert_string_equal(sorted->next->next->name, "c");
assert_int_equal(sorted->next->next->next, NULL);
DeleteItemList(sorted);
}
void test_sort_item_list_classes(void)
{
Item *head = xcalloc(1, sizeof(Item));
head->classes = xstrdup("b");
head->next = xcalloc(1, sizeof(Item));
head->next->classes = xstrdup("c");
head->next->next = xcalloc(1, sizeof(Item));
head->next->next->classes = xstrdup("a");
Item *sorted = SortItemListClasses(head);
assert_string_equal(sorted->classes, "a");
assert_string_equal(sorted->next->classes, "b");
assert_string_equal(sorted->next->next->classes, "c");
assert_int_equal(sorted->next->next->next, NULL);
DeleteItemList(sorted);
}
void test_sort_item_list_counters(void)
{
Item *head = xcalloc(1, sizeof(Item));
head->counter = -1;
head->next = xcalloc(1, sizeof(Item));
head->next->counter = 42;
head->next->next = xcalloc(1, sizeof(Item));
head->next->next->counter = 146;
Item *sorted = SortItemListCounters(head);
/* Weird. Counters are sorted backwards */
assert_int_equal(sorted->counter, 146);
assert_int_equal(sorted->next->counter, 42);
assert_int_equal(sorted->next->next->counter, -1);
assert_int_equal(sorted->next->next->next, NULL);
DeleteItemList(sorted);
}
void test_sort_item_list_times(void)
{
Item *head = xcalloc(1, sizeof(Item));
head->time = 1;
head->next = xcalloc(1, sizeof(Item));
head->next->time = 1998;
head->next->next = xcalloc(1, sizeof(Item));
head->next->next->time = 4000;
Item *sorted = SortItemListCounters(head);
assert_int_equal(sorted->time, 4000);
assert_int_equal(sorted->next->time, 1998);
assert_int_equal(sorted->next->next->time, 1);
assert_int_equal(sorted->next->next->next, NULL);
DeleteItemList(sorted);
}
bool FirstItemShorter(const char *lhs, const char *rhs)
{
return (strlen(lhs) < strlen(rhs));
}
void test_sort_rlist(void)
{
Rlist *list = NULL;
RlistAppendScalar(&list, "bbb");
RlistAppendScalar(&list, "cc");
RlistAppendScalar(&list, "a");
Rlist *sorted = SortRlist(list, &FirstItemShorter);
assert_string_equal(RlistScalarValue(sorted), "a");
assert_string_equal(RlistScalarValue(sorted->next), "cc");
assert_string_equal(RlistScalarValue(sorted->next->next), "bbb");
assert_int_equal(sorted->next->next->next, NULL);
RlistDestroy(sorted);
}
void test_alpha_sort_rlist_names(void)
{
Rlist *list = NULL;
RlistAppendScalar(&list, "c");
RlistAppendScalar(&list, "a");
RlistAppendScalar(&list, "b");
Rlist *sorted = AlphaSortRListNames(list);
assert_string_equal(RlistScalarValue(sorted), "a");
assert_string_equal(RlistScalarValue(sorted->next), "b");
assert_string_equal(RlistScalarValue(sorted->next->next), "c");
assert_int_equal(sorted->next->next->next, NULL);
RlistDestroy(sorted);
}
int main()
{
PRINT_TEST_BANNER();
const UnitTest tests[] =
{
unit_test(test_sort_item_list_names),
unit_test(test_sort_item_list_classes),
unit_test(test_sort_item_list_counters),
unit_test(test_sort_item_list_times),
unit_test(test_sort_rlist),
unit_test(test_alpha_sort_rlist_names),
};
return run_tests(tests);
}
/* STUBS */
/*
void __ProgrammingError(const char *file, int lineno, const char *format, ...)
{
fail();
exit(42);
}
void FatalError(char *s, ...)
{
fail();
exit(42);
}
*/