Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/environment/UnitTestRuntimeWarning.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 "gtest/gtest.h"
36 #include "stk_util/environment/RuntimeMessage.hpp" // for MessageCode
37 #include "stk_util/environment/RuntimeWarning.hpp" // for RuntimeWarningAdHoc
38 #include "stk_util/util/ReportHandler.hpp" // for set_report_handler
39 #include <iostream> // for operator<<, endl, basic_ostream, basi...
40 #include <string> // for char_traits, string
41
42
43 static std::ostringstream s_os;
44
45 void my_report_handler(const char* message, int type) {
46 s_os << type<<": "<<message << "; ";
47 }
48
49 namespace {
50
51 TEST(UnitTestRuntimeWarning, Throttle)
52 {
53 stk::set_report_handler(my_report_handler);
54 s_os.str(std::string());
55 stk::reset_warning_count();
56
57 size_t throttleLimit = 3;
58 stk::MessageCode id(throttleLimit);
59 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
60 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
61 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
62 stk::RuntimeWarningAdHoc(id) << "runtime-warning XY" << std::endl;
63
64 std::string warningstring = s_os.str();
65 std::string expected("0: runtime-warning XX\n; 0: runtime-warning XX\n; 0: runtime-warning XX\n; -2147483648: Maximum count for this unknown (previous message) has been exceeded and will no longer be displayed; ");
66 EXPECT_EQ(expected, warningstring);
67
68 EXPECT_EQ(4u, stk::get_warning_count());
69 EXPECT_EQ(throttleLimit, stk::get_warning_printed_count());
70 EXPECT_EQ(throttleLimit, stk::get_warning_printed_count(id));
71 }
72
73 TEST(UnitTestRuntimeWarning, ThrottlePrinted)
74 {
75 stk::set_report_handler(my_report_handler);
76 s_os.str(std::string());
77 stk::reset_warning_count();
78
79 size_t throttle_limit = 3;
80 stk::MessageCode id(throttle_limit);
81 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
82 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
83 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
84 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
85 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
86 stk::RuntimeWarningAdHoc(id) << "runtime-warning XX" << std::endl;
87
88 stk::RuntimeWarning() << "runtime-warning default not throttled" << std::endl;
89 stk::RuntimeWarning() << "runtime-warning default not throttled" << std::endl;
90 stk::RuntimeWarning() << "runtime-warning default not throttled" << std::endl;
91 stk::RuntimeWarning() << "runtime-warning default not throttled" << std::endl;
92
93 EXPECT_EQ(10u, stk::get_warning_count());
94
95 EXPECT_EQ(throttle_limit, stk::get_warning_printed_count(id));
96 EXPECT_EQ(throttle_limit + 4u, stk::get_warning_printed_count());
97
98 stk::MessageCode defaultMessageCode = stk::MessageCode::s_defaultMessageCode;
99 EXPECT_EQ(4u, stk::get_warning_printed_count(defaultMessageCode));
100 }
101
102 TEST(UnitTestRuntimeWarningP0, WarningPrintedCountParallel)
103 {
104 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { GTEST_SKIP(); }
105
106 stk::set_report_handler(my_report_handler);
107 s_os.str(std::string());
108 stk::reset_warning_count();
109
110 stk::RuntimeWarningP0() << "runtime-warning XX" << std::endl;
111 stk::RuntimeWarningP0() << "runtime-warning XX" << std::endl;
112 stk::RuntimeWarningP0() << "runtime-warning XX" << std::endl;
113 stk::RuntimeWarningP0() << "runtime-warning XX" << std::endl;
114 stk::RuntimeWarningP0() << "runtime-warning XX" << std::endl;
115 stk::RuntimeWarningP0() << "runtime-warning XX" << std::endl;
116
117 unsigned expectedWarningCount = (stk::parallel_machine_rank(MPI_COMM_WORLD)==0) ? 6 : 0;
118
119 EXPECT_EQ(expectedWarningCount, stk::get_warning_count());
120 EXPECT_EQ(expectedWarningCount, stk::get_warning_printed_count());
121 }
122
123 }
124