YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
TestUtils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025-2025 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "TestUtils.h"
7
8#include <yarp/os/Time.h>
9#include <yarp/os/LogStream.h>
10#include <memory>
11#include <numeric>
12
13using namespace std;
14
15namespace yarp::dev::tests
16{
17 void YARP_dev_API wait_safe(const double seconds)
18 {
19 yarp::os::Time::delay(seconds);
20 }
21
22 // Utility function to compare two int vectors for equality
23 bool YARP_dev_API vectors_equal(const std::vector<double>& a, const std::vector<double>& b)
24 {
25 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
26 }
27
28 bool YARP_dev_API vectors_equal(const std::vector<int>& a, const std::vector<int>& b)
29 {
30 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
31 }
32
33 bool YARP_dev_API check_approximate(double value, double expected, double tolerance)
34 {
35 if (tolerance < 0.0)
36 {
37 tolerance = -tolerance; // Ensure tolerance is non-negative
38 }
39 if (std::abs(value - expected) <= tolerance)
40 {
41 return true;
42 }
43 yError() << "Value:" << value << ", Expected Value:" << expected;
44 return false;
45 }
46
47 bool YARP_dev_API vectors_equal(const size_t siz, const double* a, const double* b)
48 {
49 for (size_t i = 0; i < siz; i++)
50 {
51 if (a[i] != b[i]) return false;
52 }
53 return true;
54 }
55
56 bool YARP_dev_API vectors_equal(const size_t siz, const int* a, const int* b)
57 {
58 for (size_t i = 0; i < siz; i++)
59 {
60 if (a[i] != b[i]) return false;
61 }
62 return true;
63 }
64
65 void YARP_dev_API set_vector_crescent(std::vector<double>& a, double val)
66 {
67 std::iota(a.begin(), a.end(), val);
68 }
69
70 void YARP_dev_API set_vector_crescent(const size_t siz, double* a, double val)
71 {
72 for (size_t i = 0; i < siz; i++)
73 {
74 a[i] = val;
75 val = val + 1.0;
76 }
77 }
78
79 void YARP_dev_API set_vector_crescent(const size_t siz, int* a, int val)
80 {
81 for (size_t i = 0; i < siz; i++)
82 {
83 a[i] = val;
84 val = val + 1;
85 }
86 }
87}
#define yError(...)
Definition Log.h:361
STL namespace.
bool vectors_equal(const std::vector< double > &a, const std::vector< double > &b)
Definition TestUtils.cpp:23
void set_vector_crescent(std::vector< double > &a, double val)
Definition TestUtils.cpp:65
bool check_approximate(double value, double expected, double tolerance)
Definition TestUtils.cpp:33
void wait_safe(const double seconds)
Definition TestUtils.cpp:17
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
#define YARP_dev_API
Definition api.h:18