summaryrefslogtreecommitdiffstats
path: root/Src/Wasabi/wasabitest.cpp
blob: e88775cb64c483ead96f27e72b300b6dcaf338d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <precomp.h>
#include <api/api.h>
#include <api/apiinit.h>
#include <api/service/svcmgr.h>
#include <api/timer/timerclient.h>
#include <api/timer/timermul.h>
#include <api/xml/xmlreader.h>
#include <bfc/string/bigstring.h>
#include <bfc/string/stringdict.h>

// This is the Wasabi Library Test Application

DECLARE_MODULE_SVCMGR

#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

int failed = 0;

#if defined(WASABI_COMPILE_TIMERS) || defined(WASABI_COMPILE_WND) || defined(WASABI_COMPILE_TEXTMODE)

int exitpump = 0;

//-------------------------------------------------------------------------------------------

void doMessagePump() {
  exitpump = 0;
  // Despite appearances, this is portable
  MSG msg;
  while (!exitpump && GetMessage( &msg, NULL, 0, 0 ) ) {
#ifdef WASABI_COMPILE_WND
    TranslateMessage( &msg );
#endif
    DispatchMessage( &msg );
  }
}

//-------------------------------------------------------------------------------------------

void exitMessagePump() {
  exitpump = 1;
}

#endif

//-------------------------------------------------------------------------------------------
void fail(const char *module, const char *test) {
  failed++;
  printf("\n\n*** FAILED *** : %s (%s)\n\n", module, test);
  fflush(stdout);
}

//-------------------------------------------------------------------------------------------

#ifdef WASABI_COMPILE_TIMERS

// Multiplexed timers test

int timer[10];

class TestTimer : public TimerClientDI {
public:
  TestTimer() {
    for (int id = 0; id < 10; id++) {
      timer[id] = 0;
      timerclient_setTimer(id+1, id*100+100);
    }
  }
  virtual ~TestTimer() {
    for (int id = 0; id < 10; id++) {
      timerclient_killTimer(id+1);
    }
  }
  virtual void timerclient_timerCallback(int id) {
    if (id >= 1 && id <= 10) {
      timer[id-1]+=timerclient_getSkipped()+1;
      if (id == 1 || id == 10) {
        printf("\r");
        for (int i = 0; i < 10; i++) {
          if (i > 0) printf(" | ");
          printf("%d:%3d", i+1, timer[i]);
        }
        fflush(stdout);
      }
    if (id == 10 && timer[id-1] >= TIMER_TEST_DURATION)
      exitMessagePump();
    }
  }
};
#endif

//-------------------------------------------------------------------------------------------
#ifdef WASABI_COMPILE_XMLPARSER
// Xml parser test
enum XML_TEST_TAGS {
  XML_TEST_ROOT,
  XML_TEST_TEST1,
  XML_TEST_TEST2,
};

BEGIN_STRINGDICTIONARY(_XMLTESTTAGS);
SDI("WasabiTest", XML_TEST_ROOT);
SDI("Test1",      XML_TEST_TEST1);
SDI("Test2",      XML_TEST_TEST2);
END_STRINGDICTIONARY(_XMLTESTTAGS, xmltesttags);

class XmlTest : public XmlReaderCallbackI {
public:
  XmlTest() : m_failed(0), m_inroot(0), m_intest1(0), m_intest2(0) {
    BigString str;
    str += "buf:";
    str += "<WasabiTest>\n";
    str += " <Test1>success</Test1>\n";
    str += " <Test2 result=\"success\"/>\n";
    str += "</WasabiTest>\n";
    XmlReader::registerCallback("*", static_cast<api_xmlreadercallback*>(this));
    XmlReader::loadFile(str, NULL, static_cast<api_xmlreadercallback*>(this));
    XmlReader::unregisterCallback(static_cast<api_xmlreadercallback*>(this));
  }
  virtual ~XmlTest() {
  }
  virtual int xmlReaderDisplayErrors() { return 0; }
  virtual void xmlReaderOnError(const char *filename, int linenum, const char *incpath, int errcode, const char *errstr) {
    fail("XML TEST", "Parse error");
  }
  virtual void xmlReaderOnStartElementCallback(const char *xmlpath, const char *xmltag, api_xmlreaderparams *params) {
    switch (xmltesttags.getId(xmltag)) {
      case XML_TEST_ROOT:
        m_inroot++;
        printf("  <WasabiTest>\n");
        fflush(stdout);
        break;
      case XML_TEST_TEST1:
        m_intest1++;
        printf("    <Test1>\n");
        fflush(stdout);
        break;
      case XML_TEST_TEST2:
        m_intest2++;
        printf("    <Test2\n");
        int _failed = 0;
        for (int i=0;i<params->getNbItems();i++) {
          printf("      %s = \"%s\"\n", params->getItemName(i), params->getItemValue(i));
          if (i == 1) {
            if (!STRCASEEQLSAFE(params->getItemValue(i), "success")) _failed = 1;
            if (!STRCASEEQLSAFE(params->getItemName(i), "result")) _failed = 1;
          }
        }
        if (_failed) {
          m_failed = 1;
          fail("XML PARSER", "Not receiving the right params in Test2");
        }
        fflush(stdout);
        break;
    }
  }
  virtual void xmlReaderOnEndElementCallback(const char *xmlpath, const char *xmltag) {
    switch (xmltesttags.getId(xmltag)) {
      case XML_TEST_ROOT:
        m_inroot--;
        printf("  </WasabiTest>\n");
        fflush(stdout);
        break;
      case XML_TEST_TEST1:
        m_intest1--;
        printf("    </Test1>\n");
        fflush(stdout);
        break;
      case XML_TEST_TEST2:
        m_intest2--;
        printf("    />\n");
        fflush(stdout);
        break;
    }
  }
  virtual void xmlReaderOnCharacterDataCallback(const char *xmlpath, const char *xmltag, const char *str) {
    if (m_intest1) {
      printf("      Character Data reads \"%s\"\n", str);
      if (!STRCASEEQL(str, "success")) {
        m_failed = 1;
        fail("XML PARSER", "Not receiving the right character data in Test1");
      }
      fflush(stdout);
    }
  }
  int didFail() { return m_failed; }
private:
  int m_failed;
  int m_inroot;
  int m_intest1;
  int m_intest2;
};

