1 /***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht          *
3 * Copyright (c) QuantStack                                                 *
4 *                                                                          *
5 * Distributed under the terms of the BSD 3-Clause License.                 *
6 *                                                                          *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9 
10 #ifndef XTENSOR_ENABLE_ASSERT
11 #define XTENSOR_ENABLE_ASSERT
12 #endif
13 
14 #include <string>
15 
16 #include "test_common_macros.hpp"
17 #include "xtensor/xexception.hpp"
18 #include "test_common_macros.hpp"
19 
20 namespace xt
21 {
22 
TEST(xexception,macros)23     TEST(xexception, macros)
24     {
25         XT_EXPECT_THROW(XTENSOR_ASSERT_MSG(false, "Intentional error"), std::runtime_error);
26         XT_EXPECT_THROW(XTENSOR_PRECONDITION(false, "Intentional error"), std::runtime_error);
27     }
28 
29 
30 #if !defined(XTENSOR_DISABLE_EXCEPTIONS)
TEST(xexception,assert)31     TEST(xexception, assert)
32     {
33         try
34         {
35             XTENSOR_ASSERT_MSG(false, "Intentional error");
36             CHECK_MESSAGE(false, "no exception thrown");
37         }
38         catch (std::runtime_error& e)
39         {
40             std::string expected("Assertion error!\nIntentional error");
41             std::string message(e.what());
42             EXPECT_TRUE(0 == expected.compare(message.substr(0, expected.size())));
43         }
44         try
45         {
46             XTENSOR_PRECONDITION(false, "Intentional error");
47             CHECK_MESSAGE(false, "no exception thrown");
48         }
49         catch (std::runtime_error& e)
50         {
51             std::string expected("Precondition violation!\nIntentional error");
52             std::string message(e.what());
53             EXPECT_TRUE(0 == expected.compare(message.substr(0, expected.size())));
54         }
55     }
56 #endif
57 
58 }  // namespace xt
59