qsort-sorts-and-copies.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2020, Sahan Fernando <sahan.h.fernando@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/QuickSort.h>
  27. #include <AK/String.h>
  28. #include <AK/Vector.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. const size_t NUM_RUNS = 100;
  32. struct SortableObject {
  33. int m_key;
  34. int m_payload;
  35. };
  36. int compare_sortable_object(const void* a, const void* b) {
  37. const int key1 = static_cast<const SortableObject*>(a)->m_key;
  38. const int key2 = static_cast<const SortableObject*>(b)->m_key;
  39. if (key1 < key2) {
  40. return -1;
  41. } else if (key1 == key2) {
  42. return 0;
  43. } else {
  44. return 1;
  45. }
  46. }
  47. int calc_payload_for_pos(size_t pos) {
  48. pos *= 231;
  49. return pos ^ (pos << 8) ^ (pos << 16) ^ (pos << 24);
  50. }
  51. void shuffle_vec(Vector<SortableObject> &test_objects)
  52. {
  53. for (size_t i = 0; i < test_objects.size() * 3; ++i) {
  54. auto i1 = rand() % test_objects.size();
  55. auto i2 = rand() % test_objects.size();
  56. swap(test_objects[i1], test_objects[i2]);
  57. }
  58. }
  59. int main()
  60. {
  61. // Generate vector of SortableObjects in sorted order, with payloads determined by their sorted positions
  62. Vector<SortableObject> test_objects;
  63. for (auto i = 0; i < 1024; ++i) {
  64. test_objects.append({i * 137, calc_payload_for_pos(i)});
  65. }
  66. for (size_t i = 0; i < NUM_RUNS; i++) {
  67. // Shuffle the vector, then sort it again
  68. shuffle_vec(test_objects);
  69. qsort(test_objects.data(), test_objects.size(), sizeof(SortableObject), compare_sortable_object);
  70. // Check that the objects are sorted by key
  71. for (auto i = 0u; i + 1 < test_objects.size(); ++i) {
  72. const auto &key1 = test_objects[i].m_key;
  73. const auto &key2 = test_objects[i + 1].m_key;
  74. if (key1 > key2) {
  75. printf("\x1b[01;35mTests failed: saw key %d before key %d\n", key1, key2);
  76. return 1;
  77. }
  78. }
  79. // Check that the object's payloads have not been corrupted
  80. for (auto i = 0u; i < test_objects.size(); ++i) {
  81. const auto expected = calc_payload_for_pos(i);
  82. const auto payload = test_objects[i].m_payload;
  83. if (payload != expected) {
  84. printf("\x1b[01;35mTests failed: expected payload %d for pos %u, got payload %d\n", expected, i, payload);
  85. return 1;
  86. }
  87. }
  88. }
  89. printf("Tests succeeded\n");
  90. return 0;
  91. }