|
14 | 14 |
|
15 | 15 | #include <algorithm> |
16 | 16 | #include <array> |
| 17 | +#include <chrono> |
17 | 18 | #include <cstdint> |
18 | 19 | #include <string> |
19 | 20 |
|
@@ -168,3 +169,103 @@ TEST_CASE("Get error as string", "[CanFrame]") |
168 | 169 | std::string error = frame.get_error(); |
169 | 170 | REQUIRE(!error.empty()); |
170 | 171 | } |
| 172 | + |
| 173 | +TEST_CASE("Set and get bus timestamp with uint64_t", "[CanFrame]") |
| 174 | +{ |
| 175 | + polymath::socketcan::CanFrame frame; |
| 176 | + std::uint64_t timestamp_us = 1000000; // 1 second in microseconds |
| 177 | + |
| 178 | + frame.set_bus_timestamp(timestamp_us); |
| 179 | + auto bus_time = frame.get_bus_time(); |
| 180 | + |
| 181 | + auto expected = std::chrono::system_clock::time_point(std::chrono::microseconds(timestamp_us)); |
| 182 | + REQUIRE(bus_time == expected); |
| 183 | +} |
| 184 | + |
| 185 | +TEST_CASE("Set and get receive timestamp with uint64_t", "[CanFrame]") |
| 186 | +{ |
| 187 | + polymath::socketcan::CanFrame frame; |
| 188 | + std::uint64_t timestamp_us = 2000000; // 2 seconds in microseconds |
| 189 | + |
| 190 | + frame.set_receive_timestamp(timestamp_us); |
| 191 | + auto receive_time = frame.get_receive_time(); |
| 192 | + |
| 193 | + auto expected = std::chrono::steady_clock::time_point(std::chrono::microseconds(timestamp_us)); |
| 194 | + REQUIRE(receive_time == expected); |
| 195 | +} |
| 196 | + |
| 197 | +TEST_CASE("Set and get bus timestamp with time_point", "[CanFrame]") |
| 198 | +{ |
| 199 | + polymath::socketcan::CanFrame frame; |
| 200 | + auto timestamp = std::chrono::system_clock::now(); |
| 201 | + |
| 202 | + frame.set_bus_timestamp(timestamp); |
| 203 | + auto bus_time = frame.get_bus_time(); |
| 204 | + |
| 205 | + REQUIRE(bus_time == timestamp); |
| 206 | +} |
| 207 | + |
| 208 | +TEST_CASE("Set and get receive timestamp with time_point", "[CanFrame]") |
| 209 | +{ |
| 210 | + polymath::socketcan::CanFrame frame; |
| 211 | + auto timestamp = std::chrono::steady_clock::now(); |
| 212 | + |
| 213 | + frame.set_receive_timestamp(timestamp); |
| 214 | + auto receive_time = frame.get_receive_time(); |
| 215 | + |
| 216 | + REQUIRE(receive_time == timestamp); |
| 217 | +} |
| 218 | + |
| 219 | +TEST_CASE("Constructor initializes receive_time to now", "[CanFrame]") |
| 220 | +{ |
| 221 | + auto before = std::chrono::steady_clock::now(); |
| 222 | + |
| 223 | + canid_t raw_id = 0x123; |
| 224 | + std::array<unsigned char, CAN_MAX_DLC> data = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 225 | + std::uint64_t bus_timestamp = 123456789; |
| 226 | + polymath::socketcan::CanFrame frame(raw_id, data, bus_timestamp); |
| 227 | + |
| 228 | + auto after = std::chrono::steady_clock::now(); |
| 229 | + auto receive_time = frame.get_receive_time(); |
| 230 | + |
| 231 | + REQUIRE(receive_time >= before); |
| 232 | + REQUIRE(receive_time <= after); |
| 233 | +} |
| 234 | + |
| 235 | +TEST_CASE("Constructor sets bus_time from timestamp parameter", "[CanFrame]") |
| 236 | +{ |
| 237 | + canid_t raw_id = 0x123; |
| 238 | + std::array<unsigned char, CAN_MAX_DLC> data = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 239 | + std::uint64_t bus_timestamp = 5000000; // 5 seconds in microseconds |
| 240 | + |
| 241 | + polymath::socketcan::CanFrame frame(raw_id, data, bus_timestamp); |
| 242 | + auto bus_time = frame.get_bus_time(); |
| 243 | + |
| 244 | + auto expected = std::chrono::system_clock::time_point(std::chrono::microseconds(bus_timestamp)); |
| 245 | + REQUIRE(bus_time == expected); |
| 246 | +} |
| 247 | + |
| 248 | +TEST_CASE("Extended constructor initializes timestamps correctly", "[CanFrame]") |
| 249 | +{ |
| 250 | + auto before = std::chrono::steady_clock::now(); |
| 251 | + |
| 252 | + canid_t raw_id = 0x789; |
| 253 | + std::array<unsigned char, CAN_MAX_DLC> data = {9, 8, 7, 6, 5, 4, 3, 2}; |
| 254 | + std::uint64_t bus_timestamp = 10000000; // 10 seconds in microseconds |
| 255 | + auto frame_type = polymath::socketcan::FrameType::REMOTE; |
| 256 | + auto frame_id_type = polymath::socketcan::IdType::EXTENDED; |
| 257 | + |
| 258 | + polymath::socketcan::CanFrame frame(raw_id, data, bus_timestamp, frame_type, frame_id_type); |
| 259 | + |
| 260 | + auto after = std::chrono::steady_clock::now(); |
| 261 | + |
| 262 | + // Check receive_time was set to approximately now |
| 263 | + auto receive_time = frame.get_receive_time(); |
| 264 | + REQUIRE(receive_time >= before); |
| 265 | + REQUIRE(receive_time <= after); |
| 266 | + |
| 267 | + // Check bus_time was set from the parameter |
| 268 | + auto bus_time = frame.get_bus_time(); |
| 269 | + auto expected_bus_time = std::chrono::system_clock::time_point(std::chrono::microseconds(bus_timestamp)); |
| 270 | + REQUIRE(bus_time == expected_bus_time); |
| 271 | +} |
0 commit comments