annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/compat/gtest.h @ 69:33d812a61356

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