view src/ctrie_test.c @ 14:ef73b96fdcae

Many ctrie updates
author Eris Caffee <discordia@eldalin.com>
date Tue, 02 Oct 2012 20:00:38 -0500
parents 7886d2da8cc4
children d11dfc49b559
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "list.h"
6 #include "ctrie.h"
8 void print_val(char * key, void * data)
9 {
10 printf("%s\t\t%s\n", key, data);
11 }
13 int main(int argc, char ** argv)
14 {
15 struct ctrie * ctrie = ctrie_new();
17 char * words[] = { "hello", "interjection (used to express a greeting, answer a telephone, or attract attention.)",
18 "help", "to give or provide what is necessary to accomplish a task or satisfy a need.",
19 "helper", "a person or thing that helps or gives assistance, support, etc",
20 "helping", "the act of a person or thing that helps.",
21 "an", "indefinite article. the form of a before an initial vowel sound.",
22 "a", "indefinite article. Used before an initial consonant sound.",
23 "anne", "a female name",
24 "any", "one, a, an, or some; one or more without specification or identification",
25 "anyway", "in any case; anyhow; nonetheless; regardless",
26 "anna", "a female name",
27 "anyhow", "in any way whatever",
28 "anybody", "any person",
29 "Apple", "A computer manufacturer.",
30 "ABLE", "having necessary power, skill, resources, or qualifications",
31 /*
32 */
33 NULL, NULL
34 };
35 int i = 0;
36 while (words[i])
37 {
38 printf("Adding %s\n", words[i]);
39 ctrie_insert(ctrie, words[i], words[i+1]);
40 i += 2;
41 }
43 ctrie_dump_raw(ctrie);
44 ctrie_dump(ctrie);
45 ctrie_walk_keys(ctrie, print_val);
46 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
48 exit(EXIT_SUCCESS);
50 ////////////////////////////////////////////
51 printf("===================================================\n");
52 char word[100];;
53 strcpy(word, "any");
54 printf("searching for %s\n", word);
56 void * data = ctrie_find(ctrie, word);
57 if (NULL == data)
58 printf("Failed to find 'any'\n");
59 else
60 printf("any: %s\n", (char *) data);
63 ////////////////////////////////////////////
64 printf("===================================================\n");
65 strcpy(word, "an");
66 printf("Looking for completions of '%s':\n", word);
67 struct list * list = ctrie_get_subkeys(ctrie, word, 0);
68 if (NULL == list)
69 printf("Nothing found for '%s'\n", word);
70 else
71 {
72 printf("list size is %d\n", list_size(list));
73 struct list_iterator * it = list_begin(list);
74 struct list_iterator * next = it;
75 while (NULL != next)
76 {
77 printf("%s\n", list_value(it));
78 next = list_next(it);
79 }
80 ctrie_free_subkey_list(list);
81 }
83 ////////////////////////////////////////////
84 printf("===================================================\n");
85 strcpy(word, "a");
86 printf("Looking for first 5 completions of '%s':\n", word);
87 list = ctrie_get_subkeys(ctrie, word, 5);
88 if (NULL == list)
89 printf("Nothing found for '%s'\n", word);
90 else
91 {
92 printf("list size is %d\n", list_size(list));
93 struct list_iterator * it = list_begin(list);
94 struct list_iterator * next = it;
95 while (NULL != next)
96 {
97 printf("%s\n", list_value(it));
98 next = list_next(it);
99 }
100 ctrie_free_subkey_list(list);
101 }
103 ////////////////////////////////////////////
104 printf("===================================================\n");
105 strcpy(word, "anybody");
106 printf("remove '%s'\n", word);
107 ctrie_remove(ctrie, word);
109 strcpy(word, "ann");
110 printf("remove '%s'\n", word);
111 ctrie_remove(ctrie, word);
113 strcpy(word, "help");
114 printf("remove '%s'\n", word);
115 ctrie_remove(ctrie, word);
117 strcpy(word, "a");
118 printf("remove '%s'\n", word);
119 ctrie_remove(ctrie, word);
121 ctrie_dump(ctrie);
122 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
124 ////////////////////////////////////////////
125 printf("===================================================\n");
126 strcpy(word, "anybody");
127 printf("re-inserting '%s'\n", word);
128 ctrie_insert(ctrie, word, "fake definition");
129 ctrie_dump(ctrie);
130 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
132 exit(EXIT_SUCCESS);
133 }