ArmNN
 20.05
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 struct ConfigureLoggingFixture
12 {
13  ConfigureLoggingFixture()
14  {
16  }
17 };
18 
19 BOOST_GLOBAL_FIXTURE(ConfigureLoggingFixture);
20 
21 // On Windows, duplicate the boost test logging output to the Visual Studio output window using OutputDebugString.
22 #if defined(_MSC_VER)
23 
24 #include <boost/iostreams/filtering_stream.hpp>
25 #include <boost/iostreams/tee.hpp>
26 #include <iostream>
27 #include <Windows.h>
28 
29 using namespace boost::iostreams;
30 using namespace std;
31 
32 struct DebugOutputSink : boost::iostreams::sink
33 {
34  std::streamsize write(const char* s, std::streamsize n)
35  {
36  // The given string is not null-terminated, so we need to copy it.
37  std::string s2(s, boost::numeric_cast<size_t>(n));
38  OutputDebugString(s2.c_str());
39  return n;
40  }
41 };
42 
43 class SetupDebugOutput
44 {
45 public:
46  SetupDebugOutput()
47  {
48  // Sends the output to both cout (as standard) and the debug output.
49  m_OutputStream.push(tee(std::cout));
50  m_OutputStream.push(m_DebugOutputSink);
51 
52  boost::unit_test::unit_test_log.set_stream(m_OutputStream);
53  }
54 private:
55  filtering_ostream m_OutputStream;
56  DebugOutputSink m_DebugOutputSink;
57 };
58 
59 BOOST_GLOBAL_FIXTURE(SetupDebugOutput);
60 
61 #endif // defined(_MSC_VER)
62 
63 
64 BOOST_AUTO_TEST_SUITE(LoggerSuite)
65 
67 {
68  std::stringstream ss;
69 
70  {
71  struct StreamRedirector
72  {
73  public:
74  StreamRedirector(std::ostream& stream, std::streambuf* newStreamBuffer)
75  : m_Stream(stream)
76  , m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer))
77  {}
78  ~StreamRedirector() { m_Stream.rdbuf(m_BackupBuffer); }
79 
80  private:
81  std::ostream& m_Stream;
82  std::streambuf* m_BackupBuffer;
83  };
84 
85 
86  StreamRedirector redirect(std::cout, ss.rdbuf());
87 
88  using namespace armnn;
89  SetLogFilter(LogSeverity::Trace);
90  SetAllLoggingSinks(true, false, false);
91 
92 
93  ARMNN_LOG(trace) << "My trace message; " << -2;
94  ARMNN_LOG(debug) << "My debug message; " << -1;
95  ARMNN_LOG(info) << "My info message; " << 0;
96  ARMNN_LOG(warning) << "My warning message; " << 1;
97  ARMNN_LOG(error) << "My error message; " << 2;
98  ARMNN_LOG(fatal) << "My fatal message; " << 3;
99 
100  SetLogFilter(LogSeverity::Fatal);
101 
102  }
103 
104  BOOST_CHECK(ss.str().find("Trace: My trace message; -2") != std::string::npos);
105  BOOST_CHECK(ss.str().find("Debug: My debug message; -1") != std::string::npos);
106  BOOST_CHECK(ss.str().find("Info: My info message; 0") != std::string::npos);
107  BOOST_CHECK(ss.str().find("Warning: My warning message; 1") != std::string::npos);
108  BOOST_CHECK(ss.str().find("Error: My error message; 2") != std::string::npos);
109  BOOST_CHECK(ss.str().find("Fatal: My fatal message; 3") != std::string::npos);
110 }
111 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_AUTO_TEST_CASE(LoggerTest)
Definition: UnitTests.cpp:66
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)