test/testqsort.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 10 Mar 2016 01:50:43 -0500
changeset 10109 54209cfab627
child 10110 3dd0469150ce
permissions -rw-r--r--
A simple test program for SDL_qsort().
icculus@10109
     1
/*
icculus@10109
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
icculus@10109
     3
icculus@10109
     4
  This software is provided 'as-is', without any express or implied
icculus@10109
     5
  warranty.  In no event will the authors be held liable for any damages
icculus@10109
     6
  arising from the use of this software.
icculus@10109
     7
icculus@10109
     8
  Permission is granted to anyone to use this software for any purpose,
icculus@10109
     9
  including commercial applications, and to alter it and redistribute it
icculus@10109
    10
  freely.
icculus@10109
    11
*/
icculus@10109
    12
icculus@10109
    13
#include <stdio.h>
icculus@10109
    14
#include <stdlib.h>
icculus@10109
    15
icculus@10109
    16
#include "SDL.h"
icculus@10109
    17
#include "SDL_revision.h"
icculus@10109
    18
icculus@10109
    19
static int
icculus@10109
    20
num_compare(const void *_a, const void *_b)
icculus@10109
    21
{
icculus@10109
    22
    const int a = *((const int *) _a);
icculus@10109
    23
    const int b = *((const int *) _b);
icculus@10109
    24
    return (a < b) ? -1 : ((a > b) ? 1 : 0);
icculus@10109
    25
}
icculus@10109
    26
icculus@10109
    27
static void
icculus@10109
    28
test_sort(const char *desc, int *nums, const int arraylen)
icculus@10109
    29
{
icculus@10109
    30
    int i;
icculus@10109
    31
    int prev;
icculus@10109
    32
icculus@10109
    33
    SDL_Log("test: %s arraylen=%d", desc, arraylen);
icculus@10109
    34
icculus@10109
    35
    SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare);
icculus@10109
    36
icculus@10109
    37
    prev = nums[0];
icculus@10109
    38
    for (i = 1; i < arraylen; i++) {
icculus@10109
    39
        const int val = nums[i];
icculus@10109
    40
        if (val < prev) {
icculus@10109
    41
            SDL_Log("sort is broken!");
icculus@10109
    42
            return;
icculus@10109
    43
        }
icculus@10109
    44
        prev = val;
icculus@10109
    45
    }
icculus@10109
    46
}
icculus@10109
    47
icculus@10109
    48
int
icculus@10109
    49
main(int argc, char *argv[])
icculus@10109
    50
{
icculus@10109
    51
    static int nums[1024 * 100];
icculus@10109
    52
    static const int itervals[] = { SDL_arraysize(nums), 12 };
icculus@10109
    53
    int iteration;
icculus@10109
    54
icculus@10109
    55
    for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
icculus@10109
    56
        const int arraylen = itervals[iteration];
icculus@10109
    57
        int i;
icculus@10109
    58
icculus@10109
    59
        for (i = 0; i < arraylen; i++) {
icculus@10109
    60
            nums[i] = i;
icculus@10109
    61
        }
icculus@10109
    62
        test_sort("already sorted", nums, arraylen);
icculus@10109
    63
icculus@10109
    64
        for (i = 0; i < arraylen; i++) {
icculus@10109
    65
            nums[i] = i;
icculus@10109
    66
        }
icculus@10109
    67
        nums[arraylen-1] = -1;
icculus@10109
    68
        test_sort("already sorted except last element", nums, arraylen);
icculus@10109
    69
icculus@10109
    70
        for (i = 0; i < arraylen; i++) {
icculus@10109
    71
            nums[i] = (arraylen-1) - i;
icculus@10109
    72
        }
icculus@10109
    73
        test_sort("reverse sorted", nums, arraylen);
icculus@10109
    74
icculus@10109
    75
        for (i = 0; i < arraylen; i++) {
icculus@10109
    76
            nums[i] = random();
icculus@10109
    77
        }
icculus@10109
    78
        test_sort("random sorted", nums, arraylen);
icculus@10109
    79
    }
icculus@10109
    80
icculus@10109
    81
    return 0;
icculus@10109
    82
}
icculus@10109
    83
icculus@10109
    84
/* vi: set ts=4 sw=4 expandtab: */
icculus@10109
    85