isomorfeus-ferret 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +612 -0
  3. data/README.md +44 -0
  4. data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
  5. data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
  6. data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
  7. data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
  8. data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
  9. data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
  10. data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
  11. data/ext/isomorfeus_ferret_ext/email.rl +21 -0
  12. data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
  13. data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
  14. data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
  15. data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
  16. data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
  17. data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
  18. data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
  19. data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
  20. data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
  21. data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
  22. data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
  23. data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
  24. data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
  25. data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
  26. data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
  27. data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
  28. data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
  29. data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
  30. data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
  31. data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
  32. data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
  33. data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
  34. data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
  35. data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
  36. data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
  37. data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
  38. data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
  39. data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
  40. data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
  41. data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
  42. data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
  43. data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
  44. data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
  45. data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
  46. data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
  47. data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
  48. data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
  49. data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
  50. data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
  51. data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
  52. data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
  53. data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
  54. data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
  55. data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
  56. data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
  57. data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
  58. data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
  59. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
  60. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
  61. data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
  62. data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
  63. data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
  64. data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
  65. data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
  66. data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
  67. data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
  68. data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
  69. data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
  70. data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
  71. data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
  72. data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
  73. data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
  74. data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
  75. data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
  76. data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
  77. data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
  78. data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
  79. data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
  80. data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
  81. data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
  82. data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
  83. data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
  84. data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
  85. data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
  86. data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
  87. data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
  88. data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
  89. data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
  90. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
  91. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
  92. data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
  93. data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
  94. data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
  95. data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
  96. data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
  97. data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
  98. data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
  99. data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
  100. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
  101. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
  102. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
  103. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
  104. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
  105. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
  106. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
  107. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
  108. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
  109. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
  110. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
  111. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
  112. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
  113. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
  114. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
  115. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
  116. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
  117. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
  118. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
  119. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
  120. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
  121. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
  122. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
  123. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
  124. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
  125. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
  126. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
  127. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
  128. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
  129. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
  130. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
  131. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
  132. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
  133. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
  134. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
  135. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
  136. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
  137. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
  138. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
  139. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
  140. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
  141. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
  142. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
  143. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
  144. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
  145. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
  146. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
  147. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
  148. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
  149. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
  150. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
  151. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
  152. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
  153. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
  154. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
  155. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
  156. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
  157. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
  158. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
  159. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
  160. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
  161. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
  162. data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
  163. data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
  164. data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
  165. data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
  166. data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
  167. data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
  168. data/ext/isomorfeus_ferret_ext/test.c +850 -0
  169. data/ext/isomorfeus_ferret_ext/test.h +416 -0
  170. data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
  171. data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
  172. data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
  173. data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
  174. data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
  175. data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
  176. data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
  177. data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
  178. data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
  179. data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
  180. data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
  181. data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
  182. data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
  183. data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
  184. data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
  185. data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
  186. data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
  187. data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
  188. data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
  189. data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
  190. data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
  191. data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
  192. data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
  193. data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
  194. data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
  195. data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
  196. data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
  197. data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
  198. data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
  199. data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
  200. data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
  201. data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
  202. data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
  203. data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
  204. data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
  205. data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
  206. data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
  207. data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
  208. data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
  209. data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
  210. data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
  211. data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
  212. data/ext/isomorfeus_ferret_ext/url.rl +27 -0
  213. data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
  214. data/lib/isomorfeus/ferret/document.rb +132 -0
  215. data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
  216. data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
  217. data/lib/isomorfeus/ferret/index/index.rb +970 -0
  218. data/lib/isomorfeus/ferret/monitor.rb +323 -0
  219. data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
  220. data/lib/isomorfeus/ferret/version.rb +5 -0
  221. data/lib/isomorfeus-ferret.rb +8 -0
  222. metadata +307 -0
