/trunk/libs/libnghttp2/nghttp2-1.43.0/tests/nghttp2_map_test.c

https://gitlab.com/thanhnhat041/padavan-ng · C · 181 lines · 119 code · 31 blank · 31 comment · 29 complexity · 26a1c10b9083d06babb938b2c4ec5aa8 MD5 · raw file

  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "nghttp2_map_test.h"
  26. #include <CUnit/CUnit.h>
  27. #include "nghttp2_map.h"
  28. typedef struct strentry {
  29. nghttp2_map_entry map_entry;
  30. const char *str;
  31. } strentry;
  32. static void strentry_init(strentry *entry, key_type key, const char *str) {
  33. nghttp2_map_entry_init(&entry->map_entry, key);
  34. entry->str = str;
  35. }
  36. void test_nghttp2_map(void) {
  37. strentry foo, FOO, bar, baz, shrubbery;
  38. nghttp2_map map;
  39. nghttp2_map_init(&map, nghttp2_mem_default());
  40. strentry_init(&foo, 1, "foo");
  41. strentry_init(&FOO, 1, "FOO");
  42. strentry_init(&bar, 2, "bar");
  43. strentry_init(&baz, 3, "baz");
  44. strentry_init(&shrubbery, 4, "shrubbery");
  45. CU_ASSERT(0 == nghttp2_map_insert(&map, &foo.map_entry));
  46. CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
  47. CU_ASSERT(1 == nghttp2_map_size(&map));
  48. CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT ==
  49. nghttp2_map_insert(&map, &FOO.map_entry));
  50. CU_ASSERT(1 == nghttp2_map_size(&map));
  51. CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
  52. CU_ASSERT(0 == nghttp2_map_insert(&map, &bar.map_entry));
  53. CU_ASSERT(2 == nghttp2_map_size(&map));
  54. CU_ASSERT(0 == nghttp2_map_insert(&map, &baz.map_entry));
  55. CU_ASSERT(3 == nghttp2_map_size(&map));
  56. CU_ASSERT(0 == nghttp2_map_insert(&map, &shrubbery.map_entry));
  57. CU_ASSERT(4 == nghttp2_map_size(&map));
  58. CU_ASSERT(strcmp("baz", ((strentry *)nghttp2_map_find(&map, 3))->str) == 0);
  59. nghttp2_map_remove(&map, 3);
  60. CU_ASSERT(3 == nghttp2_map_size(&map));
  61. CU_ASSERT(NULL == nghttp2_map_find(&map, 3));
  62. nghttp2_map_remove(&map, 1);
  63. CU_ASSERT(2 == nghttp2_map_size(&map));
  64. CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
  65. /* Erasing non-existent entry */
  66. nghttp2_map_remove(&map, 1);
  67. CU_ASSERT(2 == nghttp2_map_size(&map));
  68. CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
  69. CU_ASSERT(strcmp("bar", ((strentry *)nghttp2_map_find(&map, 2))->str) == 0);
  70. CU_ASSERT(strcmp("shrubbery", ((strentry *)nghttp2_map_find(&map, 4))->str) ==
  71. 0);
  72. nghttp2_map_free(&map);
  73. }
  74. static void shuffle(int *a, int n) {
  75. int i;
  76. for (i = n - 1; i >= 1; --i) {
  77. size_t j = (size_t)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
  78. int t = a[j];
  79. a[j] = a[i];
  80. a[i] = t;
  81. }
  82. }
  83. static int eachfun(nghttp2_map_entry *entry, void *ptr) {
  84. (void)entry;
  85. (void)ptr;
  86. return 0;
  87. }
  88. #define NUM_ENT 6000
  89. static strentry arr[NUM_ENT];
  90. static int order[NUM_ENT];
  91. void test_nghttp2_map_functional(void) {
  92. nghttp2_map map;
  93. int i;
  94. nghttp2_map_init(&map, nghttp2_mem_default());
  95. for (i = 0; i < NUM_ENT; ++i) {
  96. strentry_init(&arr[i], i + 1, "foo");
  97. order[i] = i + 1;
  98. }
  99. /* insertion */
  100. shuffle(order, NUM_ENT);
  101. for (i = 0; i < NUM_ENT; ++i) {
  102. CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[order[i] - 1].map_entry));
  103. }
  104. /* traverse */
  105. nghttp2_map_each(&map, eachfun, NULL);
  106. /* find */
  107. shuffle(order, NUM_ENT);
  108. for (i = 0; i < NUM_ENT; ++i) {
  109. nghttp2_map_find(&map, order[i]);
  110. }
  111. /* remove */
  112. shuffle(order, NUM_ENT);
  113. for (i = 0; i < NUM_ENT; ++i) {
  114. CU_ASSERT(0 == nghttp2_map_remove(&map, order[i]));
  115. }
  116. /* each_free (but no op function for testing purpose) */
  117. for (i = 0; i < NUM_ENT; ++i) {
  118. strentry_init(&arr[i], i + 1, "foo");
  119. }
  120. /* insert once again */
  121. for (i = 0; i < NUM_ENT; ++i) {
  122. CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[i].map_entry));
  123. }
  124. nghttp2_map_each_free(&map, eachfun, NULL);
  125. nghttp2_map_free(&map);
  126. }
  127. static int entry_free(nghttp2_map_entry *entry, void *ptr) {
  128. nghttp2_mem *mem = ptr;
  129. mem->free(entry, NULL);
  130. return 0;
  131. }
  132. void test_nghttp2_map_each_free(void) {
  133. nghttp2_mem *mem = nghttp2_mem_default();
  134. strentry *foo = mem->malloc(sizeof(strentry), NULL),
  135. *bar = mem->malloc(sizeof(strentry), NULL),
  136. *baz = mem->malloc(sizeof(strentry), NULL),
  137. *shrubbery = mem->malloc(sizeof(strentry), NULL);
  138. nghttp2_map map;
  139. nghttp2_map_init(&map, nghttp2_mem_default());
  140. strentry_init(foo, 1, "foo");
  141. strentry_init(bar, 2, "bar");
  142. strentry_init(baz, 3, "baz");
  143. strentry_init(shrubbery, 4, "shrubbery");
  144. nghttp2_map_insert(&map, &foo->map_entry);
  145. nghttp2_map_insert(&map, &bar->map_entry);
  146. nghttp2_map_insert(&map, &baz->map_entry);
  147. nghttp2_map_insert(&map, &shrubbery->map_entry);
  148. nghttp2_map_each_free(&map, entry_free, mem);
  149. nghttp2_map_free(&map);
  150. }