OSDN Git Service

* cygpath.cc (get_device_name): Fix path length test.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / utils / testsuite.cc
1 /* testsuite.cc
2
3    Copyright 2008 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 /* This file implements a driver for performing tests on the file/path
12    translation code in path.cc.  This file is meant to be generic, all
13    test harness data is in testsuite.h.  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 #define TESTSUITE
21 #include "testsuite.h"
22
23 typedef struct
24   {
25     const char *cwd;    /* in win32 form, as if by GetCurrentDirectory */
26     const char *posix;  /* input */
27     const char *win32;  /* expected output */
28   } test_t;
29
30 #define TESTSUITE_TESTS
31 #include "testsuite.h"
32 #undef TESTSUITE_TESTS
33
34 static int curtest;
35
36 /* A replacement for the w32api GetCurrentDirectory() that returns
37    the cwd that the current test specifies.  */
38 DWORD
39 testsuite_getcwd (DWORD nBufferLength, LPSTR lpBuffer)
40 {
41   unsigned len = strlen (testsuite_tests[curtest].cwd) + 1;
42
43   /* If the test specified NO_CWD, then it means we should not have
44      needed the CWD for that test as the test was for an absolute path,
45      and so if we see that here return 0, simulating a
46      GetCurrentDirectory() error.  */
47   if (strcmp (testsuite_tests[curtest].cwd, NO_CWD) == 0)
48     return 0;
49
50   if (nBufferLength >= len)
51     {
52       strcpy (lpBuffer, testsuite_tests[curtest].cwd);
53       return len - 1;
54     }
55   return len;
56 }
57
58 extern char *cygpath (const char *s, ...);
59
60 int
61 main (int argc, char **argv)
62 {
63   int numpass = 0;
64
65   for (test_t &t = testsuite_tests[curtest]; t.posix; t = testsuite_tests[++curtest])
66     {
67       char *result = cygpath (t.posix, NULL);
68       bool pass = (strcmp (result, t.win32) == 0);
69       
70       if (pass)
71         {
72           numpass++;
73           printf ("test %03d: PASS cwd=%-18s input=%-22s expected+actual=%s\n",
74                   curtest, t.cwd, t.posix, result);
75         }
76       else
77         {
78           printf ("test %03d: FAIL cwd=%-18s input=%-29s expected=%-25s actual=%s\n",
79                   curtest, t.cwd, t.posix, t.win32, result);
80         }
81     }
82   printf ("\n"
83           "total tests: %d\n"
84           "pass       : %d (%.1f%%)\n"
85           "fail       : %d (%.1f%%)\n",
86           curtest, numpass, ((float)numpass)/curtest * 100.0F, curtest - numpass,
87           ((float)curtest - numpass)/curtest * 100.0F);
88   return (numpass < curtest ? 1 : 0);
89 }