gwenhywfar  4.99.8beta
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 <gwenhywfar/gwendate.h>
12 #include <errno.h>
13 #include "gwenhywfar.h"
14 
15 
16 
17 int check1() {
18  const char *testString="01234567890123456789";
19  int rv;
20  GWEN_BUFFER *buf1;
21  GWEN_BUFFER *buf2;
22  const char *p1, *p2;
23  int i;
24  int len;
25 
26  fprintf(stderr, "Check 1 ...");
27 
28  buf1=GWEN_Buffer_new(0, 256, 0, 1);
29  rv=GWEN_Base64_Encode((const unsigned char*)testString,
30  strlen(testString),
31  buf1, 0);
32  if (rv) {
33  fprintf(stderr, "FAILED: Could not encode.\n");
34  return 2;
35  }
36 
37  buf2=GWEN_Buffer_new(0, 256, 0, 1);
38  rv=GWEN_Base64_Decode((const unsigned char*)GWEN_Buffer_GetStart(buf1), 0,
39  buf2);
40  if (rv) {
41  fprintf(stderr, "FAILED: Could not decode.\n");
42  return 2;
43  }
44 
45  p1=testString;
46  len=strlen(testString);
47  p2=GWEN_Buffer_GetStart(buf2);
48  if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
49  fprintf(stderr, "Data differs in size\n");
50  return 3;
51  }
52  rv=0;
53  for (i=0; i<len; i++) {
54  if (p1[i]!=p2[i]) {
55  fprintf(stderr, "Buffer1:\n%s\n", testString);
56  fprintf(stderr, "Buffer2:\n");
57  GWEN_Buffer_Dump(buf2, 2);
58 
59  fprintf(stderr, "Differ at %d (%04x)\n", i, i);
60  rv=-1;
61  }
62  }
63 
64  if (rv) {
65  fprintf(stderr, "Data differs in content\n");
66  return 3;
67  }
68 
69  fprintf(stderr, "PASSED.\n");
70 
71  return 0;
72 }
73 
74 
75 
76 int test_gui(int test_with_interaction) {
77  char buffer[50];
78  int rv;
79  GWEN_GUI *gui = GWEN_Gui_CGui_new();
80 
81  /* Set the static GUI object */
82  assert(gui);
83  GWEN_Gui_SetGui(gui);
85 
86  rv = GWEN_Gui_ShowBox(0,
87  "This is a ShowBox test title",
88  "This is a ShowBox test.",
89  0);
90  printf("GWEN_Gui_ShowBox: rv=%d\n", rv);
91  GWEN_Gui_HideBox(rv);
92  printf("GWEN_Gui_HideBox called.\n\n");
93 
94  if (test_with_interaction) {
95  rv = GWEN_Gui_InputBox(0,
96  "This is a InputBox test title",
97  "Just enter something.",
98  buffer,
99  1, 40,
100  0);
101  printf("GWEN_Gui_InputBox: rv=%d, result=\"%s\"\n\n",
102  rv, buffer);
103 
104  rv = GWEN_Gui_MessageBox(0,
105  "Third test title, this time MessageBox",
106  "Just press the first or second button.",
107  "First button.", "Second button", NULL,
108  0);
109  printf("GWEN_Gui_MessageBox: rv=%d; button=%s\n", rv,
110  (rv == 1 ? "first" : (rv == 2 ? "second" : "unknown")));
111  }
112 
113  GWEN_Gui_free(gui);
114  return 0;
115 }
116 
117 
118 
119 #ifndef MAX_PATH
120 # define MAX_PATH 200
121 #endif
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  }
138  else {
139  rv = GWEN_Directory_Close(dir);
140  }
141  GWEN_Directory_free(dir);
142  return rv;
143 }
144 
145 #define ASSERT(expr) if (!(expr)) \
146  { printf("FAILED assertion in " __FILE__ ": %d: " #expr "\n", \
147  __LINE__); return -1; }
148 int check_list() {
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  const char *e1 = "one", *e2 = "two", *e3 = "three";
201  GWEN_CONSTLIST *list;
203 
204  list = GWEN_ConstList_new();
205  ASSERT(GWEN_ConstList_GetSize(list) == 0);
206  GWEN_ConstList_PushBack(list, e2);
207  ASSERT(GWEN_ConstList_GetSize(list) == 1);
208  GWEN_ConstList_PushBack(list, e3);
209  ASSERT(GWEN_ConstList_GetSize(list) == 2);
210  GWEN_ConstList_PushFront(list, e1);
211  ASSERT(GWEN_ConstList_GetSize(list) == 3);
212  ASSERT(GWEN_ConstList_GetFront(list) == e1);
213  ASSERT(GWEN_ConstList_GetBack(list) == e3);
214 
216  ASSERT(GWEN_ConstList_GetSize(list) == 2);
217  ASSERT(GWEN_ConstList_GetFront(list) == e1);
218  ASSERT(GWEN_ConstList_GetBack(list) == e2);
219 
220  GWEN_ConstList_PushBack(list, e3);
221  ASSERT(GWEN_ConstList_GetSize(list) == 3);
222  ASSERT(GWEN_ConstList_GetFront(list) == e1);
223  ASSERT(GWEN_ConstList_GetBack(list) == e3);
224 
225  iter = GWEN_ConstList_First(list);
226  ASSERT(GWEN_ConstListIterator_Data(iter) == e1);
227  ASSERT(GWEN_ConstListIterator_Next(iter) == e2);
228  ASSERT(GWEN_ConstListIterator_Data(iter) == e2);
229 
231 
232  GWEN_ConstList_Clear(list);
233  ASSERT(GWEN_ConstList_GetSize(list) == 0);
234 
235  GWEN_ConstList_free(list);
237  printf("check_constlist: All tests passed.\n");
238  return 0;
239 }
240 
241 void *printfunc(const char *s, void *u) {
242  const char *pathname = u;
243  printf("Path %s contains: %s\n", pathname, s);
244  return 0;
245 }
246 int print_paths() {
247  const char *paths[] = { GWEN_PM_SYSCONFDIR
251  , 0
252  };
253  const char **p = paths;
254  for ( ; *p != 0; ++p) {
255  const char *pathname = *p;
256  GWEN_STRINGLIST *sl =
258  printf("Path %s has %d elements.\n", pathname, GWEN_StringList_Count(sl));
259  GWEN_StringList_ForEach(sl, printfunc, (void*)pathname);
260  }
261  return 0;
262 }
263 
264 
265 
266 int check2() {
267  const char *testString="01234567890123456789";
268  int rv;
269  GWEN_BUFFER *buf1;
270  GWEN_BUFFER *buf2;
271  const char *p1, *p2;
272  int i;
273  int len;
274 
275  fprintf(stderr, "Check 2 ...");
276 
277  buf1=GWEN_Buffer_new(0, 256, 0, 1);
278  GWEN_Buffer_AppendString(buf1, testString);
279  rv=GWEN_Padd_PaddWithIso9796_2(buf1, 256);
280  if (rv) {
281  fprintf(stderr, "FAILED: Could not padd.\n");
282  return 2;
283  }
284 
285  buf2=GWEN_Buffer_new(0, 256, 0, 1);
286  GWEN_Buffer_AppendBuffer(buf2, buf1);
288  if (rv) {
289  fprintf(stderr, "FAILED: Could not unpadd.\n");
290  return 2;
291  }
292 
293  p1=testString;
294  len=strlen(testString);
295  p2=GWEN_Buffer_GetStart(buf2);
296  if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
297  fprintf(stderr, "Data differs in size\n");
298  return 3;
299  }
300  rv=0;
301  for (i=0; i<len; i++) {
302  if (p1[i]!=p2[i]) {
303  fprintf(stderr, "Buffer1:\n%s\n", testString);
304  fprintf(stderr, "Buffer2:\n");
305  GWEN_Buffer_Dump(buf2, 2);
306 
307  fprintf(stderr, "Differ at %d (%04x)\n", i, i);
308  rv=-1;
309  }
310  }
311 
312  if (rv) {
313  fprintf(stderr, "Data differs in content\n");
314  return 3;
315  }
316 
317  fprintf(stderr, "PASSED.\n");
318 
319  return 0;
320 }
321 
322 
323 int test_date() {
324  GWEN_DATE *dt1;
325  GWEN_DATE *dt2;
326  time_t tt;
327 
328  dt1=GWEN_Date_CurrentDate();
329  assert(dt1);
330  tt=GWEN_Date_toLocalTime(dt1);
331 
332  dt2=GWEN_Date_fromLocalTime(tt);
333  if (GWEN_Date_Compare(dt1, dt2)!=0) {
334  fprintf(stderr, "Error: Date doesn't match: dt1: %s dt2: %s\n",
336  return 3;
337  }
338  else {
339  fprintf(stderr, "Date is okay (%s)\n", GWEN_Date_GetString(dt2));
340  }
341 
342  return 0;
343 }
344 
345 
346 
347 int main(int argc, char **argv) {
348  int rv;
349  const char *cmd;
350 
351  if (argc>1)
352  cmd=argv[1];
353  else
354  cmd="check";
355 
356  if (strcasecmp(cmd, "check")==0) {
357  rv=check1() ||
358  check2() ||
359  test_gui(0) ||
360  check_directory() ||
361  check_list() ||
363  || print_paths()
364  ;
365  }
366  else if (strcasecmp(cmd, "gui")==0) {
367  rv=test_gui(1);
368  }
369  else if (strcasecmp(cmd, "date")==0) {
370  rv=test_date();
371  }
372  else {
373  fprintf(stderr, "Unknown command \"%s\"\n", cmd);
374  return 1;
375  }
376  return rv;
377 }
378 
379 
int check2()
Definition: testlib.c:266
#define ASSERT(expr)
Definition: testlib.c:145
void * GWEN_StringList_ForEach(const GWEN_STRINGLIST *l, void *(*func)(const char *s, void *u), void *user_data)
Definition: stringlist.c:501
int print_paths()
Definition: testlib.c:246
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
GWENHYWFAR_API void GWEN_Directory_free(GWEN_DIRECTORY *d)
const void * GWEN_ConstListIterator_Data(GWEN_CONSTLIST_ITERATOR *li)
void GWEN_ConstList_PushBack(GWEN_CONSTLIST *l, const void *p)
void GWEN_ConstList_PushFront(GWEN_CONSTLIST *l, const void *p)
struct GWEN_LIST_ITERATOR GWEN_LIST_ITERATOR
Definition: list.h:72
GWEN_CONSTLIST_ITERATOR * GWEN_ConstList_First(const GWEN_CONSTLIST *l)
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:266
void GWEN_List_Erase(GWEN_LIST *l, GWEN_LIST_ITERATOR *it)
void GWEN_ConstList_Clear(GWEN_CONSTLIST *l)
#define GWEN_GUI_FLAGS_NONINTERACTIVE
Definition: gui.h:979
void GWEN_List_PopBack(GWEN_LIST *l)
void GWEN_ConstList_PopBack(GWEN_CONSTLIST *l)
GWEN_LIST_ITERATOR * GWEN_List_First(const GWEN_LIST *l)
#define NULL
Definition: binreloc.c:290
GWENHYWFAR_API int GWEN_Directory_Close(GWEN_DIRECTORY *d)
time_t GWEN_Date_toLocalTime(const GWEN_DATE *gd)
Definition: gwendate.c:137
void * GWEN_List_GetBack(const GWEN_LIST *l)
void GWEN_Gui_AddFlags(GWEN_GUI *gui, uint32_t fl)
Definition: gui.c:674
GWEN_DATE * GWEN_Date_CurrentDate(void)
Definition: gwendate.c:178
int GWEN_Base64_Decode(const unsigned char *src, unsigned int size, GWEN_BUFFER *dst)
Definition: base64.c:132
int check_list()
Definition: testlib.c:148
int GWEN_Base64_Encode(const unsigned char *src, unsigned int size, GWEN_BUFFER *dst, unsigned int maxLineLength)
Definition: base64.c:44
int GWEN_Gui_MessageBox(uint32_t flags, const char *title, const char *text, const char *b1, const char *b2, const char *b3, uint32_t guiid)
Definition: gui.c:821
unsigned int GWEN_ConstList_GetSize(const GWEN_CONSTLIST *l)
void * printfunc(const char *s, void *u)
Definition: testlib.c:241
void GWEN_ConstList_free(GWEN_CONSTLIST *l)
GWENHYWFAR_API GWEN_DIRECTORY * GWEN_Directory_new(void)
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:38
void * GWEN_ListIterator_Previous(GWEN_LIST_ITERATOR *li)
void GWEN_Buffer_Dump(GWEN_BUFFER *bf, unsigned int insert)
Definition: buffer.c:621
int main(int argc, char **argv)
Definition: testlib.c:347
int check_constlist()
Definition: testlib.c:199
struct GWEN_DIRECTORY GWEN_DIRECTORY
Definition: directory.h:41
const void * GWEN_ConstListIterator_Next(GWEN_CONSTLIST_ITERATOR *li)
void * GWEN_List_GetFront(const GWEN_LIST *l)
GWEN_LIST * GWEN_List_new(void)
void GWEN_ListIterator_free(GWEN_LIST_ITERATOR *li)
int GWEN_Gui_InputBox(uint32_t flags, const char *title, const char *text, char *buffer, int minLen, int maxLen, uint32_t guiid)
Definition: gui.c:862
int GWEN_Padd_UnpaddWithIso9796_2(GWEN_BUFFER *buf)
Definition: padd.c:193
uint32_t GWEN_Gui_ShowBox(uint32_t flags, const char *title, const char *text, uint32_t guiid)
Definition: gui.c:881
int GWEN_Buffer_AppendBuffer(GWEN_BUFFER *bf, GWEN_BUFFER *sf)
Definition: buffer.c:549
#define MAX_PATH
Definition: testlib.c:120
GWENHYWFAR_API int GWEN_Directory_GetTmpDirectory(char *buffer, unsigned int size)
int test_gui(int test_with_interaction)
Definition: testlib.c:76
void GWEN_ConstListIterator_free(GWEN_CONSTLIST_ITERATOR *li)
struct GWEN_LIST GWEN_LIST
Doubly-linked list.
Definition: list.h:55
void GWEN_Gui_HideBox(uint32_t id)
Definition: gui.c:896
#define GWEN_PM_SYSCONFDIR
Definition: gwenhywfar.h:46
void GWEN_List_free(GWEN_LIST *l)
const void * GWEN_ConstList_GetFront(const GWEN_CONSTLIST *l)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:54
void GWEN_List_Clear(GWEN_LIST *l)
int GWEN_Padd_PaddWithIso9796_2(GWEN_BUFFER *buf, int dstSize)
Definition: padd.c:146
GWEN_STRINGLIST * GWEN_PathManager_GetPaths(const char *destLib, const char *pathName)
Definition: pathmanager.c:483
int check1()
Definition: testlib.c:17
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
int check_directory()
Definition: testlib.c:122
const void * GWEN_ConstListIterator_Previous(GWEN_CONSTLIST_ITERATOR *li)
const char * GWEN_Date_GetString(const GWEN_DATE *gd)
Definition: gwendate.c:288
GWEN_DATE * GWEN_Date_fromLocalTime(time_t t)
Definition: gwendate.c:121
unsigned int GWEN_StringList_Count(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:382
void GWEN_List_PushFront(GWEN_LIST *l, void *p)
struct GWEN_LIST_ITERATOR GWEN_CONSTLIST_ITERATOR
Definition: list.h:76
int test_date()
Definition: testlib.c:323
void * GWEN_ListIterator_Data(GWEN_LIST_ITERATOR *li)
#define GWEN_PM_PLUGINDIR
Definition: gwenhywfar.h:53
#define GWEN_PM_LOCALEDIR
Definition: gwenhywfar.h:49
#define GWEN_PM_DATADIR
Definition: gwenhywfar.h:56
#define GWEN_PM_LIBNAME
Definition: gwenhywfar.h:42
unsigned int GWEN_List_GetSize(const GWEN_LIST *l)
struct GWEN_GUI GWEN_GUI
Definition: gui.h:176
GWEN_CONSTLIST * GWEN_ConstList_new(void)
const void * GWEN_ConstList_GetBack(const GWEN_CONSTLIST *l)
void GWEN_List_Remove(GWEN_LIST *l, const void *p)
void GWEN_Gui_free(GWEN_GUI *gui)
Definition: gui.c:105
void GWEN_Gui_SetGui(GWEN_GUI *gui)
Definition: gui.c:152
GWEN_GUI * GWEN_Gui_CGui_new(void)
Definition: cgui.c:74
struct GWEN_LIST GWEN_CONSTLIST
Doubly-linked list with const objects.
Definition: list.h:64
int GWEN_Date_Compare(const GWEN_DATE *gd1, const GWEN_DATE *gd0)
Definition: gwendate.c:295
void GWEN_List_PushBack(GWEN_LIST *l, void *p)
void * GWEN_ListIterator_Next(GWEN_LIST_ITERATOR *li)
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:1014
GWENHYWFAR_API int GWEN_Directory_Open(GWEN_DIRECTORY *d, const char *n)
struct GWEN_DATE GWEN_DATE
Definition: gwendate.h:34