YARP
Yet Another Robot Platform
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
/*
2
* SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3
* SPDX-License-Identifier: BSD-3-Clause
4
*/
5
6
#include <
yarp/os/all.h
>
7
#include <
yarp/dev/ReturnValue.h
>
8
9
#include <
TestReturnValue.h
>
10
11
#include <catch2/catch_amalgamated.hpp>
12
13
using namespace
yarp::os
;
14
using
yarp::dev::ReturnValue
;
15
16
namespace
{
17
18
constexpr
auto
comm_error
=
ReturnValue::return_code::return_value_error_nws_nwc_communication_error
;
19
20
// Server side implementation, used to check that a successful call still
21
// returns the values sent by the server.
22
class
TestReturnValueServer :
public
TestReturnValue
23
{
24
public
:
25
int32_t
stored_value = 0;
26
27
ReturnValue
setValueRPC
(
const
std::int32_t value)
override
28
{
29
stored_value = value;
30
return
ReturnValue_ok
;
31
}
32
33
return_getValue
getValueRPC
()
override
34
{
35
return_getValue
r;
36
r.
ret
=
ReturnValue_ok
;
37
r.
value
= stored_value;
38
return
r;
39
}
40
41
return_getTwoValues
getTwoValuesRPC
()
override
42
{
43
return_getTwoValues
r;
44
r.
ret
=
ReturnValue_ok
;
45
r.
ret2
=
ReturnValue_error_not_ready
;
46
r.
text
=
"hello"
;
47
return
r;
48
}
49
50
bool
getBoolRPC
()
override
51
{
52
return
true
;
53
}
54
};
55
56
// Calls all the methods of a client whose write is expected to fail, and
57
// checks that every yarp::dev::ReturnValue reports a communication error.
58
void
check_failed_calls
(
TestReturnValue
& client)
59
{
60
// Case 1: yarp::dev::ReturnValue returned directly
61
ReturnValue
ret = client.
setValueRPC
(42);
62
CHECK
(ret ==
comm_error
);
63
CHECK_FALSE
(ret);
64
65
// Case 2: struct with one yarp::dev::ReturnValue field
66
return_getValue
rv
= client.
getValueRPC
();
67
CHECK
(
rv
.ret ==
comm_error
);
68
CHECK_FALSE
(
rv
.ret);
69
CHECK
(
rv
.value == 0);
70
71
// Case 2: struct with more than one yarp::dev::ReturnValue field
72
return_getTwoValues
rtv
= client.
getTwoValuesRPC
();
73
CHECK
(
rtv
.ret ==
comm_error
);
74
CHECK
(
rtv
.ret2 ==
comm_error
);
75
CHECK
(
rtv
.text.empty());
76
77
// Case 3: no yarp::dev::ReturnValue involved, default value returned
78
CHECK
(client.
getBoolRPC
() ==
false
);
79
}
80
81
}
// namespace
82
83
84
TEST_CASE
(
"IdlThriftTest4"
,
"[yarp::idl::thrift4]"
)
85
{
86
yarp::os::Network::setLocalMode
(
true
);
87
88
SECTION
(
"Failed write: client not attached to any port"
)
89
{
90
TestReturnValue
client;
91
check_failed_calls
(client);
92
}
93
94
SECTION
(
"Failed write: client port not connected to any server"
)
95
{
96
Port
port;
97
REQUIRE
(port.
open
(
"/test4/client"
));
98
TestReturnValue
client;
99
REQUIRE
(client.
yarp
().
attachAsClient
(port));
100
check_failed_calls
(client);
101
port.
close
();
102
}
103
104
SECTION
(
"Successful call: values returned by the server are preserved"
)
105
{
106
TestReturnValueServer
server
;
107
TestReturnValue
client;
108
REQUIRE
(client.
yarp
().
attachAsClient
(
server
));
109
110
ReturnValue
ret = client.
setValueRPC
(42);
111
CHECK
(ret ==
ReturnValue::return_code::return_value_ok
);
112
113
return_getValue
rv
= client.
getValueRPC
();
114
CHECK
(
rv
.ret ==
ReturnValue::return_code::return_value_ok
);
115
CHECK
(
rv
.value == 42);
116
117
return_getTwoValues
rtv
= client.
getTwoValuesRPC
();
118
CHECK
(
rtv
.ret ==
ReturnValue::return_code::return_value_ok
);
119
CHECK
(
rtv
.ret2 ==
ReturnValue::return_code::return_value_error_not_ready
);
120
CHECK
(
rtv
.text ==
"hello"
);
121
122
CHECK
(client.
getBoolRPC
() ==
true
);
123
}
124
125
yarp::os::Network::setLocalMode
(
false
);
126
}
127
128
int
main
(
int
argc,
char
* argv[])
129
{
130
Network
yarp
;
131
return
Catch::Session().
run
(argc, argv);
132
}
ReturnValue.h
ReturnValue_ok
#define ReturnValue_ok
Definition
ReturnValue.h:82
ReturnValue_error_not_ready
#define ReturnValue_error_not_ready
Definition
ReturnValue.h:88
TestReturnValue.h
TestReturnValue
Definition
TestReturnValue.h:24
TestReturnValue::getTwoValuesRPC
virtual return_getTwoValues getTwoValuesRPC()
Definition
TestReturnValue.cpp:924
TestReturnValue::getValueRPC
virtual return_getValue getValueRPC()
Definition
TestReturnValue.cpp:909
TestReturnValue::setValueRPC
virtual yarp::dev::ReturnValue setValueRPC(const std::int32_t value)
Definition
TestReturnValue.cpp:899
TestReturnValue::getBoolRPC
virtual bool getBoolRPC()
Definition
TestReturnValue.cpp:940
return_getTwoValues
Definition
return_getTwoValues.h:20
return_getTwoValues::ret2
yarp::dev::ReturnValue ret2
Definition
return_getTwoValues.h:24
return_getTwoValues::text
std::string text
Definition
return_getTwoValues.h:25
return_getTwoValues::ret
yarp::dev::ReturnValue ret
Definition
return_getTwoValues.h:23
return_getValue
Definition
return_getValue.h:20
return_getValue::value
std::int32_t value
Definition
return_getValue.h:24
return_getValue::ret
yarp::dev::ReturnValue ret
Definition
return_getValue.h:23
yarp::dev::ReturnValue
Definition
ReturnValue.h:35
yarp::dev::ReturnValue::return_code::return_value_error_nws_nwc_communication_error
@ return_value_error_nws_nwc_communication_error
Method is not (yet) implemented.
yarp::dev::ReturnValue::return_code::return_value_ok
@ return_value_ok
yarp::dev::ReturnValue::return_code::return_value_error_not_ready
@ return_value_error_not_ready
Method failed due to invalid internal status/invalid request.
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition
BufferedPort.h:60
yarp::os::NetworkBase::setLocalMode
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition
Network.cpp:1137
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition
Network.h:718
yarp::os::Port
A mini-server for network communication.
Definition
Port.h:46
yarp::os::Port::close
void close() override
Stop port activity.
Definition
Port.cpp:330
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition
Port.cpp:79
yarp::os::WireLink::attachAsClient
bool attachAsClient(yarp::os::UnbufferedContactable &port)
Tag this WireLink as a client, sending data via the specified port.
Definition
WireLink.cpp:80
yarp::os::Wire::yarp
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition
Wire.h:28
main
int main(int argc, char *argv[])
Definition
main.cpp:128
TEST_CASE
TEST_CASE("IdlThriftTest4", "[yarp::idl::thrift4]")
Definition
main.cpp:84
all.h
yarp::os
An interface to the operating system, including Port based communication.
Definition
LoadInfoData.cpp:13
yarp::run
Definition
PlatformStdlib.h:20
yarp
The main, catch-all namespace for YARP.
Definition
dirs.h:16
YARP
4.0.2+169-20261113.4+git2cc772dc6
src
commands
yarpidl_thrift
tests
test4
main.cpp
Generated on Sun Nov 15 2026 02:12:02 for YARP by
1.9.8