- Add a parameter for listing all valid test names
[reactos.git] / rostests / apitests / apitest.c
1 #include "apitest.h"
2
3 const char szFileHeader1[] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
4 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
5 "<head>\n"
6 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
7 const char szFileHeader2[] = "<style type=\"text/css\">\n"
8 "body {font-family: sans-serif;}\n"
9 "table {width: 100%;}\n"
10 "th {text-align: left;}\n"
11 "td.red {color: red;}\n"
12 "td.green {color: green;}\n"
13 "</style>\n"
14 "</head>\n"
15 "<body>\n";
16 const char szTableHeader[] = "<table><tr><th>Function</th><th>Status</th><th>Tests (all/passed/failed)</th><th>Regressions</th></tr>";
17 const char szFileFooter[] = "</table></body></html>";
18
19 void
20 OutputUsage(LPWSTR pszName)
21 {
22 printf("\nUsage:\n\n");
23 printf("%ls.exe <TestName> - Perform individual test\n", pszName);
24 printf("%ls.exe all - Perform all tests\n", pszName);
25 printf("%ls.exe tests - List the valid test names\n", pszName);
26 printf("%ls.exe status - Create api status file\n", pszName);
27 printf("%ls.exe -r ... - Perform regression testing\n", pszName);
28 printf("\n");
29 }
30
31 BOOL
32 WriteFileHeader(UINT hFile, LPWSTR pszModule)
33 {
34 char szHeader[100];
35
36 _write(hFile, szFileHeader1, strlen(szFileHeader1));
37 sprintf(szHeader, "<title>%ls Test results</title>", pszModule);
38 _write(hFile, szHeader, strlen(szHeader));
39 _write(hFile, szFileHeader2, strlen(szFileHeader2));
40
41 sprintf(szHeader, "<h1>Test results for %ls</h1>", pszModule);
42 _write(hFile, szHeader, strlen(szHeader));
43
44 _write(hFile, szTableHeader, strlen(szTableHeader));
45
46 return TRUE;
47 }
48
49 BOOL
50 WriteRow(UINT hFile, LPWSTR pszFunction, PTESTINFO pti)
51 {
52 char szLine[500];
53
54 sprintf(szLine, "<tr><td>%ls</td>", pszFunction);
55
56 switch(pti->nApiStatus)
57 {
58 case APISTATUS_NOT_FOUND:
59 strcat(szLine, "<td class=\"red\">not found</td>");
60 break;
61 case APISTATUS_UNIMPLEMENTED:
62 strcat(szLine, "<td class=\"red\">unimplemented</td>");
63 break;
64 case APISTATUS_ASSERTION_FAILED:
65 strcat(szLine, "<td class=\"red\">assertion failed</td>");
66 break;
67 case APISTATUS_REGRESSION:
68 strcat(szLine, "<td class=\"red\">Regression!</td>");
69 break;
70 case APISTATUS_NORMAL:
71 strcat(szLine, "<td class=\"green\">Implemented</td>");
72 break;
73 }
74
75 sprintf(szLine + strlen(szLine), "<td>%d / %d / %d</td><td>%d</td></tr>\n",
76 pti->passed+pti->failed, pti->passed, pti->failed, pti->rfailed);
77
78 _write(hFile, szLine, strlen(szLine));
79 return TRUE;
80 }
81
82 int
83 TestMain(LPWSTR pszName, LPWSTR pszModule)
84 {
85 INT argc, i, j;
86 LPWSTR *argv;
87 TESTINFO ti;
88 INT opassed, ofailed, orfailed;
89 BOOL bAll, bStatus;
90 UINT hFile = 0;
91
92 ti.bRegress = FALSE;
93 bAll = FALSE;
94 bStatus = FALSE;
95 opassed = ofailed = orfailed = 0;
96
97 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
98
99 if (argc < 2)
100 {
101 OutputUsage(pszName);
102 return 0;
103 }
104
105 /* Get options */
106 for (i = 1; i < argc; i++)
107 {
108 if (_wcsicmp(argv[i], L"-r") == 0)
109 {
110 ti.bRegress = TRUE;
111 }
112 else if (_wcsicmp(argv[i], L"all") == 0)
113 {
114 bAll = TRUE;
115 }
116 else if (_wcsicmp(argv[i], L"status") == 0)
117 {
118 bAll = TRUE;
119 bStatus = TRUE;
120 }
121 else if (_wcsicmp(argv[i], L"tests") == 0)
122 {
123 /* List all the tests and exit */
124 printf("Valid test names:\n\n");
125
126 for (i = 0; i < NumTests(); i++)
127 printf("%ls\n", TestList[i].Test);
128
129 return 0;
130 }
131 }
132
133 if (bStatus)
134 {
135 ti.bRegress = TRUE;
136 char szOutputFile[MAX_PATH];
137 wsprintf(szOutputFile, "%ls.html", pszName);
138 hFile = _open(szOutputFile, O_CREAT | O_TRUNC | O_RDWR, 00700);
139 if (hFile == -1)
140 {
141 printf("Could not create output file.\n");
142 return 0;
143 }
144 WriteFileHeader(hFile, pszModule);
145 }
146
147 for (i = 0; i < NumTests(); i++)
148 {
149 for (j = 1; j < argc; j++)
150 {
151 if (bAll || _wcsicmp(argv[j], TestList[i].Test) == 0)
152 {
153 ti.passed = 0;
154 ti.failed = 0;
155 ti.rfailed = 0;
156 if (!IsFunctionPresent(TestList[i].Test))
157 {
158 printf("Function %ls was not found!\n", TestList[i].Test);
159 ti.nApiStatus = APISTATUS_NOT_FOUND;
160 }
161 else
162 {
163 printf("Executing test: %ls\n", TestList[i].Test);
164 ti.nApiStatus = TestList[i].Proc(&ti);
165 opassed += ti.passed;
166 ofailed += ti.failed;
167 orfailed += ti.rfailed;
168 printf(" tests: %d, passed: %d, failed: %d\n\n", ti.passed+ti.failed, ti.passed, ti.failed);
169 }
170 if (bStatus)
171 {
172 if (ti.rfailed > 0)
173 ti.nApiStatus = APISTATUS_REGRESSION;
174 WriteRow(hFile, TestList[i].Test, &ti);
175 }
176 break;
177 }
178 }
179 }
180
181 printf("Overall:\n");
182 printf(" tests: %d, passed: %d, failed: %d\n\n", opassed+ofailed, opassed, ofailed);
183 if (ti.bRegress)
184 {
185 printf(" regressions: %d\n", orfailed);
186 }
187
188 if (bStatus)
189 {
190 _write(hFile, szFileFooter, strlen(szFileFooter));
191 _close(hFile);
192 }
193
194 if (ti.bRegress)
195 return ti.rfailed;
196
197 return ti.failed;
198 }