heapsort_test.c revision 131099
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/tools/regression/lib/libc/stdlib/test-heapsort.c 131099 2004-06-25 12:31:12Z sobomax $");
33131099Ssobomax
34131099Ssobomax#include <assert.h>
35131099Ssobomax#include <stdio.h>
36131099Ssobomax#include <stdlib.h>
37131099Ssobomax
38131099Ssobomax#include "test-sort.h"
39131099Ssobomax
40131099Ssobomaxint
41131099Ssobomaxmain(int argc, char *argv[])
42131099Ssobomax{
43131099Ssobomax  int i, j;
44131099Ssobomax  int testvector[IVEC_LEN];
45131099Ssobomax  int sresvector[IVEC_LEN];
46131099Ssobomax
47131099Ssobomax  for (j = 2; j < IVEC_LEN; j++) {
48131099Ssobomax    /* Populate test vectors */
49131099Ssobomax    for (i = 0; i < j; i++)
50131099Ssobomax      testvector[i] = sresvector[i] = initvector[i];
51131099Ssobomax
52131099Ssobomax    /* Sort using heapsort(3) */
53131099Ssobomax    heapsort(testvector, j, sizeof(testvector[0]), sorthelp);
54131099Ssobomax    /* Sort using reference slow sorting routine */
55131099Ssobomax    ssort(sresvector, j);
56131099Ssobomax
57131099Ssobomax    /* Compare results */
58131099Ssobomax    for (i = 0; i < j; i++)
59131099Ssobomax      assert(testvector[i] == sresvector[i]);
60131099Ssobomax  }
61131099Ssobomax
62131099Ssobomax  printf("PASS heapsort\n");
63131099Ssobomax
64131099Ssobomax  return(0);
65131099Ssobomax}
66