Sync to trunk r39350.
[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(HANDLE hFile, LPDWORD lpdwBytesWritten, LPWSTR pszModule)
33 {
34 char szHeader[100];
35
36 WriteFile(hFile, szFileHeader1, strlen(szFileHeader1), lpdwBytesWritten, NULL);
37 sprintf(szHeader, "<title>%ls Test results</title>", pszModule);
38 WriteFile(hFile, szHeader, strlen(szHeader), lpdwBytesWritten, NULL);
39 WriteFile(hFile, szFileHeader2, strlen(szFileHeader2), lpdwBytesWritten, NULL);
40
41 sprintf(szHeader, "<h1>Test results for %ls</h1>", pszModule);
42 WriteFile(hFile, szHeader, strlen(szHeader), lpdwBytesWritten, NULL);
43
44 WriteFile(hFile, szTableHeader, strlen(szTableHeader), lpdwBytesWritten, NULL);
45
46 return TRUE;
47 }
48
49 BOOL
50 WriteRow(HANDLE hFile, LPDWORD lpdwBytesWritten, 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 WriteFile(hFile, szLine, strlen(szLine), lpdwBytesWritten, NULL);
79
80 return TRUE;
81 }
82
83 int
84 TestMain(LPWSTR pszName, LPWSTR pszModule)
85 {
86 INT argc, i, j;
87 LPWSTR *argv;
88 TESTINFO ti;
89 INT opassed, ofailed, orfailed;
90 BOOL bAll, bStatus;
91 HANDLE hFile = NULL;
92 DWORD dwBytesWritten;
93
94 ti.bRegress = FALSE;
95 bAll = FALSE;
96 bStatus = FALSE;
97 opassed = ofailed = orfailed = 0;
98
99 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
100
101 if (argc < 2)
102 {
103 OutputUsage(pszName);
104 return 0;
105 }
106
107 /* Get options */
108 for (i = 1; i < argc; i++)
109 {
110 if (_wcsicmp(argv[i], L"-r") == 0)
111 {
112 ti.bRegress = TRUE;
113 }
114 else if (_wcsicmp(argv[i], L"all") == 0)
115 {
116 bAll = TRUE;
117 }
118 else if (_wcsicmp(argv[i], L"status") == 0)
119 {
120 bAll = TRUE;
121 bStatus = TRUE;
122 }
123 else if (_wcsicmp(argv[i], L"tests") == 0)
124 {
125 /* List all the tests and exit */
126 printf("Valid test names:\n\n");
127
128 for (i = 0; i < NumTests(); i++)
129 printf("%ls\n", TestList[i].Test);
130
131 return 0;
132 }
133 }
134
135 if (bStatus)
136 {
137 WCHAR szOutputFile[MAX_PATH];
138
139 ti.bRegress = TRUE;
140 wcscpy(szOutputFile, pszName);
141 wcscat(szOutputFile, L".html");
142 hFile = CreateFileW(szOutputFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
143
144 if (hFile == INVALID_HANDLE_VALUE)
145 {
146 printf("Could not create output file.\n");
147 return 0;
148 }
149
150 WriteFileHeader(hFile, &dwBytesWritten, pszModule);
151 }
152
153 for (i = 0; i < NumTests(); i++)
154 {
155 for (j = 1; j < argc; j++)
156 {
157 if (bAll || _wcsicmp(argv[j], TestList[i].Test) == 0)
158 {
159 ti.passed = 0;
160 ti.failed = 0;
161 ti.rfailed = 0;
162 if (!IsFunctionPresent(TestList[i].Test))
163 {
164 printf("Function %ls was not found!\n", TestList[i].Test);
165 ti.nApiStatus = APISTATUS_NOT_FOUND;
166 }
167 else
168 {
169 printf("Executing test: %ls\n", TestList[i].Test);
170 ti.nApiStatus = TestList[i].Proc(&ti);
171 opassed += ti.passed;
172 ofailed += ti.failed;
173 orfailed += ti.rfailed;
174 printf(" tests: %d, passed: %d, failed: %d\n\n", ti.passed+ti.failed, ti.passed, ti.failed);
175 }
176 if (bStatus)
177 {
178 if (ti.rfailed > 0)
179 ti.nApiStatus = APISTATUS_REGRESSION;
180 WriteRow(hFile, &dwBytesWritten, TestList[i].Test, &ti);
181 }
182 break;
183 }
184 }
185 }
186
187 printf("Overall:\n");
188 printf(" tests: %d, passed: %d, failed: %d\n\n", opassed+ofailed, opassed, ofailed);
189 if (ti.bRegress)
190 {
191 printf(" regressions: %d\n", orfailed);
192 }
193
194 if (bStatus)
195 {
196 WriteFile(hFile, szFileFooter, strlen(szFileFooter), &dwBytesWritten, NULL);
197 CloseHandle(hFile);
198 }
199
200 if (ti.bRegress)
201 return ti.rfailed;
202
203 return ti.failed;
204 }