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