| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2005 StatPro Italia srl |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include "tracing.hpp" |
| 21 | #include "utilities.hpp" |
| 22 | #include <ql/utilities/tracing.hpp> |
| 23 | #include <sstream> |
| 24 | #include <iostream> |
| 25 | |
| 26 | using namespace QuantLib; |
| 27 | using namespace boost::unit_test_framework; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class TestCaseCleaner { // NOLINT(cppcoreguidelines-special-member-functions) |
| 32 | public: |
| 33 | TestCaseCleaner() = default; |
| 34 | ~TestCaseCleaner() { |
| 35 | QL_TRACE_ON(std::cerr); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | #if defined(__clang__) && __clang_major__ >= 14 |
| 40 | #pragma clang diagnostic push |
| 41 | #pragma clang diagnostic ignored "-Wunused-but-set-variable" |
| 42 | #endif |
| 43 | |
| 44 | void testTraceOutput(bool enable, |
| 45 | #if defined(QL_ENABLE_TRACING) |
| 46 | const std::string& result) { |
| 47 | #else |
| 48 | const std::string&) { |
| 49 | #endif |
| 50 | |
| 51 | TestCaseCleaner cleaner; |
| 52 | |
| 53 | std::ostringstream output; |
| 54 | if (enable) |
| 55 | QL_TRACE_ENABLE; |
| 56 | else |
| 57 | QL_TRACE_DISABLE; |
| 58 | QL_TRACE_ON(output); |
| 59 | int i = 42; |
| 60 | QL_TRACE_VARIABLE(i); |
| 61 | i++; |
| 62 | |
| 63 | #if defined(QL_ENABLE_TRACING) |
| 64 | std::string expected = result; |
| 65 | #else |
| 66 | std::string expected; |
| 67 | #endif |
| 68 | if (output.str() != expected) { |
| 69 | BOOST_FAIL("wrong trace:\n" |
| 70 | " expected:\n" |
| 71 | "\"" + expected + "\"\n" |
| 72 | " written:\n" |
| 73 | "\"" + output.str() + "\"" ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #if defined(__clang__) && __clang_major__ >= 14 |
| 78 | #pragma clang diagnostic pop |
| 79 | #endif |
| 80 | |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void TracingTest::testOutput() { |
| 85 | |
| 86 | BOOST_TEST_MESSAGE("Testing tracing..." ); |
| 87 | |
| 88 | testTraceOutput(enable: false, "" ); |
| 89 | testTraceOutput(enable: true, "trace[0]: i = 42\n" ); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | test_suite* TracingTest::suite() { |
| 94 | auto* suite = BOOST_TEST_SUITE("Tracing tests" ); |
| 95 | |
| 96 | suite->add(QUANTLIB_TEST_CASE(&TracingTest::testOutput)); |
| 97 | return suite; |
| 98 | } |
| 99 | |