|
3 | 3 | #include <stdio.h> |
4 | 4 | #include <string.h> |
5 | 5 |
|
6 | | -static const char *arr[] = { "xyz", "abc", "ghi", "def" }; |
| 6 | +struct record { |
| 7 | + int key; |
| 8 | + char value[12]; |
| 9 | +}; |
7 | 10 |
|
8 | | -static const size_t magic = 0xDEADBEEF; |
| 11 | +struct sort_context { |
| 12 | + int descending; |
| 13 | + unsigned int calls; |
| 14 | + unsigned int magic; |
| 15 | +}; |
9 | 16 |
|
10 | | -static int cmpstringp(const void *p1, const void *p2, void *ctx) { |
11 | | - /* The actual arguments to this function are "pointers to |
12 | | - * pointers to char", but strcmp(3) arguments are "pointers |
13 | | - * to char", hence the following cast plus dereference. */ |
14 | | - assert(*(size_t *) ctx == magic); |
15 | | - return strcmp(*(const char **) p1, *(const char **) p2); |
| 17 | +static int compare_ints(const void *p1, const void *p2) { |
| 18 | + int a = *(const int *) p1; |
| 19 | + int b = *(const int *) p2; |
| 20 | + if (a < b) |
| 21 | + return -100; |
| 22 | + if (a > b) |
| 23 | + return 100; |
| 24 | + return 0; |
16 | 25 | } |
17 | 26 |
|
18 | | -int main() { |
19 | | - qsort_r(&arr[0], sizeof(arr) / sizeof(*arr), sizeof(char *), cmpstringp, (void *) &magic); |
| 27 | +static int compare_bytes(const void *p1, const void *p2) { |
| 28 | + unsigned char a = *(const unsigned char *) p1; |
| 29 | + unsigned char b = *(const unsigned char *) p2; |
| 30 | + if (a < b) |
| 31 | + return -100; |
| 32 | + if (a > b) |
| 33 | + return 100; |
| 34 | + return 0; |
| 35 | +} |
| 36 | + |
| 37 | +static int compare_records(const void *p1, const void *p2, void *ctx) { |
| 38 | + const struct record *a = p1; |
| 39 | + const struct record *b = p2; |
| 40 | + struct sort_context *context = ctx; |
| 41 | + assert(context->magic == 0xDEADBEEF); |
| 42 | + context->calls++; |
| 43 | + |
| 44 | + if (a->key == b->key) |
| 45 | + return 0; |
| 46 | + if (context->descending) |
| 47 | + return a->key < b->key ? 100 : -100; |
| 48 | + return a->key < b->key ? -100 : 100; |
| 49 | +} |
| 50 | + |
| 51 | +static int compare_strings(const void *p1, const void *p2, void *ctx) { |
| 52 | + struct sort_context *context = ctx; |
| 53 | + assert(context->magic == 0xDEADBEEF); |
| 54 | + context->calls++; |
| 55 | + return strcmp(*(const char *const *) p1, *(const char *const *) p2); |
| 56 | +} |
| 57 | + |
| 58 | +static void assert_record_values(const struct record *records, size_t count) { |
| 59 | + static const char *expected[] = { "seven", "minus3", "four", "four-b", "zero", "nineteen" }; |
| 60 | + for (size_t i = 0; i < sizeof(expected) / sizeof(expected[0]); i++) { |
| 61 | + int found = 0; |
| 62 | + for (size_t j = 0; j < count; j++) { |
| 63 | + if (!strcmp(records[j].value, expected[i])) { |
| 64 | + found = 1; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + assert(found); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +static void test_qsort(void) { |
| 73 | + // ISO C qsort(): ordinary integer ordering, duplicates, and non-trivial |
| 74 | + // comparator return values (the comparator need not return +/-1). |
| 75 | + int values[] = { 7, -3, 4, 4, 0, 19, -10, 2 }; |
| 76 | + const int expected[] = { -10, -3, 0, 2, 4, 4, 7, 19 }; |
| 77 | + qsort(values, sizeof(values) / sizeof(values[0]), sizeof(values[0]), compare_ints); |
| 78 | + assert(!memcmp(values, expected, sizeof(values))); |
| 79 | + |
| 80 | + // Sorting an already reverse-ordered input exercises repeated partitioning. |
| 81 | + int reverse[] = { 5, 4, 3, 2, 1, 0 }; |
| 82 | + qsort(reverse, sizeof(reverse) / sizeof(reverse[0]), sizeof(reverse[0]), compare_ints); |
| 83 | + for (size_t i = 0; i < sizeof(reverse) / sizeof(reverse[0]); i++) |
| 84 | + assert(reverse[i] == (int) i); |
20 | 85 |
|
21 | | - assert(!strcmp(arr[0], "abc")); |
22 | | - assert(!strcmp(arr[1], "def")); |
23 | | - assert(!strcmp(arr[2], "ghi")); |
24 | | - assert(!strcmp(arr[3], "xyz")); |
| 86 | + // qsort() must honor arbitrary element sizes, including one-byte objects. |
| 87 | + unsigned char bytes[] = { 3, 1, 2, 0, 255 }; |
| 88 | + qsort(bytes, sizeof(bytes), sizeof(bytes[0]), compare_bytes); |
| 89 | + assert(bytes[0] == 0 && bytes[1] == 1 && bytes[2] == 2 && bytes[3] == 3 && bytes[4] == 255); |
| 90 | + |
| 91 | + // A one-element range requires no comparisons or swaps. |
| 92 | + int one[] = { 42 }; |
| 93 | + qsort(one, 1, sizeof(one[0]), compare_ints); |
| 94 | + assert(one[0] == 42); |
| 95 | + |
| 96 | + // A zero-count call must not access or modify the base object. |
| 97 | + int empty[1] = { 42 }; |
| 98 | + qsort(empty, 0, sizeof(empty[0]), compare_ints); |
| 99 | + assert(empty[0] == 42); |
| 100 | +} |
25 | 101 |
|
26 | | - for(size_t i = 0; i < sizeof(arr) / sizeof(*arr); i++) { |
27 | | - fprintf(stderr, "%s\n", arr[i]); |
| 102 | +static void test_qsort_r(void) { |
| 103 | + // POSIX qsort_r(): the callback receives a caller-provided |
| 104 | + // context, and sorting must move complete multi-field records. |
| 105 | + struct { |
| 106 | + unsigned char before[16]; |
| 107 | + struct record records[6]; |
| 108 | + unsigned char after[16]; |
| 109 | + } guarded = { |
| 110 | + { 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, |
| 111 | + 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5 }, |
| 112 | + { |
| 113 | + { 7, "seven" }, { -3, "minus3" }, { 4, "four" }, |
| 114 | + { 4, "four-b" }, { 0, "zero" }, { 19, "nineteen" } |
| 115 | + }, |
| 116 | + { 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, |
| 117 | + 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A } |
| 118 | + }; |
| 119 | + struct sort_context context = { 0, 0, 0xDEADBEEF }; |
| 120 | + qsort_r(guarded.records, sizeof(guarded.records) / sizeof(guarded.records[0]), |
| 121 | + sizeof(guarded.records[0]), |
| 122 | + compare_records, &context); |
| 123 | + assert(context.calls > 0); |
| 124 | + for (size_t i = 1; i < sizeof(guarded.records) / sizeof(guarded.records[0]); i++) |
| 125 | + assert(guarded.records[i - 1].key <= guarded.records[i].key); |
| 126 | + assert_record_values(guarded.records, sizeof(guarded.records) / sizeof(guarded.records[0])); |
| 127 | + |
| 128 | + // Reuse the same context to verify that callback state can select a |
| 129 | + // different ordering direction. |
| 130 | + context.descending = 1; |
| 131 | + context.calls = 0; |
| 132 | + qsort_r(guarded.records, sizeof(guarded.records) / sizeof(guarded.records[0]), |
| 133 | + sizeof(guarded.records[0]), |
| 134 | + compare_records, &context); |
| 135 | + assert(context.calls > 0); |
| 136 | + for (size_t i = 1; i < sizeof(guarded.records) / sizeof(guarded.records[0]); i++) |
| 137 | + assert(guarded.records[i - 1].key >= guarded.records[i].key); |
| 138 | + assert_record_values(guarded.records, sizeof(guarded.records) / sizeof(guarded.records[0])); |
| 139 | + for (size_t i = 0; i < sizeof(guarded.before); i++) { |
| 140 | + assert(guarded.before[i] == 0xA5); |
| 141 | + assert(guarded.after[i] == 0x5A); |
28 | 142 | } |
29 | 143 |
|
| 144 | + // qsort_r() also handles pointer-sized elements and string comparators. |
| 145 | + char *strings[] = { "xyz", "abc", "ghi", "def" }; |
| 146 | + context.descending = 0; |
| 147 | + context.calls = 0; |
| 148 | + qsort_r(strings, sizeof(strings) / sizeof(strings[0]), sizeof(strings[0]), |
| 149 | + compare_strings, &context); |
| 150 | + assert(context.calls > 0); |
| 151 | + assert(!strcmp(strings[0], "abc")); |
| 152 | + assert(!strcmp(strings[1], "def")); |
| 153 | + assert(!strcmp(strings[2], "ghi")); |
| 154 | + assert(!strcmp(strings[3], "xyz")); |
| 155 | + |
| 156 | + // As with qsort(), a zero-count range must not invoke the comparator or |
| 157 | + // modify the element passed as the base address. |
| 158 | + struct record empty[] = { { 42, "unchanged" } }; |
| 159 | + context.calls = 0; |
| 160 | + qsort_r(empty, 0, sizeof(empty[0]), compare_records, &context); |
| 161 | + assert(context.calls == 0); |
| 162 | + assert(empty[0].key == 42); |
| 163 | + assert(!strcmp(empty[0].value, "unchanged")); |
| 164 | +} |
| 165 | + |
| 166 | +int main() { |
| 167 | + test_qsort(); |
| 168 | + test_qsort_r(); |
30 | 169 | return 0; |
31 | 170 | } |
0 commit comments