@@ -0,0 +1,485 @@
1
+ #include "frt_hash.h"
2
+ #include "frt_global.h"
3
+ #include <stdlib.h>
4
+ #include <unistd.h>
5
+ #include "test.h"
6
+ #include "testhelper.h"
7
+
8
+ static int *malloc_int(int val)
9
+ {
10
+ int *i = FRT_ALLOC(int);
11
+ *i = val;
12
+ return i;
13
+ }
14
+
15
+ static void mark_free(void *p)
16
+ {
17
+ strcpy((char *)p, "freed");
18
+ }
19
+ /**
20
+ * Basic test for string Hash. Make sure string can be retrieved
21
+ */
22
+ static void test_hash_str(TestCase *tc, void *data)
23
+ {
24
+ FrtHash *h = frt_h_new_str(NULL, &free);
25
+ FILE *f;
26
+ char buf[100], *t;
27
+ memset(buf, 0, 100);
28
+ (void)data; /* suppress unused argument warning */
29
+
30
+ Assert(frt_h_get(h, "one") == NULL, "No entries added yet");
31
+
32
+ Aiequal(0, frt_h_set(h, "one", malloc_int(1)));
33
+ Aiequal(1, *(int *)frt_h_get(h, "one"));
34
+ Aiequal(true, frt_h_del(h, "one"));
35
+
36
+ Assert(frt_h_get(h, "one") == NULL, "The Hash Entry has been deleted");
37
+
38
+ /* test that frt_h_has_key works even when value is set to null */
39
+ frt_h_set(h, "one", NULL);
40
+ Apnull(frt_h_get(h, "one"));
41
+ Atrue(frt_h_has_key(h, "one"));
42
+ frt_h_set(h, "two", malloc_int(2));
43
+ frt_h_set(h, "three", malloc_int(3));
44
+ frt_h_set(h, "four", malloc_int(4));
45
+
46
+ size_t res;
47
+ f = temp_open();
48
+ frt_h_str_print_keys(h, f);
49
+ fseek(f, 0, SEEK_SET);
50
+ res = fread(buf, 1, 100, f);
51
+ Atrue(res > 0);
52
+ fclose(f);
53
+ Asequal("keys:\n\tfour\n\tone\n\tthree\n\ttwo\n", buf);
54
+ frt_h_destroy(h);
55
+
56
+ /* test frt_h_rem with allocated key */
57
+ strcpy(buf, "key");
58
+ frt_h_new_str(&mark_free, (frt_free_ft)NULL);
59
+ frt_h_set(h, buf, (char *)"val");
60
+ Asequal("val", frt_h_get(h, "key"));
61
+ t = (char *)frt_h_rem(h, "key", false);
62
+ Asequal("val", t);
63
+ Asequal("key", buf);
64
+ frt_h_set(h, buf, (char *)"new val");
65
+ Asequal("new val", frt_h_get(h, "key"));
66
+ t = (char *)frt_h_rem(h, "key", true);
67
+ Asequal("new val", t);
68
+ Asequal("freed", buf);
69
+ frt_h_destroy(h);
70
+ }
71
+
72
+ typedef struct Point
73
+ {
74
+ int x;
75
+ int y;
76
+ } Point;
77
+
78
+ static int point_eq(const void *q1, const void *q2)
79
+ {
80
+ Point *p1 = (Point *)q1;
81
+ Point *p2 = (Point *)q2;
82
+ return p1->x == p2->x && p1->y == p2->y;
83
+ }
84
+
85
+ static unsigned long long point_hash(const void *q)
86
+ {
87
+ Point *p = (Point *)q;
88
+ return p->x * p->y;
89
+ }
90
+
91
+ static Point *point_new(int x, int y)
92
+ {
93
+ Point *p = FRT_ALLOC(Point);
94
+ p->x = x;
95
+ p->y = y;
96
+ return p;
97
+ }
98
+
99
+ /**
100
+ * Basic test for standard Hash. Make sure a non-string structure can be
101
+ * used to key the Hash
102
+ */
103
+ static void test_hash_point(TestCase *tc, void *data)
104
+ {
105
+ Point *p1 = point_new(1, 2);
106
+ Point *p2 = point_new(2, 1);
107
+ Point *p3 = point_new(1, 2);
108
+ FrtHash *h = frt_h_new(&point_hash, &point_eq, NULL, &free);
109
+ (void)data; /* suppress unused argument warning */
110
+
111
+ Assert(point_eq(p1, p3), "should be equal");
112
+
113
+ Assert(frt_h_get(h, p1) == NULL, "No entries added yet");
114
+ Assert(frt_h_get(h, p2) == NULL, "No entries added yet");
115
+ Assert(frt_h_get(h, p3) == NULL, "No entries added yet");
116
+ Aiequal(0, h->size);
117
+ Aiequal(FRT_HASH_KEY_DOES_NOT_EXIST, frt_h_set(h, p1, malloc_int(0)));
118
+ Aiequal(1, h->size);
119
+ Aiequal(0, *(int *)frt_h_get(h, p1));
120
+ Aiequal(FRT_HASH_KEY_SAME, frt_h_set(h, p1, malloc_int(1)));
121
+ Aiequal(1, h->size);
122
+ Aiequal(1, *(int *)frt_h_get(h, p1));
123
+ Aiequal(FRT_HASH_KEY_DOES_NOT_EXIST, frt_h_set(h, p2, malloc_int(2)));
124
+ Aiequal(2, h->size);
125
+ Aiequal(2, *(int *)frt_h_get(h, p2));
126
+ Aiequal(FRT_HASH_KEY_EQUAL, frt_h_set(h, p3, malloc_int(3)));
127
+ Aiequal(2, h->size);
128
+ Aiequal(3, *(int *)frt_h_get(h, p3));
129
+ Aiequal(3, *(int *)frt_h_get(h, p1));
130
+ Aiequal(true, frt_h_del(h, p1));
131
+ Aiequal(1, h->size);
132
+ Assert(frt_h_get(h, p1) == NULL, "Entry should be deleted");
133
+ frt_h_clear(h);
134
+ Assert(frt_h_get(h, p2) == NULL, "Entry should be deleted");
135
+ Aiequal(0, h->size);
136
+ frt_h_destroy(h);
137
+ free(p1);
138
+ free(p2);
139
+ free(p3);
140
+ }
141
+
142
+ /**
143
+ * Test using integers as the key. This is also an example as to how to use
144
+ * integers as the key.
145
+ */
146
+ #define HASH_INT_TEST_SIZE 1000
147
+ static void test_hash_int(TestCase *tc, void *data)
148
+ {
149
+ int i;
150
+ FrtHash *h = frt_h_new_int(&free);
151
+ char buf[100];
152
+ char *word;
153
+ (void)data; /* suppress unused argument warning */
154
+
155
+ Aiequal(0, h->size);
156
+ Aiequal(FRT_HASH_KEY_DOES_NOT_EXIST, frt_h_set_int(h, 0, frt_estrdup("one")));
157
+ Aiequal(1, h->size);
158
+ Atrue(frt_h_has_key_int(h, 0));
159
+ Assert(frt_h_set_safe_int(h, 10, frt_estrdup("ten")), "Not existing");
160
+ Assert(!frt_h_set_safe_int(h, 10, (char *)"10"), "Won't overwrite existing");
161
+ Asequal("ten", frt_h_get_int(h, 10));
162
+ Aiequal(2, h->size);
163
+ Atrue(frt_h_has_key_int(h, 10));
164
+ frt_h_set_int(h, 1000, frt_estrdup("thousand"));
165
+ Aiequal(3, h->size);
166
+ Atrue(frt_h_has_key_int(h, 1000));
167
+ Asequal("thousand", word = (char *)frt_h_rem_int(h, 1000));
168
+ Aiequal(2, h->size);
169
+ Atrue(!frt_h_has_key_int(h, 1000));
170
+ Atrue(frt_h_has_key_int(h, 10));
171
+ Atrue(!frt_h_set_safe_int(h, 10, word));
172
+ free(word);
173
+ frt_h_del_int(h, 10);
174
+
175
+ for (i = 0; i < HASH_INT_TEST_SIZE; i++) {
176
+ sprintf(buf, "<%d>", i);
177
+ frt_h_set_int(h, i, frt_estrdup(buf));
178
+ }
179
+ Asequal("<0>", frt_h_get_int(h, 0));
180
+ Asequal("<100>", frt_h_get_int(h, 100));
181
+ for (i = 0; i < HASH_INT_TEST_SIZE; i++) {
182
+ sprintf(buf, "<%d>", i);
183
+ Asequal(buf, frt_h_get_int(h, i));
184
+ }
185
+
186
+ for (i = 0; i < HASH_INT_TEST_SIZE; i++) {
187
+ frt_h_del_int(h, i);
188
+ }
189
+ Aiequal(0, h->size);
190
+
191
+ frt_h_destroy(h);
192
+ }
193
+
194
+ /**
195
+ * Test using pointers as the key. This is also an example as to how to use
196
+ * pointers as the key.
197
+ */
198
+ #define HASH_INT_TEST_SIZE 1000
199
+ static void test_hash_ptr(TestCase *tc, void *data)
200
+ {
201
+ FrtHash *h = frt_h_new_ptr(&free);
202
+ FrtSymbol word1 = rb_intern("one");
203
+ FrtSymbol word2 = rb_intern("two");
204
+ char *word_one = frt_estrdup("one");
205
+ int i;
206
+ char buf[100];
207
+ (void)data; /* suppress unused argument warning */
208
+
209
+ Aiequal(frt_ptr_hash((void *)word1), rb_intern("one"));
210
+ Atrue(frt_ptr_eq((void *)word1, (void *)rb_intern("one")));
211
+ frt_h_set(h, (void *)word1, frt_estrdup("1"));
212
+ frt_h_set(h, (void *)word2, frt_estrdup("2"));
213
+ frt_h_set(h, word_one, frt_estrdup("3"));
214
+ Asequal("1", frt_h_get(h, (void *)word1));
215
+ Asequal("2", frt_h_get(h, (void *)word2));
216
+ Asequal("3", frt_h_get(h, word_one));
217
+
218
+ Aiequal(3, h->size);
219
+ for (i = 0; i < HASH_INT_TEST_SIZE; i++) {
220
+ char *str = frt_strfmt("<%d>", i);
221
+ frt_h_set(h, (void *)rb_intern(str), str);
222
+ }
223
+ Asequal("<0>", frt_h_get(h, (void *)rb_intern("<0>")));
224
+ Asequal("<100>", frt_h_get(h, (void *)rb_intern("<100>")));
225
+ for (i = 0; i < HASH_INT_TEST_SIZE; i++) {
226
+ sprintf(buf, "<%d>", i);
227
+ Asequal(buf, frt_h_get(h, (void *)rb_intern(buf)));
228
+ }
229
+
230
+ for (i = 0; i < HASH_INT_TEST_SIZE; i++) {
231
+ sprintf(buf, "<%d>", i);
232
+ frt_h_del(h, (void *)rb_intern(buf));
233
+ }
234
+ Aiequal(3, h->size);
235
+
236
+ frt_h_destroy(h);
237
+ free(word_one);
238
+ }
239
+
240
+ /**
241
+ * Stress test the Hash. This test makes sure that the Hash still
242
+ * works as it grows in size. The test has been run with 20,000,000 elements
243
+ * on a 1Gb machine, but STRESS_SIZE is kept lower generally so that the tests
244
+ * don't take too long.
245
+ */
246
+ #define STRESS_SIZE 1000
247
+ static void stress_hash(TestCase *tc, void *data)
248
+ {
249
+ int i, j, k;
250
+ char buf[20];
251
+ (void)data; /* suppress unused argument warning */
252
+
253
+ for (k = 0; k < 1; k++) {
254
+ FrtHash *h = frt_h_new_str(&free, &free);
255
+ for (i = 0; i < STRESS_SIZE; i++) {
256
+ sprintf(buf, "(%d)", i);
257
+ if (frt_h_get(h, buf) != NULL) {
258
+ Assert(false,
259
+ "h_get returned a result when it shouldn't have\n");
260
+ return;
261
+ }
262
+ frt_h_set(h, frt_estrdup(buf), malloc_int(i));
263
+ }
264
+
265
+
266
+ for (j = 0; j < 1; j++) {
267
+ for (i = 0; i < STRESS_SIZE; i++) {
268
+ sprintf(buf, "(%d)", i);
269
+ if (i != *(int *)frt_h_get(h, buf)) {
270
+ Assert(false, "h_get returned an incorrect result\n");
271
+ return;
272
+ }
273
+ }
274
+ }
275
+
276
+ for (i = 0; i < STRESS_SIZE / 2; i++) {
277
+ sprintf(buf, "(%d)", i);
278
+ if (!frt_h_del(h, buf)) {
279
+ Assert(false, "h_del returned an error code\n");
280
+ return;
281
+ }
282
+ if (frt_h_get(h, buf) != NULL) {
283
+ Assert(false, "h_get returned an incorrect result\n");
284
+ return;
285
+ }
286
+ }
287
+
288
+ Aiequal(STRESS_SIZE / 2, h->size);
289
+ frt_h_destroy(h);
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Test that the hash table is ok while constantly growing and shrinking in
295
+ * size
296
+ */
297
+ static void test_hash_up_and_down(TestCase *tc, void *data)
298
+ {
299
+ int i, j;
300
+ char buf[20];
301
+
302
+ FrtHash *h = frt_h_new_str(&free, &free);
303
+ (void)data; /* suppress unused argument warning */
304
+
305
+ for (j = 0; j < 50; j++) {
306
+ for (i = j * 10; i < j * 10 + 10; i++) {
307
+ sprintf(buf, "(%d)", i);
308
+ if (frt_h_get(h, buf) != NULL) {
309
+ Assert(false,
310
+ "h_get returned a result when it shouldn't have\n");
311
+ return;
312
+ }
313
+ frt_h_set(h, frt_estrdup(buf), malloc_int(i));
314
+ if (i != *(int *)frt_h_get(h, buf)) {
315
+ Assert(false, "h_get returned an incorrect result\n");
316
+ return;
317
+ }
318
+ }
319
+
320
+ for (i = j * 10; i < j * 10 + 10; i++) {
321
+ sprintf(buf, "(%d)", i);
322
+ if (!frt_h_del(h, buf)) {
323
+ Assert(false, "h_del returned an error code\n");
324
+ return;
325
+ }
326
+ if (frt_h_get(h, buf) != NULL) {
327
+ Assert(false, "h_get returned an incorrect result\n");
328
+ return;
329
+ }
330
+ }
331
+ }
332
+ Aiequal(0, h->size);
333
+ frt_h_destroy(h);
334
+ }
335
+
336
+ /**
337
+ * Method used in frt_h_each test
338
+ */
339
+ static void test_each_ekv(void *key, void *value, FrtHash *h)
340
+ {
341
+ if ((strlen((char *)key) % 2) == 0) {
342
+ frt_h_del(h, key);
343
+ }
344
+ else {
345
+ frt_h_del(h, value);
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Test Hash cloning, ie. the frt_h_clone function
351
+ *
352
+ * There is also a test in here of the frt_h_each method.
353
+ */
354
+ static void test_hash_each_and_clone(TestCase *tc, void *data)
355
+ {
356
+ const char *strs[] = { "one", "two", "three", "four", "five", "six", "seven", NULL };
357
+ const char **s = strs;
358
+ FrtHash *h = frt_h_new_str(&free, &free);
359
+ FrtHash *ht2;
360
+ (void)data; /* suppress unused argument warning */
361
+
362
+ while (*s) {
363
+ frt_h_set(h, frt_estrdup(*s), frt_estrdup(*s));
364
+ s++;
365
+ }
366
+ frt_h_del(h, "two");
367
+ frt_h_del(h, "four");
368
+
369
+ Aiequal(7, h->fill);
370
+ Aiequal(5, h->size);
371
+
372
+ ht2 = frt_h_clone(h, (frt_h_clone_ft)&frt_estrdup, (frt_h_clone_ft)&frt_estrdup);
373
+
374
+ Aiequal(7, h->fill);
375
+ Aiequal(5, h->size);
376
+ Aiequal(5, ht2->fill);
377
+ Aiequal(5, ht2->size);
378
+
379
+ frt_h_del(h, "seven");
380
+
381
+ Aiequal(7, h->fill);
382
+ Aiequal(4, h->size);
383
+ Aiequal(5, ht2->fill);
384
+ Aiequal(5, ht2->size);
385
+
386
+ frt_h_each(h, (void (*)(void *k, void *v, void *a))&test_each_ekv, ht2);
387
+
388
+ Aiequal(7, h->fill);
389
+ Aiequal(4, h->size);
390
+ Aiequal(5, ht2->fill);
391
+ Aiequal(1, ht2->size);
392
+
393
+ Apnotnull(frt_h_get(ht2, "seven"));
394
+ Apnull(frt_h_get(ht2, "one"));
395
+ Apnull(frt_h_get(ht2, "two"));
396
+ Apnull(frt_h_get(ht2, "three"));
397
+ Apnull(frt_h_get(ht2, "four"));
398
+ Apnull(frt_h_get(ht2, "five"));
399
+ Apnull(frt_h_get(ht2, "six"));
400
+ frt_h_destroy(h);
401
+ frt_h_destroy(ht2);
402
+ }
403
+
404
+ /*
405
+ * The following code is given as an example of how to use the frt_h_each
406
+ * function
407
+ */
408
+
409
+ struct StringArray {
410
+ char **strings;
411
+ int cnt;
412
+ int size;
413
+ };
414
+
415
+ static void add_string_ekv(void *key, void *value, struct StringArray *str_arr)
416
+ {
417
+ (void)key; /* suppress unused argument warning */
418
+ str_arr->strings[str_arr->cnt] = (char *)value;
419
+ str_arr->cnt++;
420
+ }
421
+
422
+ static struct StringArray *frt_h_extract_strings(FrtHash *h)
423
+ {
424
+ struct StringArray *str_arr = FRT_ALLOC(struct StringArray);
425
+
426
+ str_arr->strings = FRT_ALLOC_N(char *, h->size);
427
+ str_arr->cnt = 0;
428
+ str_arr->size = h->size;
429
+
430
+ frt_h_each(h, (frt_h_each_key_val_ft)add_string_ekv, str_arr);
431
+
432
+ return str_arr;
433
+ }
434
+
435
+ /**
436
+ * Again, test the frt_h_each function, this time testing the example given in the
437
+ * documentation for the each function.
438
+ */
439
+ static void test_hash_extract_strings(TestCase *tc, void *data)
440
+ {
441
+ int i;
442
+ struct StringArray *str_arr;
443
+ const char *strs[] = {"one", "two", "three", "four", "five"};
444
+ FrtHash *h = frt_h_new_str(NULL, NULL);
445
+ (void)data; /* suppress unused argument warning */
446
+
447
+ for (i = 0; i < (int)FRT_NELEMS(strs); i++) {
448
+ frt_h_set(h, strs[i], (void *)strs[i]);
449
+ }
450
+
451
+ str_arr = frt_h_extract_strings(h);
452
+
453
+ if (Aiequal(FRT_NELEMS(strs), str_arr->size)) {
454
+ for (i = 0; i < (int)FRT_NELEMS(strs); i++) {
455
+ int j;
456
+ bool str_found = false;
457
+ for (j = 0; j < (int)FRT_NELEMS(strs); j++) {
458
+ if (strcmp(strs[i], str_arr->strings[j]) == 0) {
459
+ str_found = true;
460
+ }
461
+ }
462
+ Assert(str_found, "String was not found where it should've been");
463
+ }
464
+ }
465
+
466
+ frt_h_destroy(h);
467
+ free(str_arr->strings);
468
+ free(str_arr);
469
+ }
470
+
471
+ TestSuite *ts_hash(TestSuite *suite)
472
+ {
473
+ suite = ADD_SUITE(suite);
474
+
475
+ tst_run_test(suite, test_hash_str, NULL);
476
+ tst_run_test(suite, test_hash_point, NULL);
477
+ tst_run_test(suite, test_hash_int, NULL);
478
+ tst_run_test(suite, test_hash_ptr, NULL);
479
+ tst_run_test(suite, stress_hash, NULL);
480
+ tst_run_test(suite, test_hash_up_and_down, NULL);
481
+ tst_run_test(suite, test_hash_each_and_clone, NULL);
482
+ tst_run_test(suite, test_hash_extract_strings, NULL);
483
+
484
+ return suite;
485
+ }