xref: /aosp_15_r20/external/fmtlib/test/test-assert.h (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1 // Formatting library for C++ - test version of FMT_ASSERT
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #ifndef FMT_TEST_ASSERT_H_
9 #define FMT_TEST_ASSERT_H_
10 
11 #include <stdexcept>
12 
13 void throw_assertion_failure(const char* message);
14 #define FMT_ASSERT(condition, message) \
15   ((condition) ? (void)0 : throw_assertion_failure(message))
16 
17 #include "gtest/gtest.h"
18 
19 class assertion_failure : public std::logic_error {
20  public:
assertion_failure(const char * message)21   explicit assertion_failure(const char* message) : std::logic_error(message) {}
22 
23  private:
24   virtual void avoid_weak_vtable();
25 };
26 
avoid_weak_vtable()27 void assertion_failure::avoid_weak_vtable() {}
28 
29 // We use a separate function (rather than throw directly from FMT_ASSERT) to
30 // avoid GCC's -Wterminate warning when FMT_ASSERT is used in a destructor.
throw_assertion_failure(const char * message)31 inline void throw_assertion_failure(const char* message) {
32   throw assertion_failure(message);
33 }
34 
35 // Expects an assertion failure.
36 #define EXPECT_ASSERT(stmt, message) \
37   FMT_TEST_THROW_(stmt, assertion_failure, message, GTEST_NONFATAL_FAILURE_)
38 
39 #endif  // FMT_TEST_ASSERT_H_
40