xref: /freebsd/lib/libc/tests/stdlib/qsort_test.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*98682851SEnji Cooper /*-
2*98682851SEnji Cooper  * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org>
3*98682851SEnji Cooper  * All rights reserved.
4*98682851SEnji Cooper  *
5*98682851SEnji Cooper  * Redistribution and use in source and binary forms, with or without
6*98682851SEnji Cooper  * modification, are permitted provided that the following conditions
7*98682851SEnji Cooper  * are met:
8*98682851SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
9*98682851SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
10*98682851SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
11*98682851SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
12*98682851SEnji Cooper  *    documentation and/or other materials provided with the distribution.
13*98682851SEnji Cooper  *
14*98682851SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*98682851SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*98682851SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*98682851SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*98682851SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*98682851SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*98682851SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*98682851SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*98682851SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*98682851SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*98682851SEnji Cooper  * SUCH DAMAGE.
25*98682851SEnji Cooper  */
26*98682851SEnji Cooper 
27*98682851SEnji Cooper /*
28*98682851SEnji Cooper  * Test for qsort() routine.
29*98682851SEnji Cooper  */
30*98682851SEnji Cooper 
31*98682851SEnji Cooper #include <assert.h>
32*98682851SEnji Cooper #include <stdio.h>
33*98682851SEnji Cooper #include <stdlib.h>
34*98682851SEnji Cooper 
35*98682851SEnji Cooper #include "test-sort.h"
36*98682851SEnji Cooper 
37*98682851SEnji Cooper ATF_TC_WITHOUT_HEAD(qsort_test);
ATF_TC_BODY(qsort_test,tc)38*98682851SEnji Cooper ATF_TC_BODY(qsort_test, tc)
39*98682851SEnji Cooper {
40*98682851SEnji Cooper 	int testvector[IVEC_LEN];
41*98682851SEnji Cooper 	int sresvector[IVEC_LEN];
42*98682851SEnji Cooper 	int i, j;
43*98682851SEnji Cooper 
44*98682851SEnji Cooper 	for (j = 2; j < IVEC_LEN; j++) {
45*98682851SEnji Cooper 		/* Populate test vectors */
46*98682851SEnji Cooper 		for (i = 0; i < j; i++)
47*98682851SEnji Cooper 			testvector[i] = sresvector[i] = initvector[i];
48*98682851SEnji Cooper 
49*98682851SEnji Cooper 		/* Sort using qsort(3) */
50*98682851SEnji Cooper 		qsort(testvector, j, sizeof(testvector[0]), sorthelp);
51*98682851SEnji Cooper 		/* Sort using reference slow sorting routine */
52*98682851SEnji Cooper 		ssort(sresvector, j);
53*98682851SEnji Cooper 
54*98682851SEnji Cooper 		/* Compare results */
55*98682851SEnji Cooper 		for (i = 0; i < j; i++)
56*98682851SEnji Cooper 			ATF_CHECK_MSG(testvector[i] == sresvector[i],
57*98682851SEnji Cooper 			    "item at index %d didn't match: %d != %d",
58*98682851SEnji Cooper 			    i, testvector[i], sresvector[i]);
59*98682851SEnji Cooper 	}
60*98682851SEnji Cooper }
61*98682851SEnji Cooper 
ATF_TP_ADD_TCS(tp)62*98682851SEnji Cooper ATF_TP_ADD_TCS(tp)
63*98682851SEnji Cooper {
64*98682851SEnji Cooper 
65*98682851SEnji Cooper 	ATF_TP_ADD_TC(tp, qsort_test);
66*98682851SEnji Cooper 
67*98682851SEnji Cooper 	return (atf_no_error());
68*98682851SEnji Cooper }
69