]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/tests/stdlib/qsort_r_test.c
Merge ^/vendor/llvm-openmp/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / lib / libc / tests / stdlib / qsort_r_test.c
1 /*-
2  * Copyright (C) 2020 Edward Tomasz Napierala <trasz@FreeBSD.org>
3  * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * Test for qsort_r(3) routine.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "test-sort.h"
39
40 #define THUNK 42
41
42 static int
43 sorthelp_r(void *thunk, const void *a, const void *b)
44 {
45         const int *oa, *ob;
46
47         ATF_REQUIRE_EQ(*(int *)thunk, THUNK);
48
49         oa = a;
50         ob = b;
51         /* Don't use "return *oa - *ob" since it's easy to cause overflow! */
52         if (*oa > *ob)
53                 return (1);
54         if (*oa < *ob)
55                 return (-1);
56         return (0);
57 }
58
59 ATF_TC_WITHOUT_HEAD(qsort_r_test);
60 ATF_TC_BODY(qsort_r_test, tc)
61 {
62         int testvector[IVEC_LEN];
63         int sresvector[IVEC_LEN];
64         int i, j;
65         int thunk = THUNK;
66
67         for (j = 2; j < IVEC_LEN; j++) {
68                 /* Populate test vectors */
69                 for (i = 0; i < j; i++)
70                         testvector[i] = sresvector[i] = initvector[i];
71
72                 /* Sort using qsort_r(3) */
73                 qsort_r(testvector, j, sizeof(testvector[0]), &thunk,
74                     sorthelp_r);
75                 /* Sort using reference slow sorting routine */
76                 ssort(sresvector, j);
77
78                 /* Compare results */
79                 for (i = 0; i < j; i++)
80                         ATF_CHECK_MSG(testvector[i] == sresvector[i],
81                             "item at index %d didn't match: %d != %d",
82                             i, testvector[i], sresvector[i]);
83         }
84 }
85
86 ATF_TP_ADD_TCS(tp)
87 {
88
89         ATF_TP_ADD_TC(tp, qsort_r_test);
90
91         return (atf_no_error());
92 }