Home | History | Annotate | Download | only in tests
      1 /*
      2  *
      3  *  Copyright (C) 2006-2007  Peter Johnson
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
     15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
     18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     24  * POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 #include <stdio.h>
     27 #include <stdlib.h>
     28 #include <string.h>
     29 
     30 #include "libyasm/file.h"
     31 #include "libyasm/coretype.h"
     32 
     33 typedef struct Test_Entry {
     34     /* combpath function to test */
     35     char * (*combpath) (const char *from, const char *to);
     36 
     37     /* input "from" path */
     38     const char *from;
     39 
     40     /* input "to" path */
     41     const char *to;
     42 
     43     /* correct path returned */
     44     const char *out;
     45 } Test_Entry;
     46 
     47 static Test_Entry tests[] = {
     48     /* UNIX */
     49     {yasm__combpath_unix, "file1", "file2", "file2"},
     50     {yasm__combpath_unix, "./file1.ext", "./file2.ext", "file2.ext"},
     51     {yasm__combpath_unix, "/file1", "file2", "/file2"},
     52     {yasm__combpath_unix, "file1", "/file2", "/file2"},
     53     {yasm__combpath_unix, "/foo/file1", "../../file2", "/file2"},
     54     {yasm__combpath_unix, "/foo//file1", "../../file2", "/file2"},
     55     {yasm__combpath_unix, "foo/bar/file1", "../file2", "foo/file2"},
     56     {yasm__combpath_unix, "foo/bar/file1", "../../../file2", "../file2"},
     57     {yasm__combpath_unix, "foo/bar//file1", "../..//..//file2", "../file2"},
     58     {yasm__combpath_unix, "foo/bar/", "file2", "foo/bar/file2"},
     59     {yasm__combpath_unix, "../../file1", "../../file2", "../../../../file2"},
     60     {yasm__combpath_unix, "../foo/bar/../file1", "../../file2", "../foo/bar/../../../file2"},
     61     {yasm__combpath_unix, "/", "../file2", "/file2"},
     62     {yasm__combpath_unix, "../foo/", "../file2", "../file2"},
     63     {yasm__combpath_unix, "../foo/file1", "../../bar/file2", "../../bar/file2"},
     64 
     65     /* Windows */
     66     {yasm__combpath_win, "file1", "file2", "file2"},
     67     {yasm__combpath_win, "./file1.ext", "./file2.ext", "file2.ext"},
     68     {yasm__combpath_win, "./file1.ext", ".\\file2.ext", "file2.ext"},
     69     {yasm__combpath_win, ".\\file1.ext", "./file2.ext", "file2.ext"},
     70     {yasm__combpath_win, "/file1", "file2", "\\file2"},
     71     {yasm__combpath_win, "\\file1", "file2", "\\file2"},
     72     {yasm__combpath_win, "file1", "/file2", "\\file2"},
     73     {yasm__combpath_win, "file1", "\\file2", "\\file2"},
     74     {yasm__combpath_win, "/foo\\file1", "../../file2", "\\file2"},
     75     {yasm__combpath_win, "\\foo\\\\file1", "..\\../file2", "\\file2"},
     76     {yasm__combpath_win, "foo/bar/file1", "../file2", "foo\\file2"},
     77     {yasm__combpath_win, "foo/bar/file1", "../..\\../file2", "..\\file2"},
     78     {yasm__combpath_win, "foo/bar//file1", "../..\\\\..//file2", "..\\file2"},
     79     {yasm__combpath_win, "foo/bar/", "file2", "foo\\bar\\file2"},
     80     {yasm__combpath_win, "..\\../file1", "../..\\file2", "..\\..\\..\\..\\file2"},
     81     {yasm__combpath_win, "../foo/bar\\\\../file1", "../..\\file2", "..\\foo\\bar\\..\\..\\..\\file2"},
     82     {yasm__combpath_win, "/", "../file2", "\\file2"},
     83     {yasm__combpath_win, "../foo/", "../file2", "..\\file2"},
     84     {yasm__combpath_win, "../foo/file1", "../..\\bar\\file2", "..\\..\\bar\\file2"},
     85     {yasm__combpath_win, "c:/file1.ext", "./file2.ext", "c:\\file2.ext"},
     86     {yasm__combpath_win, "e:\\path\\to/file1.ext", ".\\file2.ext", "e:\\path\\to\\file2.ext"},
     87     {yasm__combpath_win, ".\\file1.ext", "g:file2.ext", "g:file2.ext"},
     88 };
     89 
     90 static char failed[1000];
     91 static char failmsg[100];
     92 
     93 static int
     94 run_test(Test_Entry *test)
     95 {
     96     char *out;
     97     const char *funcname;
     98 
     99     if (test->combpath == &yasm__combpath_unix)
    100         funcname = "unix";
    101     else
    102         funcname = "win";
    103 
    104     out = test->combpath(test->from, test->to);
    105 
    106     if (strcmp(out, test->out) != 0) {
    107         sprintf(failmsg,
    108                 "combpath_%s(\"%s\", \"%s\"): expected \"%s\", got \"%s\"!",
    109                 funcname, test->from, test->to, test->out, out);
    110         yasm_xfree(out);
    111         return 1;
    112     }
    113 
    114     yasm_xfree(out);
    115     return 0;
    116 }
    117 
    118 int
    119 main(void)
    120 {
    121     int nf = 0;
    122     int numtests = sizeof(tests)/sizeof(Test_Entry);
    123     int i;
    124 
    125     failed[0] = '\0';
    126     printf("Test combpath_test: ");
    127     for (i=0; i<numtests; i++) {
    128         int fail = run_test(&tests[i]);
    129         printf("%c", fail>0 ? 'F':'.');
    130         fflush(stdout);
    131         if (fail)
    132             sprintf(failed, "%s ** F: %s\n", failed, failmsg);
    133         nf += fail;
    134     }
    135 
    136     printf(" +%d-%d/%d %d%%\n%s",
    137            numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed);
    138     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    139 }
    140