annotate osx/include/kj/compat/gtest.h @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@62 2 // Licensed under the MIT License:
cannam@62 3 //
cannam@62 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@62 5 // of this software and associated documentation files (the "Software"), to deal
cannam@62 6 // in the Software without restriction, including without limitation the rights
cannam@62 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@62 8 // copies of the Software, and to permit persons to whom the Software is
cannam@62 9 // furnished to do so, subject to the following conditions:
cannam@62 10 //
cannam@62 11 // The above copyright notice and this permission notice shall be included in
cannam@62 12 // all copies or substantial portions of the Software.
cannam@62 13 //
cannam@62 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@62 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@62 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@62 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@62 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@62 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@62 20 // THE SOFTWARE.
cannam@62 21
cannam@62 22 #ifndef KJ_COMPAT_GTEST_H_
cannam@62 23 #define KJ_COMPAT_GTEST_H_
cannam@62 24 // This file defines compatibility macros converting Google Test tests into KJ tests.
cannam@62 25 //
cannam@62 26 // This is only intended to cover the most common functionality. Many tests will likely need
cannam@62 27 // additional tweaks. For instance:
cannam@62 28 // - Using operator<< to print information on failure is not supported. Instead, switch to
cannam@62 29 // KJ_ASSERT/KJ_EXPECT and pass in stuff to print as additional parameters.
cannam@62 30 // - Test fixtures are not supported. Allocate your "test fixture" on the stack instead. Do setup
cannam@62 31 // in the constructor, teardown in the destructor.
cannam@62 32
cannam@62 33 #include "../test.h"
cannam@62 34
cannam@62 35 namespace kj {
cannam@62 36
cannam@62 37 namespace _ { // private
cannam@62 38
cannam@62 39 template <typename T>
cannam@62 40 T abs(T value) { return value < 0 ? -value : value; }
cannam@62 41
cannam@62 42 inline bool floatAlmostEqual(float a, float b) {
cannam@62 43 return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-5;
cannam@62 44 }
cannam@62 45
cannam@62 46 inline bool doubleAlmostEqual(double a, double b) {
cannam@62 47 return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-12;
cannam@62 48 }
cannam@62 49
cannam@62 50 } // namespace _ (private)
cannam@62 51
cannam@62 52 #define EXPECT_FALSE(x) KJ_EXPECT(!(x))
cannam@62 53 #define EXPECT_TRUE(x) KJ_EXPECT(x)
cannam@62 54 #define EXPECT_EQ(x, y) KJ_EXPECT((x) == (y), x, y)
cannam@62 55 #define EXPECT_NE(x, y) KJ_EXPECT((x) != (y), x, y)
cannam@62 56 #define EXPECT_LE(x, y) KJ_EXPECT((x) <= (y), x, y)
cannam@62 57 #define EXPECT_GE(x, y) KJ_EXPECT((x) >= (y), x, y)
cannam@62 58 #define EXPECT_LT(x, y) KJ_EXPECT((x) < (y), x, y)
cannam@62 59 #define EXPECT_GT(x, y) KJ_EXPECT((x) > (y), x, y)
cannam@62 60 #define EXPECT_STREQ(x, y) KJ_EXPECT(::strcmp(x, y) == 0, x, y)
cannam@62 61 #define EXPECT_FLOAT_EQ(x, y) KJ_EXPECT(::kj::_::floatAlmostEqual(y, x), y, x);
cannam@62 62 #define EXPECT_DOUBLE_EQ(x, y) KJ_EXPECT(::kj::_::doubleAlmostEqual(y, x), y, x);
cannam@62 63
cannam@62 64 #define ASSERT_FALSE(x) KJ_ASSERT(!(x))
cannam@62 65 #define ASSERT_TRUE(x) KJ_ASSERT(x)
cannam@62 66 #define ASSERT_EQ(x, y) KJ_ASSERT((x) == (y), x, y)
cannam@62 67 #define ASSERT_NE(x, y) KJ_ASSERT((x) != (y), x, y)
cannam@62 68 #define ASSERT_LE(x, y) KJ_ASSERT((x) <= (y), x, y)
cannam@62 69 #define ASSERT_GE(x, y) KJ_ASSERT((x) >= (y), x, y)
cannam@62 70 #define ASSERT_LT(x, y) KJ_ASSERT((x) < (y), x, y)
cannam@62 71 #define ASSERT_GT(x, y) KJ_ASSERT((x) > (y), x, y)
cannam@62 72 #define ASSERT_STREQ(x, y) KJ_ASSERT(::strcmp(x, y) == 0, x, y)
cannam@62 73 #define ASSERT_FLOAT_EQ(x, y) KJ_ASSERT(::kj::_::floatAlmostEqual(y, x), y, x);
cannam@62 74 #define ASSERT_DOUBLE_EQ(x, y) KJ_ASSERT(::kj::_::doubleAlmostEqual(y, x), y, x);
cannam@62 75
cannam@62 76 class AddFailureAdapter {
cannam@62 77 public:
cannam@62 78 AddFailureAdapter(const char* file, int line): file(file), line(line) {}
cannam@62 79
cannam@62 80 ~AddFailureAdapter() {
cannam@62 81 if (!handled) {
cannam@62 82 _::Debug::log(file, line, LogSeverity::ERROR, "expectation failed");
cannam@62 83 }
cannam@62 84 }
cannam@62 85
cannam@62 86 template <typename T>
cannam@62 87 void operator<<(T&& info) {
cannam@62 88 handled = true;
cannam@62 89 _::Debug::log(file, line, LogSeverity::ERROR, "\"expectation failed\", info",
cannam@62 90 "expectation failed", kj::fwd<T>(info));
cannam@62 91 }
cannam@62 92
cannam@62 93 private:
cannam@62 94 bool handled = false;
cannam@62 95 const char* file;
cannam@62 96 int line;
cannam@62 97 };
cannam@62 98
cannam@62 99 #define ADD_FAILURE() ::kj::AddFailureAdapter(__FILE__, __LINE__)
cannam@62 100
cannam@62 101 #if KJ_NO_EXCEPTIONS
cannam@62 102 #define EXPECT_ANY_THROW(code) \
cannam@62 103 KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, nullptr, [&]() { code; }))
cannam@62 104 #else
cannam@62 105 #define EXPECT_ANY_THROW(code) \
cannam@62 106 KJ_EXPECT(::kj::runCatchingExceptions([&]() { code; }) != nullptr)
cannam@62 107 #endif
cannam@62 108
cannam@62 109 #define EXPECT_NONFATAL_FAILURE(code) \
cannam@62 110 EXPECT_TRUE(kj::runCatchingExceptions([&]() { code; }) != nullptr);
cannam@62 111
cannam@62 112 #ifdef KJ_DEBUG
cannam@62 113 #define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW
cannam@62 114 #else
cannam@62 115 #define EXPECT_DEBUG_ANY_THROW(EXP)
cannam@62 116 #endif
cannam@62 117
cannam@62 118 #define TEST(x, y) KJ_TEST("legacy test: " #x "/" #y)
cannam@62 119
cannam@62 120 } // namespace kj
cannam@62 121
cannam@62 122 #endif // KJ_COMPAT_GTEST_H_