assertion.cpp 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//*****************************************************************************
// Copyright 2017-2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

#include <gtest/gtest.h>

#include "ngraph/assertion.hpp"

using namespace ngraph;
using namespace std;

TEST(assertion, assertion_true)
{
    NGRAPH_ASSERT(true) << "this should not throw";
}

TEST(assertion, assertion_false)
{
    EXPECT_THROW({ NGRAPH_ASSERT(false) << "this should throw"; }, AssertionFailure);
}

TEST(assertion, assertion_with_explanation)
{
    bool assertion_failure_thrown = false;

    try
    {
        NGRAPH_ASSERT(false) << "xyzzyxyzzy";
    }
    catch (const AssertionFailure& e)
    {
        assertion_failure_thrown = true;
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "Assertion 'false' failed", e.what());
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "xyzzyxyzzy", e.what());
    }

    EXPECT_TRUE(assertion_failure_thrown);
}

TEST(assertion, assertion_throws_at_semicolon)
{
    bool assertion_failure_thrown = false;
    bool got_past_semicolon = false;

    try
    {
        NGRAPH_ASSERT(false) << "first assert";
        got_past_semicolon = true;
        NGRAPH_ASSERT(false) << "second assert";
    }
    catch (const AssertionFailure& e)
    {
        assertion_failure_thrown = true;
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "first assert", e.what());
    }

    EXPECT_FALSE(got_past_semicolon);
    EXPECT_TRUE(assertion_failure_thrown);
}

TEST(assertion, assertion_no_explanation)
{
    bool assertion_failure_thrown = false;

    try
    {
        NGRAPH_ASSERT(false);
    }
    catch (const AssertionFailure& e)
    {
        assertion_failure_thrown = true;
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "Assertion 'false' failed", e.what());
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "(no explanation given)", e.what());
    }

    EXPECT_TRUE(assertion_failure_thrown);
}

// Internally, NGRAPH_ASSERT works by throwing from the destructor of an "AssertionHelper" object
// generated inside the macro. This can be dangerous if a throw happens somewhere else while the
// AssertionHelper is in scope, because stack unwinding will cause a call ~AssertionHelper, and
// this causes a "double-throw", resulting in uncatchable program termination.
//
// To avoid this, ~AssertionHelper destructor checks std::uncaught_exception() and does not throw
// if it returns true. This avoids the most likely double-throw scenario in ordinary usage, where
// the expressions feeding the stream throw exceptions themselves.
//
// Here we are testing to make sure that the exception from the stream-feeding expression is
// propagated properly, and that ~AssertionHelper itself does not throw even though the assertion
// is false.
TEST(assertion, throw_in_stream)
{
    auto f = []() -> std::string {
        // The choice of exception class here is arbitrary.
        throw std::domain_error("this should throw std::domain_error");
    };

    EXPECT_THROW({ NGRAPH_ASSERT(false) << f(); }, std::domain_error);
}

TEST(assertion, fail_with_explanation)
{
    bool assertion_failure_thrown = false;

    try
    {
        NGRAPH_FAIL() << "xyzzyxyzzy";
    }
    catch (const AssertionFailure& e)
    {
        assertion_failure_thrown = true;
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "Failure", e.what());
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "xyzzyxyzzy", e.what());
    }

    EXPECT_TRUE(assertion_failure_thrown);
}

TEST(assertion, fail_no_explanation)
{
    bool assertion_failure_thrown = false;

    try
    {
        NGRAPH_FAIL();
    }
    catch (const AssertionFailure& e)
    {
        assertion_failure_thrown = true;
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "Failure", e.what());
        EXPECT_PRED_FORMAT2(testing::IsSubstring, "(no explanation given)", e.what());
    }

    EXPECT_TRUE(assertion_failure_thrown);
}