Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TestTopics.cpp
00001 /************************************************************************************** 00002 * Copyright (c) 2016, Tomoaki Yamaguchi 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Tomoaki Yamaguchi - initial API and implementation 00015 * Tieto Poland Sp. z o.o. - Topic test improvements 00016 **************************************************************************************/ 00017 00018 #include <stdlib.h> 00019 #include <string.h> 00020 #include <cassert> 00021 #include "TestTopics.h" 00022 00023 using namespace std; 00024 using namespace MQTTSNGW; 00025 00026 TestTopics::TestTopics() 00027 { 00028 _topics = new Topics(); 00029 } 00030 00031 TestTopics::~TestTopics() 00032 { 00033 delete _topics; 00034 } 00035 00036 bool testIsMatch(const char* topicFilter, const char* topicName) 00037 { 00038 string* filter = new string(topicFilter); 00039 string* name = new string(topicName); 00040 00041 Topic topic(filter, MQTTSN_TOPIC_TYPE_NORMAL); 00042 bool isMatch = topic.isMatch(name); 00043 00044 delete name; 00045 00046 return isMatch; 00047 } 00048 00049 bool testGetTopicByName(const char* topicName, const char* searchedTopicName) 00050 { 00051 Topics topics; 00052 MQTTSN_topicid topicid, serchId; 00053 topicid.type = MQTTSN_TOPIC_TYPE_NORMAL; 00054 topicid.data.long_.len = strlen(topicName); 00055 topicid.data.long_.name = const_cast<char*>(topicName); 00056 00057 topics.add(&topicid); 00058 00059 serchId.type = MQTTSN_TOPIC_TYPE_NORMAL; 00060 serchId.data.long_.len = strlen(searchedTopicName); 00061 serchId.data.long_.name = const_cast<char*>(searchedTopicName); 00062 00063 return topics.getTopicByName(&serchId) != 0; 00064 } 00065 00066 bool testGetTopicById(const char* topicName, const char* searchedTopicName) 00067 { 00068 Topics topics; 00069 MQTTSN_topicid topicid, stopicid; 00070 topicid.type = MQTTSN_TOPIC_TYPE_NORMAL; 00071 topicid.data.long_.len = strlen(topicName); 00072 topicid.data.long_.name = const_cast<char*>(topicName); 00073 stopicid.type = MQTTSN_TOPIC_TYPE_NORMAL; 00074 stopicid.data.long_.len = strlen(searchedTopicName); 00075 stopicid.data.long_.name = const_cast<char*>(searchedTopicName); 00076 00077 Topic* tp = topics.add(&topicid); 00078 Topic*stp = topics.add(&stopicid); 00079 topicid.data.id = tp->getTopicId(); 00080 stopicid.data.id = stp->getTopicId(); 00081 00082 stp = topics.getTopicById(&stopicid); 00083 00084 return stp->getTopicId() == tp->getTopicId(); 00085 } 00086 00087 bool testGetPredefinedTopicByName(const char* topicName, const uint16_t id, const char* searchedTopicName) 00088 { 00089 Topics topics; 00090 MQTTSN_topicid topicid; 00091 00092 topics.add(topicName, id); 00093 00094 topicid.type = MQTTSN_TOPIC_TYPE_PREDEFINED; 00095 topicid.data.long_.len = strlen(searchedTopicName); 00096 topicid.data.long_.name = const_cast<char*>(searchedTopicName); 00097 00098 return topics.getTopicByName(&topicid) != 0; 00099 } 00100 00101 bool testGetPredefinedTopicById(const char* topicName, const uint16_t id, uint16_t sid) 00102 { 00103 Topics topics; 00104 MQTTSN_topicid topicid; 00105 00106 Topic* t = topics.add(topicName, id); 00107 00108 topicid.type = MQTTSN_TOPIC_TYPE_PREDEFINED; 00109 topicid.data.id = sid; 00110 00111 Topic* tp = topics.getTopicById(&topicid); 00112 00113 if ( tp ) 00114 { 00115 return tp->getTopicId() == id && strcmp(t->getTopicName()->c_str(), topicName) == 0; 00116 } 00117 else 00118 { 00119 return false; 00120 } 00121 } 00122 00123 void TestTopics::test(void) 00124 { 00125 const int TOPIC_COUNT = 13; 00126 00127 MQTTSN_topicid topic[TOPIC_COUNT]; 00128 char tp[TOPIC_COUNT][10]; 00129 00130 /* create Topic */ 00131 strcpy(tp[0], "Topic/+"); 00132 tp[0][7] = 0; 00133 topic[0].type = MQTTSN_TOPIC_TYPE_NORMAL; 00134 topic[0].data.long_.len = strlen(tp[0]); 00135 topic[0].data.long_.name = tp[0]; 00136 00137 for ( int i = 1; i < 10 ; i++ ) 00138 { 00139 sprintf(tp[i], "Topic/+/%d", i); 00140 topic[i].type = MQTTSN_TOPIC_TYPE_NORMAL; 00141 topic[i].data.long_.len = strlen(tp[i]); 00142 topic[i].data.long_.name = tp[i]; 00143 } 00144 00145 strcpy(tp[10], "TOPIC/#"); 00146 tp[10][7] = 0; 00147 topic[10].type = MQTTSN_TOPIC_TYPE_NORMAL; 00148 topic[10].data.long_.len = strlen(tp[10]); 00149 topic[10].data.long_.name = tp[10]; 00150 00151 strcpy(tp[11], "+/0/#"); 00152 tp[11][7] = 0; 00153 topic[11].type = MQTTSN_TOPIC_TYPE_NORMAL; 00154 topic[11].data.long_.len = strlen(tp[11]); 00155 topic[11].data.long_.name = tp[11]; 00156 00157 tp[12][0] = '#'; 00158 tp[12][1] = 0; 00159 topic[12].type = MQTTSN_TOPIC_TYPE_NORMAL; 00160 topic[12].data.long_.len = strlen(tp[12]); 00161 topic[12].data.long_.name = tp[12]; 00162 00163 00164 /* Test EraseNorma() */ 00165 for ( int i = 0; i < TOPIC_COUNT; i++ ) 00166 { 00167 MQTTSN_topicid pos = topic[i]; 00168 Topic* t = _topics->add(&pos); 00169 //printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId()); 00170 assert(t !=0); 00171 } 00172 _topics->eraseNormal(); 00173 assert(_topics->getCount() == 0); 00174 00175 /* Add Topic to Topics */ 00176 for ( int i = 0; i < TOPIC_COUNT; i++ ) 00177 { 00178 MQTTSN_topicid pos = topic[i]; 00179 Topic* t = _topics->add(&pos); 00180 //printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId()); 00181 assert(t !=0); 00182 } 00183 00184 for ( int i = 0; i < 5; i++ ) 00185 { 00186 string str = "Test/"; 00187 str += 0x30 + i; 00188 Topic* t = _topics->add(str.c_str()); 00189 //printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId()); 00190 assert(t !=0); 00191 } 00192 00193 /* Get Topic by MQTTSN_topicid by Name*/ 00194 for ( int i = 0; i < TOPIC_COUNT; i++ ) 00195 { 00196 Topic* t = _topics->getTopicByName(&topic[i]); 00197 //printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId()); 00198 assert(strcmp(t->getTopicName()->c_str(), topic[i].data.long_.name) == 0 ); 00199 } 00200 00201 /* Get Topic by MQTTSN_topicid by ID*/ 00202 for ( int i = 0; i < TOPIC_COUNT; i++ ) 00203 { 00204 Topic* t = _topics->getTopicByName(&topic[i]); 00205 MQTTSN_topicid stpid; 00206 stpid.type = MQTTSN_TOPIC_TYPE_NORMAL; 00207 stpid.data.id =t->getTopicId(); 00208 Topic* st = _topics->getTopicById(&stpid); 00209 //printf("Topic=%s ID=%d ID=%d\n", t->getTopicName()->c_str(), t->getTopicId(), st->getTopicId()); 00210 assert(t->getTopicId() == st->getTopicId() ); 00211 } 00212 00213 /* Test Wildcard */ 00214 for ( int i = 0; i < 10 ; i++ ) 00215 { 00216 MQTTSN_topicid tp1; 00217 char tp0[10]; 00218 sprintf(tp0, "Topic/%d/%d", i, i); 00219 tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; 00220 tp1.data.long_.len = strlen(tp0); 00221 tp1.data.long_.name = tp0; 00222 00223 Topic* t = _topics->match(&tp1); 00224 /* 00225 if (t) 00226 { 00227 printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str()); 00228 } 00229 else 00230 { 00231 printf("Topic=%s unmatch\n", tp0); 00232 } 00233 */ 00234 assert(t != 0); 00235 } 00236 00237 for ( int i = 0; i < 10 ; i++ ) 00238 { 00239 MQTTSN_topicid tp1; 00240 char tp0[10]; 00241 sprintf(tp0, "Topic/%d", i); 00242 tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; 00243 tp1.data.long_.len = strlen(tp0); 00244 tp1.data.long_.name = tp0; 00245 00246 Topic* t = _topics->match(&tp1); 00247 /* 00248 if (t) 00249 { 00250 printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str()); 00251 } 00252 else 00253 { 00254 printf("Topic=%s unmatch\n", tp0); 00255 } 00256 */ 00257 assert(t != 0); 00258 assert(t->getTopicName()->compare(tp[0]) == 0); 00259 } 00260 00261 for ( int i = 0; i < 10 ; i++ ) 00262 { 00263 MQTTSN_topicid tpid1; 00264 char tp0[10]; 00265 sprintf(tp0, "TOPIC/%d/%d", i, i); 00266 tpid1.type = MQTTSN_TOPIC_TYPE_NORMAL; 00267 tpid1.data.long_.len = strlen(tp0); 00268 tpid1.data.long_.name = tp0; 00269 00270 Topic* t = _topics->match(&tpid1); 00271 /* 00272 if (t) 00273 { 00274 printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str()); 00275 } 00276 else 00277 { 00278 printf("Topic=%s unmatch\n", tp0); 00279 } 00280 */ 00281 assert( t != 0); 00282 assert(t->getTopicName()->compare(tp[10]) == 0); 00283 } 00284 00285 { 00286 MQTTSN_topicid tp1; 00287 char tp0[10]; 00288 strcpy(tp0, "Topic"); 00289 tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; 00290 tp1.data.long_.len = strlen(tp0); 00291 tp1.data.long_.name = tp0; 00292 00293 Topic* t = _topics->match(&tp1); 00294 00295 assert(t != 0); 00296 assert(t->getTopicName()->compare(tp[12]) == 0); 00297 } 00298 00299 { 00300 MQTTSN_topicid tp1; 00301 char tp0[20]; 00302 strcpy(tp0, "Topic/multi/level"); 00303 tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; 00304 tp1.data.long_.len = strlen(tp0); 00305 tp1.data.long_.name = tp0; 00306 00307 Topic* t = _topics->match(&tp1); 00308 00309 assert(t != 0); 00310 assert(t->getTopicName()->compare(tp[12]) == 0); 00311 } 00312 00313 00314 assert(testIsMatch("#", "one")); 00315 assert(testIsMatch("#", "one/")); 00316 assert(testIsMatch("#", "one/two")); 00317 assert(testIsMatch("#", "one/two/")); 00318 assert(testIsMatch("#", "one/two/three")); 00319 assert(testIsMatch("#", "one/two/three/")); 00320 00321 assert(!testIsMatch("one/+", "one")); 00322 assert(testIsMatch("one/+", "one/")); 00323 assert(testIsMatch("one/+", "one/two")); 00324 assert(!testIsMatch("one/+", "one/two/")); 00325 assert(!testIsMatch("one/+", "one/two/three")); 00326 00327 assert(!testIsMatch("one/+/three/+", "one/two/three")); 00328 assert(testIsMatch("one/+/three/+", "one/two/three/")); 00329 assert(testIsMatch("one/+/three/+", "one/two/three/four")); 00330 assert(!testIsMatch("one/+/three/+", "one/two/three/four/")); 00331 00332 assert(testIsMatch("one/+/three/#", "one/two/three")); 00333 assert(testIsMatch("one/+/three/#", "one/two/three/")); 00334 assert(testIsMatch("one/+/three/#", "one/two/three/four")); 00335 assert(testIsMatch("one/+/three/#", "one/two/three/four/")); 00336 assert(testIsMatch("one/+/three/#", "one/two/three/four/five")); 00337 00338 // examples from MQTT specification 00339 assert(testIsMatch("sport/tennis/player1/#", "sport/tennis/player1")); 00340 assert(testIsMatch("sport/tennis/player1/#", "sport/tennis/player1/ranking")); 00341 assert(testIsMatch("sport/tennis/player1/#", "sport/tennis/player1/score/wimbledon")); 00342 assert(testIsMatch("sport/tennis/+", "sport/tennis/player1")); 00343 assert(testIsMatch("sport/tennis/+", "sport/tennis/player2")); 00344 assert(!testIsMatch("sport/tennis/+", "sport/tennis/player1/ranking")); 00345 assert(testIsMatch("+/+", "/finance")); 00346 assert(testIsMatch("/+", "/finance")); 00347 assert(!testIsMatch("+", "/finance")); 00348 00349 assert(testGetTopicById("mytopic", "mytopic")); 00350 assert(!testGetTopicById("mytopic", "mytop")); 00351 assert(!testGetTopicById("mytopic", "mytopiclong")); 00352 00353 assert(testGetTopicByName("mytopic", "mytopic")); 00354 assert(!testGetTopicByName("mytopic", "mytop")); 00355 assert(!testGetTopicByName("mytopic", "mytopiclong")); 00356 00357 assert(testGetPredefinedTopicByName("mypretopic", 1, "mypretopic")); 00358 assert(!testGetPredefinedTopicByName("mypretopic", 1, "mypretop")); 00359 assert(!testGetPredefinedTopicByName("mypretopic", 1, "mypretopiclong")); 00360 00361 assert(testGetPredefinedTopicById("mypretopic2", 2, 2)); 00362 assert(!testGetPredefinedTopicById("mypretopic2", 2, 1)); 00363 00364 printf("[ OK ]\n"); 00365 }
Generated on Wed Jul 13 2022 10:46:03 by
1.7.2