Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / parameter_tests.cpp
1 // Copyright (C) 2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include <tests_common.hpp>
7 #include <ie_parameter.hpp>
8 #include <ie_layouts.h>
9
10 using namespace InferenceEngine;
11
12 class DestructorTest {
13 public:
14     DestructorTest() {
15         constructorCount++;
16     }
17
18     DestructorTest(const DestructorTest& c) {
19         constructorCount++;
20     }
21
22     DestructorTest(const DestructorTest&& c) {
23         constructorCount++;
24     }
25
26     ~DestructorTest() {
27         destructorCount++;
28     }
29
30     static size_t destructorCount;
31     static size_t constructorCount;
32 };
33 size_t DestructorTest::destructorCount = 0;
34 size_t DestructorTest::constructorCount = 0;
35
36 class ParameterTests : public TestsCommon {
37 public:
38     void SetUp() override {
39         TestsCommon::SetUp();
40         DestructorTest::destructorCount = 0;
41         DestructorTest::constructorCount = 0;
42     }
43 };
44
45 TEST_F(ParameterTests, ParameterAsInt) {
46     Parameter p = 4;
47     ASSERT_TRUE(p.is<int>());
48     int test = p;
49     ASSERT_EQ(4, test);
50 }
51
52 TEST_F(ParameterTests, ParameterAsUInt) {
53     Parameter p = 4u;
54     ASSERT_TRUE(p.is<unsigned int>());
55     ASSERT_FALSE(p.is<size_t>());
56     unsigned int test = p;
57     ASSERT_EQ(4, test);
58 }
59
60 TEST_F(ParameterTests, ParameterAsSize_t) {
61     size_t ref = 4;
62     Parameter p = ref;
63     ASSERT_TRUE(p.is<size_t>());
64     size_t test = p;
65     ASSERT_EQ(ref, test);
66 }
67
68 TEST_F(ParameterTests, ParameterAsFloat) {
69     Parameter p = 4.f;
70     ASSERT_TRUE(p.is<float>());
71     float test = p;
72     ASSERT_EQ(4.f, test);
73 }
74
75 TEST_F(ParameterTests, ParameterAsString) {
76     std::string ref = "test";
77     Parameter p = ref;
78     std::string test = p;
79     ASSERT_TRUE(p.is<std::string>());
80     ASSERT_EQ(ref, test);
81 }
82
83 TEST_F(ParameterTests, ParameterAsStringInLine) {
84     Parameter p = "test";
85     std::string test = p;
86     ASSERT_TRUE(p.is<std::string>());
87     ASSERT_EQ("test", test);
88 }
89
90 TEST_F(ParameterTests, IntParameterAsString) {
91     Parameter p = 4;
92     ASSERT_TRUE(p.is<int>());
93     ASSERT_FALSE(p.is<std::string>());
94     ASSERT_THROW(std::string test = p, std::bad_cast);
95     ASSERT_THROW(std::string test = p.as<std::string>(), std::bad_cast);
96 }
97
98 TEST_F(ParameterTests, StringParameterAsInt) {
99     Parameter p = "4";
100     ASSERT_FALSE(p.is<int>());
101     ASSERT_TRUE(p.is<std::string>());
102     ASSERT_THROW(int test = p, std::bad_cast);
103     ASSERT_THROW(int test = p.as<int>(), std::bad_cast);
104 }
105
106 TEST_F(ParameterTests, ParameterAsTensorDesc) {
107     TensorDesc ref(Precision::FP32, {1, 3, 2, 2}, Layout::NCHW);
108     Parameter p = ref;
109     ASSERT_TRUE(p.is<TensorDesc>());
110     TensorDesc test = p;
111     ASSERT_EQ(ref, test);
112 }
113
114 TEST_F(ParameterTests, ParameterAsInts) {
115     std::vector<int> ref = {1, 2, 3, 4, 5};
116     Parameter p = ref;
117     ASSERT_TRUE(p.is<std::vector<int>>());
118     std::vector<int> test = p;
119     ASSERT_EQ(ref.size(), test.size());
120     for (size_t i = 0; i < ref.size(); i++) {
121         ASSERT_EQ(ref[i], test[i]);
122     }
123 }
124
125 TEST_F(ParameterTests, ParameterAsUInts) {
126     std::vector<unsigned int> ref = {1, 2, 3, 4, 5};
127     Parameter p = ref;
128     ASSERT_TRUE(p.is<std::vector<unsigned int>>());
129     std::vector<unsigned int> test = p;
130     ASSERT_EQ(ref.size(), test.size());
131     for (size_t i = 0; i < ref.size(); i++) {
132         ASSERT_EQ(ref[i], test[i]);
133     }
134 }
135
136 TEST_F(ParameterTests, ParameterAsSize_ts) {
137     std::vector<size_t> ref = {1, 2, 3, 4, 5};
138     Parameter p = ref;
139     ASSERT_TRUE(p.is<std::vector<size_t>>());
140     std::vector<size_t> test = p;
141     ASSERT_EQ(ref.size(), test.size());
142     for (size_t i = 0; i < ref.size(); i++) {
143         ASSERT_EQ(ref[i], test[i]);
144     }
145 }
146
147 TEST_F(ParameterTests, ParameterAsFloats) {
148     std::vector<float> ref = {1, 2, 3, 4, 5};
149     Parameter p = ref;
150     ASSERT_TRUE(p.is<std::vector<float>>());
151     std::vector<float> test = p;
152     ASSERT_EQ(ref.size(), test.size());
153     for (size_t i = 0; i < ref.size(); i++) {
154         ASSERT_EQ(ref[i], test[i]);
155     }
156 }
157
158 TEST_F(ParameterTests, ParameterAsStrings) {
159     std::vector<std::string> ref = {"test1", "test2", "test3", "test4", "test1"};
160     Parameter p = ref;
161     ASSERT_TRUE(p.is<std::vector<std::string>>());
162     std::vector<std::string> test = p;
163     ASSERT_EQ(ref.size(), test.size());
164     for (size_t i = 0; i < ref.size(); i++) {
165         ASSERT_EQ(ref[i], test[i]);
166     }
167 }
168
169 TEST_F(ParameterTests, ParameterAsMapOfParameters) {
170     std::map<std::string, Parameter> refMap;
171     refMap["testParamInt"] = 4;
172     refMap["testParamString"] = "test";
173     Parameter p = refMap;
174     bool isMap = p.is<std::map<std::string, Parameter>>();
175     ASSERT_TRUE(isMap);
176     std::map<std::string, Parameter> testMap = p;
177
178     ASSERT_NE(testMap.find("testParamInt"), testMap.end());
179     ASSERT_NE(testMap.find("testParamString"), testMap.end());
180
181     int testInt = testMap["testParamInt"];
182     std::string testString = testMap["testParamString"];
183
184     ASSERT_EQ(refMap["testParamInt"].as<int>(), testInt);
185     ASSERT_EQ(refMap["testParamString"].as<std::string>(), testString);
186 }
187
188 TEST_F(ParameterTests, ParameterNotEmpty) {
189     Parameter p = 4;
190     ASSERT_FALSE(p.empty());
191 }
192
193 TEST_F(ParameterTests, ParameterEmpty) {
194     Parameter p;
195     ASSERT_TRUE(p.empty());
196 }
197
198 TEST_F(ParameterTests, ParameterClear) {
199     Parameter p = 4;
200     ASSERT_FALSE(p.empty());
201     p.clear();
202     ASSERT_TRUE(p.empty());
203 }
204
205 TEST_F(ParameterTests, ParametersNotEqualByType) {
206     Parameter p1 = 4;
207     Parameter p2 = "string";
208     ASSERT_TRUE(p1 != p2);
209     ASSERT_FALSE(p1 == p2);
210 }
211
212 TEST_F(ParameterTests, ParametersNotEqualByValue) {
213     Parameter p1 = 4;
214     Parameter p2 = 5;
215     ASSERT_TRUE(p1 != p2);
216     ASSERT_FALSE(p1 == p2);
217 }
218
219 TEST_F(ParameterTests, ParametersEqual) {
220     Parameter p1 = 4;
221     Parameter p2 = 4;
222     ASSERT_TRUE(p1 == p2);
223     ASSERT_FALSE(p1 != p2);
224 }
225
226 TEST_F(ParameterTests, CompareParametersWithoutEqualOperator) {
227     class TestClass {
228     public:
229         TestClass(int test, int* testPtr): test(test), testPtr(testPtr) {}
230
231     private:
232         int test;
233         int* testPtr;
234     };
235
236     TestClass a(2, (int *)0x234);
237     TestClass b(2, (int *)0x234);
238     TestClass c(3, (int *)0x234);
239     Parameter parA = a;
240     Parameter parB = b;
241     Parameter parC = c;
242
243     ASSERT_THROW(bool equal = parA == parB, details::InferenceEngineException);
244     ASSERT_THROW(bool equal = parA != parB, details::InferenceEngineException);
245     ASSERT_THROW(bool equal = parA == parC, details::InferenceEngineException);
246     ASSERT_THROW(bool equal = parA != parC, details::InferenceEngineException);
247 }
248
249 TEST_F(ParameterTests, ParameterRemovedRealObject) {
250     ASSERT_EQ(0, DestructorTest::constructorCount);
251     ASSERT_EQ(0, DestructorTest::destructorCount);
252     {
253         DestructorTest t;
254         Parameter p1 = t;
255     }
256     ASSERT_EQ(2, DestructorTest::constructorCount);
257     ASSERT_EQ(2, DestructorTest::destructorCount);
258 }
259
260 TEST_F(ParameterTests, ParameterRemovedRealObjectWithDuplication) {
261     ASSERT_EQ(0, DestructorTest::constructorCount);
262     ASSERT_EQ(0, DestructorTest::destructorCount);
263     {
264         DestructorTest t;
265         Parameter p = t;
266         ASSERT_EQ(0, DestructorTest::destructorCount);
267         p = t;
268         ASSERT_EQ(2, DestructorTest::destructorCount);
269     }
270     ASSERT_EQ(4, DestructorTest::constructorCount);
271     ASSERT_EQ(4, DestructorTest::destructorCount);
272 }
273
274 TEST_F(ParameterTests, ParameterRemovedRealObjectPointerWithDuplication) {
275     ASSERT_EQ(0, DestructorTest::constructorCount);
276     ASSERT_EQ(0, DestructorTest::destructorCount);
277     {
278         auto * t = new DestructorTest();
279         Parameter p = t;
280         ASSERT_EQ(1, DestructorTest::constructorCount);
281         ASSERT_EQ(0, DestructorTest::destructorCount);
282         p = t;
283         ASSERT_TRUE(p.is<DestructorTest *>());
284         DestructorTest* t2 = p;
285         ASSERT_EQ(0, DestructorTest::destructorCount);
286         delete t;
287         auto * t3 = p.as<DestructorTest *>();
288         ASSERT_EQ(t2, t3);
289     }
290     ASSERT_EQ(1, DestructorTest::constructorCount);
291     ASSERT_EQ(1, DestructorTest::destructorCount);
292 }