1 // Check that a qsort() comparator that calls qsort() works as expected
2 // RUN: %clangxx -O2 %s -o %t
3 // RUN: %run %t 2>&1 | FileCheck %s
11 int global_array
[12] = {7, 11, 9, 10, 1, 2, 4, 3, 6, 5, 8, 12};
13 #define array_size(x) (sizeof(x) / sizeof(x[0]))
15 int ascending_compare_ints(const void *a
, const void *b
) {
16 return *(const int *)a
- *(const int *)b
;
19 int descending_compare_ints(const void *a
, const void *b
) {
20 // Add another qsort() call to check more than one level of recursion
21 qsort(global_array
, array_size(global_array
), sizeof(int), &ascending_compare_ints
);
22 return *(const int *)b
- *(const int *)a
;
25 int sort_and_compare(const void *a
, const void *b
) {
26 struct Foo
*f1
= (struct Foo
*)a
;
27 struct Foo
*f2
= (struct Foo
*)b
;
28 printf("sort_and_compare({%d, %d}, {%d, %d})\n", f1
->array
[0], f1
->array
[1],
29 f2
->array
[0], f2
->array
[1]);
30 // Call qsort from within qsort() to check that interceptors handle this case:
31 qsort(&f1
->array
, array_size(f1
->array
), sizeof(int), &descending_compare_ints
);
32 qsort(&f2
->array
, array_size(f2
->array
), sizeof(int), &descending_compare_ints
);
33 // Sort by second array element:
34 return f1
->array
[1] - f2
->array
[1];
38 // Note: 16 elements should be large enough to trigger a recursive qsort() call.
39 struct Foo qsortArg
[16] = {
57 // Sort the individual arrays in descending order and the over all struct
58 // Foo array in ascending order of the second array element.
59 qsort(qsortArg
, array_size(qsortArg
), sizeof(qsortArg
[0]), &sort_and_compare
);
61 printf("Sorted result:");
62 for (const auto &f
: qsortArg
) {
63 printf(" {%d,%d}", f
.array
[0], f
.array
[1]);
66 // CHECK: Sorted result: {1,0} {99,1} {3,2} {3,3} {11,4} {17,5} {8,6} {9,7} {13,8} {21,12} {17,16} {32,23} {42,41} {43,42} {45,44} {99,98}
67 printf("Sorted global_array:");
68 for (int i
: global_array
) {
72 // CHECK: Sorted global_array: 1 2 3 4 5 6 7 8 9 10 11 12