gwenhywfar  4.8.0beta
testlib.c
Go to the documentation of this file.
1 
2 
3 #include <gwenhywfar/buffer.h>
4 #include <gwenhywfar/base64.h>
5 #include <gwenhywfar/debug.h>
6 #include <gwenhywfar/padd.h>
7 #include <gwenhywfar/cgui.h>
8 #include <gwenhywfar/directory.h>
9 #include <gwenhywfar/list.h>
10 #include <gwenhywfar/pathmanager.h>
11 #include <errno.h>
12 #include "gwenhywfar.h"
13 
14 
15 
16 int check1() {
17  const char *testString="01234567890123456789";
18  int rv;
19  GWEN_BUFFER *buf1;
20  GWEN_BUFFER *buf2;
21  const char *p1, *p2;
22  int i;
23  int len;
24 
25  fprintf(stderr, "Check 1 ...");
26 
27  buf1=GWEN_Buffer_new(0, 256, 0, 1);
28  rv=GWEN_Base64_Encode((const unsigned char*)testString,
29  strlen(testString),
30  buf1, 0);
31  if (rv) {
32  fprintf(stderr, "FAILED: Could not encode.\n");
33  return 2;
34  }
35 
36  buf2=GWEN_Buffer_new(0, 256, 0, 1);
37  rv=GWEN_Base64_Decode((const unsigned char*)GWEN_Buffer_GetStart(buf1), 0,
38  buf2);
39  if (rv) {
40  fprintf(stderr, "FAILED: Could not decode.\n");
41  return 2;
42  }
43 
44  p1=testString;
45  len=strlen(testString);
46  p2=GWEN_Buffer_GetStart(buf2);
47  if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
48  fprintf(stderr, "Data differs in size\n");
49  return 3;
50  }
51  rv=0;
52  for (i=0; i<len; i++) {
53  if (p1[i]!=p2[i]) {
54  fprintf(stderr, "Buffer1:\n%s\n", testString);
55  fprintf(stderr, "Buffer2:\n");
56  GWEN_Buffer_Dump(buf2, 2);
57 
58  fprintf(stderr, "Differ at %d (%04x)\n", i, i);
59  rv=-1;
60  }
61  }
62 
63  if (rv) {
64  fprintf(stderr, "Data differs in content\n");
65  return 3;
66  }
67 
68  fprintf(stderr, "PASSED.\n");
69 
70  return 0;
71 }
72 
73 
74 
75 int test_gui(int test_with_interaction) {
76  char buffer[50];
77  int rv;
78  GWEN_GUI *gui = GWEN_Gui_CGui_new();
79 
80  /* Set the static GUI object */
81  assert(gui);
82  GWEN_Gui_SetGui(gui);
84 
85  rv = GWEN_Gui_ShowBox(0,
86  "This is a ShowBox test title",
87  "This is a ShowBox test.",
88  0);
89  printf("GWEN_Gui_ShowBox: rv=%d\n", rv);
90  GWEN_Gui_HideBox(rv);
91  printf("GWEN_Gui_HideBox called.\n\n");
92 
93  if (test_with_interaction) {
94  rv = GWEN_Gui_InputBox(0,
95  "This is a InputBox test title",
96  "Just enter something.",
97  buffer,
98  1, 40,
99  0);
100  printf("GWEN_Gui_InputBox: rv=%d, result=\"%s\"\n\n",
101  rv, buffer);
102 
103  rv = GWEN_Gui_MessageBox(0,
104  "Third test title, this time MessageBox",
105  "Just press the first or second button.",
106  "First button.", "Second button", NULL,
107  0);
108  printf("GWEN_Gui_MessageBox: rv=%d; button=%s\n", rv,
109  (rv == 1 ? "first" : (rv == 2 ? "second" : "unknown")));
110  }
111 
112  GWEN_Gui_free(gui);
113  return 0;
114 }
115 
116 
117 
118 #ifndef MAX_PATH
119 # define MAX_PATH 200
120 #endif
122 {
123  char tmpdir[MAX_PATH];
124  GWEN_DIRECTORY *dir;
125  int rv;
126 
128  printf("GWEN_Directory_GetTmpDirectory returns \"%s\" as tmp directory\n",
129  tmpdir);
130 
131  dir = GWEN_Directory_new();
132  rv = GWEN_Directory_Open(dir, tmpdir);
133  if (rv) {
134  /* error */
135  printf("Error on GWEN_Directory_Open(\"%s\"): errno=%d: %s\n",
136  tmpdir, errno, strerror(errno));
137  } else {
138  rv = GWEN_Directory_Close(dir);
139  }
140  GWEN_Directory_free(dir);
141  return rv;
142 }
143 
144 #define ASSERT(expr) if (!(expr)) \
145  { printf("FAILED assertion in " __FILE__ ": %d: " #expr "\n", \
146  __LINE__); return -1; }
148 {
149  const char *e1 = "one", *e2 = "two", *e3 = "three";
150  GWEN_LIST *list;
151  GWEN_LIST_ITERATOR *iter;
152 
153  list = GWEN_List_new();
154  ASSERT(GWEN_List_GetSize(list) == 0);
155  GWEN_List_PushBack(list, (void*) e2);
156  ASSERT(GWEN_List_GetSize(list) == 1);
157  GWEN_List_PushBack(list, (void*) e3);
158  ASSERT(GWEN_List_GetSize(list) == 2);
159  GWEN_List_PushFront(list, (void*) e1);
160  ASSERT(GWEN_List_GetSize(list) == 3);
161  ASSERT(GWEN_List_GetFront(list) == e1);
162  ASSERT(GWEN_List_GetBack(list) == e3);
163 
164  GWEN_List_Remove(list, e2);
165  ASSERT(GWEN_List_GetSize(list) == 2);
166  ASSERT(GWEN_List_GetFront(list) == e1);
167  ASSERT(GWEN_List_GetBack(list) == e3);
168 
169  GWEN_List_PopBack(list);
170  ASSERT(GWEN_List_GetSize(list) == 1);
171  ASSERT(GWEN_List_GetFront(list) == e1);
172  ASSERT(GWEN_List_GetBack(list) == e1);
173 
174  GWEN_List_PushBack(list, (void*) e2);
175  ASSERT(GWEN_List_GetSize(list) == 2);
176  ASSERT(GWEN_List_GetFront(list) == e1);
177  ASSERT(GWEN_List_GetBack(list) == e2);
178 
179  iter = GWEN_List_First(list);
180  ASSERT(GWEN_ListIterator_Data(iter) == e1);
181  ASSERT(GWEN_ListIterator_Next(iter) == e2);
182  ASSERT(GWEN_ListIterator_Data(iter) == e2);
183 
184  ASSERT(GWEN_ListIterator_Previous(iter) == e1);
185  GWEN_List_Erase(list, iter);
186  ASSERT(GWEN_List_GetSize(list) == 1);
187  ASSERT(GWEN_List_GetFront(list) == e2);
188  ASSERT(GWEN_List_GetBack(list) == e2);
189 
190  GWEN_List_Clear(list);
191  ASSERT(GWEN_List_GetSize(list) == 0);
192 
193  GWEN_List_free(list);
195  printf("check_list: All tests passed.\n");
196  return 0;
197 }
198 
200 {
201  const char *e1 = "one", *e2 = "two", *e3 = "three";
202  GWEN_CONSTLIST *list;
204 
205  list = GWEN_ConstList_new();
206  ASSERT(GWEN_ConstList_GetSize(list) == 0);
207  GWEN_ConstList_PushBack(list, e2);
208  ASSERT(GWEN_ConstList_GetSize(list) == 1);
209  GWEN_ConstList_PushBack(list, e3);
210  ASSERT(GWEN_ConstList_GetSize(list) == 2);
211  GWEN_ConstList_PushFront(list, e1);
212  ASSERT(GWEN_ConstList_GetSize(list) == 3);
213  ASSERT(GWEN_ConstList_GetFront(list) == e1);
214  ASSERT(GWEN_ConstList_GetBack(list) == e3);
215 
217  ASSERT(GWEN_ConstList_GetSize(list) == 2);
218  ASSERT(GWEN_ConstList_GetFront(list) == e1);
219  ASSERT(GWEN_ConstList_GetBack(list) == e2);
220 
221  GWEN_ConstList_PushBack(list, e3);
222  ASSERT(GWEN_ConstList_GetSize(list) == 3);
223  ASSERT(GWEN_ConstList_GetFront(list) == e1);
224  ASSERT(GWEN_ConstList_GetBack(list) == e3);
225 
226  iter = GWEN_ConstList_First(list);
227  ASSERT(GWEN_ConstListIterator_Data(iter) == e1);
228  ASSERT(GWEN_ConstListIterator_Next(iter) == e2);
229  ASSERT(GWEN_ConstListIterator_Data(iter) == e2);
230 
232 
233  GWEN_ConstList_Clear(list);
234  ASSERT(GWEN_ConstList_GetSize(list) == 0);
235 
236  GWEN_ConstList_free(list);
238  printf("check_constlist: All tests passed.\n");
239  return 0;
240 }
241 
242 void *printfunc(const char *s, void *u)
243 {
244  const char *pathname = u;
245  printf("Path %s contains: %s\n", pathname, s);
246  return 0;
247 }
249 {
250  const char *paths[] = { GWEN_PM_SYSCONFDIR
254  , 0 };
255  const char **p = paths;
256  for ( ; *p != 0; ++p) {
257  const char *pathname = *p;
258  GWEN_STRINGLIST *sl =
260  printf("Path %s has %d elements.\n", pathname, GWEN_StringList_Count(sl));
261  GWEN_StringList_ForEach(sl, printfunc, (void*)pathname);
262  }
263  return 0;
264 }
265 
266 
267 
268 int check2() {
269  const char *testString="01234567890123456789";
270  int rv;
271  GWEN_BUFFER *buf1;
272  GWEN_BUFFER *buf2;
273  const char *p1, *p2;
274  int i;
275  int len;
276 
277  fprintf(stderr, "Check 2 ...");
278 
279  buf1=GWEN_Buffer_new(0, 256, 0, 1);
280  GWEN_Buffer_AppendString(buf1, testString);
281  rv=GWEN_Padd_PaddWithIso9796_2(buf1, 256);
282  if (rv) {
283  fprintf(stderr, "FAILED: Could not padd.\n");
284  return 2;
285  }
286 
287  buf2=GWEN_Buffer_new(0, 256, 0, 1);
288  GWEN_Buffer_AppendBuffer(buf2, buf1);
290  if (rv) {
291  fprintf(stderr, "FAILED: Could not unpadd.\n");
292  return 2;
293  }
294 
295  p1=testString;
296  len=strlen(testString);
297  p2=GWEN_Buffer_GetStart(buf2);
298  if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
299  fprintf(stderr, "Data differs in size\n");
300  return 3;
301  }
302  rv=0;
303  for (i=0; i<len; i++) {
304  if (p1[i]!=p2[i]) {
305  fprintf(stderr, "Buffer1:\n%s\n", testString);
306  fprintf(stderr, "Buffer2:\n");
307  GWEN_Buffer_Dump(buf2, 2);
308 
309  fprintf(stderr, "Differ at %d (%04x)\n", i, i);
310  rv=-1;
311  }
312  }
313 
314  if (rv) {
315  fprintf(stderr, "Data differs in content\n");
316  return 3;
317  }
318 
319  fprintf(stderr, "PASSED.\n");
320 
321  return 0;
322 }
323 
324 
325 int main(int argc, char **argv) {
326  int rv;
327  const char *cmd;
328 
329  if (argc>1)
330  cmd=argv[1];
331  else
332  cmd="check";
333 
334  if (strcasecmp(cmd, "check")==0) {
335  rv=check1() ||
336  check2() ||
337  test_gui(0) ||
338  check_directory() ||
339  check_list() ||
341  || print_paths()
342  ;
343  }
344  else if (strcasecmp(cmd, "gui")==0) {
345  rv=test_gui(1);
346  }
347  else {
348  fprintf(stderr, "Unknown command \"%s\"\n", cmd);
349  return 1;
350  }
351  return rv;
352 }
353 
354