gwenhywfar  4.3.3
testlib.c
Go to the documentation of this file.
00001 
00002 
00003 #include <gwenhywfar/buffer.h>
00004 #include <gwenhywfar/base64.h>
00005 #include <gwenhywfar/debug.h>
00006 #include <gwenhywfar/padd.h>
00007 #include <gwenhywfar/cgui.h>
00008 #include <gwenhywfar/directory.h>
00009 #include <gwenhywfar/list.h>
00010 #include <gwenhywfar/pathmanager.h>
00011 #include <errno.h>
00012 #include "gwenhywfar.h"
00013 
00014 
00015 
00016 int check1() {
00017   const char *testString="01234567890123456789";
00018   int rv;
00019   GWEN_BUFFER *buf1;
00020   GWEN_BUFFER *buf2;
00021   const char *p1, *p2;
00022   int i;
00023   int len;
00024 
00025   fprintf(stderr, "Check 1 ...");
00026 
00027   buf1=GWEN_Buffer_new(0, 256, 0, 1);
00028   rv=GWEN_Base64_Encode((const unsigned char*)testString,
00029                         strlen(testString),
00030                         buf1, 0);
00031   if (rv) {
00032     fprintf(stderr, "FAILED: Could not encode.\n");
00033     return 2;
00034   }
00035 
00036   buf2=GWEN_Buffer_new(0, 256, 0, 1);
00037   rv=GWEN_Base64_Decode((const unsigned char*)GWEN_Buffer_GetStart(buf1), 0,
00038                         buf2);
00039   if (rv) {
00040     fprintf(stderr, "FAILED: Could not decode.\n");
00041     return 2;
00042   }
00043 
00044   p1=testString;
00045   len=strlen(testString);
00046   p2=GWEN_Buffer_GetStart(buf2);
00047   if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
00048     fprintf(stderr, "Data differs in size\n");
00049     return 3;
00050   }
00051   rv=0;
00052   for (i=0; i<len; i++) {
00053     if (p1[i]!=p2[i]) {
00054       fprintf(stderr, "Buffer1:\n%s\n", testString);
00055       fprintf(stderr, "Buffer2:\n");
00056       GWEN_Buffer_Dump(buf2, 2);
00057 
00058       fprintf(stderr, "Differ at %d (%04x)\n", i, i);
00059       rv=-1;
00060     }
00061   }
00062 
00063   if (rv) {
00064     fprintf(stderr, "Data differs in content\n");
00065     return 3;
00066   }
00067 
00068   fprintf(stderr, "PASSED.\n");
00069 
00070   return 0;
00071 }
00072 
00073 
00074 
00075 int test_gui(int test_with_interaction) {
00076   char buffer[50];
00077   int rv;
00078   GWEN_GUI *gui = GWEN_Gui_CGui_new();
00079 
00080   /* Set the static GUI object */
00081   assert(gui);
00082   GWEN_Gui_SetGui(gui);
00083   GWEN_Gui_AddFlags(gui, GWEN_GUI_FLAGS_NONINTERACTIVE);
00084 
00085   rv = GWEN_Gui_ShowBox(0,
00086                         "This is a ShowBox test title",
00087                         "This is a ShowBox test.",
00088                         0);
00089   printf("GWEN_Gui_ShowBox: rv=%d\n", rv);
00090   GWEN_Gui_HideBox(rv);
00091   printf("GWEN_Gui_HideBox called.\n\n");
00092 
00093   if (test_with_interaction) {
00094     rv = GWEN_Gui_InputBox(0,
00095                            "This is a InputBox test title",
00096                            "Just enter something.",
00097                            buffer,
00098                            1, 40,
00099                            0);
00100     printf("GWEN_Gui_InputBox: rv=%d, result=\"%s\"\n\n",
00101            rv, buffer);
00102   
00103     rv = GWEN_Gui_MessageBox(0,
00104                              "Third test title, this time MessageBox",
00105                              "Just press the first or second button.",
00106                              "First button.", "Second button", NULL,
00107                              0);
00108     printf("GWEN_Gui_MessageBox: rv=%d; button=%s\n", rv,
00109            (rv == 1 ? "first" : (rv == 2 ? "second" : "unknown")));
00110   }
00111 
00112   GWEN_Gui_free(gui);
00113   return 0;
00114 }
00115 
00116 
00117 
00118 #ifndef MAX_PATH
00119 # define MAX_PATH 200
00120 #endif
00121 int check_directory()
00122 {
00123   char tmpdir[MAX_PATH];
00124   GWEN_DIRECTORY *dir;
00125   int rv;
00126 
00127   GWEN_Directory_GetTmpDirectory(tmpdir, MAX_PATH);
00128   printf("GWEN_Directory_GetTmpDirectory returns \"%s\" as tmp directory\n",
00129          tmpdir);
00130 
00131   dir = GWEN_Directory_new();
00132   rv = GWEN_Directory_Open(dir, tmpdir);
00133   if (rv) {
00134     /* error */
00135     printf("Error on GWEN_Directory_Open(\"%s\"): errno=%d: %s\n",
00136            tmpdir, errno, strerror(errno));
00137   } else {
00138     rv = GWEN_Directory_Close(dir);
00139   }
00140   GWEN_Directory_free(dir);
00141   return rv;
00142 }
00143 
00144 #define ASSERT(expr) if (!(expr)) \
00145  { printf("FAILED assertion in " __FILE__ ": %d: " #expr "\n", \
00146           __LINE__); return -1; }
00147 int check_list()
00148 {
00149   const char *e1 = "one", *e2 = "two", *e3 = "three";
00150   GWEN_LIST *list;
00151   GWEN_LIST_ITERATOR *iter;
00152 
00153   list = GWEN_List_new();
00154   ASSERT(GWEN_List_GetSize(list) == 0);
00155   GWEN_List_PushBack(list, (void*) e2);
00156   ASSERT(GWEN_List_GetSize(list) == 1);
00157   GWEN_List_PushBack(list, (void*) e3);
00158   ASSERT(GWEN_List_GetSize(list) == 2);
00159   GWEN_List_PushFront(list, (void*) e1);
00160   ASSERT(GWEN_List_GetSize(list) == 3);
00161   ASSERT(GWEN_List_GetFront(list) == e1);
00162   ASSERT(GWEN_List_GetBack(list) == e3);
00163 
00164   GWEN_List_Remove(list, e2);
00165   ASSERT(GWEN_List_GetSize(list) == 2);
00166   ASSERT(GWEN_List_GetFront(list) == e1);
00167   ASSERT(GWEN_List_GetBack(list) == e3);
00168 
00169   GWEN_List_PopBack(list);
00170   ASSERT(GWEN_List_GetSize(list) == 1);
00171   ASSERT(GWEN_List_GetFront(list) == e1);
00172   ASSERT(GWEN_List_GetBack(list) == e1);
00173 
00174   GWEN_List_PushBack(list, (void*) e2);
00175   ASSERT(GWEN_List_GetSize(list) == 2);
00176   ASSERT(GWEN_List_GetFront(list) == e1);
00177   ASSERT(GWEN_List_GetBack(list) == e2);
00178 
00179   iter = GWEN_List_First(list);
00180   ASSERT(GWEN_ListIterator_Data(iter) == e1);
00181   ASSERT(GWEN_ListIterator_Next(iter) == e2);
00182   ASSERT(GWEN_ListIterator_Data(iter) == e2);
00183 
00184   ASSERT(GWEN_ListIterator_Previous(iter) == e1);
00185   GWEN_List_Erase(list, iter);
00186   ASSERT(GWEN_List_GetSize(list) == 1);
00187   ASSERT(GWEN_List_GetFront(list) == e2);
00188   ASSERT(GWEN_List_GetBack(list) == e2);
00189 
00190   GWEN_List_Clear(list);
00191   ASSERT(GWEN_List_GetSize(list) == 0);
00192 
00193   GWEN_List_free(list);
00194   GWEN_ListIterator_free(iter);
00195   printf("check_list: All tests passed.\n");
00196   return 0;
00197 }
00198 
00199 int check_constlist()
00200 {
00201   const char *e1 = "one", *e2 = "two", *e3 = "three";
00202   GWEN_CONSTLIST *list;
00203   GWEN_CONSTLIST_ITERATOR *iter;
00204 
00205   list = GWEN_ConstList_new();
00206   ASSERT(GWEN_ConstList_GetSize(list) == 0);
00207   GWEN_ConstList_PushBack(list, e2);
00208   ASSERT(GWEN_ConstList_GetSize(list) == 1);
00209   GWEN_ConstList_PushBack(list, e3);
00210   ASSERT(GWEN_ConstList_GetSize(list) == 2);
00211   GWEN_ConstList_PushFront(list, e1);
00212   ASSERT(GWEN_ConstList_GetSize(list) == 3);
00213   ASSERT(GWEN_ConstList_GetFront(list) == e1);
00214   ASSERT(GWEN_ConstList_GetBack(list) == e3);
00215 
00216   GWEN_ConstList_PopBack(list);
00217   ASSERT(GWEN_ConstList_GetSize(list) == 2);
00218   ASSERT(GWEN_ConstList_GetFront(list) == e1);
00219   ASSERT(GWEN_ConstList_GetBack(list) == e2);
00220 
00221   GWEN_ConstList_PushBack(list, e3);
00222   ASSERT(GWEN_ConstList_GetSize(list) == 3);
00223   ASSERT(GWEN_ConstList_GetFront(list) == e1);
00224   ASSERT(GWEN_ConstList_GetBack(list) == e3);
00225 
00226   iter = GWEN_ConstList_First(list);
00227   ASSERT(GWEN_ConstListIterator_Data(iter) == e1);
00228   ASSERT(GWEN_ConstListIterator_Next(iter) == e2);
00229   ASSERT(GWEN_ConstListIterator_Data(iter) == e2);
00230 
00231   ASSERT(GWEN_ConstListIterator_Previous(iter) == e1);
00232 
00233   GWEN_ConstList_Clear(list);
00234   ASSERT(GWEN_ConstList_GetSize(list) == 0);
00235 
00236   GWEN_ConstList_free(list);
00237   GWEN_ConstListIterator_free(iter);
00238   printf("check_constlist: All tests passed.\n");
00239   return 0;
00240 }
00241 
00242 void *printfunc(const char *s, void *u)
00243 {
00244   const char *pathname = u;
00245   printf("Path %s contains: %s\n", pathname, s);
00246   return 0;
00247 }
00248 int print_paths()
00249 {
00250   const char *paths[] = { GWEN_PM_SYSCONFDIR
00251                           , GWEN_PM_LOCALEDIR
00252                           , GWEN_PM_PLUGINDIR
00253                           , GWEN_PM_DATADIR
00254                           , 0 };
00255   const char **p = paths;
00256   for ( ; *p != 0; ++p) {
00257     const char *pathname = *p;
00258     GWEN_STRINGLIST *sl =
00259       GWEN_PathManager_GetPaths(GWEN_PM_LIBNAME, pathname);
00260     printf("Path %s has %d elements.\n", pathname, GWEN_StringList_Count(sl));
00261     GWEN_StringList_ForEach(sl, printfunc, (void*)pathname);
00262   }
00263   return 0;
00264 }
00265 
00266 
00267 
00268 int check2() {
00269   const char *testString="01234567890123456789";
00270   int rv;
00271   GWEN_BUFFER *buf1;
00272   GWEN_BUFFER *buf2;
00273   const char *p1, *p2;
00274   int i;
00275   int len;
00276 
00277   fprintf(stderr, "Check 2 ...");
00278 
00279   buf1=GWEN_Buffer_new(0, 256, 0, 1);
00280   GWEN_Buffer_AppendString(buf1, testString);
00281   rv=GWEN_Padd_PaddWithIso9796_2(buf1, 256);
00282   if (rv) {
00283     fprintf(stderr, "FAILED: Could not padd.\n");
00284     return 2;
00285   }
00286 
00287   buf2=GWEN_Buffer_new(0, 256, 0, 1);
00288   GWEN_Buffer_AppendBuffer(buf2, buf1);
00289   rv=GWEN_Padd_UnpaddWithIso9796_2(buf2);
00290   if (rv) {
00291     fprintf(stderr, "FAILED: Could not unpadd.\n");
00292     return 2;
00293   }
00294 
00295   p1=testString;
00296   len=strlen(testString);
00297   p2=GWEN_Buffer_GetStart(buf2);
00298   if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
00299     fprintf(stderr, "Data differs in size\n");
00300     return 3;
00301   }
00302   rv=0;
00303   for (i=0; i<len; i++) {
00304     if (p1[i]!=p2[i]) {
00305       fprintf(stderr, "Buffer1:\n%s\n", testString);
00306       fprintf(stderr, "Buffer2:\n");
00307       GWEN_Buffer_Dump(buf2, 2);
00308 
00309       fprintf(stderr, "Differ at %d (%04x)\n", i, i);
00310       rv=-1;
00311     }
00312   }
00313 
00314   if (rv) {
00315     fprintf(stderr, "Data differs in content\n");
00316     return 3;
00317   }
00318 
00319   fprintf(stderr, "PASSED.\n");
00320 
00321   return 0;
00322 }
00323 
00324 
00325 int main(int argc, char **argv) {
00326   int rv;
00327   const char *cmd;
00328 
00329   if (argc>1)
00330     cmd=argv[1];
00331   else
00332     cmd="check";
00333 
00334   if (strcasecmp(cmd, "check")==0) {
00335     rv=check1() ||
00336       check2() ||
00337       test_gui(0) ||
00338       check_directory() ||
00339       check_list() ||
00340       check_constlist()
00341       || print_paths()
00342       ;
00343   }
00344   else if (strcasecmp(cmd, "gui")==0) {
00345     rv=test_gui(1);
00346   }
00347   else {
00348     fprintf(stderr, "Unknown command \"%s\"\n", cmd);
00349     return 1;
00350   }
00351   return rv;
00352 }
00353 
00354