forked from aldebaran/libqi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_types.cpp
More file actions
471 lines (409 loc) · 12.4 KB
/
test_types.cpp
File metadata and controls
471 lines (409 loc) · 12.4 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
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
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <gtest/gtest.h>
#include <qi/anyobject.hpp>
#include <qi/session.hpp>
#include <qi/jsoncodec.hpp>
#include <qipython/pysession.hpp>
#include <qipython/common.hpp>
#include <qipython/pyguard.hpp>
#include <boost/thread.hpp>
#include "common.hpp"
namespace py = pybind11;
struct AnyValueFrom
{
template<typename T>
qi::AnyValue operator()(T&& t)
{
return qi::AnyValue::from(std::forward<T>(t));
}
};
struct PybindObjectCast
{
qi::AnyValue operator()(py::object obj)
{
qi::py::GILAcquire lock;
return obj.cast<qi::AnyValue>();
}
};
template<typename Convert>
struct ToAnyValueConversionTest : qi::py::GILAcquire, testing::Test
{
template<typename T>
qi::AnyValue toAnyValue(T&& t)
{
return Convert()(std::forward<T>(t));
}
};
using ConvertTypes = testing::Types<AnyValueFrom, PybindObjectCast>;
TYPED_TEST_SUITE(ToAnyValueConversionTest, ConvertTypes);
TYPED_TEST(ToAnyValueConversionTest, PyInt)
{
auto v = this->toAnyValue(py::int_(42));
EXPECT_EQ(qi::TypeKind_Int, v.kind());
const auto itf = static_cast<qi::IntTypeInterface*>(v.type());
EXPECT_EQ(sizeof(long long), itf->size());
EXPECT_TRUE(itf->isSigned());
EXPECT_EQ(42, v.toInt());
v.setInt(12);
EXPECT_EQ(12, v.toInt());
EXPECT_TRUE(v.template to<py::int_>().equal(py::int_(12)));
}
TYPED_TEST(ToAnyValueConversionTest, PyFloat)
{
auto v = this->toAnyValue(py::float_(3.14));
EXPECT_EQ(qi::TypeKind_Float, v.kind());
EXPECT_DOUBLE_EQ(3.14, v.toDouble());
v.setDouble(329.329);
EXPECT_DOUBLE_EQ(329.329, v.toDouble());
auto isclose = py::module::import("math").attr("isclose");
EXPECT_TRUE(isclose(py::float_(329.329), v.template to<py::float_>()).template cast<bool>());
}
TYPED_TEST(ToAnyValueConversionTest, PyBool)
{
auto v = this->toAnyValue(py::bool_(true));
EXPECT_EQ(qi::TypeKind_Int, v.kind());
const auto itf = static_cast<qi::IntTypeInterface*>(v.type());
EXPECT_EQ(0u, itf->size());
EXPECT_FALSE(itf->isSigned());
EXPECT_TRUE(v.template to<bool>());
v.set(false);
EXPECT_FALSE(v.template to<bool>());
EXPECT_EQ(py::bool_(false), v.template to<py::bool_>());
}
TYPED_TEST(ToAnyValueConversionTest, PyStr)
{
auto v = this->toAnyValue(py::str("cookies"));
EXPECT_EQ(qi::TypeKind_String, v.kind());
EXPECT_EQ("cookies", v.toString());
v.setString("muffins");
EXPECT_EQ("muffins", v.toString());
EXPECT_TRUE(v.template to<py::str>().equal(py::str("muffins")));
}
TYPED_TEST(ToAnyValueConversionTest, PyBytes)
{
auto v = this->toAnyValue(py::bytes("cupcakes"));
EXPECT_EQ(qi::TypeKind_String, v.kind());
EXPECT_EQ("cupcakes", v.toString());
v.setString("donuts");
EXPECT_EQ("donuts", v.toString());
EXPECT_TRUE(v.template to<py::bytes>().equal(py::bytes("donuts")));
}
struct ToAnyValueObjectConversionTest : qi::py::GILAcquire, testing::Test {};
TEST_F(ToAnyValueObjectConversionTest, AnyValueFromReturnsDynamic)
{
auto v = qi::AnyValue::from(py::object(py::int_(42)));
EXPECT_EQ(qi::TypeKind_Dynamic, v.kind());
EXPECT_TRUE(v.template to<py::object>().equal(py::int_(42)));
}
// No test with `PybindObjectCast` conversion with `PyObject` as it's equivalent
// to `PybindObjectCast` conversion with the underlying object which should
// already be tested.
struct ToAnyValueListConversionTest : qi::py::GILAcquire, testing::Test
{
static std::vector<int> toVec(qi::AnyReference listRef)
{
const auto vcVec = listRef.asListValuePtr();
std::vector<int> res;
std::transform(vcVec.begin(), vcVec.end(), std::back_inserter(res),
[](const qi::AnyReference& v) { return v.to<int>(); });
return res;
}
const std::vector<int> values = {1, 2, 4, 8, 16};
const py::list list = py::cast(values);
};
TEST_F(ToAnyValueListConversionTest, AnyValueFromReturnsDynamic)
{
auto v = qi::AnyValue::from(list);
EXPECT_EQ(qi::TypeKind_Dynamic, v.kind());
EXPECT_TRUE(v.template to<py::list>().equal(list));
EXPECT_EQ(values, toVec(v.content()));
}
TEST_F(ToAnyValueListConversionTest, PybindObjectCastReturnsList)
{
auto v = list.cast<qi::AnyValue>();
EXPECT_EQ(qi::TypeKind_List, v.kind());
EXPECT_TRUE(v.template to<py::list>().equal(list));
EXPECT_EQ(values, toVec(v.asReference()));
}
using DictTypes = testing::Types<py::dict, py::kwargs>;
template<typename Dict>
struct ToAnyValueDictConversionTest : qi::py::GILAcquire, testing::Test
{
static std::map<std::string, int> toMap(qi::AnyReference map)
{
const auto vcMap = map.asMapValuePtr();
std::map<std::string, int> res;
std::transform(vcMap.begin(), vcMap.end(), std::inserter(res, res.begin()),
[](const std::pair<qi::AnyReference, qi::AnyReference>& v) {
return std::make_pair(v.first.to<std::string>(), v.second.to<int>());
});
return res;
}
const std::map<std::string, int> values = {{ "two", 2 },
{ "four", 4 },
{ "eight", 8 },
{ "sixteen", 16 }};
const Dict dict = py::cast(values);
};
TYPED_TEST_SUITE(ToAnyValueDictConversionTest, DictTypes);
TYPED_TEST(ToAnyValueDictConversionTest, AnyValueFromReturnsDynamic)
{
using Dict = TypeParam;
auto v = qi::AnyValue::from(this->dict);
ASSERT_EQ(qi::TypeKind_Dynamic, v.kind());
EXPECT_TRUE(v.template to<Dict>().equal(this->dict));
EXPECT_EQ(this->values, this->toMap(v.content()));
}
TYPED_TEST(ToAnyValueDictConversionTest, PybindObjectCastReturnsMap)
{
using Dict = TypeParam;
auto v = this->dict.template cast<qi::AnyValue>();
ASSERT_EQ(qi::TypeKind_Map, v.kind());
EXPECT_TRUE(v.template to<Dict>().equal(this->dict));
EXPECT_EQ(this->values, this->toMap(v.asReference()));
}
using TupleTypes = testing::Types<py::tuple, py::args>;
template <typename Tuple>
struct ToAnyValueTupleConversionTest : qi::py::GILAcquire, testing::Test
{
static std::tuple<int, std::string, std::int8_t> toTuple(qi::AnyReference tuple)
{
const auto vcMap = tuple.asTupleValuePtr();
const auto res =
std::make_tuple(vcMap[0].to<int>(), vcMap[1].toString(),
vcMap[2].to<std::int8_t>());
return res;
}
const std::tuple<int, std::string, std::int8_t> values = std::make_tuple(21, "cookies", 15);
const Tuple tuple = py::cast(values);
};
TYPED_TEST_SUITE(ToAnyValueTupleConversionTest, TupleTypes);
TYPED_TEST(ToAnyValueTupleConversionTest, AnyValueReturnsDynamic)
{
using Tuple = TypeParam;
auto v = qi::AnyValue::from(this->tuple);
ASSERT_EQ(qi::TypeKind_Dynamic, v.kind());
EXPECT_TRUE(v.template to<Tuple>().equal(this->tuple));
EXPECT_EQ(this->values, this->toTuple(v.content()));
}
TYPED_TEST(ToAnyValueTupleConversionTest, PybindObjectCastReturnsTuple)
{
using Tuple = TypeParam;
auto v = this->tuple.template cast<qi::AnyValue>();
ASSERT_EQ(qi::TypeKind_Tuple, v.kind());
EXPECT_TRUE(v.template to<Tuple>().equal(this->tuple));
EXPECT_EQ(this->values, this->toTuple(v.asReference()));
}
struct TypePassing : qi::py::test::Execute,
testing::Test
{
protected:
virtual void SetUp()
{
session = qi::makeSession();
session->listenStandalone("tcp://127.0.0.1:0");
qi::py::GILAcquire lock;
locals()["sd"] = qi::py::makeSession(session);
}
virtual void TearDown()
{
session->close();
}
void registerService()
{
exec(
"m = TestService()\n"
"sd.registerService('TestService', m)\n"
);
}
qi::AnyObject getService()
{
return session->service("TestService").value();
}
qi::SessionPtr session;
};
TEST_F(TypePassing, Int)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return 42\n"
);
registerService();
EXPECT_EQ(42, getService().call<int>("func"));
}
TEST_F(TypePassing, Bool)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return True\n"
);
registerService();
EXPECT_EQ(true, getService().call<bool>("func"));
}
TEST_F(TypePassing, Float)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return 2.5\n"
);
registerService();
EXPECT_FLOAT_EQ(2.5, getService().call<float>("func"));
}
TEST_F(TypePassing, String)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return 'can i help you?'\n"
);
registerService();
EXPECT_EQ("can i help you?", getService().call<std::string>("func"));
}
TEST_F(TypePassing, ByteArray)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return bytearray('can i help you?', encoding='ascii')\n"
);
registerService();
EXPECT_EQ("can i help you?", getService().call<std::string>("func"));
}
TEST_F(TypePassing, Bytes)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return b'can i help you?'\n"
);
registerService();
EXPECT_EQ("can i help you?", getService().call<std::string>("func"));
}
TEST_F(TypePassing, List)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return [1, 2, 3]\n"
);
registerService();
const std::vector<int> expected { 1, 2, 3 };
EXPECT_EQ(expected, getService().call<std::vector<int> >("func"));
}
TEST_F(TypePassing, Dict)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return {'one' : 1, 'two' : 2, 'three' : 3}\n"
);
registerService();
const std::map<std::string, int> expected {
{"one", 1},
{"two", 2},
{"three", 3},
};
EXPECT_EQ(expected, (getService().call<std::map<std::string, int> >("func")));
}
TEST_F(TypePassing, Recursive)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return {'one' : 1, 'two' : [1, 2], 'three' : {42 : 'answer'}}\n"
);
registerService();
const auto v = getService().call<qi::AnyValue>("func");
const auto map = v.to<std::map<std::string, qi::AnyValue> >();
ASSERT_EQ(3u, map.size());
EXPECT_EQ(1, map.at("one").toInt());
{
const auto two = map.at("two").to<std::vector<int>>();
const std::vector<int> expected{ 1, 2 };
EXPECT_EQ(expected, two);
}
{
const auto three = map.at("three").to<std::map<int, std::string>>();
const std::map<int, std::string> expected{ { 42, "answer" } };
EXPECT_EQ(expected, three);
}
}
TEST_F(TypePassing, ReverseList)
{
exec(
"class TestService:\n"
" def func(self, list):\n"
// Test the iterator interface.
" for value in list:\n"
" assert(isinstance(value, str))\n"
" assert(list == ['hello', 'world'])\n"
);
registerService();
const std::vector<const char*> list {"hello", "world"};
getService().call<void>("func", list);
}
TEST_F(TypePassing, ReverseDict)
{
exec(
"class TestService:\n"
" def func(self, dict):\n"
// Test the iterator interface.
" for key, value in dict.items():\n"
" assert(isinstance(key, str))\n"
" assert(isinstance(value, int))\n"
" assert(dict == {'one' : 1, 'two' : 2, 'three' : 3})\n"
);
registerService();
const std::map<std::string, int> dict {
{"one", 1},
{"two", 2},
{"three", 3},
};
getService().call<void>("func", dict);
}
TEST_F(TypePassing, LogLevel)
{
exec(
"class TestService:\n"
" def func(self):\n"
" return qi.LogLevel.Info\n"
);
registerService();
EXPECT_EQ(qi::LogLevel_Info, getService().call<int>("func"));
}
TEST_F(TypePassing, DictViews)
{
exec(
"class TestService:\n"
" def func(self, kind):\n"
" d = dict(one=1, two=2)\n"
" if kind == 0:\n"
" return d.keys()\n"
" if kind == 1:\n"
" return d.items()\n"
" return d.values()\n"
);
registerService();
{ // Keys
using Keys = std::set<std::string>;
const Keys expected{ "one", "two" };
EXPECT_EQ(expected, getService().call<Keys>("func", 0));
}
{ // Items
using Items = std::set<std::pair<std::string, int>>;
const Items expected{ { "one", 1 }, { "two", 2 } };
EXPECT_EQ(expected, getService().call<Items>("func", 1));
}
{ // Values
using Values = std::vector<int>;
const Values expected{ 1, 2 };
auto values = getService().call<Values>("func", 2);
std::sort(values.begin(), values.end());
EXPECT_EQ(expected, values);
}
}