lhash_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /*
  10. * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <openssl/opensslconf.h>
  15. #include <openssl/lhash.h>
  16. #include <openssl/err.h>
  17. #include <openssl/crypto.h>
  18. #include "e_os.h"
  19. #include "testutil.h"
  20. /*
  21. * The macros below generate unused functions which error out one of the clang
  22. * builds. We disable this check here.
  23. */
  24. #ifdef __clang__
  25. #pragma clang diagnostic ignored "-Wunused-function"
  26. #endif
  27. DEFINE_LHASH_OF(int);
  28. static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
  29. -17, 16, 17, -23, 35, 37, 173, 11 };
  30. static const unsigned int n_int_tests = OSSL_NELEM(int_tests);
  31. static short int_found[OSSL_NELEM(int_tests)];
  32. static unsigned long int int_hash(const int *p)
  33. {
  34. return 3 & *p; /* To force collisions */
  35. }
  36. static int int_cmp(const int *p, const int *q)
  37. {
  38. return *p != *q;
  39. }
  40. static int int_find(int n)
  41. {
  42. unsigned int i;
  43. for (i = 0; i < n_int_tests; i++)
  44. if (int_tests[i] == n)
  45. return i;
  46. return -1;
  47. }
  48. static void int_doall(int *v)
  49. {
  50. int_found[int_find(*v)]++;
  51. }
  52. static void int_doall_arg(int *p, short *f)
  53. {
  54. f[int_find(*p)]++;
  55. }
  56. IMPLEMENT_LHASH_DOALL_ARG(int, short);
  57. static int test_int_lhash(void)
  58. {
  59. static struct {
  60. int data;
  61. int null;
  62. } dels[] = {
  63. { 65537, 0 },
  64. { 173, 0 },
  65. { 999, 1 },
  66. { 37, 0 },
  67. { 1, 0 },
  68. { 34, 1 }
  69. };
  70. const unsigned int n_dels = OSSL_NELEM(dels);
  71. LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
  72. unsigned int i;
  73. int testresult = 0, j, *p;
  74. if (!TEST_ptr(h))
  75. goto end;
  76. /* insert */
  77. for (i = 0; i < n_int_tests; i++)
  78. if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
  79. TEST_info("int insert %d", i);
  80. goto end;
  81. }
  82. /* num_items */
  83. if (!TEST_int_eq(lh_int_num_items(h), n_int_tests))
  84. goto end;
  85. /* retrieve */
  86. for (i = 0; i < n_int_tests; i++)
  87. if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
  88. TEST_info("lhash int retrieve value %d", i);
  89. goto end;
  90. }
  91. for (i = 0; i < n_int_tests; i++)
  92. if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
  93. TEST_info("lhash int retrieve address %d", i);
  94. goto end;
  95. }
  96. j = 1;
  97. if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
  98. goto end;
  99. /* replace */
  100. j = 13;
  101. if (!TEST_ptr(p = lh_int_insert(h, &j)))
  102. goto end;
  103. if (!TEST_ptr_eq(p, int_tests + 1))
  104. goto end;
  105. if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
  106. goto end;
  107. /* do_all */
  108. memset(int_found, 0, sizeof(int_found));
  109. lh_int_doall(h, &int_doall);
  110. for (i = 0; i < n_int_tests; i++)
  111. if (!TEST_int_eq(int_found[i], 1)) {
  112. TEST_info("lhash int doall %d", i);
  113. goto end;
  114. }
  115. /* do_all_arg */
  116. memset(int_found, 0, sizeof(int_found));
  117. lh_int_doall_short(h, int_doall_arg, int_found);
  118. for (i = 0; i < n_int_tests; i++)
  119. if (!TEST_int_eq(int_found[i], 1)) {
  120. TEST_info("lhash int doall arg %d", i);
  121. goto end;
  122. }
  123. /* delete */
  124. for (i = 0; i < n_dels; i++) {
  125. const int b = lh_int_delete(h, &dels[i].data) == NULL;
  126. if (!TEST_int_eq(b ^ dels[i].null, 0)) {
  127. TEST_info("lhash int delete %d", i);
  128. goto end;
  129. }
  130. }
  131. /* error */
  132. if (!TEST_int_eq(lh_int_error(h), 0))
  133. goto end;
  134. testresult = 1;
  135. end:
  136. lh_int_free(h);
  137. return testresult;
  138. }
  139. static unsigned long int stress_hash(const int *p)
  140. {
  141. return *p;
  142. }
  143. static int test_stress(void)
  144. {
  145. LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
  146. const unsigned int n = 2500000;
  147. unsigned int i;
  148. int testresult = 0, *p;
  149. if (!TEST_ptr(h))
  150. goto end;
  151. /* insert */
  152. for (i = 0; i < n; i++) {
  153. p = OPENSSL_malloc(sizeof(i));
  154. if (!TEST_ptr(p)) {
  155. TEST_info("lhash stress out of memory %d", i);
  156. goto end;
  157. }
  158. *p = 3 * i + 1;
  159. lh_int_insert(h, p);
  160. }
  161. /* num_items */
  162. if (!TEST_int_eq(lh_int_num_items(h), n))
  163. goto end;
  164. fprintf(stderr, "hash full statistics:\n");
  165. OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
  166. fprintf(stderr, "\nhash full node usage:\n");
  167. OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
  168. /* delete in a different order */
  169. for (i = 0; i < n; i++) {
  170. const int j = (7 * i + 4) % n * 3 + 1;
  171. if (!TEST_ptr(p = lh_int_delete(h, &j))) {
  172. TEST_info("lhash stress delete %d\n", i);
  173. goto end;
  174. }
  175. if (!TEST_int_eq(*p, j)) {
  176. TEST_info("lhash stress bad value %d", i);
  177. goto end;
  178. }
  179. OPENSSL_free(p);
  180. }
  181. fprintf(stderr, "\nhash empty statistics:\n");
  182. OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
  183. fprintf(stderr, "\nhash empty node usage:\n");
  184. OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
  185. testresult = 1;
  186. end:
  187. lh_int_free(h);
  188. return testresult;
  189. }
  190. void register_tests(void)
  191. {
  192. ADD_TEST(test_int_lhash);
  193. ADD_TEST(test_stress);
  194. }