ArmNN
 20.02
UnitTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #define BOOST_TEST_MODULE UnitTests
6 #include <boost/test/unit_test.hpp>
7 
8 #include "UnitTests.hpp"
9 #include <armnn/Logging.hpp>
10 
11 #include <boost/algorithm/string.hpp>
12 
13 struct ConfigureLoggingFixture
14 {
15  ConfigureLoggingFixture()
16  {
18  }
19 };
20 
21 BOOST_GLOBAL_FIXTURE(ConfigureLoggingFixture);
22 
23 // On Windows, duplicate the boost test logging output to the Visual Studio output window using OutputDebugString.
24 #if defined(_MSC_VER)
25 
26 #include <boost/iostreams/filtering_stream.hpp>
27 #include <boost/iostreams/tee.hpp>
28 #include <iostream>
29 #include <Windows.h>
30 
31 using namespace boost::iostreams;
32 using namespace std;
33 
34 struct DebugOutputSink : boost::iostreams::sink
35 {
36  std::streamsize write(const char* s, std::streamsize n)
37  {
38  // The given string is not null-terminated, so we need to copy it.
39  std::string s2(s, boost::numeric_cast<size_t>(n));
40  OutputDebugString(s2.c_str());
41  return n;
42  }
43 };
44 
45 class SetupDebugOutput
46 {
47 public:
48  SetupDebugOutput()
49  {
50  // Sends the output to both cout (as standard) and the debug output.
51  m_OutputStream.push(tee(std::cout));
52  m_OutputStream.push(m_DebugOutputSink);
53 
54  boost::unit_test::unit_test_log.set_stream(m_OutputStream);
55  }
56 private:
57  filtering_ostream m_OutputStream;
58  DebugOutputSink m_DebugOutputSink;
59 };
60 
61 BOOST_GLOBAL_FIXTURE(SetupDebugOutput);
62 
63 #endif // defined(_MSC_VER)
64 
65 
66 BOOST_AUTO_TEST_SUITE(LoggerSuite)
67 
69 {
70  std::stringstream ss;
71 
72  {
73  struct StreamRedirector
74  {
75  public:
76  StreamRedirector(std::ostream& stream, std::streambuf* newStreamBuffer)
77  : m_Stream(stream)
78  , m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer))
79  {}
80  ~StreamRedirector() { m_Stream.rdbuf(m_BackupBuffer); }
81 
82  private:
83  std::ostream& m_Stream;
84  std::streambuf* m_BackupBuffer;
85  };
86 
87 
88  StreamRedirector redirect(std::cout, ss.rdbuf());
89 
90  using namespace armnn;
91  SetLogFilter(LogSeverity::Trace);
92  SetAllLoggingSinks(true, false, false);
93 
94 
95  ARMNN_LOG(trace) << "My trace message; " << -2;
96  ARMNN_LOG(debug) << "My debug message; " << -1;
97  ARMNN_LOG(info) << "My info message; " << 0;
98  ARMNN_LOG(warning) << "My warning message; " << 1;
99  ARMNN_LOG(error) << "My error message; " << 2;
100  ARMNN_LOG(fatal) << "My fatal message; " << 3;
101 
102  SetLogFilter(LogSeverity::Fatal);
103 
104  }
105 
106  BOOST_CHECK(boost::contains(ss.str(), "Trace: My trace message; -2"));
107  BOOST_CHECK(boost::contains(ss.str(), "Debug: My debug message; -1"));
108  BOOST_CHECK(boost::contains(ss.str(), "Info: My info message; 0"));
109  BOOST_CHECK(boost::contains(ss.str(), "Warning: My warning message; 1"));
110  BOOST_CHECK(boost::contains(ss.str(), "Error: My error message; 2"));
111  BOOST_CHECK(boost::contains(ss.str(), "Fatal: My fatal message; 3"));
112 }
113 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_AUTO_TEST_CASE(LoggerTest)
Definition: UnitTests.cpp:68
void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:146
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
Copyright (c) 2020 ARM Limited.
BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::WaitingForAck)
void SetLogFilter(LogSeverity level)
Definition: Logging.cpp:28
void ConfigureLoggingTest()
Definition: UnitTests.hpp:15
BOOST_AUTO_TEST_SUITE_END()
BOOST_GLOBAL_FIXTURE(ConfigureLoggingFixture)