/* glibc quick sort test originally by Qualys, enhanced for Rocky Linux */ #include #include #include #include #include static int cmp(const void * const pa, const void * const pb) { const int a = *(const int *)pa; const int b = *(const int *)pb; return (a - b); } int main(const int argc, const char * const argv[]) { if (argc != 2) return __LINE__; const size_t nmemb = strtoul(argv[1], NULL, 0); if (nmemb <= 0 || nmemb >= (1<<28)) return __LINE__; int * const pcanary1 = calloc(1 + nmemb + 1, sizeof(int)); if (!pcanary1) return __LINE__; int * const array = pcanary1 + 1; int * const pcanary2 = array + nmemb; struct timeval tv; if (gettimeofday(&tv, NULL)) return __LINE__; srandom((tv.tv_sec << 16) ^ tv.tv_usec); const int canary1 = *pcanary1 = (random() << 16) ^ random(); const int canary2 = *pcanary2 = (random() << 16) ^ random(); array[random() % nmemb] = INT_MIN; /* Force fallback from merge to quick sort */ const struct rlimit rlim = {}; setrlimit(RLIMIT_AS, &rlim); qsort(array, nmemb, sizeof(int), cmp); if (*pcanary1 != canary1) abort(); if (*pcanary2 != canary2) abort(); for (size_t i = 0; i < nmemb; i++) array[i] = random(); qsort(array, nmemb, sizeof(int), cmp); if (*pcanary1 != canary1) abort(); if (*pcanary2 != canary2) abort(); for (size_t i = 1; i < nmemb; i++) if (array[i - 1] > array[i]) abort(); puts("PASSED"); return 0; }