test/testqsort.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
icculus@10109
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
slouken@10415
    13
#include "SDL_test.h"
icculus@10109
    14
icculus@10109
    15
static int
icculus@10109
    16
num_compare(const void *_a, const void *_b)
icculus@10109
    17
{
icculus@10109
    18
    const int a = *((const int *) _a);
icculus@10109
    19
    const int b = *((const int *) _b);
icculus@10109
    20
    return (a < b) ? -1 : ((a > b) ? 1 : 0);
icculus@10109
    21
}
icculus@10109
    22
icculus@10109
    23
static void
icculus@10109
    24
test_sort(const char *desc, int *nums, const int arraylen)
icculus@10109
    25
{
icculus@10109
    26
    int i;
icculus@10109
    27
    int prev;
icculus@10109
    28
icculus@10109
    29
    SDL_Log("test: %s arraylen=%d", desc, arraylen);
icculus@10109
    30
icculus@10109
    31
    SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare);
icculus@10109
    32
icculus@10109
    33
    prev = nums[0];
icculus@10109
    34
    for (i = 1; i < arraylen; i++) {
icculus@10109
    35
        const int val = nums[i];
icculus@10109
    36
        if (val < prev) {
icculus@10109
    37
            SDL_Log("sort is broken!");
icculus@10109
    38
            return;
icculus@10109
    39
        }
icculus@10109
    40
        prev = val;
icculus@10109
    41
    }
icculus@10109
    42
}
icculus@10109
    43
icculus@10109
    44
int
icculus@10109
    45
main(int argc, char *argv[])
icculus@10109
    46
{
icculus@10109
    47
    static int nums[1024 * 100];
icculus@10109
    48
    static const int itervals[] = { SDL_arraysize(nums), 12 };
icculus@10109
    49
    int iteration;
slouken@10415
    50
    SDLTest_RandomContext rndctx;
slouken@10415
    51
slouken@10415
    52
    if (argc > 1)
slouken@10415
    53
    {
slouken@10415
    54
        int success;
slouken@10415
    55
        Uint64 seed = 0;
slouken@10415
    56
        if (argv[1][0] == '0' && argv[1][1] == 'x')
slouken@10415
    57
            success = SDL_sscanf(argv[1] + 2, "%llx", &seed);
slouken@10415
    58
        else
slouken@10415
    59
            success = SDL_sscanf(argv[1], "%llu", &seed);
slouken@10415
    60
        if (!success)
slouken@10415
    61
        {
slouken@10415
    62
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
slouken@10415
    63
            return 1;
slouken@10415
    64
        }
slouken@10415
    65
        if (seed <= 0xffffffff)
slouken@10415
    66
        {
slouken@10415
    67
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
slouken@10415
    68
            return 1;
slouken@10415
    69
        }
slouken@10415
    70
        SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
slouken@10415
    71
    }
slouken@10415
    72
    else
slouken@10415
    73
    {
slouken@10415
    74
        SDLTest_RandomInitTime(&rndctx);
slouken@10415
    75
    }
slouken@10415
    76
    SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
icculus@10109
    77
icculus@10109
    78
    for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
icculus@10109
    79
        const int arraylen = itervals[iteration];
icculus@10109
    80
        int i;
icculus@10109
    81
icculus@10109
    82
        for (i = 0; i < arraylen; i++) {
icculus@10109
    83
            nums[i] = i;
icculus@10109
    84
        }
icculus@10109
    85
        test_sort("already sorted", nums, arraylen);
icculus@10109
    86
icculus@10109
    87
        for (i = 0; i < arraylen; i++) {
icculus@10109
    88
            nums[i] = i;
icculus@10109
    89
        }
icculus@10109
    90
        nums[arraylen-1] = -1;
icculus@10109
    91
        test_sort("already sorted except last element", nums, arraylen);
icculus@10109
    92
icculus@10109
    93
        for (i = 0; i < arraylen; i++) {
icculus@10109
    94
            nums[i] = (arraylen-1) - i;
icculus@10109
    95
        }
icculus@10109
    96
        test_sort("reverse sorted", nums, arraylen);
icculus@10109
    97
icculus@10109
    98
        for (i = 0; i < arraylen; i++) {
slouken@10415
    99
            nums[i] = SDLTest_RandomInt(&rndctx);
icculus@10109
   100
        }
icculus@10109
   101
        test_sort("random sorted", nums, arraylen);
icculus@10109
   102
    }
icculus@10109
   103
icculus@10109
   104
    return 0;
icculus@10109
   105
}
icculus@10109
   106
icculus@10109
   107
/* vi: set ts=4 sw=4 expandtab: */
icculus@10109
   108