-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphic_type_mismatch.cpp
More file actions
148 lines (124 loc) · 4.54 KB
/
Copy pathpolymorphic_type_mismatch.cpp
File metadata and controls
148 lines (124 loc) · 4.54 KB
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
//
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "common.hpp"
#include "common/my_intrusive_ptr_polymorphic.hpp"
#include "polymorphic_test_classes.hpp"
#include <iostream>
template <class IArchive, class OArchive>
inline void test_polymorphic_shared_ptr_type_mismatch() {
std::mt19937 rng(0x5EED);
Initializer init{rng};
std::shared_ptr<RootSimpleA> o_ptr0 = std::make_shared<LeafNonFinalA_SimpleA>(init);
std::shared_ptr<RootSimpleA> o_ptr1 = o_ptr0;
std::shared_ptr<RootSimpleA> i_ptr0;
std::shared_ptr<RootSimpleB> i_ptr1; //< This will not be the same type as the data was saved with
std::ostringstream os; {
OArchive oar(os);
oar(o_ptr0);
oar(o_ptr1);
} {
std::istringstream is(os.str());
IArchive iar(is);
iar(i_ptr0);
CHECK_THROWS_AS(iar(i_ptr1), vide::Exception);
}
}
template <class IArchive, class OArchive>
inline void test_polymorphic_shared_ptr_type_mismatch_base() {
std::mt19937 rng(0x5EED);
Initializer init{rng};
auto o_ptr0 = std::make_shared<RootSimpleA>(init);
auto o_ptr1 = o_ptr0;
std::shared_ptr<RootSimpleA> i_ptr0;
std::shared_ptr<RootSimpleB> i_ptr1; //< This will not be the same type as the data was saved with
std::ostringstream os; {
OArchive oar(os);
oar(o_ptr0);
oar(o_ptr1);
} {
std::istringstream is(os.str());
IArchive iar(is);
iar(i_ptr0);
CHECK_THROWS_AS(iar(i_ptr1), vide::Exception);
}
}
template <class IArchive, class OArchive>
inline void test_polymorphic_and_non_polymorphic_mismatch() {
std::mt19937 rng(0x5EED);
Initializer init{rng};
std::shared_ptr<RootSimpleA> o_ptr0 = std::static_pointer_cast<RootSimpleA>(std::make_shared<LeafNonFinalA_SimpleA>(init));
std::shared_ptr<RootSimpleA> o_ptr1 = o_ptr0;
std::shared_ptr<RootSimpleA> i_ptr0;
std::shared_ptr<int> i_ptr1; //< This will not be the same type as the data was saved with
std::ostringstream os; {
OArchive oar(os);
oar(o_ptr0);
oar(o_ptr1);
} {
std::istringstream is(os.str());
IArchive iar(is);
iar(i_ptr0);
CHECK_THROWS_AS(iar(i_ptr1), vide::Exception);
}
}
template <class IArchive, class OArchive>
inline void test_polymorphic_and_non_polymorphic_mismatch_base() {
std::mt19937 rng(0x5EED);
Initializer init{rng};
std::shared_ptr<RootSimpleA> o_ptr0 = std::make_shared<RootSimpleA>(init);
std::shared_ptr<RootSimpleA> o_ptr1 = o_ptr0;
std::shared_ptr<RootSimpleA> i_ptr0;
std::shared_ptr<int> i_ptr1; //< This will not be the same type as the data was saved with
std::ostringstream os; {
OArchive oar(os);
oar(o_ptr0);
oar(o_ptr1);
} {
std::istringstream is(os.str());
IArchive iar(is);
iar(i_ptr0);
CHECK_THROWS_AS(iar(i_ptr1), vide::Exception);
}
}
template <class IArchive, class OArchive>
inline void test_non_polymorphic_and_polymorphic_mismatch() {
std::shared_ptr<int> o_ptr0 = std::make_shared<int>(32);
std::shared_ptr<int> o_ptr1 = o_ptr0;
std::shared_ptr<int> i_ptr0;
std::shared_ptr<RootSimpleA> i_ptr1; //< This will not be the same type as the data was saved with
std::ostringstream os; {
OArchive oar(os);
oar(o_ptr0);
oar(o_ptr1);
} {
std::istringstream is(os.str());
IArchive iar(is);
iar(i_ptr0);
CHECK_THROWS_AS(iar(i_ptr1), vide::Exception);
}
}
template <class IArchive, class OArchive>
inline void test_polymorphic_pointer_type_mismatch() {
std::shared_ptr<TestRefCountedPolymorphic> o_ptr0 = std::make_shared<TestRefCountedPolymorphic>(32);
std::shared_ptr<TestRefCountedPolymorphic> o_ptr1 = o_ptr0;
std::shared_ptr<TestRefCountedPolymorphic> i_ptr0;
my::intrusive_ptr<TestRefCountedPolymorphic> i_ptr1; //< This will not be the same type as the data was saved with
std::ostringstream os; {
OArchive oar(os);
oar(o_ptr0);
oar(o_ptr1);
} {
std::istringstream is(os.str());
IArchive iar(is);
iar(i_ptr0);
CHECK_THROWS_AS(iar(i_ptr1), vide::Exception);
}
}
TEST_SUITE_BEGIN("polymorphic");
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("polymorphic_shared_ptr_type_mismatch", test_polymorphic_shared_ptr_type_mismatch)
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("polymorphic_shared_ptr_type_mismatch_base", test_polymorphic_shared_ptr_type_mismatch_base)
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("polymorphic_and_non_polymorphic_mismatch", test_polymorphic_and_non_polymorphic_mismatch)
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("polymorphic_and_non_polymorphic_mismatch_base", test_polymorphic_and_non_polymorphic_mismatch_base)
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("non_polymorphic_and_polymorphic_mismatch", test_non_polymorphic_and_polymorphic_mismatch)
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("polymorphic_pointer_type_mismatch", test_polymorphic_pointer_type_mismatch)
TEST_SUITE_END();