IRSOL
C++ code implementing socket server for interacting with Baumer camera.
test_utils.cpp
Go to the documentation of this file.
1#include "irsol/utils.hpp"
2
3#include <catch2/catch_all.hpp>
4#include <set>
5
6TEST_CASE("toInt(protocol_value_t)", "[Utils]")
7{
8 auto rawValue = GENERATE(-42, 0, 2, 1000);
11}
12
13TEST_CASE("toDouble(protocol_value_t)", "[Utils]")
14{
15 auto rawValue = GENERATE(-42.6, 0.3, 2.2, 1000.1231231);
17 CHECK(irsol::utils::toDouble(protocolValue) == Catch::Approx(rawValue));
18}
19
20TEST_CASE("toString(protocol_value_t)", "[Utils]")
21{
22 std::string rawValue = GENERATE("", "hello", "hello world!");
25}
26
27TEST_CASE("uuid()", "[Utils]")
28{
29 std::set<std::string> results;
30 for(size_t i = 0; i < 10000; ++i) {
31 auto uuid = irsol::utils::uuid();
32 CHECK(results.find(uuid) == results.end());
33 results.insert(uuid);
34 }
35}
36
37TEST_CASE("split()", "[Utils]")
38{
39
40 std::string inString = "Hello world";
41 {
42 char delimiter = ' ';
43 auto result = irsol::utils::split(inString, delimiter);
44 std::vector<std::string> expected{"Hello", "world"};
46 }
47 {
48 char delimiter = '!';
49 auto result = irsol::utils::split(inString, delimiter);
50 std::vector<std::string> expected{"Hello world"};
52 }
53 {
54 char delimiter = 'l';
55 auto result = irsol::utils::split(inString, delimiter);
56 std::vector<std::string> expected{"He", "o wor", "d"};
58 }
59}
60
61TEST_CASE("strip()", "[Utils]")
62{
63 {
64 std::string inString = " \tHello world\n \r";
65 {
66 auto result = irsol::utils::strip(inString);
67 std::string expected = "Hello world";
69 }
70
71 {
72 std::string delimiters = "d";
73 auto result = irsol::utils::strip(inString, delimiters);
74 std::string expected = inString;
76 }
77 }
78 {
79 std::string inString = "Hello world";
80 {
81 auto result = irsol::utils::strip(inString);
82 std::string expected = "Hello world";
84 }
85
86 {
87 std::string delimiters = "d";
88 auto result = irsol::utils::strip(inString, delimiters);
89 std::string expected = "Hello worl";
91 }
92
93 {
94 std::string delimiters = "Hd";
95 auto result = irsol::utils::strip(inString, delimiters);
96 std::string expected = "ello worl";
98 }
99 {
100 std::string delimiters = "Hello ";
101 auto result = irsol::utils::strip(inString, delimiters);
102 std::string expected = "world";
104 }
105 }
106}
107
108TEST_CASE("stripString()", "[Utils]")
109{
110 {
111 std::string inString = "Hello world";
112 {
113 std::string strippedString = "";
114 auto result = irsol::utils::stripString(inString, strippedString);
115 std::string expected = "Hello world";
117 }
118
119 {
120 std::string strippedString = "d";
121 auto result = irsol::utils::stripString(inString, strippedString);
122 std::string expected = "Hello worl";
124 }
125
126 {
127 std::string strippedString = "Hd";
128 auto result = irsol::utils::stripString(inString, strippedString);
129 std::string expected = "Hello world";
131 }
132 {
133 std::string strippedString = "Hello ";
134 auto result = irsol::utils::stripString(inString, strippedString);
135 std::string expected = "world";
137 }
138 }
139}
140TEST_CASE("durationToString()", "[Utils]")
141{
142 {
143 irsol::types::duration_t duration{};
144 CHECK(irsol::utils::durationToString(duration) == "0ns");
145 }
146 {
147 irsol::types::duration_t duration = std::chrono::nanoseconds(4);
148 CHECK(irsol::utils::durationToString(duration) == "4ns");
149 }
150 {
151 irsol::types::duration_t duration = std::chrono::nanoseconds(4000);
152 CHECK(irsol::utils::durationToString(duration) == "4us");
153 }
154 {
155 irsol::types::duration_t duration = std::chrono::nanoseconds(4030);
156 CHECK(irsol::utils::durationToString(duration) == "4us 030ns");
157 }
158 {
159 irsol::types::duration_t duration = std::chrono::microseconds(234);
160 CHECK(irsol::utils::durationToString(duration) == "234us");
161 }
162 {
163 irsol::types::duration_t duration = std::chrono::microseconds(23442);
164 CHECK(irsol::utils::durationToString(duration) == "23ms 442us");
165 }
166 {
167 irsol::types::duration_t duration = std::chrono::milliseconds(499);
168 CHECK(irsol::utils::durationToString(duration) == "499ms");
169 }
170 {
171 irsol::types::duration_t duration = std::chrono::seconds(16);
172 CHECK(irsol::utils::durationToString(duration) == "16s");
173 }
174 {
175 irsol::types::duration_t duration = std::chrono::seconds(200);
176 CHECK(irsol::utils::durationToString(duration) == "3minutes 20s");
177 }
178 {
179 irsol::types::duration_t duration = std::chrono::minutes(54);
180 CHECK(irsol::utils::durationToString(duration) == "54minutes");
181 }
182 {
183 irsol::types::duration_t duration = std::chrono::hours(270);
184 CHECK(irsol::utils::durationToString(duration) == "270hours");
185 }
186}
187
188TEST_CASE("stringToBytes()", "[Utils]")
189{
190 {
191 std::string empty;
193 CHECK(result.size() == 0);
194 }
195 {
196 std::string singleChar = "a";
197 auto result = irsol::utils::stringToBytes(singleChar);
198 CHECK(result.size() == 1);
200 }
201 {
202 std::string longerString = GENERATE("hello", "world", "How are you?");
203 auto result = irsol::utils::stringToBytes(longerString);
204 CHECK(result.size() == longerString.size());
205 for(size_t i = 0; i < longerString.size(); ++i) {
206 CHECK(
207 result[i] ==
208 static_cast<irsol::types::byte_t>(static_cast<unsigned char>(longerString[i])));
209 }
210 }
211}
212
213TEST_CASE("bytesToString()", "[Utils]")
214{
215 {
216 std::vector<irsol::types::byte_t> bytes;
218 CHECK(result.size() == 0);
219 }
220 {
221 std::vector<irsol::types::byte_t> bytes{irsol::types::byte_t{'a'}};
223 CHECK(result.size() == 1);
224 CHECK(result == "a");
225 }
226 {
227 std::vector<irsol::types::byte_t> bytes{irsol::types::byte_t{'H'},
235 CHECK(result == "Hello !");
236 }
237}
clock_t::duration duration_t
Alias for a duration of time as defined by clock_t.
Definition types.hpp:128
std::variant< int, double, std::string > protocol_value_t
Variant type representing protocol values that can be one of several types.
Definition types.hpp:150
std::byte byte_t
Alias for a single byte used in serialization or binary data handling.
Definition types.hpp:165
double toDouble(const irsol::types::protocol_value_t &x)
Converts a protocol value to a double.
Definition utils.hpp:55
std::string bytesToString(const std::vector< irsol::types::byte_t > &input)
Converts a std::vector of irsol::types::byte_t to a std::string.
Definition utils.cpp:189
std::string strip(const std::string &s, const std::string &delimiters=" \t\r\n")
Removes leading and trailing characters from a string.
Definition utils.cpp:71
int toInt(const irsol::types::protocol_value_t &x)
Converts a protocol value to an integer.
Definition utils.hpp:39
std::string stripString(const std::string &s, const std::string &strippedString)
Removes all occurrences of a specific substring from the start and end of a string.
Definition utils.cpp:89
std::string uuid()
Generates a new UUID string.
Definition utils.cpp:16
std::vector< std::string > split(const std::string &s, char delimiter)
Splits a string into tokens based on a delimiter.
Definition utils.cpp:50
std::vector< irsol::types::byte_t > stringToBytes(const std::string &s)
Converts a std::string to a std::vector of irsol::types::byte_t.
Definition utils.cpp:182
std::string toString(const irsol::types::protocol_value_t &x)
Converts a protocol value to a string.
Definition utils.hpp:71
std::string durationToString(irsol::types::duration_t dr)
Converts a duration to a human-readable string.
Definition utils.cpp:133
TEST_CASE("fromString<int>", "[Protocol][Protocol::Utils]")
Definition test_utils.cpp:6
std::vector< std::string > expected
CHECK(irsol::utils::toInt(protocolValue)==rawValue)
auto protocolValue
Definition test_utils.cpp:9
auto result
General utility functions used throughout the irsol library.