capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

gtest.h (4585B)


      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 "../test.h"
     33 #include <kj/windows-sanity.h>  // work-around macro conflict with `ERROR`
     34 
     35 namespace kj {
     36 
     37 namespace _ {  // private
     38 
     39 template <typename T>
     40 T abs(T value) { return value < 0 ? -value : value; }
     41 
     42 inline bool floatAlmostEqual(float a, float b) {
     43   return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-5;
     44 }
     45 
     46 inline bool doubleAlmostEqual(double a, double b) {
     47   return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-12;
     48 }
     49 
     50 }  // namespace _ (private)
     51 
     52 #define EXPECT_FALSE(x) KJ_EXPECT(!(x))
     53 #define EXPECT_TRUE(x) KJ_EXPECT(x)
     54 #define EXPECT_EQ(x, y) KJ_EXPECT((x) == (y), x, y)
     55 #define EXPECT_NE(x, y) KJ_EXPECT((x) != (y), x, y)
     56 #define EXPECT_LE(x, y) KJ_EXPECT((x) <= (y), x, y)
     57 #define EXPECT_GE(x, y) KJ_EXPECT((x) >= (y), x, y)
     58 #define EXPECT_LT(x, y) KJ_EXPECT((x) <  (y), x, y)
     59 #define EXPECT_GT(x, y) KJ_EXPECT((x) >  (y), x, y)
     60 #define EXPECT_STREQ(x, y) KJ_EXPECT(::strcmp(x, y) == 0, x, y)
     61 #define EXPECT_FLOAT_EQ(x, y) KJ_EXPECT(::kj::_::floatAlmostEqual(y, x), y, x);
     62 #define EXPECT_DOUBLE_EQ(x, y) KJ_EXPECT(::kj::_::doubleAlmostEqual(y, x), y, x);
     63 
     64 #define ASSERT_FALSE(x) KJ_ASSERT(!(x))
     65 #define ASSERT_TRUE(x) KJ_ASSERT(x)
     66 #define ASSERT_EQ(x, y) KJ_ASSERT((x) == (y), x, y)
     67 #define ASSERT_NE(x, y) KJ_ASSERT((x) != (y), x, y)
     68 #define ASSERT_LE(x, y) KJ_ASSERT((x) <= (y), x, y)
     69 #define ASSERT_GE(x, y) KJ_ASSERT((x) >= (y), x, y)
     70 #define ASSERT_LT(x, y) KJ_ASSERT((x) <  (y), x, y)
     71 #define ASSERT_GT(x, y) KJ_ASSERT((x) >  (y), x, y)
     72 #define ASSERT_STREQ(x, y) KJ_ASSERT(::strcmp(x, y) == 0, x, y)
     73 #define ASSERT_FLOAT_EQ(x, y) KJ_ASSERT(::kj::_::floatAlmostEqual(y, x), y, x);
     74 #define ASSERT_DOUBLE_EQ(x, y) KJ_ASSERT(::kj::_::doubleAlmostEqual(y, x), y, x);
     75 
     76 class AddFailureAdapter {
     77 public:
     78   AddFailureAdapter(const char* file, int line): file(file), line(line) {}
     79 
     80   ~AddFailureAdapter() {
     81     if (!handled) {
     82       _::Debug::log(file, line, LogSeverity::ERROR, "expectation failed");
     83     }
     84   }
     85 
     86   template <typename T>
     87   void operator<<(T&& info) {
     88     handled = true;
     89     _::Debug::log(file, line, LogSeverity::ERROR, "\"expectation failed\", info",
     90                   "expectation failed", kj::fwd<T>(info));
     91   }
     92 
     93 private:
     94   bool handled = false;
     95   const char* file;
     96   int line;
     97 };
     98 
     99 #define ADD_FAILURE() ::kj::AddFailureAdapter(__FILE__, __LINE__)
    100 
    101 #if KJ_NO_EXCEPTIONS
    102 #define EXPECT_ANY_THROW(code) \
    103     KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, nullptr, [&]() { code; }))
    104 #else
    105 #define EXPECT_ANY_THROW(code) \
    106     KJ_EXPECT(::kj::runCatchingExceptions([&]() { code; }) != nullptr)
    107 #endif
    108 
    109 #define EXPECT_NONFATAL_FAILURE(code) \
    110   EXPECT_TRUE(kj::runCatchingExceptions([&]() { code; }) != nullptr);
    111 
    112 #ifdef KJ_DEBUG
    113 #define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW
    114 #else
    115 #define EXPECT_DEBUG_ANY_THROW(EXP)
    116 #endif
    117 
    118 #define TEST(x, y) KJ_TEST("legacy test: " #x "/" #y)
    119 
    120 }  // namespace kj