1 // Copyright (c) 2017-2021, Lawrence Livermore National Security, LLC and
2 // other Axom Project Developers. See the top-level LICENSE file for details.
3 //
4 // SPDX-License-Identifier: (BSD-3-Clause)
5 
6 #include "gtest/gtest.h"
7 
8 #include "axom/slic/interface/slic.hpp"
9 #include "axom/slic/core/SimpleLogger.hpp"
10 using axom::slic::SimpleLogger;
11 
12 /*!
13  * \file
14  *
15  * The tests in this file check that SLIC macros properly output their message
16  * and exit (when appropriate) when run from constructors and destructors.
17  * They also exercise the SLIC SimpleLogger.
18  */
19 
20 namespace
21 {
22 /*!
23  * A simple struct with an assert in the constructor
24  */
25 struct AssertCtor
26 {
AssertCtor__anon635a49720111::AssertCtor27   AssertCtor() { SLIC_ASSERT_MSG(false, "Testing assert in .ctor"); }
28 };
29 
30 /*!
31  *  A simple struct with an assert in a method (foo)
32  */
33 struct AssertMethod
34 {
foo__anon635a49720111::AssertMethod35   void foo() { SLIC_ASSERT_MSG(false, "Testing assert in class method"); }
36 };
37 
38 /*!
39  *  A simple struct with an assert in the destructor
40  */
41 struct AssertDtor
42 {
~AssertDtor__anon635a49720111::AssertDtor43   ~AssertDtor() { SLIC_ASSERT_MSG(false, "Testing assert in .dtor"); }
44 };
45 
46 /*!
47  *  A simple testing fixture with a SLIC_WARNING in the constructor.
48  *  Note: gtest EXPECT_DEATH_IF_SUPPORTED has a return, so it cannot be
49  *  used in a constructor.
50  */
51 class SetFixtureC : public ::testing::Test
52 {
53 public:
SetFixtureC()54   SetFixtureC()
55   {
56     SLIC_WARNING(
57       "Testing warning in fixture .ctor -- this warning message should be "
58       "logged");
59   }
60 };
61 
62 /*!
63  *  A simple testing fixture with an assert in the SetUp function.
64  */
65 class SetFixtureS : public ::testing::Test
66 {
67 public:
SetUp()68   void SetUp()
69   {
70 #ifdef AXOM_DEBUG
71     EXPECT_DEATH_IF_SUPPORTED(
72       SLIC_ASSERT_MSG(false, "Testing assert in fixture setup"),
73       "");
74 #else
75     SLIC_WARNING("Testing warning in fixture setup");
76 #endif
77   }
78 };
79 
80 /*!
81  *  A simple testing fixture with an assert in the TearDown function.
82  */
83 class SetFixtureT : public ::testing::Test
84 {
85 public:
TearDown()86   void TearDown()
87   {
88 #ifdef AXOM_DEBUG
89     EXPECT_DEATH_IF_SUPPORTED(
90       SLIC_ASSERT_MSG(false, "Testing assert in fixture teardown"),
91       "");
92 #else
93     SLIC_WARNING("Testing warning in fixture teardown");
94 #endif
95   }
96 };
97 
98 /*!
99  *  A simple testing fixture with a SLIC_WARNING in the destructor.
100  *  Note: gtest EXPECT_DEATH_IF_SUPPORTED has a return, so it cannot be used
101  *  in a destructor.
102  *
103  */
104 class SetFixtureD : public ::testing::Test
105 {
106 public:
~SetFixtureD()107   ~SetFixtureD()
108   {
109     SLIC_WARNING(
110       "Testing warning in fixture .dtor -- this warning message should be "
111       "logged");
112   }
113 };
114 
115 }  // namespace
116 
117 // ********  A series of tests exercising SLIC_ASSERT
118 
TEST(slic_usage,in_test)119 TEST(slic_usage, in_test)
120 {
121   SLIC_ASSERT_MSG(true, "Testing SLIC assert (true) in test body");
122 #ifdef AXOM_DEBUG
123   EXPECT_DEATH_IF_SUPPORTED(
124     SLIC_ASSERT_MSG(false, "Testing SLIC assert(false) in test body"),
125     "")
126     << "SLIC assert (false) from a test";
127 #else
128   EXPECT_DEATH_IF_SUPPORTED(
129     SLIC_ERROR_IF(true, "Testing SLIC error in test body for release mode"),
130     "")
131     << "SLIC_ERROR_IF(false) from a test";
132 
133 #endif
134 }
135 
TEST(slic_usage,in_ctor)136 TEST(slic_usage, in_ctor)
137 {
138 #ifdef AXOM_DEBUG
139   EXPECT_DEATH_IF_SUPPORTED(AssertCtor(), "")
140     << " SLIC assert from class .ctor ";
141 #else
142   AssertCtor();
143 #endif
144 }
145 
TEST(slic_usage,in_method)146 TEST(slic_usage, in_method)
147 {
148   AssertMethod am;
149 #ifdef AXOM_DEBUG
150   EXPECT_DEATH_IF_SUPPORTED(am.foo(), "") << " SLIC assert from class method ";
151 #else
152   am.foo();
153 #endif
154 }
155 
TEST(slic_usage,in_dtor)156 TEST(slic_usage, in_dtor)
157 {
158 #ifdef AXOM_DEBUG
159   EXPECT_DEATH_IF_SUPPORTED(AssertDtor(), "")
160     << " SLIC assert from class .ctor ";
161 #else
162   AssertDtor();
163 #endif
164 }
165 
166 // A test using a test fixture with an assert in the setup phase
TEST_F(SetFixtureS,in_fixture_setup)167 TEST_F(SetFixtureS, in_fixture_setup) { }
168 
169 // A test using a test fixture with an assert in the teardown phase
TEST_F(SetFixtureT,in_fixture_teardown)170 TEST_F(SetFixtureT, in_fixture_teardown) { }
171 
172 // Note (KW): the following two tests are warnings since ASSERT_DEATH does not
173 // work in a constructor or destructor. Specifically, the ASSERT_DEATH macro
174 // has a return statement which is not allowed in constructors or destructors
175 
176 // A test using a test fixture with an assert in the constructor
TEST_F(SetFixtureC,in_fixture_ctor)177 TEST_F(SetFixtureC, in_fixture_ctor) { }
178 
179 // A test using a test fixture with an assert in the destructor
TEST_F(SetFixtureD,in_fixture_dtor)180 TEST_F(SetFixtureD, in_fixture_dtor) { }
181 
main(int argc,char * argv[])182 int main(int argc, char* argv[])
183 {
184   int result = 0;
185 
186   ::testing::InitGoogleTest(&argc, argv);
187 
188   SimpleLogger logger;  // create & initialize test logger,
189                         // finalized when exiting main scope
190 
191   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
192   result = RUN_ALL_TESTS();
193 
194   return result;
195 }
196