1 #include <cstdio>
2 #include <cstring>
3 #include <mosquittopp.h>
4
print_error(const char * topic,char ** topics,int topic_count)5 void print_error(const char *topic, char **topics, int topic_count)
6 {
7 int i;
8 printf("TOPIC: %s\n", topic);
9 printf("TOKENS: ");
10 for(i=0; i<topic_count; i++){
11 printf("%s", topics[i]);
12 if(i+1<topic_count){
13 printf("/");
14 }
15 }
16 printf("\n");
17 }
18
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 char **topics;
22 int topic_count;
23
24 if(mosqpp::sub_topic_tokenise("topic", &topics, &topic_count)){
25 printf("Out of memory.\n");
26 return 1;
27 }
28 if(topic_count != 1 || strcmp(topics[0], "topic")){
29 print_error("topic", topics, topic_count);
30 return 1;
31 }
32
33 if(mosqpp::sub_topic_tokenise("a/deep/topic/hierarchy", &topics, &topic_count)){
34 printf("Out of memory.\n");
35 return 1;
36 }
37 if(topic_count != 4
38 || strcmp(topics[0], "a")
39 || strcmp(topics[1], "deep")
40 || strcmp(topics[2], "topic")
41 || strcmp(topics[3], "hierarchy")){
42 print_error("a/deep/topic/hierarchy", topics, topic_count);
43 return 1;
44 }
45
46 if(mosqpp::sub_topic_tokenise("/a/deep/topic/hierarchy", &topics, &topic_count)){
47 printf("Out of memory.\n");
48 return 1;
49 }
50 if(topic_count != 5
51 || topics[0]
52 || strcmp(topics[1], "a")
53 || strcmp(topics[2], "deep")
54 || strcmp(topics[3], "topic")
55 || strcmp(topics[4], "hierarchy")){
56 print_error("/a/deep/topic/hierarchy", topics, topic_count);
57 return 1;
58 }
59
60 if(mosqpp::sub_topic_tokenise("a/b/c", &topics, &topic_count)){
61 printf("Out of memory.\n");
62 return 1;
63 }
64 if(topic_count != 3
65 || strcmp(topics[0], "a")
66 || strcmp(topics[1], "b")
67 || strcmp(topics[2], "c")){
68 print_error("a/b/c", topics, topic_count);
69 return 1;
70 }
71
72 if(mosqpp::sub_topic_tokenise("/a/b/c", &topics, &topic_count)){
73 printf("Out of memory.\n");
74 return 1;
75 }
76 if(topic_count != 4
77 || topics[0]
78 || strcmp(topics[1], "a")
79 || strcmp(topics[2], "b")
80 || strcmp(topics[3], "c")){
81 print_error("/a/b/c", topics, topic_count);
82 return 1;
83 }
84
85 if(mosqpp::sub_topic_tokenise("a///hierarchy", &topics, &topic_count)){
86 printf("Out of memory.\n");
87 return 1;
88 }
89 if(topic_count != 4
90 || strcmp(topics[0], "a")
91 || topics[1]
92 || topics[2]
93 || strcmp(topics[3], "hierarchy")){
94 print_error("a///hierarchy", topics, topic_count);
95 return 1;
96 }
97
98 if(mosqpp::sub_topic_tokenise("/a///hierarchy", &topics, &topic_count)){
99 printf("Out of memory.\n");
100 return 1;
101 }
102 if(topic_count != 5
103 || topics[0]
104 || strcmp(topics[1], "a")
105 || topics[2]
106 || topics[3]
107 || strcmp(topics[4], "hierarchy")){
108 print_error("/a///hierarchy", topics, topic_count);
109 return 1;
110 }
111
112 if(mosqpp::sub_topic_tokenise("/a///hierarchy/", &topics, &topic_count)){
113 printf("Out of memory.\n");
114 return 1;
115 }
116 if(topic_count != 6
117 || topics[0]
118 || strcmp(topics[1], "a")
119 || topics[2]
120 || topics[3]
121 || strcmp(topics[4], "hierarchy")
122 || topics[3]){
123 print_error("/a///hierarchy/", topics, topic_count);
124 return 1;
125 }
126
127 return 0;
128 }
129
130