CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
test
common
test_msgpack.cpp
Go to the documentation of this file.
1
// Copyright (c) 2026 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
9
#include <
carla/MsgPackAdaptors.h
>
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);
38
Response<std::vector<float>
> rv;
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
());
44
er =
Response<void>::Success
();
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
namespace
{
75
// rpc::Actor as it was before spawn_frame was appended.
76
struct
ActorWithoutSpawnFrame {
77
carla::rpc::ActorId
id
= 0u;
78
carla::rpc::ActorId
parent_id = 0u;
79
ActorDescription description;
80
carla::geom::BoundingBox bounding_box;
81
std::vector<uint8_t> semantic_tags;
82
std::vector<unsigned char>
stream_token
;
83
MSGPACK_DEFINE_ARRAY(
id
, parent_id, description, bounding_box, semantic_tags, stream_token);
84
};
85
}
// namespace
86
87
TEST
(msgpack, actor_spawn_frame_is_compatible_both_ways) {
88
namespace
c =
carla
;
89
Actor
actor;
90
actor.
id
= 42u;
91
actor.
parent_id
= 7u;
92
actor.
description
.
id
=
"vehicle.tesla.model3"
;
93
actor.
spawn_frame
= 123456u;
94
95
auto
round_trip = c::MsgPack::UnPack<Actor>(c::MsgPack::Pack(actor));
96
ASSERT_EQ(round_trip.spawn_frame, actor.
spawn_frame
);
97
98
// A new client reading an old server's reply: no spawn_frame, defaults to 0.
99
ActorWithoutSpawnFrame old_actor;
100
old_actor.id = 42u;
101
old_actor.parent_id = 7u;
102
old_actor.description.
id
=
"vehicle.tesla.model3"
;
103
auto
from_old = c::MsgPack::UnPack<Actor>(c::MsgPack::Pack(old_actor));
104
ASSERT_EQ(from_old.id, old_actor.id);
105
ASSERT_EQ(from_old.parent_id, old_actor.parent_id);
106
ASSERT_EQ(from_old.description.id, old_actor.description.
id
);
107
ASSERT_EQ(from_old.spawn_frame, 0u);
108
109
// An old client reading a new server's reply ignores the extra field.
110
auto
from_new = c::MsgPack::UnPack<ActorWithoutSpawnFrame>(c::MsgPack::Pack(actor));
111
ASSERT_EQ(from_new.id, actor.
id
);
112
ASSERT_EQ(from_new.parent_id, actor.
parent_id
);
113
ASSERT_EQ(from_new.description.id, actor.
description
.
id
);
114
}
115
116
TEST
(msgpack, variant) {
117
using
mp =
carla::MsgPack
;
118
119
std::variant<bool, float, std::string> var;
120
121
var =
true
;
122
auto
result = mp::UnPack<decltype(var)>(mp::Pack(var));
123
ASSERT_EQ(result.index(), 0);
124
ASSERT_EQ(std::get<bool>(result),
true
);
125
126
var = 42.0f;
127
result = mp::UnPack<decltype(var)>(mp::Pack(var));
128
ASSERT_EQ(result.index(), 1);
129
ASSERT_EQ(std::get<float>(result), 42.0f);
130
131
var = std::string(
"hola!"
);
132
result = mp::UnPack<decltype(var)>(mp::Pack(var));
133
ASSERT_EQ(result.index(), 2);
134
ASSERT_EQ(std::get<std::string>(result),
"hola!"
);
135
}
136
137
TEST
(msgpack, optional) {
138
using
mp =
carla::MsgPack
;
139
140
std::optional<float> var;
141
142
auto
result = mp::UnPack<decltype(var)>(mp::Pack(var));
143
ASSERT_FALSE(result.has_value());
144
145
var = 42.0f;
146
result = mp::UnPack<decltype(var)>(mp::Pack(var));
147
ASSERT_TRUE(result.has_value());
148
ASSERT_EQ(*result, 42.0f);
149
}
MsgPackAdaptors.h
Response.h
carla::MsgPack
Definition
MsgPack.h:15
carla::geom::BoundingBox
Definition
LibCarla/source/carla/geom/BoundingBox.h:26
carla::rpc::ActorDescription::uid
ActorId uid
Definition
LibCarla/source/carla/rpc/ActorDescription.h:30
carla::rpc::ActorDescription::id
std::string id
Definition
LibCarla/source/carla/rpc/ActorDescription.h:32
carla::rpc::Actor
Definition
rpc/Actor.h:20
carla::rpc::Actor::id
ActorId id
Definition
rpc/Actor.h:25
carla::rpc::Actor::spawn_frame
uint64_t spawn_frame
Server frame counter when a spawn call created this actor: snapshots up to and including this frame p...
Definition
rpc/Actor.h:58
carla::rpc::Actor::parent_id
ActorId parent_id
Definition
rpc/Actor.h:27
carla::rpc::Actor::description
ActorDescription description
Definition
rpc/Actor.h:29
carla::rpc::Actor::bounding_box
geom::BoundingBox bounding_box
Definition
rpc/Actor.h:31
carla::rpc::ResponseError::ResponseError
ResponseError()=default
carla::rpc::ResponseError::What
const std::string & What() const
Definition
Response.h:37
carla::rpc::Response
Definition
Response.h:52
carla::rpc::Response::Reset
void Reset(TValue &&value)
Definition
Response.h:65
carla::rpc::Response::Get
value_type & Get()
Definition
Response.h:83
carla::rpc::Response::SetError
void SetError(Ts &&... args)
Definition
Response.h:74
carla::rpc::Response::HasError
bool HasError() const
Definition
Response.h:69
carla::rpc::Response::GetError
const error_type & GetError() const
Definition
Response.h:78
carla::geom
Definition
Misc.h:24
carla::rpc
Definition
DebugShapeDrawer.h:11
carla::rpc::ActorId
uint32_t ActorId
Definition
ActorId.h:14
carla::streaming::stream_token
detail::token_type stream_token
Definition
streaming/Client.h:20
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
Actor.h
carla::geom::Vector3D
Definition
geom/Vector3D.h:17
test.h
TEST
TEST(agents_types, road_option_values_match_python)
Definition
test_agents.cpp:40
Generated by
1.18.0