cstringtrie_test.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdio.h>
  2. #include <misc/debug.h>
  3. #include <misc/array_length.h>
  4. #include <structure/CStringTrie.h>
  5. #include "cstringtrie_test_trie.h"
  6. #include <structure/CStringTrie_decl.h>
  7. #include "cstringtrie_test_trie.h"
  8. #include <structure/CStringTrie_impl.h>
  9. const char *strings[] = {
  10. "hello", "world", "hell", "he", "war", "warning", "warned", "", "heap", "why", "not", "nowhere", "neither",
  11. "normal", "how", "apple", "apear", "appreciate", "systematic", "systemic", "system", "self", "serious"
  12. };
  13. #define NUM_STRINGS B_ARRAY_LENGTH(strings)
  14. const char *other_strings[] = {
  15. "warn", "wor", "helloo", "norma", "systems", "server", "no", "when", "nothing"
  16. };
  17. #define NUM_OTHER_STRINGS B_ARRAY_LENGTH(other_strings)
  18. int main ()
  19. {
  20. int res;
  21. MyTrie trie;
  22. res = MyTrie_Init(&trie);
  23. ASSERT_FORCE(res);
  24. for (int i = 0; i < NUM_STRINGS; i++) {
  25. res = MyTrie_Set(&trie, strings[i], i);
  26. ASSERT_FORCE(res);
  27. }
  28. for (int i = 0; i < NUM_STRINGS; i++) {
  29. int value = MyTrie_Get(&trie, strings[i]);
  30. ASSERT_FORCE(value == i);
  31. }
  32. for (int i = 0; i < NUM_STRINGS; i++) {
  33. res = MyTrie_Set(&trie, strings[i], NUM_STRINGS - 1 - i);
  34. ASSERT_FORCE(res);
  35. }
  36. for (int i = 0; i < NUM_STRINGS; i++) {
  37. int value = MyTrie_Get(&trie, strings[i]);
  38. ASSERT_FORCE(value == NUM_STRINGS - 1 - i);
  39. }
  40. for (int i = 0; i < NUM_OTHER_STRINGS; i++) {
  41. int value = MyTrie_Get(&trie, other_strings[i]);
  42. ASSERT_FORCE(value == -1);
  43. }
  44. MyTrie_Free(&trie);
  45. return 0;
  46. }