testqsort.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include "SDL_test.h"
  11. static int
  12. num_compare(const void *_a, const void *_b)
  13. {
  14. const int a = *((const int *) _a);
  15. const int b = *((const int *) _b);
  16. return (a < b) ? -1 : ((a > b) ? 1 : 0);
  17. }
  18. static void
  19. test_sort(const char *desc, int *nums, const int arraylen)
  20. {
  21. int i;
  22. int prev;
  23. SDL_Log("test: %s arraylen=%d", desc, arraylen);
  24. SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare);
  25. prev = nums[0];
  26. for (i = 1; i < arraylen; i++) {
  27. const int val = nums[i];
  28. if (val < prev) {
  29. SDL_Log("sort is broken!");
  30. return;
  31. }
  32. prev = val;
  33. }
  34. }
  35. int
  36. main(int argc, char *argv[])
  37. {
  38. static int nums[1024 * 100];
  39. static const int itervals[] = { SDL_arraysize(nums), 12 };
  40. int iteration;
  41. SDLTest_RandomContext rndctx;
  42. if (argc > 1)
  43. {
  44. int success;
  45. Uint64 seed = 0;
  46. if (argv[1][0] == '0' && argv[1][1] == 'x')
  47. success = SDL_sscanf(argv[1] + 2, "%llx", &seed);
  48. else
  49. success = SDL_sscanf(argv[1], "%llu", &seed);
  50. if (!success)
  51. {
  52. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
  53. return 1;
  54. }
  55. if (seed <= 0xffffffff)
  56. {
  57. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
  58. return 1;
  59. }
  60. SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
  61. }
  62. else
  63. {
  64. SDLTest_RandomInitTime(&rndctx);
  65. }
  66. SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
  67. for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
  68. const int arraylen = itervals[iteration];
  69. int i;
  70. for (i = 0; i < arraylen; i++) {
  71. nums[i] = i;
  72. }
  73. test_sort("already sorted", nums, arraylen);
  74. for (i = 0; i < arraylen; i++) {
  75. nums[i] = i;
  76. }
  77. nums[arraylen-1] = -1;
  78. test_sort("already sorted except last element", nums, arraylen);
  79. for (i = 0; i < arraylen; i++) {
  80. nums[i] = (arraylen-1) - i;
  81. }
  82. test_sort("reverse sorted", nums, arraylen);
  83. for (i = 0; i < arraylen; i++) {
  84. nums[i] = SDLTest_RandomInt(&rndctx);
  85. }
  86. test_sort("random sorted", nums, arraylen);
  87. }
  88. return 0;
  89. }
  90. /* vi: set ts=4 sw=4 expandtab: */