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 #include <stdio.h> 34 #include <stdlib.h> 35 36 #include "test-sort.h" 37 38 #define THUNK 42 39 40 static int 41 sorthelp_r(const void *a, const void *b, void *thunk) 42 { 43 const int *oa, *ob; 44 45 ATF_REQUIRE_EQ(*(int *)thunk, THUNK); 46 47 oa = a; 48 ob = b; 49 /* Don't use "return *oa - *ob" since it's easy to cause overflow! */ 50 if (*oa > *ob) 51 return (1); 52 if (*oa < *ob) 53 return (-1); 54 return (0); 55 } 56 57 ATF_TC_WITHOUT_HEAD(qsort_r_test); 58 ATF_TC_BODY(qsort_r_test, tc) 59 { 60 int testvector[IVEC_LEN]; 61 int sresvector[IVEC_LEN]; 62 int i, j; 63 int thunk = THUNK; 64 65 for (j = 2; j < IVEC_LEN; j++) { 66 /* Populate test vectors */ 67 for (i = 0; i < j; i++) 68 testvector[i] = sresvector[i] = initvector[i]; 69 70 /* Sort using qsort_r(3) */ 71 qsort_r(testvector, j, sizeof(testvector[0]), sorthelp_r, 72 &thunk); 73 /* Sort using reference slow sorting routine */ 74 ssort(sresvector, j); 75 76 /* Compare results */ 77 for (i = 0; i < j; i++) 78 ATF_CHECK_MSG(testvector[i] == sresvector[i], 79 "item at index %d didn't match: %d != %d", 80 i, testvector[i], sresvector[i]); 81 } 82 } 83 84 ATF_TP_ADD_TCS(tp) 85 { 86 87 ATF_TP_ADD_TC(tp, qsort_r_test); 88 89 return (atf_no_error()); 90 } 91