Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/environment/UnitTestReportHandler.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include <algorithm>
36 #include <iostream> // for operator<<, ostringstream, basic_ostream, bas...
37 #include <memory>
38 #include <stdexcept> // for runtime_error
39 #include <string> // for operator==, string, char_traits
40 #include <vector>
41
42 #include "gtest/gtest.h"
43 #include "stk_util/diag/String.hpp"
44 #include "stk_util/util/ReportHandler.hpp" // for source_relative_path, set_report_handler, report
45
46 namespace {
47
48 std::ostringstream &
49 test_ostringstream()
50 {
51 static std::ostringstream s_testStringStream;
52
53 return s_testStringStream;
54 }
55
56
57 std::ostream &
58 test_stream()
59 {
60 return test_ostringstream();
61 }
62
63
64 void
65 test_report_handler(
66 const char * message,
67 int type)
68 {
69 test_stream() << "Message type " << type << ": " << message << std::endl;
70 }
71
72 } // namespace <empty>
73
74 TEST(UnitTestReportHandler, UnitTest)
75 {
76 // Set and restore report handler.
77 stk::report("This is a test", 0);
78
79 stk::REH original_reh = stk::set_report_handler(test_report_handler);
80
81 stk::report("This is a test", 0);
82
83 stk::set_report_handler(original_reh);
84
85 ASSERT_THROW(stk::set_report_handler(0), std::runtime_error);
86
87 ASSERT_EQ((std::string("Message type 0: This is a test\n") == test_ostringstream().str()), true);
88 ASSERT_EQ((std::string("Test.cpp") == stk::source_relative_path("/src/Test.cpp")), true);
89 ASSERT_EQ((std::string("Test.hpp") == stk::source_relative_path("/include/Test.hpp")), true);
90 ASSERT_EQ((std::string("Apps_Test.cpp") == stk::source_relative_path("/Apps_Test.cpp")), true);
91 ASSERT_EQ((std::string("stk_Test.cpp") == stk::source_relative_path("/stk_Test.cpp")), true);
92 ASSERT_EQ((std::string("/smile/Test.cpp") == stk::source_relative_path("/smile/Test.cpp")), true);
93 }
94
95 namespace
96 {
97 template <typename T>
98 bool condition_test(const T& /*val*/)
99 {
100 return stk::impl::is_valid_throw_condition<T>::value;
101 }
102 } // namespace
103
104 TEST(UnitTestReportHandler, ConditionType)
105 {
106 EXPECT_TRUE(condition_test(bool()));
107 EXPECT_TRUE(condition_test(double()));
108 EXPECT_TRUE(condition_test(int()));
109
110 EXPECT_FALSE(condition_test("test"));
111 EXPECT_FALSE(condition_test(std::string("test")));
112 EXPECT_FALSE(condition_test(sierra::String("test")));
113
114 {
115 const char* test = "test";
116 EXPECT_FALSE(condition_test(test));
117 }
118 {
119 const double* test = nullptr;
120 EXPECT_TRUE(condition_test(test));
121 }
122 {
123 const std::unique_ptr<double> test{};
124 EXPECT_TRUE(condition_test(test));
125 }
126 {
127 std::vector<bool> someVec = {true, false};
128 EXPECT_TRUE(condition_test(std::any_of(someVec.begin(), someVec.end(), [](bool x) { return x; })));
129 }
130 }
131
132 namespace
133 {
134 class TestCondition
135 {
136 bool res;
137 unsigned call_cnt;
138
139 public:
140 TestCondition(bool res_) : res(res_), call_cnt(0) {}
141
142 bool get()
143 {
144 ++call_cnt;
145 return res;
146 }
147 unsigned count() { return call_cnt; }
148 };
149 } // namespace
150
151 #define COUNT_CALLS_AND_CHECK_THROW(expr, val) \
152 { \
153 TestCondition cond(!val); \
154 EXPECT_ANY_THROW(expr); \
155 EXPECT_EQ(cond.count(), 1u); \
156 } \
157 { \
158 TestCondition cond(val); \
159 EXPECT_NO_THROW(expr); \
160 EXPECT_EQ(cond.count(), 1u); \
161 }
162
163 TEST(UnitTestReportHandler, ExprCalledOnce)
164 {
165 // Host throw require/if functions
166 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowRequireWithSierraHelpMsg(cond.get()), true);
167 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowRequireMsg(cond.get(), ""), true);
168 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowRequire(cond.get()), true);
169 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowErrorMsgIf(cond.get(), ""), false);
170 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowErrorIf(cond.get()), false);
171 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowInvalidArgMsgIf(cond.get(), ""), false);
172 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowInvalidArgIf(cond.get()), false);
173
174 // Device require functions
175 COUNT_CALLS_AND_CHECK_THROW(STK_NGP_ThrowRequireMsg(cond.get(), ""), true);
176 COUNT_CALLS_AND_CHECK_THROW(STK_NGP_ThrowRequire(cond.get()), true);
177 COUNT_CALLS_AND_CHECK_THROW(STK_NGP_ThrowErrorMsgIf(cond.get(), ""), false);
178 COUNT_CALLS_AND_CHECK_THROW(STK_NGP_ThrowErrorIf(cond.get()), false);
179
180 // Debug only
181 #ifndef NDEBUG
182 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowAssertMsg(cond.get(), ""), true);
183 COUNT_CALLS_AND_CHECK_THROW(STK_ThrowAssert(cond.get()), true);
184 COUNT_CALLS_AND_CHECK_THROW(STK_NGP_ThrowAssertMsg(cond.get(), ""), true);
185 COUNT_CALLS_AND_CHECK_THROW(STK_NGP_ThrowAssert(cond.get()), true);
186 #endif
187 }
188
189 #undef COUNT_CALLS_AND_CHECK_THROW
190 #undef CONDITION_TEST_MACRO