trie.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright (c) 2013 Martin Sustrik All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include "../src/protocols/pubsub/trie.c"
  20. #include "../src/utils/alloc.c"
  21. #include "../src/utils/err.c"
  22. #include <stdio.h>
  23. int main ()
  24. {
  25. int rc;
  26. struct nn_trie trie;
  27. /* Try matching with an empty trie. */
  28. nn_trie_init (&trie);
  29. rc = nn_trie_match (&trie, (const uint8_t*) "", 0);
  30. nn_assert (rc == 0);
  31. rc = nn_trie_match (&trie, (const uint8_t*) "ABC", 3);
  32. nn_assert (rc == 0);
  33. nn_trie_term (&trie);
  34. /* Try matching with "all" subscription. */
  35. nn_trie_init (&trie);
  36. rc = nn_trie_subscribe (&trie, (const uint8_t*) "", 0);
  37. nn_assert (rc == 1);
  38. rc = nn_trie_match (&trie, (const uint8_t*) "", 0);
  39. nn_assert (rc == 1);
  40. rc = nn_trie_match (&trie, (const uint8_t*) "ABC", 3);
  41. nn_assert (rc == 1);
  42. nn_trie_term (&trie);
  43. /* Try some simple matching. */
  44. nn_trie_init (&trie);
  45. rc = nn_trie_subscribe (&trie, (const uint8_t*) "ABC", 3);
  46. nn_assert (rc == 1);
  47. rc = nn_trie_match (&trie, (const uint8_t*) "DEF", 3);
  48. nn_assert (rc == 0);
  49. rc = nn_trie_match (&trie, (const uint8_t*) "AB", 2);
  50. nn_assert (rc == 0);
  51. rc = nn_trie_match (&trie, (const uint8_t*) "ABC", 3);
  52. nn_assert (rc == 1);
  53. rc = nn_trie_match (&trie, (const uint8_t*) "ABCDE", 5);
  54. nn_assert (rc == 1);
  55. nn_trie_term (&trie);
  56. /* Try a long subcsription. */
  57. nn_trie_init (&trie);
  58. rc = nn_trie_subscribe (&trie,
  59. (const uint8_t*) "01234567890123456789012345678901234", 35);
  60. nn_assert (rc == 1);
  61. rc = nn_trie_match (&trie, (const uint8_t*) "", 0);
  62. nn_assert (rc == 0);
  63. rc = nn_trie_match (&trie, (const uint8_t*) "012456789", 10);
  64. nn_assert (rc == 0);
  65. rc = nn_trie_match (&trie, (const uint8_t*) "012345678901234567", 18);
  66. nn_assert (rc == 0);
  67. rc = nn_trie_match (&trie,
  68. (const uint8_t*) "01234567890123456789012345678901234", 35);
  69. nn_assert (rc == 1);
  70. nn_trie_term (&trie);
  71. /* Try matching with a sparse node involved. */
  72. nn_trie_init (&trie);
  73. rc = nn_trie_subscribe (&trie, (const uint8_t*) "ABC", 3);
  74. nn_assert (rc == 1);
  75. rc = nn_trie_subscribe (&trie, (const uint8_t*) "ADE", 3);
  76. nn_assert (rc == 1);
  77. rc = nn_trie_match (&trie, (const uint8_t*) "A", 1);
  78. nn_assert (rc == 0);
  79. rc = nn_trie_match (&trie, (const uint8_t*) "AD", 2);
  80. nn_assert (rc == 0);
  81. nn_trie_term (&trie);
  82. /* Try matching with a dense node involved. */
  83. nn_trie_init (&trie);
  84. rc = nn_trie_subscribe (&trie, (const uint8_t*) "A", 1);
  85. nn_assert (rc == 1);
  86. rc = nn_trie_subscribe (&trie, (const uint8_t*) "B", 1);
  87. nn_assert (rc == 1);
  88. rc = nn_trie_subscribe (&trie, (const uint8_t*) "C", 1);
  89. nn_assert (rc == 1);
  90. rc = nn_trie_subscribe (&trie, (const uint8_t*) "0", 1);
  91. nn_assert (rc == 1);
  92. rc = nn_trie_subscribe (&trie, (const uint8_t*) "E", 1);
  93. nn_assert (rc == 1);
  94. rc = nn_trie_subscribe (&trie, (const uint8_t*) "F", 1);
  95. nn_assert (rc == 1);
  96. rc = nn_trie_subscribe (&trie, (const uint8_t*) "1", 1);
  97. nn_assert (rc == 1);
  98. rc = nn_trie_subscribe (&trie, (const uint8_t*) "@", 1);
  99. nn_assert (rc == 1);
  100. rc = nn_trie_subscribe (&trie, (const uint8_t*) "b", 1);
  101. nn_assert (rc == 1);
  102. rc = nn_trie_subscribe (&trie, (const uint8_t*) "f", 1);
  103. nn_assert (rc == 1);
  104. rc = nn_trie_match (&trie, (const uint8_t*) "0", 1);
  105. nn_assert (rc == 1);
  106. rc = nn_trie_match (&trie, (const uint8_t*) "A", 1);
  107. nn_assert (rc == 1);
  108. rc = nn_trie_match (&trie, (const uint8_t*) "f", 1);
  109. nn_assert (rc == 1);
  110. rc = nn_trie_match (&trie, (const uint8_t*) "000", 3);
  111. nn_assert (rc == 1);
  112. rc = nn_trie_match (&trie, (const uint8_t*) "a", 1);
  113. nn_assert (rc == 0);
  114. rc = nn_trie_match (&trie, (const uint8_t*) "c", 1);
  115. nn_assert (rc == 0);
  116. nn_trie_term (&trie);
  117. /* Check prefix splitting and compaction. */
  118. nn_trie_init (&trie);
  119. rc = nn_trie_subscribe (&trie, (const uint8_t*) "ABCD", 4);
  120. nn_assert (rc == 1);
  121. rc = nn_trie_subscribe (&trie, (const uint8_t*) "AB", 2);
  122. nn_assert (rc == 1);
  123. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "AB", 2);
  124. nn_assert (rc == 1);
  125. rc = nn_trie_match (&trie, (const uint8_t*) "AB", 2);
  126. nn_assert (rc == 0);
  127. rc = nn_trie_match (&trie, (const uint8_t*) "ABCDEF", 6);
  128. nn_assert (rc == 1);
  129. rc = nn_trie_subscribe (&trie, (const uint8_t*) "ABEF", 4);
  130. nn_assert (rc == 1);
  131. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "ABCD", 4);
  132. nn_assert (rc == 1);
  133. rc = nn_trie_match (&trie, (const uint8_t*) "ABCD", 4);
  134. nn_assert (rc == 0);
  135. rc = nn_trie_match (&trie, (const uint8_t*) "ABEF", 4);
  136. nn_assert (rc == 1);
  137. nn_trie_term (&trie);
  138. /* Check whether there's no problem with removing all subscriptions. */
  139. nn_trie_init (&trie);
  140. rc = nn_trie_subscribe (&trie, (const uint8_t*) "A", 1);
  141. nn_assert (rc == 1);
  142. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "A", 1);
  143. nn_assert (rc == 1);
  144. rc = nn_trie_match (&trie, (const uint8_t*) "", 0);
  145. nn_assert (rc == 0);
  146. rc = nn_trie_match (&trie, (const uint8_t*) "A", 1);
  147. nn_assert (rc == 0);
  148. nn_trie_term (&trie);
  149. /* Check converting from sparse node to dense node and vice versa. */
  150. nn_trie_init (&trie);
  151. rc = nn_trie_subscribe (&trie, (const uint8_t*) "A", 1);
  152. nn_assert (rc == 1);
  153. rc = nn_trie_subscribe (&trie, (const uint8_t*) "B", 1);
  154. nn_assert (rc == 1);
  155. rc = nn_trie_subscribe (&trie, (const uint8_t*) "C", 1);
  156. nn_assert (rc == 1);
  157. rc = nn_trie_subscribe (&trie, (const uint8_t*) "0", 1);
  158. nn_assert (rc == 1);
  159. rc = nn_trie_subscribe (&trie, (const uint8_t*) "E", 1);
  160. nn_assert (rc == 1);
  161. rc = nn_trie_subscribe (&trie, (const uint8_t*) "F", 1);
  162. nn_assert (rc == 1);
  163. rc = nn_trie_subscribe (&trie, (const uint8_t*) "1", 1);
  164. nn_assert (rc == 1);
  165. rc = nn_trie_subscribe (&trie, (const uint8_t*) "@", 1);
  166. nn_assert (rc == 1);
  167. rc = nn_trie_subscribe (&trie, (const uint8_t*) "b", 1);
  168. nn_assert (rc == 1);
  169. rc = nn_trie_subscribe (&trie, (const uint8_t*) "f", 1);
  170. nn_assert (rc == 1);
  171. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "0", 1);
  172. nn_assert (rc == 1);
  173. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "f", 1);
  174. nn_assert (rc == 1);
  175. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "E", 1);
  176. nn_assert (rc == 1);
  177. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "B", 1);
  178. nn_assert (rc == 1);
  179. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "A", 1);
  180. nn_assert (rc == 1);
  181. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "1", 1);
  182. nn_assert (rc == 1);
  183. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "@", 1);
  184. nn_assert (rc == 1);
  185. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "F", 1);
  186. nn_assert (rc == 1);
  187. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "C", 1);
  188. nn_assert (rc == 1);
  189. rc = nn_trie_unsubscribe (&trie, (const uint8_t*) "b", 1);
  190. nn_assert (rc == 1);
  191. nn_trie_term (&trie);
  192. return 0;
  193. }