heapsort_test.c revision 290538
1131099Ssobomax/*- 2131099Ssobomax * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> 3131099Ssobomax * All rights reserved. 4131099Ssobomax * 5131099Ssobomax * Redistribution and use in source and binary forms, with or without 6131099Ssobomax * modification, are permitted provided that the following conditions 7131099Ssobomax * are met: 8131099Ssobomax * 1. Redistributions of source code must retain the above copyright 9131099Ssobomax * notice, this list of conditions and the following disclaimer. 10131099Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 11131099Ssobomax * notice, this list of conditions and the following disclaimer in the 12131099Ssobomax * documentation and/or other materials provided with the distribution. 13131099Ssobomax * 14131099Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15131099Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16131099Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17131099Ssobomax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18131099Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19131099Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20131099Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21131099Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22131099Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23131099Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24131099Ssobomax * SUCH DAMAGE. 25131099Ssobomax */ 26131099Ssobomax 27131099Ssobomax/* 28131099Ssobomax * Test for heapsort() routine. 29131099Ssobomax */ 30131099Ssobomax 31131099Ssobomax#include <sys/cdefs.h> 32131099Ssobomax__FBSDID("$FreeBSD: head/lib/libc/tests/stdlib/heapsort_test.c 290538 2015-11-08 07:03:17Z ngie $"); 33131099Ssobomax 34131099Ssobomax#include <assert.h> 35131099Ssobomax#include <stdio.h> 36131099Ssobomax#include <stdlib.h> 37131099Ssobomax 38131099Ssobomax#include "test-sort.h" 39131099Ssobomax 40290538SngieATF_TC_WITHOUT_HEAD(heapsort_test); 41290538SngieATF_TC_BODY(heapsort_test, tc) 42131099Ssobomax{ 43290538Sngie int sresvector[IVEC_LEN]; 44290538Sngie int testvector[IVEC_LEN]; 45290538Sngie int i, j; 46131099Ssobomax 47290538Sngie for (j = 2; j < IVEC_LEN; j++) { 48290538Sngie /* Populate test vectors */ 49290538Sngie for (i = 0; i < j; i++) 50290538Sngie testvector[i] = sresvector[i] = initvector[i]; 51131099Ssobomax 52290538Sngie /* Sort using heapsort(3) */ 53290538Sngie heapsort(testvector, j, sizeof(testvector[0]), sorthelp); 54290538Sngie /* Sort using reference slow sorting routine */ 55290538Sngie ssort(sresvector, j); 56131099Ssobomax 57290538Sngie /* Compare results */ 58290538Sngie for (i = 0; i < j; i++) 59290538Sngie ATF_CHECK_MSG(testvector[i] == sresvector[i], 60290538Sngie "item at index %d didn't match: %d != %d", 61290538Sngie i, testvector[i], sresvector[i]); 62290538Sngie } 63290538Sngie} 64131099Ssobomax 65290538SngieATF_TP_ADD_TCS(tp) 66290538Sngie{ 67131099Ssobomax 68290538Sngie ATF_TP_ADD_TC(tp, heapsort_test); 69290538Sngie 70290538Sngie return (atf_no_error()); 71131099Ssobomax} 72