Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / exception_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include "tests_utils.hpp"
7 #include <details/ie_exception.hpp>
8 #include <cpp_interfaces/exception2status.hpp>
9 #include <details/ie_exception_conversion.hpp>
10
11 using namespace InferenceEngine;
12 class ExceptionTests : public ::testing::Test {};
13
14 TEST_F(ExceptionTests, canThrowUsingMacro) {
15     std::string message = "Exception message!";
16     EXPECT_THROW(THROW_IE_EXCEPTION << message, InferenceEngine::details::InferenceEngineException);
17 }
18
19 TEST_F(ExceptionTests, canThrowScoringException) {
20     InferenceEngine::details::InferenceEngineException exception(__FILE__, __LINE__);
21     EXPECT_THROW(throw exception, InferenceEngine::details::InferenceEngineException);
22 }
23
24 TEST_F(ExceptionTests, canDefineExceptionContent) {
25     InferenceEngine::details::InferenceEngineException exception(__FILE__, __LINE__);
26
27     ASSERT_STR_CONTAINS(exception.what(), "");
28 }
29
30 TEST_F(ExceptionTests, exceptionShowsCorrectMessage) {
31     std::string message = "Exception message!";
32     try {
33         THROW_IE_EXCEPTION << message;
34     }
35     catch (InferenceEngine::details::InferenceEngineException ex) {
36         ASSERT_STR_CONTAINS(ex.what(), message);
37     }
38 }
39
40 //TODO: enable test once info appears in exception message
41 TEST_F(ExceptionTests, DISABLED_exceptionShowsCorrectFileAndLineNumbers) {
42     std::string message = __FILE__;
43     int lineNum = 0;
44     try {
45         lineNum = __LINE__; THROW_IE_EXCEPTION;
46     }
47     catch (InferenceEngine::details::InferenceEngineException ex) {
48         message += ":" + std::to_string(lineNum);
49         ASSERT_STR_CONTAINS(ex.what(), message); 
50     }
51 }
52
53 TEST_F(ExceptionTests, exceptionCanBeCoughtAsStandard) {
54     ASSERT_THROW(THROW_IE_EXCEPTION, std::exception);
55 }
56
57 // Status Code tests
58 TEST_F(ExceptionTests, canThrowStatusCode) {
59     try {
60         THROW_IE_EXCEPTION << InferenceEngine::details::as_status << InferenceEngine::StatusCode::INFER_NOT_STARTED;
61     }
62     catch (const InferenceEngine::details::InferenceEngineException &iex) {
63         ASSERT_TRUE(iex.hasStatus());
64         ASSERT_EQ(iex.getStatus(), InferenceEngine::StatusCode::INFER_NOT_STARTED);
65     }
66 }
67
68 TEST_F(ExceptionTests, handleOnlyFirstStatus) {
69     try {
70         THROW_IE_EXCEPTION<< InferenceEngine::details::as_status <<
71                 InferenceEngine::StatusCode::NETWORK_NOT_LOADED << InferenceEngine::StatusCode::NOT_FOUND;
72     }
73     catch (const InferenceEngine::details::InferenceEngineException &iex) {
74         ASSERT_TRUE(iex.hasStatus());
75         ASSERT_EQ(iex.getStatus(), InferenceEngine::StatusCode::NETWORK_NOT_LOADED);
76     }
77 }
78
79 TEST_F(ExceptionTests, ignoreNotStatusCodeEnumAfterManip) {
80     enum testEnum : int { FIRST = 1 };
81     try {
82         THROW_IE_EXCEPTION<< InferenceEngine::details::as_status << testEnum::FIRST;
83     }
84     catch (const InferenceEngine::details::InferenceEngineException &iex) {
85         ASSERT_FALSE(iex.hasStatus());
86     }
87 }
88
89 TEST_F(ExceptionTests, canUseManipulatorStandalone) {
90     auto iex = details::InferenceEngineException("filename", 1);
91     as_status(iex);
92     try {
93         throw iex << NOT_IMPLEMENTED;
94     } catch (const InferenceEngine::details::InferenceEngineException &iex) {
95         ASSERT_TRUE(iex.hasStatus());
96         ASSERT_EQ(iex.getStatus(), InferenceEngine::StatusCode::NOT_IMPLEMENTED);
97     }
98 }
99
100 TEST_F(ExceptionTests, statusCodeNotAppearInMessageAfterCatch) {
101     std::string message = "Exception message!";
102     std::string strStatusCode = std::to_string(StatusCode::NETWORK_NOT_LOADED);
103     try {
104         THROW_IE_EXCEPTION<< "<unique--" << InferenceEngine::details::as_status  <<
105         InferenceEngine::StatusCode::NETWORK_NOT_LOADED << "--unique>" << message;
106     }
107     catch (const InferenceEngine::details::InferenceEngineException &iex) {
108         ASSERT_STR_CONTAINS(iex.what(), message);
109         ASSERT_STR_DOES_NOT_CONTAIN(iex.what(), "<unique--" + strStatusCode + "--unique>");
110     }
111 }
112
113 TEST_F(ExceptionTests, statusCodeAppearInMessageAfterCatch) {
114     std::string message = "Exception message!";
115     std::string strStatusCode = std::to_string(StatusCode::NETWORK_NOT_LOADED);
116     try {
117         THROW_IE_EXCEPTION<< "<unique--" << InferenceEngine::StatusCode::NETWORK_NOT_LOADED << "--unique>" << message;
118     }
119     catch (const InferenceEngine::details::InferenceEngineException &iex) {
120         ASSERT_STR_CONTAINS(iex.what(), message);
121         ASSERT_STR_CONTAINS(iex.what(), "<unique--" + strStatusCode + "--unique>");
122     }
123 }
124
125 template <StatusCode T>
126 class WrapperClass {
127 public:
128     static InferenceEngine::StatusCode toStatusWrapper(InferenceEngine::ResponseDesc* resp) {
129         TO_STATUS(THROW_IE_EXCEPTION << details::as_status << T);
130     }
131     static InferenceEngine::StatusCode toStatusWrapperMsg(std::string &msg, InferenceEngine::ResponseDesc* resp) {
132         TO_STATUS(THROW_IE_EXCEPTION << details::as_status << T << msg);
133     }
134 };
135
136 // TO_STATUS macros tests
137 TEST_F(ExceptionTests, canConvertToStatus) {
138     ResponseDesc *resp = nullptr;
139     ASSERT_EQ(WrapperClass<StatusCode::GENERAL_ERROR>::toStatusWrapper(resp), StatusCode::GENERAL_ERROR);
140     ASSERT_EQ(WrapperClass<StatusCode::NOT_IMPLEMENTED>::toStatusWrapper(resp), StatusCode::NOT_IMPLEMENTED);
141     ASSERT_EQ(WrapperClass<StatusCode::NETWORK_NOT_LOADED>::toStatusWrapper(resp), StatusCode::NETWORK_NOT_LOADED);
142     ASSERT_EQ(WrapperClass<StatusCode::PARAMETER_MISMATCH>::toStatusWrapper(resp), StatusCode::PARAMETER_MISMATCH);
143     ASSERT_EQ(WrapperClass<StatusCode::NOT_FOUND>::toStatusWrapper(resp), StatusCode::NOT_FOUND);
144     ASSERT_EQ(WrapperClass<StatusCode::OUT_OF_BOUNDS>::toStatusWrapper(resp), StatusCode::OUT_OF_BOUNDS);
145     ASSERT_EQ(WrapperClass<StatusCode::UNEXPECTED>::toStatusWrapper(resp), StatusCode::UNEXPECTED);
146     ASSERT_EQ(WrapperClass<StatusCode::REQUEST_BUSY>::toStatusWrapper(resp), StatusCode::REQUEST_BUSY);
147     ASSERT_EQ(WrapperClass<StatusCode::RESULT_NOT_READY>::toStatusWrapper(resp), StatusCode::RESULT_NOT_READY);
148     ASSERT_EQ(WrapperClass<StatusCode::NOT_ALLOCATED>::toStatusWrapper(resp), StatusCode::NOT_ALLOCATED);
149     ASSERT_EQ(WrapperClass<StatusCode::INFER_NOT_STARTED>::toStatusWrapper(resp), StatusCode::INFER_NOT_STARTED);
150 }
151
152 // CALL_STATUS_FNC macros tests
153 TEST_F(ExceptionTests, canConvertStatusToException) {
154     std::shared_ptr<WrapperClass<StatusCode::INFER_NOT_STARTED>> actual;
155     ASSERT_THROW(CALL_STATUS_FNC_NO_ARGS(toStatusWrapper), InferenceEngine::InferNotStarted);
156 }
157
158 TEST_F(ExceptionTests, throwAfterConvertStatusToClassContainMessage) {
159     std::string message = "Exception message!";
160     std::shared_ptr<WrapperClass<StatusCode::NOT_ALLOCATED>> actual;
161     try {
162         CALL_STATUS_FNC(toStatusWrapperMsg, message);
163     } catch (const NotAllocated &iex) {
164         ASSERT_STR_CONTAINS(iex.what(), message);
165     }
166 }
167
168 #ifdef    NDEBUG //disabled for debug as macros calls assert()
169 TEST_F(ExceptionTests, exceptionWithAssertThrowsNothingIfTrue) {
170     EXPECT_NO_THROW(IE_ASSERT(true) << "shouldn't assert if true");
171 }
172
173 TEST_F(ExceptionTests, exceptionWithAssertThrowsNothingIfExpressionTrue) {
174     EXPECT_NO_THROW(IE_ASSERT(2 > 0) << "shouldn't assert if true expression");
175 }
176
177 TEST_F(ExceptionTests, exceptionWithAssertThrowsExceptionIfFalse) {
178     EXPECT_THROW(IE_ASSERT(false), InferenceEngine::details::InferenceEngineException);
179 }
180
181 TEST_F(ExceptionTests, exceptionWithAssertThrowsExceptionIfFalseExpession) {
182     EXPECT_THROW(IE_ASSERT(0 == 1), InferenceEngine::details::InferenceEngineException);
183 }
184 #endif  //NDEBUG