Mercurial > hg > sv-dependency-builds
comparison win32-mingw/include/capnp/test-util.h @ 50:37d53a7e8262
Headers for KJ/Capnp Win32
author | Chris Cannam |
---|---|
date | Wed, 26 Oct 2016 13:18:45 +0100 |
parents | |
children | eccd51b72864 |
comparison
equal
deleted
inserted
replaced
49:3ab5a40c4e3b | 50:37d53a7e8262 |
---|---|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors | |
2 // Licensed under the MIT License: | |
3 // | |
4 // Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 // of this software and associated documentation files (the "Software"), to deal | |
6 // in the Software without restriction, including without limitation the rights | |
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 // copies of the Software, and to permit persons to whom the Software is | |
9 // furnished to do so, subject to the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included in | |
12 // all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 // THE SOFTWARE. | |
21 | |
22 #ifndef CAPNP_TEST_UTIL_H_ | |
23 #define CAPNP_TEST_UTIL_H_ | |
24 | |
25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) | |
26 #pragma GCC system_header | |
27 #endif | |
28 | |
29 #include <capnp/test.capnp.h> | |
30 #include <iostream> | |
31 #include "blob.h" | |
32 #include <kj/compat/gtest.h> | |
33 | |
34 #if !CAPNP_LITE | |
35 #include "dynamic.h" | |
36 #endif // !CAPNP_LITE | |
37 | |
38 #if KJ_NO_EXCEPTIONS | |
39 #undef EXPECT_ANY_THROW | |
40 #define EXPECT_ANY_THROW(code) EXPECT_DEATH(code, ".") | |
41 #endif | |
42 | |
43 #define EXPECT_NONFATAL_FAILURE(code) \ | |
44 EXPECT_TRUE(kj::runCatchingExceptions([&]() { code; }) != nullptr); | |
45 | |
46 #ifdef KJ_DEBUG | |
47 #define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW | |
48 #else | |
49 #define EXPECT_DEBUG_ANY_THROW(EXP) | |
50 #endif | |
51 | |
52 // TODO(cleanup): Auto-generate stringification functions for union discriminants. | |
53 namespace capnproto_test { | |
54 namespace capnp { | |
55 namespace test { | |
56 inline kj::String KJ_STRINGIFY(TestUnion::Union0::Which which) { | |
57 return kj::str(static_cast<uint16_t>(which)); | |
58 } | |
59 inline kj::String KJ_STRINGIFY(TestUnion::Union1::Which which) { | |
60 return kj::str(static_cast<uint16_t>(which)); | |
61 } | |
62 inline kj::String KJ_STRINGIFY(TestUnion::Union2::Which which) { | |
63 return kj::str(static_cast<uint16_t>(which)); | |
64 } | |
65 inline kj::String KJ_STRINGIFY(TestUnion::Union3::Which which) { | |
66 return kj::str(static_cast<uint16_t>(which)); | |
67 } | |
68 inline kj::String KJ_STRINGIFY(TestUnnamedUnion::Which which) { | |
69 return kj::str(static_cast<uint16_t>(which)); | |
70 } | |
71 inline kj::String KJ_STRINGIFY(TestGroups::Groups::Which which) { | |
72 return kj::str(static_cast<uint16_t>(which)); | |
73 } | |
74 inline kj::String KJ_STRINGIFY(TestInterleavedGroups::Group1::Which which) { | |
75 return kj::str(static_cast<uint16_t>(which)); | |
76 } | |
77 } // namespace test | |
78 } // namespace capnp | |
79 } // namespace capnproto_test | |
80 | |
81 namespace capnp { | |
82 namespace _ { // private | |
83 | |
84 inline Data::Reader data(const char* str) { | |
85 return Data::Reader(reinterpret_cast<const byte*>(str), strlen(str)); | |
86 } | |
87 | |
88 namespace test = capnproto_test::capnp::test; | |
89 | |
90 // We don't use "using namespace" to pull these in because then things would still compile | |
91 // correctly if they were generated in the global namespace. | |
92 using ::capnproto_test::capnp::test::TestAllTypes; | |
93 using ::capnproto_test::capnp::test::TestDefaults; | |
94 using ::capnproto_test::capnp::test::TestEnum; | |
95 using ::capnproto_test::capnp::test::TestUnion; | |
96 using ::capnproto_test::capnp::test::TestUnionDefaults; | |
97 using ::capnproto_test::capnp::test::TestNestedTypes; | |
98 using ::capnproto_test::capnp::test::TestUsing; | |
99 using ::capnproto_test::capnp::test::TestListDefaults; | |
100 | |
101 void initTestMessage(TestAllTypes::Builder builder); | |
102 void initTestMessage(TestDefaults::Builder builder); | |
103 void initTestMessage(TestListDefaults::Builder builder); | |
104 | |
105 void checkTestMessage(TestAllTypes::Builder builder); | |
106 void checkTestMessage(TestDefaults::Builder builder); | |
107 void checkTestMessage(TestListDefaults::Builder builder); | |
108 | |
109 void checkTestMessage(TestAllTypes::Reader reader); | |
110 void checkTestMessage(TestDefaults::Reader reader); | |
111 void checkTestMessage(TestListDefaults::Reader reader); | |
112 | |
113 void checkTestMessageAllZero(TestAllTypes::Builder builder); | |
114 void checkTestMessageAllZero(TestAllTypes::Reader reader); | |
115 | |
116 #if !CAPNP_LITE | |
117 void initDynamicTestMessage(DynamicStruct::Builder builder); | |
118 void initDynamicTestLists(DynamicStruct::Builder builder); | |
119 void checkDynamicTestMessage(DynamicStruct::Builder builder); | |
120 void checkDynamicTestLists(DynamicStruct::Builder builder); | |
121 void checkDynamicTestMessage(DynamicStruct::Reader reader); | |
122 void checkDynamicTestLists(DynamicStruct::Reader reader); | |
123 void checkDynamicTestMessageAllZero(DynamicStruct::Builder builder); | |
124 void checkDynamicTestMessageAllZero(DynamicStruct::Reader reader); | |
125 #endif // !CAPNP_LITE | |
126 | |
127 template <typename T> | |
128 inline void checkElement(T a, T b) { | |
129 EXPECT_EQ(a, b); | |
130 } | |
131 | |
132 template <> | |
133 inline void checkElement<float>(float a, float b) { | |
134 EXPECT_FLOAT_EQ(a, b); | |
135 } | |
136 | |
137 template <> | |
138 inline void checkElement<double>(double a, double b) { | |
139 EXPECT_DOUBLE_EQ(a, b); | |
140 } | |
141 | |
142 template <typename T, typename L = typename T::Reads> | |
143 void checkList(T reader, std::initializer_list<decltype(reader[0])> expected) { | |
144 ASSERT_EQ(expected.size(), reader.size()); | |
145 for (uint i = 0; i < expected.size(); i++) { | |
146 checkElement<decltype(reader[0])>(expected.begin()[i], reader[i]); | |
147 } | |
148 } | |
149 | |
150 template <typename T, typename L = typename T::Builds, bool = false> | |
151 void checkList(T reader, std::initializer_list<decltype(typename L::Reader()[0])> expected) { | |
152 ASSERT_EQ(expected.size(), reader.size()); | |
153 for (uint i = 0; i < expected.size(); i++) { | |
154 checkElement<decltype(typename L::Reader()[0])>(expected.begin()[i], reader[i]); | |
155 } | |
156 } | |
157 | |
158 inline void checkList(List<test::TestOldVersion>::Reader reader, | |
159 std::initializer_list<int64_t> expectedData, | |
160 std::initializer_list<Text::Reader> expectedPointers) { | |
161 ASSERT_EQ(expectedData.size(), reader.size()); | |
162 for (uint i = 0; i < expectedData.size(); i++) { | |
163 EXPECT_EQ(expectedData.begin()[i], reader[i].getOld1()); | |
164 EXPECT_EQ(expectedPointers.begin()[i], reader[i].getOld2()); | |
165 } | |
166 } | |
167 | |
168 // Hack because as<>() is a template-parameter-dependent lookup everywhere below... | |
169 #define as template as | |
170 | |
171 template <typename T> void expectPrimitiveEq(T a, T b) { EXPECT_EQ(a, b); } | |
172 inline void expectPrimitiveEq(float a, float b) { EXPECT_FLOAT_EQ(a, b); } | |
173 inline void expectPrimitiveEq(double a, double b) { EXPECT_DOUBLE_EQ(a, b); } | |
174 inline void expectPrimitiveEq(Text::Reader a, Text::Builder b) { EXPECT_EQ(a, b); } | |
175 inline void expectPrimitiveEq(Data::Reader a, Data::Builder b) { EXPECT_EQ(a, b); } | |
176 | |
177 #if !CAPNP_LITE | |
178 template <typename Element, typename T> | |
179 void checkList(T reader, std::initializer_list<ReaderFor<Element>> expected) { | |
180 auto list = reader.as<DynamicList>(); | |
181 ASSERT_EQ(expected.size(), list.size()); | |
182 for (uint i = 0; i < expected.size(); i++) { | |
183 expectPrimitiveEq(expected.begin()[i], list[i].as<Element>()); | |
184 } | |
185 | |
186 auto typed = reader.as<List<Element>>(); | |
187 ASSERT_EQ(expected.size(), typed.size()); | |
188 for (uint i = 0; i < expected.size(); i++) { | |
189 expectPrimitiveEq(expected.begin()[i], typed[i]); | |
190 } | |
191 } | |
192 #endif // !CAPNP_LITE | |
193 | |
194 #undef as | |
195 | |
196 // ======================================================================================= | |
197 // Interface implementations. | |
198 | |
199 #if !CAPNP_LITE | |
200 | |
201 class TestInterfaceImpl final: public test::TestInterface::Server { | |
202 public: | |
203 TestInterfaceImpl(int& callCount); | |
204 | |
205 kj::Promise<void> foo(FooContext context) override; | |
206 | |
207 kj::Promise<void> baz(BazContext context) override; | |
208 | |
209 private: | |
210 int& callCount; | |
211 }; | |
212 | |
213 class TestExtendsImpl final: public test::TestExtends2::Server { | |
214 public: | |
215 TestExtendsImpl(int& callCount); | |
216 | |
217 kj::Promise<void> foo(FooContext context) override; | |
218 | |
219 kj::Promise<void> grault(GraultContext context) override; | |
220 | |
221 private: | |
222 int& callCount; | |
223 }; | |
224 | |
225 class TestPipelineImpl final: public test::TestPipeline::Server { | |
226 public: | |
227 TestPipelineImpl(int& callCount); | |
228 | |
229 kj::Promise<void> getCap(GetCapContext context) override; | |
230 | |
231 private: | |
232 int& callCount; | |
233 }; | |
234 | |
235 class TestCallOrderImpl final: public test::TestCallOrder::Server { | |
236 public: | |
237 kj::Promise<void> getCallSequence(GetCallSequenceContext context) override; | |
238 | |
239 private: | |
240 uint count = 0; | |
241 }; | |
242 | |
243 class TestTailCallerImpl final: public test::TestTailCaller::Server { | |
244 public: | |
245 TestTailCallerImpl(int& callCount); | |
246 | |
247 kj::Promise<void> foo(FooContext context) override; | |
248 | |
249 private: | |
250 int& callCount; | |
251 }; | |
252 | |
253 class TestTailCalleeImpl final: public test::TestTailCallee::Server { | |
254 public: | |
255 TestTailCalleeImpl(int& callCount); | |
256 | |
257 kj::Promise<void> foo(FooContext context) override; | |
258 | |
259 private: | |
260 int& callCount; | |
261 }; | |
262 | |
263 class TestMoreStuffImpl final: public test::TestMoreStuff::Server { | |
264 public: | |
265 TestMoreStuffImpl(int& callCount, int& handleCount); | |
266 | |
267 kj::Promise<void> getCallSequence(GetCallSequenceContext context) override; | |
268 | |
269 kj::Promise<void> callFoo(CallFooContext context) override; | |
270 | |
271 kj::Promise<void> callFooWhenResolved(CallFooWhenResolvedContext context) override; | |
272 | |
273 kj::Promise<void> neverReturn(NeverReturnContext context) override; | |
274 | |
275 kj::Promise<void> hold(HoldContext context) override; | |
276 | |
277 kj::Promise<void> callHeld(CallHeldContext context) override; | |
278 | |
279 kj::Promise<void> getHeld(GetHeldContext context) override; | |
280 | |
281 kj::Promise<void> echo(EchoContext context) override; | |
282 | |
283 kj::Promise<void> expectCancel(ExpectCancelContext context) override; | |
284 | |
285 kj::Promise<void> getHandle(GetHandleContext context) override; | |
286 | |
287 kj::Promise<void> getNull(GetNullContext context) override; | |
288 | |
289 private: | |
290 int& callCount; | |
291 int& handleCount; | |
292 test::TestInterface::Client clientToHold = nullptr; | |
293 | |
294 kj::Promise<void> loop(uint depth, test::TestInterface::Client cap, ExpectCancelContext context); | |
295 }; | |
296 | |
297 class TestCapDestructor final: public test::TestInterface::Server { | |
298 // Implementation of TestInterface that notifies when it is destroyed. | |
299 | |
300 public: | |
301 TestCapDestructor(kj::Own<kj::PromiseFulfiller<void>>&& fulfiller) | |
302 : fulfiller(kj::mv(fulfiller)), impl(dummy) {} | |
303 | |
304 ~TestCapDestructor() { | |
305 fulfiller->fulfill(); | |
306 } | |
307 | |
308 kj::Promise<void> foo(FooContext context) { | |
309 return impl.foo(context); | |
310 } | |
311 | |
312 private: | |
313 kj::Own<kj::PromiseFulfiller<void>> fulfiller; | |
314 int dummy = 0; | |
315 TestInterfaceImpl impl; | |
316 }; | |
317 | |
318 #endif // !CAPNP_LITE | |
319 | |
320 } // namespace _ (private) | |
321 } // namespace capnp | |
322 | |
323 #endif // TEST_UTIL_H_ |