Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/util/UnitTestFPExceptions.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/stk_config.h"
37 #include "stk_util/util/FPExceptions.hpp"
38 #include <sstream>
39
40 TEST(FPExceptions, SimpleAdditionNoError)
41 {
42 if (!stk::util::have_errno() && !stk::util::have_errexcept()) GTEST_SKIP();
43
44 stk::util::clear_fp_errors();
45 double x = 1.0 + 2.0;
46 EXPECT_NO_THROW(stk::util::throw_on_fp_error());
47 EXPECT_EQ(x, 3.0); // appease the compiler
48 }
49
50 TEST(FPExceptions, Log0Error)
51 {
52 if (!stk::util::have_errno() && !stk::util::have_errexcept()) GTEST_SKIP();
53
54 stk::util::clear_fp_errors();
55 std::log(0.0);
56 EXPECT_ANY_THROW(stk::util::throw_on_fp_error());
57 }
58
59 TEST(FPExceptions, FlagsAreClearedAfterThrow)
60 {
61 if (!stk::util::have_errno() && !stk::util::have_errexcept()) GTEST_SKIP();
62
63 stk::util::clear_fp_errors();
64 std::log(0.0);
65 EXPECT_ANY_THROW(stk::util::throw_on_fp_error());
66 EXPECT_NO_THROW(stk::util::throw_on_fp_error());
67 }
68
69 TEST(FPExceptions, ErrorMessageContainsName)
70 {
71 if (!stk::util::have_errno() && !stk::util::have_errexcept()) GTEST_SKIP();
72
73 stk::util::clear_fp_errors();
74 std::log(0.0);
75
76 std::string fname = "my_very_specific_and_clear_function_name";
77 try {
78 stk::util::throw_on_fp_error(fname.c_str());
79 } catch (std::exception& except)
80 {
81 std::string msg(except.what());
82 size_t pos = msg.find(fname);
83 EXPECT_NE(pos, std::string::npos);
84 }
85 }
86
87 TEST(FPExceptions, NoWarning)
88 {
89 if (!stk::util::have_errno() && !stk::util::have_errexcept()) GTEST_SKIP();
90
91 stk::util::clear_fp_errors();
92 double x = 1.0 + 2.0;
93 std::stringstream ss;
94 EXPECT_NO_THROW(stk::util::warn_on_fp_error(nullptr, ss));
95 EXPECT_EQ(ss.str().size(), 0U);
96 EXPECT_EQ(x, 3.0); // appease the compiler
97 }
98
99 TEST(FPExceptions, Warning)
100 {
101 if (!stk::util::have_errno() && !stk::util::have_errexcept()) GTEST_SKIP();
102
103 stk::util::clear_fp_errors();
104 std::log(0.0);
105 std::stringstream ss;
106 EXPECT_NO_THROW(stk::util::warn_on_fp_error(nullptr, ss));
107 EXPECT_GT(ss.str().size(), 0U);
108 }