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