forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any.cpp
737 lines (623 loc) · 20.2 KB
/
any.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "openvino/core/any.hpp"
#include <gtest/gtest.h>
#include <string>
#include "common_test_utils/test_assertions.hpp"
#include "openvino/core/runtime_attribute.hpp"
using namespace ov;
class DestructorTest {
public:
// Base member type defined. ov:Any can access all derived class
using Base = std::tuple<DestructorTest>;
DestructorTest() {
constructorCount++;
}
DestructorTest(const DestructorTest& c) {
constructorCount++;
}
DestructorTest(const DestructorTest&& c) {
constructorCount++;
}
virtual ~DestructorTest() {
destructorCount++;
}
virtual const std::type_info& type_info() const {
return typeid(DestructorTest);
}
static size_t destructorCount;
static size_t constructorCount;
};
class Derived : public DestructorTest {
public:
using DestructorTest::DestructorTest;
const std::type_info& type_info() const override {
return typeid(Derived);
}
};
size_t DestructorTest::destructorCount = 0;
size_t DestructorTest::constructorCount = 0;
class AnyTests : public ::testing::Test {
public:
void SetUp() override {
DestructorTest::destructorCount = 0;
DestructorTest::constructorCount = 0;
}
};
TEST_F(AnyTests, parameter_std_string) {
auto parameter = Any{"My string"};
ASSERT_TRUE(parameter.is<std::string>());
EXPECT_EQ(parameter.as<std::string>(), "My string");
}
TEST_F(AnyTests, parameter_int64_t) {
auto parameter = Any{int64_t(27)};
ASSERT_TRUE(parameter.is<int64_t>());
EXPECT_FALSE(parameter.is<std::string>());
EXPECT_EQ(parameter.as<int64_t>(), 27);
}
struct Ship {
Ship(const std::string& name_, const int16_t x_, const int16_t y_) : name{name_}, x{x_}, y{y_} {}
std::string name;
int16_t x;
int16_t y;
};
TEST_F(AnyTests, parameter_ship) {
{
auto parameter = Any{Ship{"Lollipop", 3, 4}};
ASSERT_TRUE(parameter.is<Ship>());
Ship& ship = parameter.as<Ship>();
EXPECT_EQ(ship.name, "Lollipop");
EXPECT_EQ(ship.x, 3);
EXPECT_EQ(ship.y, 4);
}
{
auto parameter = Any::make<Ship>("Lollipop", int16_t(3), int16_t(4));
ASSERT_TRUE(parameter.is<Ship>());
Ship& ship = parameter.as<Ship>();
EXPECT_EQ(ship.name, "Lollipop");
EXPECT_EQ(ship.x, 3);
EXPECT_EQ(ship.y, 4);
}
}
TEST_F(AnyTests, AnyAsInt) {
Any p = 4;
ASSERT_TRUE(p.is<int>());
int test = p.as<int>();
ASSERT_EQ(4, test);
}
TEST_F(AnyTests, AnyAsUInt) {
Any p = uint32_t(4);
ASSERT_TRUE(p.is<uint32_t>());
ASSERT_TRUE(p.is<uint32_t>());
uint32_t test = p.as<uint32_t>();
ASSERT_EQ(4, test);
}
TEST_F(AnyTests, AnyAsString) {
std::string ref = "test";
Any p = ref;
ASSERT_TRUE(p.is<std::string>());
std::string test = p.as<std::string>();
ASSERT_EQ(ref, test);
}
TEST_F(AnyTests, AnyAsStringInLine) {
Any p = "test";
ASSERT_TRUE(p.is<std::string>());
std::string test = p.as<std::string>();
ASSERT_EQ("test", test);
}
TEST_F(AnyTests, AnyAsInts) {
std::vector<int> ref = {1, 2, 3, 4, 5};
Any p = ref;
ASSERT_TRUE(p.is<std::vector<int>>());
std::vector<int> test = p.as<std::vector<int>>();
ASSERT_EQ(ref.size(), test.size());
for (size_t i = 0; i < ref.size(); i++) {
ASSERT_EQ(ref[i], test[i]);
}
}
TEST_F(AnyTests, AnyAsMapOfAnys) {
std::map<std::string, Any> refMap;
refMap["testParamInt"] = 4;
refMap["testParamString"] = "test";
Any p = refMap;
bool isMap = p.is<std::map<std::string, Any>>();
ASSERT_TRUE(isMap);
auto testMap = p.as<std::map<std::string, Any>>();
ASSERT_NE(testMap.find("testParamInt"), testMap.end());
ASSERT_NE(testMap.find("testParamString"), testMap.end());
int testInt = testMap["testParamInt"].as<int>();
std::string testString = testMap["testParamString"].as<std::string>();
ASSERT_EQ(refMap["testParamInt"].as<int>(), testInt);
ASSERT_EQ(refMap["testParamString"].as<std::string>(), testString);
}
TEST_F(AnyTests, AnyAsSetOfAnys) {
std::set<std::string> refSet0;
std::set<int> refSet1;
refSet0.insert("test");
refSet1.insert(4);
Any s0 = refSet0;
Any s1 = refSet1;
bool isSet0 = s0.is<std::set<std::string>>();
bool isSet1 = s1.is<std::set<int>>();
ASSERT_TRUE(isSet0);
ASSERT_TRUE(isSet1);
auto testSet0 = s0.as<std::set<std::string>>();
auto testSet1 = s1.as<std::set<int>>();
ASSERT_NE(testSet0.count("test"), 0);
ASSERT_NE(testSet1.count(4), 0);
}
TEST_F(AnyTests, AnyAsMapOfMapOfAnys) {
std::map<std::string, Any> refMap1;
refMap1["testParamInt"] = 4;
refMap1["testParamString"] = "test";
std::map<std::string, Any> refMap2;
refMap2["testParamInt"] = 5;
refMap2["testParamString"] = "test2";
std::map<std::string, Any> refMap;
refMap["refMap1"] = refMap1;
refMap["refMap2"] = refMap2;
Any p = refMap;
bool isMap = p.is<std::map<std::string, Any>>();
ASSERT_TRUE(isMap);
auto testMap = p.as<std::map<std::string, Any>>();
ASSERT_NE(testMap.find("refMap1"), testMap.end());
auto testMap1 = testMap.at("refMap1").as<std::map<std::string, Any>>();
ASSERT_NE(testMap1.find("testParamInt"), testMap1.end());
ASSERT_NE(testMap1.find("testParamString"), testMap1.end());
int testInt1 = testMap1["testParamInt"].as<int>();
std::string testString1 = testMap1["testParamString"].as<std::string>();
ASSERT_EQ(refMap1["testParamInt"].as<int>(), testInt1);
ASSERT_EQ(refMap1["testParamString"].as<std::string>(), testString1);
ASSERT_NE(testMap.find("refMap2"), testMap.end());
auto testMap2 = testMap.at("refMap2").as<std::map<std::string, Any>>();
ASSERT_NE(testMap2.find("testParamInt"), testMap2.end());
ASSERT_NE(testMap2.find("testParamString"), testMap2.end());
int testInt2 = testMap2["testParamInt"].as<int>();
std::string testString2 = testMap2["testParamString"].as<std::string>();
ASSERT_EQ(refMap2["testParamInt"].as<int>(), testInt2);
ASSERT_EQ(refMap2["testParamString"].as<std::string>(), testString2);
}
TEST_F(AnyTests, AnyAsMapOfMapOfAnysFromString) {
const std::string string_props = "{map1:{prop1:1,prop2:2.0},map2:{prop1:value}}";
ov::Any any(string_props);
ov::AnyMap map;
ASSERT_TRUE(any.is<std::string>());
ASSERT_FALSE(any.is<ov::AnyMap>());
OV_ASSERT_NO_THROW(map = any.as<ov::AnyMap>());
ASSERT_EQ(string_props, ov::Any(map).as<std::string>());
// check map1
using MapStrDouble = std::map<std::string, double>;
MapStrDouble map1;
ASSERT_TRUE(map["map1"].is<std::string>());
ASSERT_FALSE(map["map1"].is<ov::AnyMap>());
ASSERT_FALSE(map["map1"].is<MapStrDouble>());
OV_ASSERT_NO_THROW(map1 = map["map1"].as<MapStrDouble>());
ASSERT_EQ(2, map1.size());
// check map1:prop1
ASSERT_EQ(1.0, map1["prop1"]);
// check map1:prop2
ASSERT_EQ(2.0, map1["prop2"]);
// check map2
ov::AnyMap map2;
ASSERT_TRUE(map["map2"].is<std::string>());
ASSERT_FALSE(map["map2"].is<ov::AnyMap>());
OV_ASSERT_NO_THROW(map2 = map["map2"].as<ov::AnyMap>());
ASSERT_EQ(1, map2.size());
// check map1:prop1
ASSERT_TRUE(map2["prop1"].is<std::string>());
ASSERT_FALSE(map2["prop1"].is<int>());
ASSERT_EQ("value", map2["prop1"].as<std::string>());
}
TEST_F(AnyTests, AnyAsMapOfMapOfMapOfAnysFromString) {
const std::string string_props = "{map1:{subprop_map:{prop:value}},prop1:1,prop2:2.0}";
ov::Any any(string_props);
ov::AnyMap map;
ASSERT_TRUE(any.is<std::string>());
ASSERT_FALSE(any.is<ov::AnyMap>());
OV_ASSERT_NO_THROW(map = any.as<ov::AnyMap>());
ASSERT_EQ(3, map.size());
ASSERT_EQ(string_props, ov::Any(map).as<std::string>());
// check prop1
ASSERT_TRUE(map["prop1"].is<std::string>());
ASSERT_FALSE(map["prop1"].is<int>());
ASSERT_EQ("1", map["prop1"].as<std::string>());
ASSERT_EQ(1, map["prop1"].as<int>());
// check prop2
ASSERT_TRUE(map["prop2"].is<std::string>());
ASSERT_FALSE(map["prop2"].is<int>());
ASSERT_FALSE(map["prop2"].is<double>());
ASSERT_EQ("2.0", map["prop2"].as<std::string>());
ASSERT_EQ(2, map["prop2"].as<int>());
ASSERT_EQ(2.0, map["prop2"].as<double>());
// check map1
ov::AnyMap map1;
ASSERT_TRUE(map["map1"].is<std::string>());
ASSERT_FALSE(map["map1"].is<ov::AnyMap>());
OV_ASSERT_NO_THROW(map1 = map["map1"].as<ov::AnyMap>());
// check subprop
ov::AnyMap subprop_map;
ASSERT_TRUE(map1["subprop_map"].is<std::string>());
ASSERT_FALSE(map1["subprop_map"].is<ov::AnyMap>());
OV_ASSERT_NO_THROW(subprop_map = map1["subprop_map"].as<ov::AnyMap>());
// check prop
ASSERT_TRUE(subprop_map["prop"].is<std::string>());
ASSERT_FALSE(subprop_map["prop"].is<ov::AnyMap>());
ASSERT_EQ("value", subprop_map["prop"].as<std::string>());
}
TEST_F(AnyTests, AnyDoesNotShareValues) {
// simple types
{
Any a = 1;
Any b = a;
a = 2;
ASSERT_EQ(1, b.as<int>());
ASSERT_EQ(2, a.as<int>());
b = 3;
ASSERT_EQ(2, a.as<int>());
ASSERT_EQ(3, b.as<int>());
}
// AnyMap's
{
AnyMap map{
{"1", ov::Any(1)},
{"2", ov::Any(2)},
};
Any a = map;
// check initial state
ASSERT_EQ(1, a.as<AnyMap>()["1"].as<int>());
ASSERT_EQ(2, a.as<AnyMap>()["2"].as<int>());
map["1"] = 3; // change map
ASSERT_EQ(1, a.as<AnyMap>()["1"].as<int>()); // Any is not changed
a.as<AnyMap>()["2"] = 4; // change Any
ASSERT_EQ(2, map["2"].as<int>()); // map is not changed
// erase from Any's map
AnyMap from_any_map = a.as<AnyMap>();
from_any_map.erase(from_any_map.begin());
ASSERT_EQ(2, map.size());
// erase from map
map.erase(map.find("2"));
ASSERT_NE(from_any_map.end(), from_any_map.find("2"));
ASSERT_EQ(4, a.as<AnyMap>()["2"].as<int>());
}
}
TEST_F(AnyTests, AnyMapSharesValues) {
AnyMap map{
{"1", 1},
{"2", 2},
};
AnyMap copy_map = map;
// check initial state
ASSERT_EQ(1, copy_map["1"].as<int>());
ASSERT_EQ(2, copy_map["2"].as<int>());
// change map
map["1"].as<int>() = 110;
// check copied state
EXPECT_EQ(110, map["1"].as<int>());
EXPECT_EQ(1, copy_map["1"].as<int>());
}
TEST_F(AnyTests, AnyMapSharesComplexValues) {
const std::string string_props = "{map1:{subprop_map:{prop:value}},prop1:1,prop2:2.0}";
ov::Any any(string_props);
ov::AnyMap map;
OV_ASSERT_NO_THROW(map = any.as<ov::AnyMap>());
AnyMap copy_map = map;
// check initial state
ASSERT_EQ(1, copy_map["prop1"].as<int>());
// change map
map["prop1"].as<std::string>() = "110";
// check original and copied state
EXPECT_EQ("110", map["prop1"].as<std::string>());
EXPECT_EQ("1", copy_map["prop1"].as<std::string>());
}
TEST_F(AnyTests, AnyNotEmpty) {
Any p = 4;
ASSERT_FALSE(p.empty());
}
TEST_F(AnyTests, AnyEmpty) {
Any p;
ASSERT_TRUE(p.empty());
}
TEST_F(AnyTests, AnyClear) {
Any p = 4;
ASSERT_FALSE(p.empty());
p = {};
ASSERT_TRUE(p.empty());
}
TEST_F(AnyTests, AnysNotEqualByType) {
Any p1 = 4;
Any p2 = "string";
ASSERT_TRUE(p1 != p2);
ASSERT_FALSE(p1 == p2);
}
TEST_F(AnyTests, AnysNotEqualByValue) {
Any p1 = 4;
Any p2 = 5;
ASSERT_TRUE(p1 != p2);
ASSERT_FALSE(p1 == p2);
}
TEST_F(AnyTests, AnysEqual) {
Any p1 = 4;
Any p2 = 4;
ASSERT_TRUE(p1 == p2);
ASSERT_FALSE(p1 != p2);
}
TEST_F(AnyTests, AnysStringEqual) {
std::string s1 = "abc";
std::string s2 = std::string("a") + "bc";
Any p1 = s1;
Any p2 = s2;
ASSERT_TRUE(s1 == s2);
ASSERT_TRUE(p1 == p2);
ASSERT_FALSE(p1 != p2);
}
TEST_F(AnyTests, MapOfAnysEqual) {
std::map<std::string, Any> map0;
map0["testParamInt"] = 4;
map0["testParamString"] = "test";
const auto map1 = map0;
Any p0 = map0;
Any p1 = map1;
ASSERT_TRUE(p0 == p1);
ASSERT_FALSE(p0 != p1);
}
TEST_F(AnyTests, CompareAnysWithoutEqualOperator) {
class TestClass {
public:
TestClass(int test, int* testPtr) : test(test), testPtr(testPtr) {}
int get_test() {
return test;
}
int* get_test_ptr() {
return testPtr;
}
private:
int test;
int* testPtr;
};
TestClass a(2, reinterpret_cast<int*>(0x234));
TestClass b(2, reinterpret_cast<int*>(0x234));
TestClass c(3, reinterpret_cast<int*>(0x234));
Any parA = a;
Any parB = b;
Any parC = c;
ASSERT_THROW((void)(parA == parB), ov::Exception);
ASSERT_THROW((void)(parA != parB), ov::Exception);
ASSERT_THROW((void)(parA == parC), ov::Exception);
ASSERT_THROW((void)(parA != parC), ov::Exception);
}
TEST_F(AnyTests, AnyRemovedRealObject) {
ASSERT_EQ(0, DestructorTest::constructorCount);
ASSERT_EQ(0, DestructorTest::destructorCount);
{
DestructorTest t;
Any p1 = t;
}
ASSERT_EQ(2, DestructorTest::constructorCount);
ASSERT_EQ(2, DestructorTest::destructorCount);
}
TEST_F(AnyTests, AnyRemovedRealObjectWithDuplication) {
ASSERT_EQ(0, DestructorTest::constructorCount);
ASSERT_EQ(0, DestructorTest::destructorCount);
{
DestructorTest t;
Any p = t;
ASSERT_EQ(0, DestructorTest::destructorCount);
p = t;
ASSERT_EQ(1, DestructorTest::destructorCount);
}
ASSERT_EQ(3, DestructorTest::constructorCount);
ASSERT_EQ(3, DestructorTest::destructorCount);
}
TEST_F(AnyTests, AnyRemovedRealObjectPointerWithDuplication) {
ASSERT_EQ(0, DestructorTest::constructorCount);
ASSERT_EQ(0, DestructorTest::destructorCount);
{
auto* t = new DestructorTest();
Any p = t;
ASSERT_EQ(1, DestructorTest::constructorCount);
ASSERT_EQ(0, DestructorTest::destructorCount);
p = t;
ASSERT_TRUE(p.is<DestructorTest*>());
DestructorTest* t2 = p.as<DestructorTest*>();
ASSERT_EQ(0, DestructorTest::destructorCount);
delete t;
auto* t3 = p.as<DestructorTest*>();
ASSERT_EQ(t2, t3);
}
ASSERT_EQ(1, DestructorTest::constructorCount);
ASSERT_EQ(1, DestructorTest::destructorCount);
}
void PrintTo(const Any& object, std::ostream* stream);
void PrintTo(const Any& object, std::ostream* stream) {
if (object.empty() || !stream) {
return;
}
object.print(*stream);
}
TEST_F(AnyTests, PrintToEmpty) {
Any p;
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::string{});
}
TEST_F(AnyTests, PrintToIntAny) {
int value = -5;
Any p = value;
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::to_string(value));
}
TEST_F(AnyTests, ReadToIntAny) {
int value = -5;
std::stringstream strm;
strm << value;
Any p = int{};
OV_ASSERT_NO_THROW(p.read(strm));
ASSERT_FALSE(strm.fail());
ASSERT_EQ(value, p.as<int>());
}
TEST_F(AnyTests, PrintToUIntAny) {
unsigned int value = 5;
Any p = value;
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::to_string(value));
}
TEST_F(AnyTests, PrintToSize_tAny) {
std::size_t value = 5;
Any p = value;
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::to_string(value));
}
TEST_F(AnyTests, PrintToFloatAny) {
Any p = 5.5f;
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::string{"5.5"});
}
TEST_F(AnyTests, PrintToStringAny) {
std::string value = "some text";
Any p = value;
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), value);
}
TEST_F(AnyTests, PrintToVectorOfInts) {
Any p = std::vector<int>{-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::string{"-5 -4 -3 -2 -1 0 1 2 3 4 5"});
}
TEST_F(AnyTests, PrintToVectorOfUInts) {
Any p = std::vector<unsigned int>{0, 1, 2, 3, 4, 5};
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::string{"0 1 2 3 4 5"});
}
TEST_F(AnyTests, PrintToVectorOfFloats) {
auto ref_vec = std::vector<float>{0.0f, 1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
{
Any p = std::vector<float>{0.0f, 1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
ASSERT_EQ(p.as<std::string>(), std::string{"0 1.1 2.2 3.3 4.4 5.5"});
}
{
Any p = "0 1.1 2.2 3.3 4.4 5.5";
ASSERT_EQ((p.as<std::vector<float>>()), ref_vec);
}
}
TEST_F(AnyTests, PrintToVectorOfStrings) {
Any p = std::vector<std::string>{"zero", "one", "two", "three", "four", "five"};
std::stringstream stream;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::string{"zero one two three four five"});
}
TEST_F(AnyTests, PrintToMapOfAnys) {
std::map<std::string, Any> refMap;
refMap["testParamInt"] = 4;
refMap["testParamString"] = "test";
std::stringstream stream;
{
Any p = refMap;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(stream.str(), std::string{"{testParamInt:4,testParamString:test}"});
}
}
TEST_F(AnyTests, PrintToMapOfMapsOfAnys) {
std::map<std::string, Any> refMap1;
refMap1["testParamInt"] = 4;
refMap1["testParamString"] = "test";
std::map<std::string, Any> refMap2;
refMap2["testParamInt"] = 5;
refMap2["testParamString"] = "test2";
std::map<std::string, Any> refMap;
refMap["refMap1"] = refMap1;
refMap["refMap2"] = refMap2;
std::stringstream stream;
{
Any p = refMap;
OV_ASSERT_NO_THROW(p.print(stream));
ASSERT_EQ(
stream.str(),
std::string{
"{refMap1:{testParamInt:4,testParamString:test},refMap2:{testParamInt:5,testParamString:test2}}"});
}
}
TEST_F(AnyTests, accessUsingBaseReference) {
ASSERT_EQ(0, DestructorTest::constructorCount);
ASSERT_EQ(0, DestructorTest::destructorCount);
{
using Base = DestructorTest;
auto p = Any::make<Derived>();
ASSERT_TRUE(p.is<Derived>());
ASSERT_TRUE(p.is<Base>());
OV_ASSERT_NO_THROW(p.as<Derived>());
OV_ASSERT_NO_THROW(p.as<Base>());
ASSERT_EQ(typeid(Derived), p.as<Base>().type_info());
}
ASSERT_EQ(1, DestructorTest::constructorCount);
ASSERT_EQ(1, DestructorTest::destructorCount);
}
struct WrongBase {
// No Base member type defined
// Should be: using Base = std::tuple<WrongBase>;
};
struct WrongDerived : public WrongBase {
// No Base member type defined
// Should be: using Base = std::tuple<WrongBase>;
};
TEST_F(AnyTests, accessUsingWrongBaseReference) {
Any p = WrongDerived{};
ASSERT_TRUE(p.is<WrongDerived>());
ASSERT_FALSE(p.is<WrongBase>());
OV_ASSERT_NO_THROW(p.as<WrongDerived>());
ASSERT_THROW(p.as<WrongBase>(), ov::Exception);
}
TEST_F(AnyTests, ToString) {
Any p = 42;
ASSERT_TRUE(p.is<int>());
ASSERT_EQ("42", p.as<std::string>());
}
TEST_F(AnyTests, FromString) {
Any p = "42";
ASSERT_TRUE(p.is<std::string>());
ASSERT_EQ(42, p.as<int>());
}
TEST_F(AnyTests, BoolFromString) {
{
Any p = "YES";
ASSERT_TRUE(p.is<std::string>());
ASSERT_EQ(true, p.as<bool>());
}
{
Any p = "NO";
ASSERT_TRUE(p.is<std::string>());
ASSERT_EQ(false, p.as<bool>());
}
{
Any p = "Some";
ASSERT_TRUE(p.is<std::string>());
ASSERT_THROW(p.as<bool>(), ov::Exception);
}
}
TEST_F(AnyTests, NotIntFromStringThrow) {
Any p = "not42";
ASSERT_TRUE(p.is<std::string>());
ASSERT_THROW(p.as<int>(), ov::Exception);
}
TEST_F(AnyTests, AddressofNoThrow) {
Any p;
ASSERT_EQ(nullptr, p.addressof());
p = 42;
ASSERT_NE(nullptr, p.addressof());
}
TEST_F(AnyTests, EmptyStringAsAny) {
Any p = "";
std::vector<float> ref_f;
std::vector<int> ref_i;
ASSERT_TRUE(p.is<std::string>());
ASSERT_EQ(p.as<int>(), 0);
ASSERT_EQ(p.as<std::vector<float>>(), ref_f);
ASSERT_EQ(p.as<std::vector<int>>(), ref_i);
}