CARLA
test_msgpack.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "test.h"
8 
10 #include <carla/rpc/Actor.h>
11 #include <carla/rpc/Response.h>
12 
13 #include <thread>
14 
15 using namespace carla::rpc;
16 
17 TEST(msgpack, response) {
18  using mp = carla::MsgPack;
19  const std::string error = "Uh ah an error!";
20  Response<int> r = ResponseError(error);
21  auto s = mp::UnPack<decltype(r)>(mp::Pack(r));
22  ASSERT_TRUE(r.HasError());
23  ASSERT_EQ(r.GetError().What(), error);
24  ASSERT_TRUE(s.HasError());
25  ASSERT_EQ(s.GetError().What(), error);
26  r.Reset(42);
27  s = mp::UnPack<decltype(r)>(mp::Pack(r));
28  ASSERT_FALSE(r.HasError());
29  ASSERT_EQ(r.Get(), 42);
30  ASSERT_FALSE(s.HasError());
31  ASSERT_EQ(s.Get(), 42);
32  r.SetError(error);
33  s = mp::UnPack<decltype(r)>(mp::Pack(r));
34  ASSERT_FALSE(r);
35  ASSERT_EQ(r.GetError().What(), error);
36  ASSERT_FALSE(s);
37  ASSERT_EQ(s.GetError().What(), error);
39  auto sv = mp::UnPack<decltype(rv)>(mp::Pack(rv));
40  ASSERT_TRUE(rv.HasError());
41  ASSERT_TRUE(sv.HasError());
42  Response<void> er;
43  ASSERT_TRUE(er.HasError());
45  auto es = mp::UnPack<decltype(er)>(mp::Pack(er));
46  ASSERT_FALSE(er.HasError());
47  ASSERT_FALSE(es.HasError());
48  er.SetError(error);
49  es = mp::UnPack<decltype(er)>(mp::Pack(er));
50  ASSERT_FALSE(er);
51  ASSERT_EQ(er.GetError().What(), error);
52  ASSERT_FALSE(es);
53  ASSERT_EQ(es.GetError().What(), error);
54 }
55 
56 TEST(msgpack, actor) {
57  namespace c = carla;
58  namespace cg = carla::geom;
59  Actor actor;
60  actor.id = 42u;
61  actor.description.uid = 2u;
62  actor.description.id = "actor.random.whatever";
63  actor.bounding_box = cg::BoundingBox{cg::Vector3D{1.0f, 2.0f, 3.0f}};
64 
65  auto buffer = c::MsgPack::Pack(actor);
66  auto result = c::MsgPack::UnPack<Actor>(buffer);
67 
68  ASSERT_EQ(result.id, actor.id);
69  ASSERT_EQ(result.description.uid, actor.description.uid);
70  ASSERT_EQ(result.description.id, actor.description.id);
71  ASSERT_EQ(result.bounding_box, actor.bounding_box);
72 }
73 
74 TEST(msgpack, variant) {
75  using mp = carla::MsgPack;
76 
77  boost::variant<bool, float, std::string> var;
78 
79  var = true;
80  auto result = mp::UnPack<decltype(var)>(mp::Pack(var));
81  ASSERT_EQ(result.which(), 0);
82  ASSERT_EQ(boost::get<bool>(result), true);
83 
84  var = 42.0f;
85  result = mp::UnPack<decltype(var)>(mp::Pack(var));
86  ASSERT_EQ(result.which(), 1);
87  ASSERT_EQ(boost::get<float>(result), 42.0f);
88 
89  var = std::string("hola!");
90  result = mp::UnPack<decltype(var)>(mp::Pack(var));
91  ASSERT_EQ(result.which(), 2);
92  ASSERT_EQ(boost::get<std::string>(result), "hola!");
93 }
94 
95 TEST(msgpack, optional) {
96  using mp = carla::MsgPack;
97 
98  boost::optional<float> var;
99 
100  auto result = mp::UnPack<decltype(var)>(mp::Pack(var));
101  ASSERT_FALSE(result.has_value());
102 
103  var = 42.0f;
104  result = mp::UnPack<decltype(var)>(mp::Pack(var));
105  ASSERT_TRUE(result.has_value());
106  ASSERT_EQ(*result, 42.0f);
107 }
void Reset(TValue &&value)
Definition: Response.h:56
TEST(msgpack, response)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
ActorDescription description
Definition: rpc/Actor.h:29
geom::BoundingBox bounding_box
Definition: rpc/Actor.h:31
value_type & Get()
Definition: Response.h:74
void SetError(Ts &&... args)
Definition: Response.h:65
const std::string & What() const
Definition: Response.h:28
bool HasError() const
Definition: Response.h:60
const error_type & GetError() const
Definition: Response.h:69