#endif
//-------------------------------------------------------------------------------------------

// 03F73CE0-987D-46fd-B2E3-DBB364A47F54
static const GUID myappguid = { 0x3f73ce0, 0x987d, 0x46fd, { 0xb2, 0xe3, 0xdb, 0xb3, 0x64, 0xa4, 0x7f, 0x54 }};

int main(int argc, char **argv) {
  printf("-------------------------------------------------------------------------------\n");
  printf("Initializing Wasabi API.\n");
  printf("-------------------------------------------------------------------------------\n");
  fflush(stdout);

  ApiInit::init((HINSTANCE)0, myappguid, "", (HWND)NULL);

  printf("\n- PASSED -\n\n");
  fflush(stdout);

  // Rudimentary svcmgr test: enumerate services
  printf("-------------------------------------------------------------------------------\n");
  printf("Testing service manager.\n");
  printf("-------------------------------------------------------------------------------\n\n");
  int n=ServiceManager::getNumServicesByGuid();
  printf("Services running : %d\n\n", n);
  fflush(stdout);
  int i;
  for (i=0;i<n;i++) {
    waServiceFactory *factory = ServiceManager::enumService(i);
    char sguid[256];
    nsGUID::toChar(factory->getGuid(), sguid);
    printf("  %d : %s (%s)\n", i, factory->getServiceName(), sguid);
  }
  if (i > 0) {
  printf("\n- PASSED -\n\n");
  } else {
    fail("SVCMGR", "service enumerator returns no service present");
  }
  fflush(stdout);

#ifdef WASABI_COMPILE_TIMERS
  {
    DWORD ds = Std::getTickCount();
    // Multiplexed timers test
    printf("-------------------------------------------------------------------------------\n");
    printf("Testing timers for %d seconds.\n", TIMER_TEST_DURATION);
    printf("-------------------------------------------------------------------------------\n\n");
    fflush(stdout);
    TestTimer tt;
    doMessagePump();
    printf("\n");
    DWORD dt = Std::getTickCount() - ds;
    // let's assume this is a VERY VERY busy cpu, we're just trying to determine if the timers have been running...
    if (dt < (TIMER_TEST_DURATION-1)*1000 || dt > (TIMER_TEST_DURATION+1)*1000) {
      fail("TIMERS", "Test was out of range by more than 1s");
    } else {
      printf("\n- PASSED -\n\n");
    }
    fflush(stdout);
  }
#endif

#ifdef WASABI_COMPILE_XMLPARSER
  {
    // Multiplexed timers test
    printf("-------------------------------------------------------------------------------\n");
    printf("Testing XML parser.\n");
    printf("-------------------------------------------------------------------------------\n\n");
    fflush(stdout);
    XmlTest xt;
    if (!xt.didFail()) {
      printf("\n- PASSED -\n\n");
      fflush(stdout);
    } // else msg is already printed
  }
#endif

  printf("-------------------------------------------------------------------------------\n");
  printf("Shutting down.\n");
  printf("-------------------------------------------------------------------------------\n");

  ApiInit::shutdown();

  printf("\n- PASSED -\n\n");
  printf("===============================================================================\n");
  printf("Result : ");
  if (failed)
    printf("*** %d FAILURE%s ***\n", failed, failed > 1 ? "S" : "");
  else
    printf("- ALL PASSED -\n");
  printf("===============================================================================\n\n");

  return 0;
}