🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

  1. /*
  2. Copyright (C) 1997-2020 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. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
  52. return 1;
  53. }
  54. if (seed <= ((Uint64)0xffffffff)) {
  55. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
  56. return 1;
  57. }
  58. SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
  59. }
  60. else
  61. {
  62. SDLTest_RandomInitTime(&rndctx);
  63. }
  64. SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
  65. for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
  66. const int arraylen = itervals[iteration];
  67. int i;
  68. for (i = 0; i < arraylen; i++) {
  69. nums[i] = i;
  70. }
  71. test_sort("already sorted", nums, arraylen);
  72. for (i = 0; i < arraylen; i++) {
  73. nums[i] = i;
  74. }
  75. nums[arraylen-1] = -1;
  76. test_sort("already sorted except last element", nums, arraylen);
  77. for (i = 0; i < arraylen; i++) {
  78. nums[i] = (arraylen-1) - i;
  79. }
  80. test_sort("reverse sorted", nums, arraylen);
  81. for (i = 0; i < arraylen; i++) {
  82. nums[i] = SDLTest_RandomInt(&rndctx);
  83. }
  84. test_sort("random sorted", nums, arraylen);
  85. }
  86. return 0;
  87. }
  88. /* vi: set ts=4 sw=4 expandtab: */