wasabitest.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <precomp.h>
  2. #include <api/api.h>
  3. #include <api/apiinit.h>
  4. #include <api/service/svcmgr.h>
  5. #include <api/timer/timerclient.h>
  6. #include <api/timer/timermul.h>
  7. #include <api/xml/xmlreader.h>
  8. #include <bfc/string/bigstring.h>
  9. #include <bfc/string/stringdict.h>
  10. // This is the Wasabi Library Test Application
  11. DECLARE_MODULE_SVCMGR
  12. #define TIMER_TEST_DURATION (MAX_TIMER_DELAY/1000)*4 // We should at least use (MAX_TIMER_DELAY/1000)*2 here so as to test the low speed timer cycle at least twice
  13. int failed = 0;
  14. #if defined(WASABI_COMPILE_TIMERS) || defined(WASABI_COMPILE_WND) || defined(WASABI_COMPILE_TEXTMODE)
  15. int exitpump = 0;
  16. //-------------------------------------------------------------------------------------------
  17. void doMessagePump() {
  18. exitpump = 0;
  19. // Despite appearances, this is portable
  20. MSG msg;
  21. while (!exitpump && GetMessage( &msg, NULL, 0, 0 ) ) {
  22. #ifdef WASABI_COMPILE_WND
  23. TranslateMessage( &msg );
  24. #endif
  25. DispatchMessage( &msg );
  26. }
  27. }
  28. //-------------------------------------------------------------------------------------------
  29. void exitMessagePump() {
  30. exitpump = 1;
  31. }
  32. #endif
  33. //-------------------------------------------------------------------------------------------
  34. void fail(const char *module, const char *test) {
  35. failed++;
  36. printf("\n\n*** FAILED *** : %s (%s)\n\n", module, test);
  37. fflush(stdout);
  38. }
  39. //-------------------------------------------------------------------------------------------
  40. #ifdef WASABI_COMPILE_TIMERS
  41. // Multiplexed timers test
  42. int timer[10];
  43. class TestTimer : public TimerClientDI {
  44. public:
  45. TestTimer() {
  46. for (int id = 0; id < 10; id++) {
  47. timer[id] = 0;
  48. timerclient_setTimer(id+1, id*100+100);
  49. }
  50. }
  51. virtual ~TestTimer() {
  52. for (int id = 0; id < 10; id++) {
  53. timerclient_killTimer(id+1);
  54. }
  55. }
  56. virtual void timerclient_timerCallback(int id) {
  57. if (id >= 1 && id <= 10) {
  58. timer[id-1]+=timerclient_getSkipped()+1;
  59. if (id == 1 || id == 10) {
  60. printf("\r");
  61. for (int i = 0; i < 10; i++) {
  62. if (i > 0) printf(" | ");
  63. printf("%d:%3d", i+1, timer[i]);
  64. }
  65. fflush(stdout);
  66. }
  67. if (id == 10 && timer[id-1] >= TIMER_TEST_DURATION)
  68. exitMessagePump();
  69. }
  70. }
  71. };
  72. #endif
  73. //-------------------------------------------------------------------------------------------
  74. #ifdef WASABI_COMPILE_XMLPARSER
  75. // Xml parser test
  76. enum XML_TEST_TAGS {
  77. XML_TEST_ROOT,
  78. XML_TEST_TEST1,
  79. XML_TEST_TEST2,
  80. };
  81. BEGIN_STRINGDICTIONARY(_XMLTESTTAGS);
  82. SDI("WasabiTest", XML_TEST_ROOT);
  83. SDI("Test1", XML_TEST_TEST1);
  84. SDI("Test2", XML_TEST_TEST2);
  85. END_STRINGDICTIONARY(_XMLTESTTAGS, xmltesttags);
  86. class XmlTest : public XmlReaderCallbackI {
  87. public:
  88. XmlTest() : m_failed(0), m_inroot(0), m_intest1(0), m_intest2(0) {
  89. BigString str;
  90. str += "buf:";
  91. str += "<WasabiTest>\n";
  92. str += " <Test1>success</Test1>\n";
  93. str += " <Test2 result=\"success\"/>\n";
  94. str += "</WasabiTest>\n";
  95. XmlReader::registerCallback("*", static_cast<api_xmlreadercallback*>(this));
  96. XmlReader::loadFile(str, NULL, static_cast<api_xmlreadercallback*>(this));
  97. XmlReader::unregisterCallback(static_cast<api_xmlreadercallback*>(this));
  98. }
  99. virtual ~XmlTest() {
  100. }
  101. virtual int xmlReaderDisplayErrors() { return 0; }
  102. virtual void xmlReaderOnError(const char *filename, int linenum, const char *incpath, int errcode, const char *errstr) {
  103. fail("XML TEST", "Parse error");
  104. }
  105. virtual void xmlReaderOnStartElementCallback(const char *xmlpath, const char *xmltag, api_xmlreaderparams *params) {
  106. switch (xmltesttags.getId(xmltag)) {
  107. case XML_TEST_ROOT:
  108. m_inroot++;
  109. printf(" <WasabiTest>\n");
  110. fflush(stdout);
  111. break;
  112. case XML_TEST_TEST1:
  113. m_intest1++;
  114. printf(" <Test1>\n");
  115. fflush(stdout);
  116. break;
  117. case XML_TEST_TEST2:
  118. m_intest2++;
  119. printf(" <Test2\n");
  120. int _failed = 0;
  121. for (int i=0;i<params->getNbItems();i++) {
  122. printf(" %s = \"%s\"\n", params->getItemName(i), params->getItemValue(i));
  123. if (i == 1) {
  124. if (!STRCASEEQLSAFE(params->getItemValue(i), "success")) _failed = 1;
  125. if (!STRCASEEQLSAFE(params->getItemName(i), "result")) _failed = 1;
  126. }
  127. }
  128. if (_failed) {
  129. m_failed = 1;
  130. fail("XML PARSER", "Not receiving the right params in Test2");
  131. }
  132. fflush(stdout);
  133. break;
  134. }
  135. }
  136. virtual void xmlReaderOnEndElementCallback(const char *xmlpath, const char *xmltag) {
  137. switch (xmltesttags.getId(xmltag)) {
  138. case XML_TEST_ROOT:
  139. m_inroot--;
  140. printf(" </WasabiTest>\n");
  141. fflush(stdout);
  142. break;
  143. case XML_TEST_TEST1:
  144. m_intest1--;
  145. printf(" </Test1>\n");
  146. fflush(stdout);
  147. break;
  148. case XML_TEST_TEST2:
  149. m_intest2--;
  150. printf(" />\n");
  151. fflush(stdout);
  152. break;
  153. }
  154. }
  155. virtual void xmlReaderOnCharacterDataCallback(const char *xmlpath, const char *xmltag, const char *str) {
  156. if (m_intest1) {
  157. printf(" Character Data reads \"%s\"\n", str);
  158. if (!STRCASEEQL(str, "success")) {
  159. m_failed = 1;
  160. fail("XML PARSER", "Not receiving the right character data in Test1");
  161. }
  162. fflush(stdout);
  163. }
  164. }
  165. int didFail() { return m_failed; }
  166. private:
  167. int m_failed;
  168. int m_inroot;
  169. int m_intest1;
  170. int m_intest2;
  171. };
  172. #endif
  173. //-------------------------------------------------------------------------------------------
  174. // 03F73CE0-987D-46fd-B2E3-DBB364A47F54
  175. static const GUID myappguid = { 0x3f73ce0, 0x987d, 0x46fd, { 0xb2, 0xe3, 0xdb, 0xb3, 0x64, 0xa4, 0x7f, 0x54 }};
  176. int main(int argc, char **argv) {
  177. printf("-------------------------------------------------------------------------------\n");
  178. printf("Initializing Wasabi API.\n");
  179. printf("-------------------------------------------------------------------------------\n");
  180. fflush(stdout);
  181. ApiInit::init((HINSTANCE)0, myappguid, "", (HWND)NULL);
  182. printf("\n- PASSED -\n\n");
  183. fflush(stdout);
  184. // Rudimentary svcmgr test: enumerate services
  185. printf("-------------------------------------------------------------------------------\n");
  186. printf("Testing service manager.\n");
  187. printf("-------------------------------------------------------------------------------\n\n");
  188. int n=ServiceManager::getNumServicesByGuid();
  189. printf("Services running : %d\n\n", n);
  190. fflush(stdout);
  191. int i;
  192. for (i=0;i<n;i++) {
  193. waServiceFactory *factory = ServiceManager::enumService(i);
  194. char sguid[256];
  195. nsGUID::toChar(factory->getGuid(), sguid);
  196. printf(" %d : %s (%s)\n", i, factory->getServiceName(), sguid);
  197. }
  198. if (i > 0) {
  199. printf("\n- PASSED -\n\n");
  200. } else {
  201. fail("SVCMGR", "service enumerator returns no service present");
  202. }
  203. fflush(stdout);
  204. #ifdef WASABI_COMPILE_TIMERS
  205. {
  206. DWORD ds = Std::getTickCount();
  207. // Multiplexed timers test
  208. printf("-------------------------------------------------------------------------------\n");
  209. printf("Testing timers for %d seconds.\n", TIMER_TEST_DURATION);
  210. printf("-------------------------------------------------------------------------------\n\n");
  211. fflush(stdout);
  212. TestTimer tt;
  213. doMessagePump();
  214. printf("\n");
  215. DWORD dt = Std::getTickCount() - ds;
  216. // let's assume this is a VERY VERY busy cpu, we're just trying to determine if the timers have been running...
  217. if (dt < (TIMER_TEST_DURATION-1)*1000 || dt > (TIMER_TEST_DURATION+1)*1000) {
  218. fail("TIMERS", "Test was out of range by more than 1s");
  219. } else {
  220. printf("\n- PASSED -\n\n");
  221. }
  222. fflush(stdout);
  223. }
  224. #endif
  225. #ifdef WASABI_COMPILE_XMLPARSER
  226. {
  227. // Multiplexed timers test
  228. printf("-------------------------------------------------------------------------------\n");
  229. printf("Testing XML parser.\n");
  230. printf("-------------------------------------------------------------------------------\n\n");
  231. fflush(stdout);
  232. XmlTest xt;
  233. if (!xt.didFail()) {
  234. printf("\n- PASSED -\n\n");
  235. fflush(stdout);
  236. } // else msg is already printed
  237. }
  238. #endif
  239. printf("-------------------------------------------------------------------------------\n");
  240. printf("Shutting down.\n");
  241. printf("-------------------------------------------------------------------------------\n");
  242. ApiInit::shutdown();
  243. printf("\n- PASSED -\n\n");
  244. printf("===============================================================================\n");
  245. printf("Result : ");
  246. if (failed)
  247. printf("*** %d FAILURE%s ***\n", failed, failed > 1 ? "S" : "");
  248. else
  249. printf("- ALL PASSED -\n");
  250. printf("===============================================================================\n\n");
  251. return 0;
  252. }