523 lines · c
1//===-- Base class for libc unittests ---------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H10#define LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H11 12// This is defined as a simple macro in test.h so that it exists for platforms13// that don't use our test infrastructure. It's defined as a proper function14// below.15#include "src/__support/macros/config.h"16#ifdef libc_make_test_file_path17#undef libc_make_test_file_path18#endif // libc_make_test_file_path19 20// This is defined as a macro here to avoid namespace issues.21#define libc_make_test_file_path(file_name) \22 (LIBC_NAMESPACE::testing::libc_make_test_file_path_func(file_name))23 24// This file can only include headers from src/__support/ or test/UnitTest. No25// other headers should be included.26 27#include "PlatformDefs.h"28 29#include "src/__support/CPP/string.h"30#include "src/__support/CPP/string_view.h"31#include "src/__support/CPP/type_traits.h"32#include "src/__support/c_string.h"33#include "src/__support/macros/properties/compiler.h"34#include "test/UnitTest/ExecuteFunction.h"35#include "test/UnitTest/TestLogger.h"36 37namespace LIBC_NAMESPACE_DECL {38namespace testing {39 40// Only the following conditions are supported. Notice that we do not have41// a TRUE or FALSE condition. That is because, C library functions do not42// return boolean values, but use integral return values to indicate true or43// false conditions. Hence, it is more appropriate to use the other comparison44// conditions for such cases.45enum class TestCond { EQ, NE, LT, LE, GT, GE };46 47struct MatcherBase {48 virtual ~MatcherBase() {}49 virtual void explainError() { tlog << "unknown error\n"; }50 // Override and return true to skip `explainError` step.51 virtual bool is_silent() const { return false; }52};53 54template <typename T> struct Matcher : public MatcherBase {55 bool match(const T &t);56};57 58namespace internal {59 60// A simple location object to allow consistent passing of __FILE__ and61// __LINE__.62struct Location {63 Location(const char *file, int line) : file(file), line(line) {}64 const char *file;65 int line;66};67 68// Supports writing a failing Location to tlog.69TestLogger &operator<<(TestLogger &logger, Location Loc);70 71#define LIBC_TEST_LOC_() \72 LIBC_NAMESPACE::testing::internal::Location(__FILE__, __LINE__)73 74// Object to forward custom logging after the EXPECT / ASSERT macros.75struct Message {76 template <typename T> Message &operator<<(T value) {77 tlog << value;78 return *this;79 }80};81 82// A trivial object to catch the Message, this enables custom logging and83// returning from the test function, see LIBC_TEST_SCAFFOLDING_ below.84struct Failure {85 void operator=([[maybe_unused]] Message msg) {}86};87 88struct RunContext {89 enum class RunResult : bool { Pass, Fail };90 91 RunResult status() const { return Status; }92 93 void markFail() { Status = RunResult::Fail; }94 95private:96 RunResult Status = RunResult::Pass;97};98 99template <typename ValType>100bool test(RunContext *Ctx, TestCond Cond, ValType LHS, ValType RHS,101 const char *LHSStr, const char *RHSStr, Location Loc);102 103} // namespace internal104 105struct TestOptions {106 // If set, then just this one test from the suite will be run.107 const char *TestFilter = nullptr;108 // Should the test results print color codes to stdout?109 bool PrintColor = true;110 // Should the test results print timing only in milliseconds, as GTest does?111 bool TimeInMs = false;112};113 114// NOTE: One should not create instances and call methods on them directly. One115// should use the macros TEST or TEST_F to write test cases.116class Test {117 Test *Next = nullptr;118 internal::RunContext *Ctx = nullptr;119 120 void setContext(internal::RunContext *C) { Ctx = C; }121 static int getNumTests();122 123public:124 virtual ~Test() {}125 virtual void SetUp() {}126 virtual void TearDown() {}127 128 static int runTests(const TestOptions &Options);129 130protected:131 static void addTest(Test *T);132 133 // We make use of a template function, with |LHS| and |RHS| as explicit134 // parameters, for enhanced type checking. Other gtest like unittest135 // frameworks have a similar function which takes a boolean argument136 // instead of the explicit |LHS| and |RHS| arguments. This boolean argument137 // is the result of the |Cond| operation on |LHS| and |RHS|. Though not bad,138 // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because139 // of type promotion.140 template <141 typename ValType,142 cpp::enable_if_t<cpp::is_integral_v<ValType> || is_big_int_v<ValType> ||143 cpp::is_fixed_point_v<ValType>,144 int> = 0>145 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,146 const char *RHSStr, internal::Location Loc) {147 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);148 }149 150 template <typename ValType,151 cpp::enable_if_t<cpp::is_enum_v<ValType>, int> = 0>152 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,153 const char *RHSStr, internal::Location Loc) {154 return internal::test(Ctx, Cond, (long long)LHS, (long long)RHS, LHSStr,155 RHSStr, Loc);156 }157 158 template <typename ValType,159 cpp::enable_if_t<cpp::is_pointer_v<ValType>, ValType> = nullptr>160 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,161 const char *RHSStr, internal::Location Loc) {162 return internal::test(Ctx, Cond, (unsigned long long)LHS,163 (unsigned long long)RHS, LHSStr, RHSStr, Loc);164 }165 166 // Helper to allow macro invocations like `ASSERT_EQ(foo, nullptr)`.167 template <typename ValType,168 cpp::enable_if_t<cpp::is_pointer_v<ValType>, ValType> = nullptr>169 bool test(TestCond Cond, ValType LHS, cpp::nullptr_t, const char *LHSStr,170 const char *RHSStr, internal::Location Loc) {171 return test(Cond, LHS, static_cast<ValType>(nullptr), LHSStr, RHSStr, Loc);172 }173 174 template <175 typename ValType,176 cpp::enable_if_t<177 cpp::is_same_v<ValType, LIBC_NAMESPACE::cpp::string_view>, int> = 0>178 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,179 const char *RHSStr, internal::Location Loc) {180 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);181 }182 183 template <typename ValType,184 cpp::enable_if_t<185 cpp::is_same_v<ValType, LIBC_NAMESPACE::cpp::string>, int> = 0>186 bool test(TestCond Cond, ValType LHS, ValType RHS, const char *LHSStr,187 const char *RHSStr, internal::Location Loc) {188 return internal::test(Ctx, Cond, LHS, RHS, LHSStr, RHSStr, Loc);189 }190 191 bool testStrEq(const char *LHS, const char *RHS, const char *LHSStr,192 const char *RHSStr, internal::Location Loc);193 194 bool testStrNe(const char *LHS, const char *RHS, const char *LHSStr,195 const char *RHSStr, internal::Location Loc);196 197 bool testMatch(bool MatchResult, MatcherBase &Matcher, const char *LHSStr,198 const char *RHSStr, internal::Location Loc);199 200 template <typename MatcherT, typename ValType>201 bool matchAndExplain(MatcherT &&Matcher, ValType Value,202 const char *MatcherStr, const char *ValueStr,203 internal::Location Loc) {204 return testMatch(Matcher.match(Value), Matcher, ValueStr, MatcherStr, Loc);205 }206 207 bool testProcessExits(testutils::FunctionCaller *Func, int ExitCode,208 const char *LHSStr, const char *RHSStr,209 internal::Location Loc);210 211 bool testProcessKilled(testutils::FunctionCaller *Func, int Signal,212 const char *LHSStr, const char *RHSStr,213 internal::Location Loc);214 215 template <typename Func> testutils::FunctionCaller *createCallable(Func f) {216 struct Callable : public testutils::FunctionCaller {217 Func f;218 Callable(Func f) : f(f) {}219 void operator()() override { f(); }220 };221 222 return new Callable(f);223 }224 225private:226 virtual void Run() = 0;227 virtual const char *getName() const = 0;228 229 static Test *Start;230 static Test *End;231};232 233extern int argc;234extern char **argv;235extern char **envp;236 237namespace internal {238 239constexpr bool same_prefix(char const *lhs, char const *rhs, int const len) {240 for (int i = 0; (*lhs || *rhs) && (i < len); ++lhs, ++rhs, ++i)241 if (*lhs != *rhs)242 return false;243 return true;244}245 246constexpr bool valid_prefix(char const *lhs) {247 return same_prefix(lhs, "LlvmLibc", 8);248}249 250// 'str' is a null terminated string of the form251// "const char *LIBC_NAMESPACE::testing::internal::GetTypeName() [ParamType =252// XXX]" We return the substring that start at character '[' or a default253// message.254constexpr char const *GetPrettyFunctionParamType(char const *str) {255 for (const char *ptr = str; *ptr != '\0'; ++ptr)256 if (*ptr == '[')257 return ptr;258 return "UNSET : declare with REGISTER_TYPE_NAME";259}260 261// This function recovers ParamType at compile time by using __PRETTY_FUNCTION__262// It can be customized by using the REGISTER_TYPE_NAME macro below.263template <typename ParamType> static constexpr const char *GetTypeName() {264#ifdef LIBC_COMPILER_IS_MSVC265 return GetPrettyFunctionParamType(__FUNCSIG__);266#else267 return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);268#endif // LIBC_COMPILER_IS_MSVC269}270 271template <typename T>272static inline void GenerateName(char *buffer, int buffer_size,273 const char *prefix) {274 if (buffer_size == 0)275 return;276 277 // Make sure string is null terminated.278 --buffer_size;279 buffer[buffer_size] = '\0';280 281 const auto AppendChar = [&](char c) {282 if (buffer_size > 0) {283 *buffer = c;284 ++buffer;285 --buffer_size;286 }287 };288 const auto AppendStr = [&](const char *str) {289 for (; str && *str != '\0'; ++str)290 AppendChar(*str);291 };292 293 AppendStr(prefix);294 AppendChar(' ');295 AppendStr(GetTypeName<T>());296 AppendChar('\0');297}298 299// TestCreator implements a linear hierarchy of test instances, effectively300// instanciating all tests with Types in a single object.301template <template <typename> class TemplatedTestClass, typename... Types>302struct TestCreator;303 304template <template <typename> class TemplatedTestClass, typename Head,305 typename... Tail>306struct TestCreator<TemplatedTestClass, Head, Tail...>307 : private TestCreator<TemplatedTestClass, Tail...> {308 TemplatedTestClass<Head> instance;309};310 311template <template <typename> class TemplatedTestClass>312struct TestCreator<TemplatedTestClass> {};313 314// A type list to declare the set of types to instantiate the tests with.315template <typename... Types> struct TypeList {316 template <template <typename> class TemplatedTestClass> struct Tests {317 using type = TestCreator<TemplatedTestClass, Types...>;318 };319};320 321} // namespace internal322 323// Make TypeList visible in LIBC_NAMESPACE::testing.324template <typename... Types> using TypeList = internal::TypeList<Types...>;325 326CString libc_make_test_file_path_func(const char *file_name);327 328} // namespace testing329} // namespace LIBC_NAMESPACE_DECL330 331// For TYPED_TEST and TYPED_TEST_F below we need to display which type was used332// to run the test. The default will return the fully qualified canonical type333// but it can be difficult to read. We provide the following macro to allow the334// client to register the type name as they see it in the code.335#define REGISTER_TYPE_NAME(TYPE) \336 template <> \337 constexpr const char * \338 LIBC_NAMESPACE::testing::internal::GetTypeName<TYPE>() { \339 return "[ParamType = " #TYPE "]"; \340 }341 342#define TYPED_TEST(SuiteName, TestName, TypeList) \343 static_assert( \344 LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteName), \345 "All LLVM-libc TYPED_TEST suite names must start with 'LlvmLibc'."); \346 template <typename T> \347 class SuiteName##_##TestName : public LIBC_NAMESPACE::testing::Test { \348 public: \349 using ParamType = T; \350 char name[256]; \351 SuiteName##_##TestName() { \352 addTest(this); \353 LIBC_NAMESPACE::testing::internal::GenerateName<T>( \354 name, sizeof(name), #SuiteName "." #TestName); \355 } \356 void Run() override; \357 const char *getName() const override { return name; } \358 }; \359 TypeList::Tests<SuiteName##_##TestName>::type \360 SuiteName##_##TestName##_Instance; \361 template <typename T> void SuiteName##_##TestName<T>::Run()362 363#define TYPED_TEST_F(SuiteClass, TestName, TypeList) \364 static_assert(LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteClass), \365 "All LLVM-libc TYPED_TEST_F suite class names must start " \366 "with 'LlvmLibc'."); \367 template <typename T> class SuiteClass##_##TestName : public SuiteClass<T> { \368 public: \369 using ParamType = T; \370 char name[256]; \371 SuiteClass##_##TestName() { \372 SuiteClass<T>::addTest(this); \373 LIBC_NAMESPACE::testing::internal::GenerateName<T>( \374 name, sizeof(name), #SuiteClass "." #TestName); \375 } \376 void Run() override; \377 const char *getName() const override { return name; } \378 }; \379 TypeList::Tests<SuiteClass##_##TestName>::type \380 SuiteClass##_##TestName##_Instance; \381 template <typename T> void SuiteClass##_##TestName<T>::Run()382 383#define TEST(SuiteName, TestName) \384 static_assert(LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteName), \385 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \386 class SuiteName##_##TestName : public LIBC_NAMESPACE::testing::Test { \387 public: \388 SuiteName##_##TestName() { addTest(this); } \389 void Run() override; \390 const char *getName() const override { return #SuiteName "." #TestName; } \391 }; \392 SuiteName##_##TestName SuiteName##_##TestName##_Instance; \393 void SuiteName##_##TestName::Run()394 395#define TEST_F(SuiteClass, TestName) \396 static_assert( \397 LIBC_NAMESPACE::testing::internal::valid_prefix(#SuiteClass), \398 "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'."); \399 class SuiteClass##_##TestName : public SuiteClass { \400 public: \401 SuiteClass##_##TestName() { addTest(this); } \402 void Run() override; \403 const char *getName() const override { return #SuiteClass "." #TestName; } \404 }; \405 SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \406 void SuiteClass##_##TestName::Run()407 408// Helper to trick the compiler into ignoring lack of braces on the else409// branch. We cannot introduce braces at this point, since it would prevent410// using `<< ...` after the test macro for additional failure output.411#define LIBC_TEST_DISABLE_DANGLING_ELSE \412 switch (0) \413 case 0: \414 default: // NOLINT415 416// If RET_OR_EMPTY is the 'return' keyword we perform an early return which417// corresponds to an assert. If it is empty the execution continues, this418// corresponds to an expect.419//420// The 'else' clause must not be enclosed into braces so that the << operator421// can be used to fill the Message.422//423// TEST is usually implemented as a function performing checking logic and424// returning a boolean. This expression is responsible for logging the425// diagnostic in case of failure.426#define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY) \427 LIBC_TEST_DISABLE_DANGLING_ELSE \428 if (TEST) \429 ; \430 else \431 RET_OR_EMPTY LIBC_NAMESPACE::testing::internal::Failure() = \432 LIBC_NAMESPACE::testing::internal::Message()433 434#define LIBC_TEST_BINOP_(COND, LHS, RHS, RET_OR_EMPTY) \435 LIBC_TEST_SCAFFOLDING_(test(LIBC_NAMESPACE::testing::TestCond::COND, LHS, \436 RHS, #LHS, #RHS, LIBC_TEST_LOC_()), \437 RET_OR_EMPTY)438 439////////////////////////////////////////////////////////////////////////////////440// Binary operations corresponding to the TestCond enum.441 442#define EXPECT_EQ(LHS, RHS) LIBC_TEST_BINOP_(EQ, LHS, RHS, )443#define ASSERT_EQ(LHS, RHS) LIBC_TEST_BINOP_(EQ, LHS, RHS, return)444 445#define EXPECT_NE(LHS, RHS) LIBC_TEST_BINOP_(NE, LHS, RHS, )446#define ASSERT_NE(LHS, RHS) LIBC_TEST_BINOP_(NE, LHS, RHS, return)447 448#define EXPECT_LT(LHS, RHS) LIBC_TEST_BINOP_(LT, LHS, RHS, )449#define ASSERT_LT(LHS, RHS) LIBC_TEST_BINOP_(LT, LHS, RHS, return)450 451#define EXPECT_LE(LHS, RHS) LIBC_TEST_BINOP_(LE, LHS, RHS, )452#define ASSERT_LE(LHS, RHS) LIBC_TEST_BINOP_(LE, LHS, RHS, return)453 454#define EXPECT_GT(LHS, RHS) LIBC_TEST_BINOP_(GT, LHS, RHS, )455#define ASSERT_GT(LHS, RHS) LIBC_TEST_BINOP_(GT, LHS, RHS, return)456 457#define EXPECT_GE(LHS, RHS) LIBC_TEST_BINOP_(GE, LHS, RHS, )458#define ASSERT_GE(LHS, RHS) LIBC_TEST_BINOP_(GE, LHS, RHS, return)459 460////////////////////////////////////////////////////////////////////////////////461// Boolean checks are handled as comparison to the true / false values.462 463#define EXPECT_TRUE(VAL) EXPECT_EQ(VAL, true)464#define ASSERT_TRUE(VAL) ASSERT_EQ(VAL, true)465 466#define EXPECT_FALSE(VAL) EXPECT_EQ(VAL, false)467#define ASSERT_FALSE(VAL) ASSERT_EQ(VAL, false)468 469////////////////////////////////////////////////////////////////////////////////470// String checks.471 472#define LIBC_TEST_STR_(TEST_FUNC, LHS, RHS, RET_OR_EMPTY) \473 LIBC_TEST_SCAFFOLDING_(TEST_FUNC(LHS, RHS, #LHS, #RHS, LIBC_TEST_LOC_()), \474 RET_OR_EMPTY)475 476#define EXPECT_STREQ(LHS, RHS) LIBC_TEST_STR_(testStrEq, LHS, RHS, )477#define ASSERT_STREQ(LHS, RHS) LIBC_TEST_STR_(testStrEq, LHS, RHS, return)478 479#define EXPECT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, )480#define ASSERT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, return)481 482////////////////////////////////////////////////////////////////////////////////483// Subprocess checks.484 485#ifdef ENABLE_SUBPROCESS_TESTS486 487#define LIBC_TEST_PROCESS_(TEST_FUNC, FUNC, VALUE, RET_OR_EMPTY) \488 LIBC_TEST_SCAFFOLDING_( \489 TEST_FUNC(LIBC_NAMESPACE::testing::Test::createCallable(FUNC), VALUE, \490 #FUNC, #VALUE, LIBC_TEST_LOC_()), \491 RET_OR_EMPTY)492 493#define EXPECT_EXITS(FUNC, EXIT) \494 LIBC_TEST_PROCESS_(testProcessExits, FUNC, EXIT, )495#define ASSERT_EXITS(FUNC, EXIT) \496 LIBC_TEST_PROCESS_(testProcessExits, FUNC, EXIT, return)497 498#define EXPECT_DEATH(FUNC, SIG) \499 LIBC_TEST_PROCESS_(testProcessKilled, FUNC, SIG, )500#define ASSERT_DEATH(FUNC, SIG) \501 LIBC_TEST_PROCESS_(testProcessKilled, FUNC, SIG, return)502 503#endif // ENABLE_SUBPROCESS_TESTS504 505////////////////////////////////////////////////////////////////////////////////506// Custom matcher checks.507 508#define LIBC_TEST_MATCH_(MATCHER, MATCH, MATCHER_STR, MATCH_STR, RET_OR_EMPTY) \509 LIBC_TEST_SCAFFOLDING_(matchAndExplain(MATCHER, MATCH, MATCHER_STR, \510 MATCH_STR, LIBC_TEST_LOC_()), \511 RET_OR_EMPTY)512 513#define EXPECT_THAT(MATCH, MATCHER) \514 LIBC_TEST_MATCH_(MATCHER, MATCH, #MATCHER, #MATCH, )515#define ASSERT_THAT(MATCH, MATCHER) \516 LIBC_TEST_MATCH_(MATCHER, MATCH, #MATCHER, #MATCH, return)517 518#define WITH_SIGNAL(X) X519 520#define LIBC_TEST_HAS_MATCHERS() (1)521 522#endif // LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H523