1 //===--------- Implementation of the base class for libc unittests --------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/raw_ostream.h"
17 // This need not be a class as all it has is a single read-write state variable.
18 // But, we make it class as then its implementation can be hidden from the
22 enum RunResult { Result_Pass = 1, Result_Fail = 2 };
24 RunResult status() const { return Status; }
26 void markFail() { Status = Result_Fail; }
29 RunResult Status = Result_Pass;
34 template <typename ValType>
35 bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS,
36 const char *LHSStr, const char *RHSStr, const char *File,
44 llvm::outs() << File << ":" << Line << ": FAILURE\n"
45 << " Expected: " << LHSStr << '\n'
46 << " Which is: " << LHS << '\n'
47 << "To be equal to: " << RHSStr << '\n'
48 << " Which is: " << RHS << '\n';
56 llvm::outs() << File << ":" << Line << ": FAILURE\n"
57 << " Expected: " << LHSStr << '\n'
58 << " Which is: " << LHS << '\n'
59 << "To be not equal to: " << RHSStr << '\n'
60 << " Which is: " << RHS << '\n';
67 llvm::outs() << File << ":" << Line << ": FAILURE\n"
68 << " Expected: " << LHSStr << '\n'
69 << " Which is: " << LHS << '\n'
70 << "To be less than: " << RHSStr << '\n'
71 << " Which is: " << RHS << '\n';
78 llvm::outs() << File << ":" << Line << ": FAILURE\n"
79 << " Expected: " << LHSStr << '\n'
80 << " Which is: " << LHS << '\n'
81 << "To be less than or equal to: " << RHSStr << '\n'
82 << " Which is: " << RHS << '\n';
89 llvm::outs() << File << ":" << Line << ": FAILURE\n"
90 << " Expected: " << LHSStr << '\n'
91 << " Which is: " << LHS << '\n'
92 << "To be greater than: " << RHSStr << '\n'
93 << " Which is: " << RHS << '\n';
100 llvm::outs() << File << ":" << Line << ": FAILURE\n"
101 << " Expected: " << LHSStr << '\n'
102 << " Which is: " << LHS << '\n'
103 << "To be greater than or equal to: " << RHSStr << '\n'
104 << " Which is: " << RHS << '\n';
108 llvm::outs() << "Unexpected test condition.\n";
113 } // namespace internal
115 Test *Test::Start = nullptr;
116 Test *Test::End = nullptr;
118 void Test::addTest(Test *T) {
119 if (End == nullptr) {
129 int Test::runTests() {
132 for (Test *T = Start; T != nullptr; T = T->Next, ++TestCount) {
133 const char *TestName = T->getName();
134 llvm::outs() << "[ RUN ] " << TestName << '\n';
139 auto Result = Ctx.status();
141 case RunContext::Result_Fail:
142 llvm::outs() << "[ FAILED ] " << TestName << '\n';
145 case RunContext::Result_Pass:
146 llvm::outs() << "[ OK ] " << TestName << '\n';
151 llvm::outs() << "Ran " << TestCount << " tests. "
152 << " PASS: " << TestCount - FailCount << ' '
153 << " FAIL: " << FailCount << '\n';
155 return FailCount > 0 ? 1 : 0;
158 template bool Test::test<char, 0>(RunContext &Ctx, TestCondition Cond, char LHS,
159 char RHS, const char *LHSStr,
160 const char *RHSStr, const char *File,
163 template bool Test::test<short, 0>(RunContext &Ctx, TestCondition Cond,
164 short LHS, short RHS, const char *LHSStr,
165 const char *RHSStr, const char *File,
168 template bool Test::test<int, 0>(RunContext &Ctx, TestCondition Cond, int LHS,
169 int RHS, const char *LHSStr,
170 const char *RHSStr, const char *File,
173 template bool Test::test<long, 0>(RunContext &Ctx, TestCondition Cond, long LHS,
174 long RHS, const char *LHSStr,
175 const char *RHSStr, const char *File,
178 template bool Test::test<long long, 0>(RunContext &Ctx, TestCondition Cond,
179 long long LHS, long long RHS,
180 const char *LHSStr, const char *RHSStr,
181 const char *File, unsigned long Line);
183 template bool Test::test<unsigned char, 0>(RunContext &Ctx, TestCondition Cond,
184 unsigned char LHS, unsigned char RHS,
186 const char *RHSStr, const char *File,
190 Test::test<unsigned short, 0>(RunContext &Ctx, TestCondition Cond,
191 unsigned short LHS, unsigned short RHS,
192 const char *LHSStr, const char *RHSStr,
193 const char *File, unsigned long Line);
195 template bool Test::test<unsigned int, 0>(RunContext &Ctx, TestCondition Cond,
196 unsigned int LHS, unsigned int RHS,
198 const char *RHSStr, const char *File,
201 template bool Test::test<unsigned long, 0>(RunContext &Ctx, TestCondition Cond,
202 unsigned long LHS, unsigned long RHS,
204 const char *RHSStr, const char *File,
207 template bool Test::test<unsigned long long, 0>(
208 RunContext &Ctx, TestCondition Cond, unsigned long long LHS,
209 unsigned long long RHS, const char *LHSStr, const char *RHSStr,
210 const char *File, unsigned long Line);
212 bool Test::testStrEq(RunContext &Ctx, const char *LHS, const char *RHS,
213 const char *LHSStr, const char *RHSStr, const char *File,
214 unsigned long Line) {
215 return internal::test(Ctx, Cond_EQ, llvm::StringRef(LHS),
216 llvm::StringRef(RHS), LHSStr, RHSStr, File, Line);
219 bool Test::testStrNe(RunContext &Ctx, const char *LHS, const char *RHS,
220 const char *LHSStr, const char *RHSStr, const char *File,
221 unsigned long Line) {
222 return internal::test(Ctx, Cond_NE, llvm::StringRef(LHS),
223 llvm::StringRef(RHS), LHSStr, RHSStr, File, Line);
226 } // namespace testing
227 } // namespace llvm_libc
229 int main() { return llvm_libc::testing::Test::runTests(); }