1 /*----------------------------------------------------------------------------
  2 Name:      xmlBlaster/testsuite/src/c/TestUtil.c
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Test C client library
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 Compile:   cd xmlBlaster; build c
  8 Invoke:    'TestUtil'
  9 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/c.client.socket.html
 10 -----------------------------------------------------------------------------*/
 11 #include <stdio.h>
 12 #include <stdlib.h>
 13 #include <string.h>
 14 #include <util/helper.h>
 15 #include <util/msgUtil.h>
 16 #include "test.h"
 17 
 18 static const char * test_trim(const char *expected, const char *source)
 19 {
 20    char tr[256];
 21    if (expected == 0 && source == 0) {
 22       trim(0);  /* SIGSEG ? */
 23       return 0;
 24    }
 25    strcpy(tr, source);
 26    trim(tr);
 27    mu_assert2("trim()", expected, tr, strlen(tr) == strlen(expected));
 28    mu_assert2("trim()", expected, tr, strlen(tr) == strlen(expected));
 29    mu_assert2("trim()", expected, tr, strcmp(tr, expected)==0);
 30    return 0;
 31 }
 32 
 33 static const char * test_trimStart(const char *expected, const char *source)
 34 {
 35    char tr[256];
 36    if (expected == 0 && source == 0) {
 37       trim(0);  /* SIGSEG ? */
 38       return 0;
 39    }
 40    strcpy(tr, source);
 41    trimStart(tr);
 42    mu_assert2("trimStart()", expected, tr, strlen(tr) == strlen(expected));
 43    mu_assert2("trimStart()", expected, tr, strlen(tr) == strlen(expected));
 44    mu_assert2("trimStart()", expected, tr, strcmp(tr, expected)==0);
 45    return 0;
 46 }
 47 
 48 static const char * test_trimEnd(const char *expected, const char *source)
 49 {
 50    char tr[256];
 51    if (expected == 0 && source == 0) {
 52       trim(0);  /* SIGSEG ? */
 53       return 0;
 54    }
 55    strcpy(tr, source);
 56    trimEnd(tr);
 57    mu_assert2("trimEnd()", expected, tr, strlen(tr) == strlen(expected));
 58    mu_assert2("trimEnd()", expected, tr, strlen(tr) == strlen(expected));
 59    mu_assert2("trimEnd()", expected, tr, strcmp(tr, expected)==0);
 60    return 0;
 61 }
 62 
 63 static const char * test_strtok_r2() {
 64    char *token, *saveptr1, *p;
 65    const char *sep = ",";
 66    const char quotechar = '"';
 67    int j;
 68    {
 69       char *savePtr, *str = strcpyAlloc("\"H,ello\",joe,,");
 70       int count = 0;
 71       for (p=str;; count++, p = 0) {
 72          if ((token = strtok_r2(p, ",", &savePtr, '"')) == 0)
 73             break;
 74         printf("%d: %s\n", count, token);
 75       }
 76       xmlBlasterFree(str);
 77    }
 78    {
 79         char *str = strcpyAlloc("Hi,,ho");
 80         for (p=str, j=0;; j++, p = NULL) {
 81                 token = strtok_r2(p, sep, &saveptr1, quotechar);
 82                 if (token == NULL)
 83                         break;
 84                 printf("%d: %s\n", j, token);
 85                 if (j==0)
 86                         mu_assert("strtok_r2", !strcmp("Hi", token));
 87                 else if (j==1)
 88                         mu_assert("strtok_r2", !strcmp("", token));
 89                 else if (j==2)
 90                         mu_assert("strtok_r2", !strcmp("ho", token));
 91         }
 92         mu_assert("strtok_r2", j==3);
 93         xmlBlasterFree(str);
 94    }
 95    {
 96         char *str = strcpyAlloc("joe");
 97         for (p=str, j=0;; j++, p = NULL) {
 98                 if ((token = strtok_r2(p, sep, &saveptr1, quotechar)) == 0)
 99                         break;
100                 printf("%d: %s\n", j, token);
101                 if (j==0)
102                         mu_assert("strtok_r2", !strcmp("joe", token));
103         }
104         mu_assert("strtok_r2", j==1);
105         xmlBlasterFree(str);
106    }
107    {
108         char *str = strcpyAlloc("\"H,a\",joe");
109         for (p=str, j=0;; j++, p = NULL) {
110                 if ((token = strtok_r2(p, sep, &saveptr1, quotechar)) == 0)
111                         break;
112                 printf("%d: %s\n", j, token);
113                 if (j==0)
114                         mu_assert("strtok_r2", !strcmp("H,a", token));
115                 else if (j==1)
116                         mu_assert("strtok_r2", !strcmp("joe", token));
117         }
118         mu_assert("strtok_r2", j==2);
119         xmlBlasterFree(str);
120    }
121    {
122         char *str = strcpyAlloc("\"H,a\",joe");
123         for (p=str, j=0;; j++, p = NULL) {
124                 if ((token = strtok_r2(p, sep, &saveptr1, 0)) == 0)
125                         break;
126                 printf("%d: %s\n", j, token);
127                 if (j==0)
128                         mu_assert("strtok_r2", !strcmp("\"H", token));
129                 else if (j==1)
130                         mu_assert("strtok_r2", !strcmp("a\"", token));
131                 else if (j==2)
132                         mu_assert("strtok_r2", !strcmp("joe", token));
133         }
134         mu_assert("strtok_r2", j==3);
135         xmlBlasterFree(str);
136    }
137    {
138         char *str = strcpyAlloc(",");
139         for (p=str, j=0;; j++, p = NULL) {
140                 token = strtok_r2(p, sep, &saveptr1, quotechar);
141                 if (token == NULL)
142                         break;
143                 printf("%d: %s\n", j, token);
144         }
145         mu_assert("strtok_r2", j==2);
146         xmlBlasterFree(str);
147    }
148    return 0;
149 }
150 
151 static const char * test_SessionName() {
152         {
153                 const char * const str = 0;
154                 SessionName *sessionName = createSessionName(str);
155                 mu_assert("SessionName", sessionName == 0);
156                 freeSessionName(sessionName);
157         }
158         {
159                 const char * const str = "/node/heron/client/joe/session/1";
160                 SessionName *sessionName = createSessionName(str);
161                 mu_assert("SessionName", !strcmp(sessionName->nodeId, "heron"));
162                 mu_assert("SessionName", !strcmp(sessionName->subjectId, "joe"));
163                 mu_assert("SessionName", sessionName->sessionId == 1);
164                 freeSessionName(sessionName);
165         }
166         {
167                 const char * const str = "client/joe/session/1";
168                 SessionName *sessionName = createSessionName(str);
169                 mu_assert("SessionName", sessionName->nodeId == 0);
170                 mu_assert("SessionName", !strcmp(sessionName->subjectId, "joe"));
171                 mu_assert("SessionName", sessionName->sessionId == 1);
172                 freeSessionName(sessionName);
173         }
174         {
175                 const char * const str = "joe/1";
176                 SessionName *sessionName = createSessionName(str);
177                 mu_assert("SessionName", sessionName->nodeId == 0);
178                 mu_assert("SessionName", !strcmp(sessionName->subjectId, "joe"));
179                 mu_assert("SessionName", sessionName->sessionId == 1);
180                 freeSessionName(sessionName);
181         }
182         {
183                 const char * const str = "joe";
184                 SessionName *sessionName = createSessionName(str);
185                 mu_assert("SessionName", sessionName->nodeId == 0);
186                 mu_assert("SessionName", !strcmp(sessionName->subjectId, "joe"));
187                 mu_assert("SessionName", sessionName->sessionId == 0);
188                 freeSessionName(sessionName);
189         }
190         printf("SessionName success\n");
191         return 0;
192 }
193 
194 static const char *all_tests()
195 {
196    mu_run_test(test_SessionName);
197    mu_run_test(test_strtok_r2);
198 
199    mu_run_test2(test_trim, "28316", "28316");
200    mu_run_test2(test_trim, "28316", "     28316");
201    mu_run_test2(test_trim, "28316", "  \t   28316   ");
202    mu_run_test2(test_trim, "", " \t\n  ");
203    mu_run_test2(test_trim, "", "");
204    mu_run_test2(test_trim, "2", " 2 ");
205    mu_run_test2(test_trim, "2 3 4", " 2 3 4\t\n");
206    mu_run_test2(test_trim, (char *)0, (char *)0);
207 
208    mu_run_test2(test_trimStart, "28316", "28316");
209    mu_run_test2(test_trimStart, "28316", "     28316");
210    mu_run_test2(test_trimStart, "28316 \t ", "  \t   28316 \t ");
211    mu_run_test2(test_trimStart, "", " \t\n  ");
212    mu_run_test2(test_trimStart, "", "");
213    mu_run_test2(test_trimStart, "2 ", " 2 ");
214    mu_run_test2(test_trimStart, "2 3 4\t\n", " 2 3 4\t\n");
215    mu_run_test2(test_trimStart, (char *)0, (char *)0);
216 
217    mu_run_test2(test_trimEnd, "28316", "28316");
218    mu_run_test2(test_trimEnd, "28316", "28316   ");
219    mu_run_test2(test_trimEnd, "  \t   28316", "  \t   28316 \t ");
220    mu_run_test2(test_trimEnd, "", " \t\n  ");
221    mu_run_test2(test_trimEnd, "", "");
222    mu_run_test2(test_trimEnd, " 2", " 2 ");
223    mu_run_test2(test_trimEnd, "\t 2 3 4", "\t 2 3 4\t\n");
224    mu_run_test2(test_trimEnd, (char *)0, (char *)0);
225    return 0;
226 }
227 
228 /**
229  * exit(0) if OK
230  */
231 int main(int argc_, char **argv_)
232 {
233    const char *result;
234 
235    result = all_tests();
236 
237    if (result != 0) {
238       printf("[TEST FAIL] %s\n", result);
239    }
240    else {
241       printf("ALL %d TESTS PASSED\n", tests_run);
242    }
243    printf("Tests run: %d\n", tests_run);
244 
245    return result != 0;
246 }


syntax highlighted by Code2HTML, v. 0.9.1