forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_test.cc
591 lines (497 loc) · 19.7 KB
/
value_test.cc
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
#include "drake/common/value.h"
#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include <gtest/gtest.h>
#include "drake/common/drake_copyable.h"
#include "drake/common/test_utilities/expect_no_throw.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/systems/framework/test_utilities/my_vector.h"
namespace drake {
namespace test {
// A type with no constructors.
struct BareStruct {
int data;
};
// A copyable type with no default constructor.
struct CopyableInt {
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(CopyableInt);
explicit CopyableInt(int i) : data{i} {}
CopyableInt(int c1, int c2) : data{c1 * c2} {}
int data;
};
// A clone-only type with no default constructor.
struct CloneableInt {
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(CloneableInt);
explicit CloneableInt(int i) : data{i} {}
std::unique_ptr<CloneableInt> Clone() const {
return std::make_unique<CloneableInt>(data);
}
const int data;
};
// A move-or-clone (not copy) type with a default constructor.
struct MoveOrCloneInt {
MoveOrCloneInt() {}
explicit MoveOrCloneInt(int i) : data{i} {}
MoveOrCloneInt(MoveOrCloneInt&& other) {
std::swap(data, other.data);
}
MoveOrCloneInt& operator=(MoveOrCloneInt&& other) {
std::swap(data, other.data);
return *this;
}
MoveOrCloneInt(const MoveOrCloneInt&) = delete;
void operator=(const MoveOrCloneInt&) = delete;
std::unique_ptr<MoveOrCloneInt> Clone() const {
return std::make_unique<MoveOrCloneInt>(data);
}
int data{};
};
// Helper for EXPECT_EQ to unwrap the data field.
template <typename T>
bool operator==(int i, const T& value) { return i == value.data; }
using systems::BasicVector;
using systems::MyVector2d;
// Boilerplate for tests that are identical across different types. Our
// TYPED_TESTs will run using all of the below types as the TypeParam.
template <typename TypeParam> class TypedValueTest : public ::testing::Test {};
typedef ::testing::Types<
int,
CopyableInt,
CloneableInt,
MoveOrCloneInt
> Implementations;
TYPED_TEST_SUITE(TypedValueTest, Implementations);
// Value<T>() should work if and only if T is default-constructible.
GTEST_TEST(ValueTest, DefaultConstructor) {
const AbstractValue& value_int = Value<int>();
EXPECT_EQ(0, value_int.get_value<int>());
const AbstractValue& value_bare_struct = Value<BareStruct>();
EXPECT_EQ(0, value_bare_struct.get_value<BareStruct>().data);
static_assert(!std::is_default_constructible_v<Value<CopyableInt>>,
"Value<CopyableInt>() should not work.");
static_assert(!std::is_default_constructible_v<Value<CloneableInt>>,
"Value<CloneableInt>() should not work.");
const AbstractValue& value_move_or_clone_int = Value<MoveOrCloneInt>();
EXPECT_EQ(0, value_move_or_clone_int.get_value<MoveOrCloneInt>().data);
}
// Value<T>(int) should work (possibly using forwarding).
TYPED_TEST(TypedValueTest, ForwardingConstructor) {
using T = TypeParam;
const AbstractValue& abstract_value = Value<T>(22);
EXPECT_EQ(22, abstract_value.get_value<T>());
}
// A two-argument constructor should work using forwarding. (The forwarding
// test case above is not quite enough, because the Value implementation treats
// the first argument and rest of the arguments separately.)
GTEST_TEST(ValueTest, ForwardingConstructorTwoArgs) {
using T = CopyableInt;
const AbstractValue& value = Value<T>(11, 2);
EXPECT_EQ(22, value.get_value<T>());
}
// Passing a single reference argument to the Value<T> constructor should use
// the `(const T&)` constructor, not the forwarding constructor.
TYPED_TEST(TypedValueTest, CopyConstructor) {
using T = TypeParam;
T param{0};
const T const_param{0};
const Value<T> xvalue(T{0}); // Called with `T&&`.
const Value<T> lvalue(param); // Called with `T&`.
const Value<T> crvalue(const_param); // Called with `const T&`.
}
// Ditto for BareStruct.
GTEST_TEST(ValueTest, BareCopyConstructor) {
using T = BareStruct;
T param{};
const T const_param{};
const Value<T> xvalue(T{}); // Called with `T&&`.
const Value<T> lvalue(param); // Called with `T&`.
const Value<T> crvalue(const_param); // Called with `const T&`.
}
// Passing a unique_ptr<T> to Value<T> should take over the value.
TYPED_TEST(TypedValueTest, UniquePtrConstructor) {
using T = TypeParam;
auto original = std::make_unique<T>(22);
const Value<T> value{std::move(original)};
EXPECT_EQ(original.get(), nullptr);
EXPECT_EQ(22, value.get_value());
}
TYPED_TEST(TypedValueTest, Make) {
using T = TypeParam;
// TODO(jwnimmer-tri) We should be able to forward this too, and lose the
// explicit construction of T{42}.
auto abstract_value = AbstractValue::Make<T>(T{42});
EXPECT_EQ(42, abstract_value->template get_value<T>());
}
GTEST_TEST(TypedValueTest, MakeDefault) {
EXPECT_EQ(0, AbstractValue::Make<int>()->get_value<int>());
EXPECT_EQ("", AbstractValue::Make<std::string>()->get_value<std::string>());
}
GTEST_TEST(ValueTest, NiceTypeName) {
auto double_value = AbstractValue::Make<double>(3.);
auto string_value = AbstractValue::Make<std::string>("hello");
auto base_value =
std::make_unique<Value<BasicVector<double>>>(MyVector2d::Make(1., 2.));
EXPECT_EQ(double_value->GetNiceTypeName(), "double");
EXPECT_EQ(string_value->GetNiceTypeName(), "std::string");
// Must return the name of the most-derived type.
EXPECT_EQ(base_value->GetNiceTypeName(),
"drake::systems::MyVector<double,2>");
}
GTEST_TEST(ValueTest, TypeInfo) {
auto double_value = AbstractValue::Make<double>(3.);
auto string_value = AbstractValue::Make<std::string>("hello");
auto base_value =
std::make_unique<Value<BasicVector<double>>>(MyVector2d::Make(1., 2.));
EXPECT_EQ(double_value->static_type_info(), typeid(double));
EXPECT_EQ(double_value->type_info(), typeid(double));
EXPECT_EQ(string_value->static_type_info(), typeid(std::string));
EXPECT_EQ(string_value->type_info(), typeid(std::string));
// The static type is BasicVector, but the runtime type is MyVector2d.
EXPECT_EQ(base_value->static_type_info(), typeid(BasicVector<double>));
EXPECT_EQ(base_value->type_info(), typeid(MyVector2d));
}
// Check that maybe_get_value() returns nullptr for wrong-type requests,
// and returns the correct value for right-type requests.
GTEST_TEST(ValueTest, MaybeGetValue) {
auto double_value = AbstractValue::Make<double>(3.);
auto string_value = AbstractValue::Make<std::string>("hello");
EXPECT_EQ(double_value->maybe_get_value<std::string>(), nullptr);
EXPECT_EQ(string_value->maybe_get_value<double>(), nullptr);
const double* const double_pointer =
double_value->maybe_get_value<double>();
const std::string* const string_pointer =
string_value->maybe_get_value<std::string>();
ASSERT_NE(double_pointer, nullptr);
ASSERT_NE(string_pointer, nullptr);
EXPECT_EQ(*double_pointer, 3.);
EXPECT_EQ(*string_pointer, "hello");
}
// Check that maybe_get_mutable_value() returns nullptr for wrong-type
// requests, and returns the correct value for right-type requests.
GTEST_TEST(ValueTest, MaybeGetMutableValue) {
auto double_value = AbstractValue::Make<double>(3.);
auto string_value = AbstractValue::Make<std::string>("hello");
EXPECT_EQ(double_value->maybe_get_mutable_value<std::string>(), nullptr);
EXPECT_EQ(string_value->maybe_get_mutable_value<double>(), nullptr);
double* const double_pointer =
double_value->maybe_get_mutable_value<double>();
std::string* const string_pointer =
string_value->maybe_get_mutable_value<std::string>();
ASSERT_NE(double_pointer, nullptr);
ASSERT_NE(string_pointer, nullptr);
EXPECT_EQ(*double_pointer, 3.);
EXPECT_EQ(*string_pointer, "hello");
*string_pointer = "goodbye";
EXPECT_EQ(string_value->get_value<std::string>(), "goodbye");
}
TYPED_TEST(TypedValueTest, Access) {
using T = TypeParam;
Value<T> value(3);
const AbstractValue& erased = value;
EXPECT_EQ(3, erased.get_value<T>());
ASSERT_NE(erased.maybe_get_value<T>(), nullptr);
EXPECT_EQ(3, *erased.maybe_get_value<T>());
}
TYPED_TEST(TypedValueTest, Clone) {
using T = TypeParam;
Value<T> value(43);
const AbstractValue& erased = value;
std::unique_ptr<AbstractValue> cloned = erased.Clone();
EXPECT_EQ(43, cloned->get_value<T>());
}
TYPED_TEST(TypedValueTest, Mutation) {
using T = TypeParam;
Value<T> value(3);
value.set_value(T{4});
AbstractValue& erased = value;
EXPECT_EQ(4, erased.get_value<T>());
erased.set_value<T>(T{5});
EXPECT_EQ(5, erased.get_value<T>());
erased.SetFrom(Value<T>(6));
EXPECT_EQ(6, erased.get_value<T>());
}
TYPED_TEST(TypedValueTest, BadCast) {
using T = TypeParam;
Value<double> value(4);
AbstractValue& erased = value;
EXPECT_THROW(erased.get_value<T>(), std::logic_error);
EXPECT_THROW(erased.get_mutable_value<T>(), std::logic_error);
EXPECT_THROW(erased.set_value<T>(T{3}), std::logic_error);
EXPECT_THROW(erased.SetFrom(Value<T>(2)), std::logic_error);
}
class PrintInterface {
public:
virtual ~PrintInterface() {}
virtual std::string print() const = 0;
protected:
// Allow our subclasses to make these public.
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(PrintInterface)
PrintInterface() = default;
};
// A trivial class that implements a trivial interface.
//
// N.B. Don't use this precise example in your own code! Normally we would
// mark this class `final` in order to avoid the slicing problem during copy,
// move, and assignment; however, the unit tests below are specifically
// checking for weird corner cases, so we can't mark it as such here.
class Point : public PrintInterface {
public:
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Point)
Point(int x, int y) : x_(x), y_(y) {}
virtual ~Point() {}
int x() const { return x_; }
int y() const { return y_; }
void set_x(int x) { x_ = x; }
void set_y(int y) { y_ = y; }
std::string print() const override {
std::ostringstream out;
out << x_ << "," << y_;
return out.str();
}
private:
int x_, y_;
};
// Tests that classes can be erased in an AbstractValue.
GTEST_TEST(ValueTest, ClassType) {
Point point(1, 2);
Value<Point> value(point);
AbstractValue& erased = value;
erased.get_mutable_value<Point>().set_x(-1);
EXPECT_EQ(-1, erased.get_value<Point>().x());
EXPECT_EQ(2, erased.get_value<Point>().y());
erased.get_mutable_value<Point>().set_y(-2);
EXPECT_EQ(-1, erased.get_value<Point>().x());
EXPECT_EQ(-2, erased.get_value<Point>().y());
}
class SubclassOfPoint : public Point {
public:
SubclassOfPoint() : Point(-1, -2) {}
};
// Tests that attempting to unerase an AbstractValue to a parent class of the
// original class throws std::logic_error.
GTEST_TEST(ValueTest, CannotUneraseToParentClass) {
SubclassOfPoint point;
Value<SubclassOfPoint> value(point);
AbstractValue& erased = value;
EXPECT_THROW(erased.get_mutable_value<Point>(), std::logic_error);
}
// A child class of Value<T> that requires T to satisfy PrintInterface, and
// also satisfies PrintInterface itself.
template <typename T>
class PrintableValue : public Value<T>, public PrintInterface {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(PrintableValue)
explicit PrintableValue(const T& v) : Value<T>(v) {}
std::unique_ptr<AbstractValue> Clone() const override {
return std::make_unique<PrintableValue<T>>(this->get_value());
}
std::string print() const override {
const PrintInterface& print_interface = Value<T>::get_value();
return print_interface.print();
}
};
// Tests that AbstractValues can be unerased to interfaces implemented by
// subclasses of Value<T>.
GTEST_TEST(ValueTest, SubclassOfValue) {
Point point(3, 4);
PrintableValue<Point> printable_value(point);
AbstractValue* erased = &printable_value;
PrintInterface* printable_erased = dynamic_cast<PrintInterface*>(erased);
ASSERT_NE(nullptr, printable_erased);
EXPECT_EQ("3,4", printable_erased->print());
}
// Tests that even after being cloned, PrintableValue can be unerased to
// PrintInterface.
GTEST_TEST(ValueTest, SubclassOfValueSurvivesClone) {
Point point(5, 6);
PrintableValue<Point> printable_value(point);
const AbstractValue& erased = printable_value;
std::unique_ptr<AbstractValue> cloned = erased.Clone();
PrintInterface* printable_erased =
dynamic_cast<PrintInterface*>(cloned.get());
ASSERT_NE(nullptr, printable_erased);
EXPECT_EQ("5,6", printable_erased->print());
}
// Tests an allowed type, and shows (by commented out examples) that pointers,
// arrays, const, volatile, and reference types should be forbidden.
GTEST_TEST(ValueTest, AllowedTypesMetaTest) {
using T = int;
Value<T>{};
// - cvref
// Value<const T>{}; // Triggers static assertion; fails without assertion.
// Value<volatile T>{}; // Trigger static assertion; works without assertion.
// Value<const T&>{}; // Triggers static assertion; fails without assertion.
// Value<T&&>{}; // Triggers static assertion; fails without assertion.
// - array / pointer
// Value<T*>{}; // Triggers static assertion; works without assertion.
// Value<T[2]>{}; // Triggers static assertion; fails without assertion.
}
// Check that TypeHash is extracting exactly the right strings from
// __PRETTY_FUNCTION__.
template <typename T>
void CheckHash(const std::string& name) {
internal::FNV1aHasher hasher;
hasher(name.data(), name.size());
EXPECT_EQ(internal::TypeHash<T>::value, size_t(hasher))
<< " for name\n"
<< " Which is: " << name << "\n"
<< " for __PRETTY_FUNCTION__\n"
<< " Which is: " << __PRETTY_FUNCTION__;
}
namespace {
struct AnonStruct {};
class AnonClass {};
enum class AnonEnum { kFoo, kBar };
// A class with a non-type template argument is not hashable.
template <AnonEnum K>
class UnadornedAnonEnumTemplate {};
// To enable hashing, the user can add a `using` statement like this.
template <AnonEnum K>
class NiceAnonEnumTemplate {
public:
using NonTypeTemplateParameter = std::integral_constant<AnonEnum, K>;
};
} // namespace
// Apple clang prior to version 13 needs a fixup for inline namespaces.
#if defined(__APPLE__) && defined(__clang__) && __clang_major__ < 13
constexpr bool kAppleInlineNamespace = true;
#else
constexpr bool kAppleInlineNamespace = false;
#endif
#ifdef __clang__
constexpr bool kClang = true;
#else
constexpr bool kClang = false;
#endif
#if __GNUC__ >= 9
constexpr bool kGcc9 = true;
#else
constexpr bool kGcc9 = false;
#endif
GTEST_TEST(TypeHashTest, WellKnownValues) {
// Simple primitives, structs, and classes.
CheckHash<int>("int");
CheckHash<double>("double");
CheckHash<Point>("drake::test::Point");
// Anonymous structs and classes, and an enum class.
CheckHash<AnonStruct>("drake::test::{anonymous}::AnonStruct");
CheckHash<AnonClass>("drake::test::{anonymous}::AnonClass");
CheckHash<AnonEnum>("drake::test::{anonymous}::AnonEnum");
// Templated containers without default template arguments.
const std::string stdcc = kAppleInlineNamespace ? "std::__1" : "std";
CheckHash<std::shared_ptr<double>>(fmt::format(
"{std}::shared_ptr<double>",
fmt::arg("std", stdcc)));
CheckHash<std::pair<int, double>>(fmt::format(
"{std}::pair<int,double>",
fmt::arg("std", stdcc)));
// Templated classes *with* default template arguments.
CheckHash<std::vector<double>>(fmt::format(
"{std}::vector<double,{std}::allocator<double>>",
fmt::arg("std", stdcc)));
CheckHash<std::vector<BasicVector<double>>>(fmt::format(
"{std}::vector<"
"drake::systems::BasicVector<double>,"
"{std}::allocator<drake::systems::BasicVector<double>>"
">", fmt::arg("std", stdcc)));
// Const-qualified types.
CheckHash<std::shared_ptr<const double>>(fmt::format(
"{std}::shared_ptr<const double>",
fmt::arg("std", stdcc)));
// Eigen classes.
CheckHash<Eigen::VectorXd>(
"Eigen::Matrix<double,int=-1,int=1,int=0,int=-1,int=1>");
CheckHash<Eigen::MatrixXd>(
"Eigen::Matrix<double,int=-1,int=-1,int=0,int=-1,int=-1>");
CheckHash<Eigen::Vector3d>(
"Eigen::Matrix<double,int=3,int=1,int=0,int=3,int=1>");
CheckHash<Eigen::Matrix3d>(
"Eigen::Matrix<double,int=3,int=3,int=0,int=3,int=3>");
// Vectors of Eigens.
CheckHash<std::vector<Eigen::VectorXd>>(fmt::format(
"{std}::vector<{eigen},{std}::allocator<{eigen}>>",
fmt::arg("std", stdcc),
fmt::arg("eigen",
"Eigen::Matrix<double,int=-1,int=1,int=0,int=-1,int=1>")));
// Everything together at once works.
using BigType = std::vector<std::pair<
const double, std::shared_ptr<Eigen::Matrix3d>>>;
CheckHash<BigType>(fmt::format(
"{std}::vector<"
"{std}::pair<"
"const double,"
"{std}::shared_ptr<{eigen}>>,"
"{std}::allocator<{std}::pair<"
"const double,"
"{std}::shared_ptr<{eigen}>>>>",
fmt::arg("std", stdcc),
fmt::arg("eigen",
"Eigen::Matrix<double,int=3,int=3,int=0,int=3,int=3>")));
// Templated on a value, but with the 'using NonTypeTemplateParameter'
// decoration so that the hash works.
const std::string kfoo =
kClang || kGcc9 ? "drake::test::{anonymous}::AnonEnum::kFoo" : "0";
CheckHash<NiceAnonEnumTemplate<AnonEnum::kFoo>>(
"drake::test::{anonymous}::NiceAnonEnumTemplate<"
"drake::test::{anonymous}::AnonEnum=" + kfoo + ">");
}
// Tests that a type mismatched is detected for a mismatched non-type template
// parameter, even in Release builds. When the TypeHash fails (is zero), it's
// important that AbstractValue fall back to using typeinfo comparison instead.
GTEST_TEST(ValueTest, NonTypeTemplateParameter) {
// We cannot compute hashes for non-type template parameters when the user
// hasn't added a `using` statement to guide us.
using T1 = UnadornedAnonEnumTemplate<AnonEnum::kFoo>;
using T2 = UnadornedAnonEnumTemplate<AnonEnum::kBar>;
ASSERT_EQ(internal::TypeHash<T1>::value, 0);
ASSERT_EQ(internal::TypeHash<T2>::value, 0);
// However, our getters and setters still catch type mismatches (by using the
// std::typeinfo comparison).
Value<T1> foo_value;
Value<T2> bar_value;
AbstractValue& foo = foo_value;
DRAKE_EXPECT_NO_THROW(foo.get_value<T1>());
EXPECT_THROW(foo.get_value<T2>(), std::exception);
EXPECT_THROW(foo.get_value<int>(), std::exception);
EXPECT_THROW(foo.SetFrom(bar_value), std::exception);
}
// When a cast fails, the error message should report the actual types found
// not to match. The request must match the static type of the Value container,
// not the dynamic possibly-more-derived type of the contained value. When the
// static and dynamic types differ, display both. See #15434.
GTEST_TEST(ValueTest, TypesInBadCastMessage) {
using MyVector1d = systems::MyVector<double, 1>;
{
// Make a base-typed value container with a more-derived value inside.
auto value =
std::make_unique<Value<BasicVector<double>>>(MyVector1d{});
AbstractValue& abstract = *value;
// This request looks like it should work, but doesn't. The error message
// should indicate why.
DRAKE_EXPECT_THROWS_MESSAGE(
abstract.get_value<MyVector1d>(),
".*request.*MyVector.*static.*BasicVector.*"
"dynamic.*MyVector.*'\\)\\.$");
// This is the proper request type.
DRAKE_EXPECT_NO_THROW(abstract.get_value<BasicVector<double>>());
}
{
// Make a value container that doesn't have the possibility of containing
// derived types.
Value<int> value;
AbstractValue& abstract = value;
// The error message in this case can be simpler. Test note: the regular
// expression here specifically rejects parentheses near the end of the
// line, to exclude the more elaborate error message matched in the case
// above.
DRAKE_EXPECT_THROWS_MESSAGE(
abstract.get_value<double>(),
".*request.*double.*static.*int'\\.$");
}
}
} // namespace test
} // namespace drake