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