- Update address of Free Software Foundation.
[reactos.git] / reactos / tools / rbuild / tests / alltests.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 #include "pch.h"
19
20 #include "rbuild.h"
21 #include "test.h"
22
23 BaseTest::BaseTest()
24 {
25 Failed = false;
26 }
27
28 BaseTest::~BaseTest()
29 {
30 }
31
32 void BaseTest::Assert(const char *message, ...)
33 {
34 va_list args;
35 va_start ( args, message );
36 vprintf(message, args);
37 va_end ( args );
38 Fail();
39 }
40
41 void BaseTest::IsNull(void* reference,
42 const char* file,
43 int line)
44 {
45 if (reference != NULL)
46 {
47 Assert("Condition was not NULL at %s:%d\n",
48 file,
49 line);
50 }
51 }
52
53 void BaseTest::IsNotNull(void* reference,
54 const char* file,
55 int line)
56 {
57 if (reference == NULL)
58 {
59 Assert("Condition was NULL at %s:%d\n",
60 file,
61 line);
62 }
63 }
64
65 void BaseTest::IsTrue(bool condition,
66 const char* file,
67 int line)
68 {
69 if (!condition)
70 {
71 Assert("Condition was not true at %s:%d\n",
72 file,
73 line);
74 }
75 }
76
77 void BaseTest::IsFalse(bool condition,
78 const char* file,
79 int line)
80 {
81 if (condition)
82 {
83 Assert("Condition was not false at %s:%d\n",
84 file,
85 line);
86 }
87 }
88
89 void BaseTest::AreEqual(int expected,
90 int actual,
91 const char* file,
92 int line)
93 {
94 if (actual != expected)
95 {
96 Assert("Expected %d/0x%.08x was %d/0x%.08x at %s:%d\n",
97 expected,
98 expected,
99 actual,
100 actual,
101 file,
102 line);
103 }
104 }
105
106 void BaseTest::AreEqual(const std::string& expected,
107 const std::string& actual,
108 const char* file,
109 int line)
110 {
111 if (actual != expected)
112 {
113 Assert("Expected '%s' was '%s' at %s:%d\n",
114 expected.c_str(),
115 actual.c_str(),
116 file,
117 line);
118 }
119 }
120
121 void BaseTest::AreNotEqual(int expected,
122 int actual,
123 const char* file,
124 int line)
125 {
126 if (actual == expected)
127 {
128 Assert("Actual value expected to be different from %d/0x%.08x at %s:%d\n",
129 expected,
130 expected,
131 file,
132 line);
133 }
134 }
135
136 void BaseTest::Fail()
137 {
138 Failed = true;
139 }
140
141 class BaseTestList : public std::vector<BaseTest*>
142 {
143 public:
144 ~BaseTestList()
145 {
146 for ( size_t i = 0; i < size(); i++ )
147 {
148 delete (*this)[i];
149 }
150 }
151 };
152
153 class TestDispatcher
154 {
155 public:
156 void Run()
157 {
158 int numberOfFailedTests = 0;
159 BaseTestList tests;
160 GetTests(tests);
161 for (size_t i = 0; i < tests.size(); i++)
162 {
163 try
164 {
165 BaseTest& test = *tests[i];
166 test.Run();
167 if (test.Failed)
168 numberOfFailedTests++;
169 }
170 catch ( Exception& ex )
171 {
172 printf ( "%s\n", (*ex).c_str () );
173 numberOfFailedTests++;
174 }
175 catch ( XMLException& ex )
176 {
177 printf ( "%s\n", (*ex).c_str () );
178 numberOfFailedTests++;
179 }
180 }
181
182 if (numberOfFailedTests > 0)
183 printf("%d tests failed\n",
184 numberOfFailedTests);
185 else
186 printf("All tests succeeded\n");
187 }
188
189 private:
190 void GetTests ( BaseTestList& tests )
191 {
192 tests.push_back(new ProjectTest());
193 tests.push_back(new ModuleTest());
194 tests.push_back(new DefineTest());
195 tests.push_back(new IncludeTest());
196 tests.push_back(new InvokeTest());
197 tests.push_back(new LinkerFlagTest());
198 tests.push_back(new IfTest());
199 tests.push_back(new FunctionTest());
200 tests.push_back(new SourceFileTest());
201 tests.push_back(new CDFileTest());
202 tests.push_back(new SymbolTest());
203 tests.push_back(new CompilationUnitTest());
204 }
205 };
206
207
208 int main(int argc,
209 char** argv)
210 {
211 InitializeEnvironment ();
212 TestDispatcher testDispatcher;
213 testDispatcher.Run();
214 return 0;
215 };