A simple test program for SDL_qsort().
authorRyan C. Gordon <icculus@icculus.org>
Thu, 10 Mar 2016 01:50:43 -0500
changeset 1010954209cfab627
parent 10108 866896c224fd
child 10110 3dd0469150ce
A simple test program for SDL_qsort().
.hgignore
test/Makefile.in
test/testqsort.c
     1.1 --- a/.hgignore	Tue Mar 08 20:26:26 2016 -0500
     1.2 +++ b/.hgignore	Thu Mar 10 01:50:43 2016 -0500
     1.3 @@ -119,6 +119,7 @@
     1.4  test/testbounds
     1.5  test/torturethread
     1.6  test/testdisplayinfo
     1.7 +test/testqsort
     1.8  test/*.exe
     1.9  test/*.dSYM
    1.10  buildbot
     2.1 --- a/test/Makefile.in	Tue Mar 08 20:26:26 2016 -0500
     2.2 +++ b/test/Makefile.in	Thu Mar 10 01:50:43 2016 -0500
     2.3 @@ -62,6 +62,7 @@
     2.4  	testrendercopyex$(EXE) \
     2.5  	testmessage$(EXE) \
     2.6  	testdisplayinfo$(EXE) \
     2.7 +	testqsort$(EXE) \
     2.8  	controllermap$(EXE) \
     2.9  	
    2.10  all: Makefile $(TARGETS)
    2.11 @@ -271,6 +272,9 @@
    2.12  testdisplayinfo$(EXE): $(srcdir)/testdisplayinfo.c
    2.13  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    2.14  
    2.15 +testqsort$(EXE): $(srcdir)/testqsort.c
    2.16 +	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    2.17 +
    2.18  testbounds$(EXE): $(srcdir)/testbounds.c
    2.19  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    2.20  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/testqsort.c	Thu Mar 10 01:50:43 2016 -0500
     3.3 @@ -0,0 +1,85 @@
     3.4 +/*
     3.5 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     3.6 +
     3.7 +  This software is provided 'as-is', without any express or implied
     3.8 +  warranty.  In no event will the authors be held liable for any damages
     3.9 +  arising from the use of this software.
    3.10 +
    3.11 +  Permission is granted to anyone to use this software for any purpose,
    3.12 +  including commercial applications, and to alter it and redistribute it
    3.13 +  freely.
    3.14 +*/
    3.15 +
    3.16 +#include <stdio.h>
    3.17 +#include <stdlib.h>
    3.18 +
    3.19 +#include "SDL.h"
    3.20 +#include "SDL_revision.h"
    3.21 +
    3.22 +static int
    3.23 +num_compare(const void *_a, const void *_b)
    3.24 +{
    3.25 +    const int a = *((const int *) _a);
    3.26 +    const int b = *((const int *) _b);
    3.27 +    return (a < b) ? -1 : ((a > b) ? 1 : 0);
    3.28 +}
    3.29 +
    3.30 +static void
    3.31 +test_sort(const char *desc, int *nums, const int arraylen)
    3.32 +{
    3.33 +    int i;
    3.34 +    int prev;
    3.35 +
    3.36 +    SDL_Log("test: %s arraylen=%d", desc, arraylen);
    3.37 +
    3.38 +    SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare);
    3.39 +
    3.40 +    prev = nums[0];
    3.41 +    for (i = 1; i < arraylen; i++) {
    3.42 +        const int val = nums[i];
    3.43 +        if (val < prev) {
    3.44 +            SDL_Log("sort is broken!");
    3.45 +            return;
    3.46 +        }
    3.47 +        prev = val;
    3.48 +    }
    3.49 +}
    3.50 +
    3.51 +int
    3.52 +main(int argc, char *argv[])
    3.53 +{
    3.54 +    static int nums[1024 * 100];
    3.55 +    static const int itervals[] = { SDL_arraysize(nums), 12 };
    3.56 +    int iteration;
    3.57 +
    3.58 +    for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
    3.59 +        const int arraylen = itervals[iteration];
    3.60 +        int i;
    3.61 +
    3.62 +        for (i = 0; i < arraylen; i++) {
    3.63 +            nums[i] = i;
    3.64 +        }
    3.65 +        test_sort("already sorted", nums, arraylen);
    3.66 +
    3.67 +        for (i = 0; i < arraylen; i++) {
    3.68 +            nums[i] = i;
    3.69 +        }
    3.70 +        nums[arraylen-1] = -1;
    3.71 +        test_sort("already sorted except last element", nums, arraylen);
    3.72 +
    3.73 +        for (i = 0; i < arraylen; i++) {
    3.74 +            nums[i] = (arraylen-1) - i;
    3.75 +        }
    3.76 +        test_sort("reverse sorted", nums, arraylen);
    3.77 +
    3.78 +        for (i = 0; i < arraylen; i++) {
    3.79 +            nums[i] = random();
    3.80 +        }
    3.81 +        test_sort("random sorted", nums, arraylen);
    3.82 +    }
    3.83 +
    3.84 +    return 0;
    3.85 +}
    3.86 +
    3.87 +/* vi: set ts=4 sw=4 expandtab: */
    3.88 +