slice_hash_table_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. *
  3. * Copyright 2017, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/lib/slice/slice_hash_table.h"
  34. #include <string.h>
  35. #include <grpc/support/alloc.h>
  36. #include <grpc/support/log.h>
  37. #include <grpc/support/string_util.h>
  38. #include "src/core/lib/slice/slice_internal.h"
  39. #include "test/core/util/test_config.h"
  40. typedef struct {
  41. char* key;
  42. char* value;
  43. } test_entry;
  44. static void populate_entries(const test_entry* input, size_t num_entries,
  45. grpc_slice_hash_table_entry* output) {
  46. for (size_t i = 0; i < num_entries; ++i) {
  47. output[i].key = grpc_slice_from_copied_string(gpr_strdup(input[i].key));
  48. output[i].value = gpr_strdup(input[i].value);
  49. }
  50. }
  51. static void check_values(const test_entry* input, size_t num_entries,
  52. grpc_slice_hash_table* table) {
  53. for (size_t i = 0; i < num_entries; ++i) {
  54. grpc_slice key = grpc_slice_from_static_string(input[i].key);
  55. char* actual = grpc_slice_hash_table_get(table, key);
  56. GPR_ASSERT(actual != NULL);
  57. GPR_ASSERT(strcmp(actual, input[i].value) == 0);
  58. grpc_slice_unref(key);
  59. }
  60. }
  61. static void check_non_existent_value(const char* key_string,
  62. grpc_slice_hash_table* table) {
  63. grpc_slice key = grpc_slice_from_static_string(key_string);
  64. GPR_ASSERT(grpc_slice_hash_table_get(table, key) == NULL);
  65. grpc_slice_unref(key);
  66. }
  67. static void destroy_string(grpc_exec_ctx* exec_ctx, void* value) {
  68. gpr_free(value);
  69. }
  70. static void test_slice_hash_table() {
  71. const test_entry test_entries[] = {
  72. {"key_0", "value_0"},
  73. {"key_1", "value_1"},
  74. {"key_2", "value_2"},
  75. {"key_3", "value_3"},
  76. {"key_4", "value_4"},
  77. {"key_5", "value_5"},
  78. {"key_6", "value_6"},
  79. {"key_7", "value_7"},
  80. {"key_8", "value_8"},
  81. {"key_9", "value_9"},
  82. {"key_10", "value_10"},
  83. {"key_11", "value_11"},
  84. {"key_12", "value_12"},
  85. {"key_13", "value_13"},
  86. {"key_14", "value_14"},
  87. {"key_15", "value_15"},
  88. {"key_16", "value_16"},
  89. {"key_17", "value_17"},
  90. {"key_18", "value_18"},
  91. {"key_19", "value_19"},
  92. {"key_20", "value_20"},
  93. {"key_21", "value_21"},
  94. {"key_22", "value_22"},
  95. {"key_23", "value_23"},
  96. {"key_24", "value_24"},
  97. {"key_25", "value_25"},
  98. {"key_26", "value_26"},
  99. {"key_27", "value_27"},
  100. {"key_28", "value_28"},
  101. {"key_29", "value_29"},
  102. {"key_30", "value_30"},
  103. {"key_31", "value_31"},
  104. {"key_32", "value_32"},
  105. {"key_33", "value_33"},
  106. {"key_34", "value_34"},
  107. {"key_35", "value_35"},
  108. {"key_36", "value_36"},
  109. {"key_37", "value_37"},
  110. {"key_38", "value_38"},
  111. {"key_39", "value_39"},
  112. {"key_40", "value_40"},
  113. {"key_41", "value_41"},
  114. {"key_42", "value_42"},
  115. {"key_43", "value_43"},
  116. {"key_44", "value_44"},
  117. {"key_45", "value_45"},
  118. {"key_46", "value_46"},
  119. {"key_47", "value_47"},
  120. {"key_48", "value_48"},
  121. {"key_49", "value_49"},
  122. {"key_50", "value_50"},
  123. {"key_51", "value_51"},
  124. {"key_52", "value_52"},
  125. {"key_53", "value_53"},
  126. {"key_54", "value_54"},
  127. {"key_55", "value_55"},
  128. {"key_56", "value_56"},
  129. {"key_57", "value_57"},
  130. {"key_58", "value_58"},
  131. {"key_59", "value_59"},
  132. {"key_60", "value_60"},
  133. {"key_61", "value_61"},
  134. {"key_62", "value_62"},
  135. {"key_63", "value_63"},
  136. {"key_64", "value_64"},
  137. {"key_65", "value_65"},
  138. {"key_66", "value_66"},
  139. {"key_67", "value_67"},
  140. {"key_68", "value_68"},
  141. {"key_69", "value_69"},
  142. {"key_70", "value_70"},
  143. {"key_71", "value_71"},
  144. {"key_72", "value_72"},
  145. {"key_73", "value_73"},
  146. {"key_74", "value_74"},
  147. {"key_75", "value_75"},
  148. {"key_76", "value_76"},
  149. {"key_77", "value_77"},
  150. {"key_78", "value_78"},
  151. {"key_79", "value_79"},
  152. {"key_80", "value_80"},
  153. {"key_81", "value_81"},
  154. {"key_82", "value_82"},
  155. {"key_83", "value_83"},
  156. {"key_84", "value_84"},
  157. {"key_85", "value_85"},
  158. {"key_86", "value_86"},
  159. {"key_87", "value_87"},
  160. {"key_88", "value_88"},
  161. {"key_89", "value_89"},
  162. {"key_90", "value_90"},
  163. {"key_91", "value_91"},
  164. {"key_92", "value_92"},
  165. {"key_93", "value_93"},
  166. {"key_94", "value_94"},
  167. {"key_95", "value_95"},
  168. {"key_96", "value_96"},
  169. {"key_97", "value_97"},
  170. {"key_98", "value_98"},
  171. {"key_99", "value_99"},
  172. };
  173. const size_t num_entries = GPR_ARRAY_SIZE(test_entries);
  174. // Construct table.
  175. grpc_slice_hash_table_entry entries[num_entries];
  176. populate_entries(test_entries, num_entries, entries);
  177. grpc_slice_hash_table* table =
  178. grpc_slice_hash_table_create(num_entries, entries, destroy_string);
  179. // Check contents of table.
  180. check_values(test_entries, num_entries, table);
  181. check_non_existent_value("XX", table);
  182. // Clean up.
  183. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  184. grpc_slice_hash_table_unref(&exec_ctx, table);
  185. grpc_exec_ctx_finish(&exec_ctx);
  186. }
  187. int main(int argc, char **argv) {
  188. grpc_test_init(argc, argv);
  189. test_slice_hash_table();
  190. return 0;
  191. }