source: trunk/src/kernel32/test/testlib.cpp@ 1870

Last change on this file since 1870 was 1870, checked in by bird, 26 years ago

API test skeleton.

File size: 8.5 KB
Line 
1/* $Id: testlib.cpp,v 1.1 1999-11-28 23:10:12 bird Exp $
2 *
3 * General test helpers.
4 *
5 * Copyright (c) 1999 knut st. osmundsen
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11/*******************************************************************************
12* Defined Constants And Macros *
13*******************************************************************************/
14#define INDENT 4
15
16
17/*******************************************************************************
18* Header Files *
19*******************************************************************************/
20#include <stdio.h>
21#include <string.h>
22#include <stdarg.h>
23#include <assert.h>
24#include "testlib.h"
25
26
27/*******************************************************************************
28* Global Variables *
29*******************************************************************************/
30static unsigned cTestTables = 0;
31static PTESTTABLE apTestTables[100] = {0};
32
33
34/*******************************************************************************
35* Internal Functions *
36*******************************************************************************/
37static void printHeadersToCurrentLevel();
38static void printAtCurrentLevel(const char *pszText, int iDelta = 0);
39
40
41/**
42 * Registers and prints an error message.
43 * @param pszFile Source filename - __FILE__.
44 * @param uLine Source code line - __LINE__.
45 * @param pszFormat Pointer to a message (printf styled) string
46 * @param ... Additional parameters.
47 * @status completely implemented.
48 * @author knut st. osmundsen
49 */
50void TstError(const char *pszFile, unsigned uLine, const char *pszFormat, ...)
51{
52 va_list arg;
53 char szOutput[0x1000];
54 int i;
55
56 /* error header */
57 i = sprintf(szOutput, "%s - Error(%s:%d): ",
58 apTestTables[cTestTables-1]->paTest[apTestTables[cTestTables-1]->iCurrentTest].pszName,
59 pszFile, uLine
60 );
61
62 /* format message */
63 va_start(arg, pszFormat);
64 i += vsprintf(&szOutput[i], pszFormat, arg);
65 va_end(arg);
66
67 if (szOutput[i-1] != '\n')
68 {
69 szOutput[i++] = '\n';
70 szOutput[i] = '\0';
71 }
72
73 printHeadersToCurrentLevel();
74 printAtCurrentLevel(szOutput);
75
76 /* update statistics */
77 apTestTables[cTestTables-1]->paTest[apTestTables[cTestTables-1]->iCurrentTest].cErrors++;
78}
79
80
81/**
82 * Registers and prints a warning message.
83 * @param pszFile Source filename - __FILE__.
84 * @param uLine Source code line - __LINE__.
85 * @param pszFormat Pointer to a message (printf styled) string
86 * @param ... Additional parameters.
87 * @status completely implemented.
88 * @author knut st. osmundsen
89 */
90void TstWarning(const char *pszFile, unsigned uLine, const char *pszFormat, ...)
91{
92 va_list arg;
93 char szOutput[0x1000];
94 int i;
95
96 /* error header */
97 i = sprintf(szOutput, "%s - Warning(%s:%d): ",
98 apTestTables[cTestTables-1]->paTest[apTestTables[cTestTables-1]->iCurrentTest].pszName,
99 pszFile, uLine
100 );
101
102 /* format message */
103 va_start(arg, pszFormat);
104 i += vsprintf(&szOutput[i], pszFormat, arg);
105 va_end(arg);
106
107 if (szOutput[i-1] != '\n')
108 {
109 szOutput[i++] = '\n';
110 szOutput[i] = '\0';
111 }
112
113 printHeadersToCurrentLevel();
114 printAtCurrentLevel(szOutput);
115
116 /* update statistics */
117 apTestTables[cTestTables-1]->paTest[apTestTables[cTestTables-1]->iCurrentTest].cWarnings++;
118}
119
120
121/**
122 * Prints an informational message.
123 * @param pszFormat Pointer to a message (printf styled) string
124 * @param ... Additional parameters.
125 * @status completely implemented.
126 * @author knut st. osmundsen
127 */
128void TstInfo(const char *pszFormat, ...)
129{
130 va_list arg;
131 char szOutput[0x1000];
132 int i;
133
134 /* error header */
135 i = sprintf(szOutput, "%s - Info: ",
136 apTestTables[cTestTables-1]->paTest[apTestTables[cTestTables-1]->iCurrentTest].pszName);
137
138 /* format message */
139 va_start(arg, pszFormat);
140 i += vsprintf(&szOutput[i], pszFormat, arg);
141 va_end(arg);
142
143 if (szOutput[i-1] != '\n')
144 {
145 szOutput[i++] = '\n';
146 szOutput[i] = '\0';
147 }
148
149 printHeadersToCurrentLevel();
150 printAtCurrentLevel(szOutput);
151}
152
153
154/**
155 * Prints an informational message.
156 * @param pszFile Source filename - __FILE__.
157 * @param uLine Source code line - __LINE__.
158 * @param pszFormat Pointer to a message (printf styled) string
159 * @param ... Additional parameters.
160 * @status completely implemented.
161 * @author knut st. osmundsen
162 * @remark Later this message type may be filtered out by a commandline option.
163 */
164void TstInfo(const char *pszFile, unsigned uLine, const char *pszFormat, ...)
165{
166 va_list arg;
167 char szOutput[0x1000];
168 int i;
169
170 /* error header */
171 i = sprintf(szOutput, "%s - Info(%s:%d): ",
172 apTestTables[cTestTables-1]->paTest[apTestTables[cTestTables-1]->iCurrentTest].pszName,
173 pszFile, uLine
174 );
175
176 /* format message */
177 va_start(arg, pszFormat);
178 i += vsprintf(&szOutput[i], pszFormat, arg);
179 va_end(arg);
180
181 if (szOutput[i-1] != '\n')
182 {
183 szOutput[i++] = '\n';
184 szOutput[i] = '\0';
185 }
186
187 printHeadersToCurrentLevel();
188 printAtCurrentLevel(szOutput);
189}
190
191
192/**
193 * This functions processes a testtable. (A test table is a set of tests.)
194 * @param pTestTable Pointer to a testtable struct.
195 * @sketch Initiate internal datamembers in the testtable struct and add it to the array.
196 *
197 * @status completely implemented.
198 * @author knut st. osmundsen
199 */
200void TstProcessTestTable(PTESTTABLE pTestTable)
201{
202 assert(pTestTable != NULL);
203
204 /* init and insert */
205 pTestTable->fPrinted = 0;
206 pTestTable->uLevel = cTestTables;
207 pTestTable->cErrors = 0;
208 pTestTable->cWarnings = 0;
209 pTestTable->iCurrentTest = 0;
210 apTestTables[cTestTables++] = pTestTable;
211
212 /* execute tests */
213 for (; pTestTable->paTest[pTestTable->iCurrentTest].pfnTest != NULL; pTestTable->iCurrentTest++)
214 {
215 pTestTable->paTest[pTestTable->iCurrentTest].cErrors = 0;
216 pTestTable->paTest[pTestTable->iCurrentTest].cWarnings = 0;
217
218 pTestTable->paTest[pTestTable->iCurrentTest].pfnTest();
219 if (pTestTable->paTest[pTestTable->iCurrentTest].cErrors > 0
220 || pTestTable->paTest[pTestTable->iCurrentTest].cWarnings > 0)
221 {
222 pTestTable->cErrors += pTestTable->paTest[pTestTable->iCurrentTest].cErrors;
223 pTestTable->cWarnings += pTestTable->paTest[pTestTable->iCurrentTest].cWarnings;
224 }
225 }
226
227 /* if errors or warnings print summary. */
228 if (pTestTable->cErrors > 0 || pTestTable->cWarnings > 0 || cTestTables == 1)
229 {
230 char szText[128];
231 sprintf(szText, "--- Summary ---\n"
232 "Errors %2d\n"
233 "Warnings %2d\n",
234 pTestTable->cErrors,
235 pTestTable->cWarnings
236 );
237 printAtCurrentLevel(szText, -1);
238 }
239
240 /* update parent test if any */
241 if (cTestTables > 1)
242 {
243 apTestTables[cTestTables - 2]->cErrors += pTestTable->cErrors;
244 apTestTables[cTestTables - 2]->cWarnings += pTestTable->cWarnings;
245 }
246
247 /* remove */
248 apTestTables[cTestTables--] = NULL;
249}
250
251
252/**
253 * Prints unprinted TestTable headers up to the current level.
254 * @status completely implemented.
255 * @author knut st. osmundsen
256 */
257static void printHeadersToCurrentLevel()
258{
259 int i;
260 for (i = 0; i < cTestTables; i++)
261 if (!apTestTables[i]->fPrinted)
262 {
263 printf("%*s%s\n", INDENT * apTestTables[i]->uLevel, "", apTestTables[i]->pszTableDescription);
264 apTestTables[i]->fPrinted = 1;
265 }
266}
267
268
269/**
270 * Indents the specified textstring to the current level.
271 * @param pszText Pointer to text.
272 * @param iDelta Level modifier.
273 * @status completely implemented.
274 * @author knut st. osmundsen
275 */
276static void printAtCurrentLevel(const char *pszText, int iDelta)
277{
278 int i;
279
280 for (i = 0; pszText[i] != '\0'; i++)
281 {
282 int j = 0;
283
284 while (pszText[i+j] != '\n' && pszText[i+j+1] != '\0')
285 j++;
286
287 printf("%*s%.*s\n",
288 INDENT * (cTestTables + iDelta), "",
289 j, &pszText[i]);
290
291 i += j;
292 }
293}
294
Note: See TracBrowser for help on using the repository browser.