2
0

priority_queue_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/opensslconf.h>
  12. #include <internal/priority_queue.h>
  13. #include <openssl/err.h>
  14. #include <openssl/crypto.h>
  15. #include "internal/nelem.h"
  16. #include "testutil.h"
  17. #define MAX_SAMPLES 500000
  18. DEFINE_PRIORITY_QUEUE_OF(size_t);
  19. static size_t num_rec_freed;
  20. static int size_t_compare(const size_t *a, const size_t *b)
  21. {
  22. if (*a < *b)
  23. return -1;
  24. if (*a > *b)
  25. return 1;
  26. return 0;
  27. }
  28. static int qsort_size_t_compare(const void *a, const void *b)
  29. {
  30. return size_t_compare((size_t *)a, (size_t *)b);
  31. }
  32. static int qsort_size_t_compare_rev(const void *a, const void *b)
  33. {
  34. return size_t_compare((size_t *)b, (size_t *)a);
  35. }
  36. static void free_checker(ossl_unused size_t *p)
  37. {
  38. num_rec_freed++;
  39. }
  40. static int test_size_t_priority_queue_int(int reserve, int order, int count,
  41. int remove, int random, int popfree)
  42. {
  43. PRIORITY_QUEUE_OF(size_t) *pq = NULL;
  44. static size_t values[MAX_SAMPLES], sorted[MAX_SAMPLES], ref[MAX_SAMPLES];
  45. size_t n;
  46. int i, res = 0;
  47. static const char *orders[3] = { "unordered", "ascending", "descending" };
  48. TEST_info("testing count %d, %s, %s, values %s, remove %d, %sfree",
  49. count, orders[order], reserve ? "reserve" : "grow",
  50. random ? "random" : "deterministic", remove,
  51. popfree ? "pop " : "");
  52. if (!TEST_size_t_le(count, MAX_SAMPLES))
  53. return 0;
  54. memset(values, 0, sizeof(values));
  55. memset(sorted, 0, sizeof(sorted));
  56. memset(ref, 0, sizeof(ref));
  57. for (i = 0; i < count; i++)
  58. values[i] = random ? test_random() : (size_t)(count - i);
  59. memcpy(sorted, values, sizeof(*sorted) * count);
  60. qsort(sorted, count, sizeof(*sorted), &qsort_size_t_compare);
  61. if (order == 1)
  62. memcpy(values, sorted, sizeof(*values) * count);
  63. else if (order == 2)
  64. qsort(values, count, sizeof(*values), &qsort_size_t_compare_rev);
  65. if (!TEST_ptr(pq = ossl_pqueue_size_t_new(&size_t_compare))
  66. || !TEST_size_t_eq(ossl_pqueue_size_t_num(pq), 0))
  67. goto err;
  68. if (reserve && !TEST_true(ossl_pqueue_size_t_reserve(pq, count)))
  69. goto err;
  70. for (i = 0; i < count; i++)
  71. if (!TEST_true(ossl_pqueue_size_t_push(pq, values + i, ref + i)))
  72. goto err;
  73. if (!TEST_size_t_eq(*ossl_pqueue_size_t_peek(pq), *sorted)
  74. || !TEST_size_t_eq(ossl_pqueue_size_t_num(pq), count))
  75. goto err;
  76. if (remove) {
  77. while (remove-- > 0) {
  78. i = test_random() % count;
  79. if (values[i] != SIZE_MAX) {
  80. if (!TEST_ptr_eq(ossl_pqueue_size_t_remove(pq, ref[i]),
  81. values + i))
  82. goto err;
  83. values[i] = SIZE_MAX;
  84. }
  85. }
  86. memcpy(sorted, values, sizeof(*sorted) * count);
  87. qsort(sorted, count, sizeof(*sorted), &qsort_size_t_compare);
  88. }
  89. for (i = 0; ossl_pqueue_size_t_peek(pq) != NULL; i++)
  90. if (!TEST_size_t_eq(*ossl_pqueue_size_t_peek(pq), sorted[i])
  91. || !TEST_size_t_eq(*ossl_pqueue_size_t_pop(pq), sorted[i]))
  92. goto err;
  93. if (popfree) {
  94. num_rec_freed = 0;
  95. n = ossl_pqueue_size_t_num(pq);
  96. ossl_pqueue_size_t_pop_free(pq, &free_checker);
  97. pq = NULL;
  98. if (!TEST_size_t_eq(num_rec_freed, n))
  99. goto err;
  100. }
  101. res = 1;
  102. err:
  103. ossl_pqueue_size_t_free(pq);
  104. return res;
  105. }
  106. static const int test_size_t_priority_counts[] = {
  107. 10, 11, 6, 5, 3, 1, 2, 7500
  108. };
  109. static int test_size_t_priority_queue(int n)
  110. {
  111. int reserve, order, count, remove, random, popfree;
  112. count = n % OSSL_NELEM(test_size_t_priority_counts);
  113. n /= OSSL_NELEM(test_size_t_priority_counts);
  114. order = n % 3;
  115. n /= 3;
  116. random = n % 2;
  117. n /= 2;
  118. reserve = n % 2;
  119. n /= 2;
  120. remove = n % 6;
  121. n /= 6;
  122. popfree = n % 2;
  123. count = test_size_t_priority_counts[count];
  124. return test_size_t_priority_queue_int(reserve, order, count, remove,
  125. random, popfree);
  126. }
  127. static int test_large_priority_queue(void)
  128. {
  129. return test_size_t_priority_queue_int(0, 0, MAX_SAMPLES, MAX_SAMPLES / 100,
  130. 1, 1);
  131. }
  132. int setup_tests(void)
  133. {
  134. ADD_ALL_TESTS(test_size_t_priority_queue,
  135. OSSL_NELEM(test_size_t_priority_counts) /* count */
  136. * 3 /* order */
  137. * 2 /* random */
  138. * 2 /* reserve */
  139. * 6 /* remove */
  140. * 2); /* pop & free */
  141. ADD_TEST(test_large_priority_queue);
  142. return 1;
  143. }