Parse <compilationunit>
[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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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",
173 ex.Message.c_str());
174 numberOfFailedTests++;
175 }
176 }
177
178 if (numberOfFailedTests > 0)
179 printf("%d tests failed\n",
180 numberOfFailedTests);
181 else
182 printf("All tests succeeded\n");
183 }
184
185 private:
186 void GetTests ( BaseTestList& tests )
187 {
188 tests.push_back(new ProjectTest());
189 tests.push_back(new ModuleTest());
190 tests.push_back(new DefineTest());
191 tests.push_back(new IncludeTest());
192 tests.push_back(new InvokeTest());
193 tests.push_back(new LinkerFlagTest());
194 tests.push_back(new IfTest());
195 tests.push_back(new FunctionTest());
196 tests.push_back(new SourceFileTest());
197 tests.push_back(new CDFileTest());
198 tests.push_back(new SymbolTest());
199 tests.push_back(new CompilationUnitTest());
200 }
201 };
202
203
204 int main(int argc,
205 char** argv)
206 {
207 InitializeEnvironment ();
208 TestDispatcher testDispatcher;
209 testDispatcher.Run();
210 return 0;
211 };