|
| 1 | +#include "common.hpp" |
| 2 | +#include "defines.hpp" |
| 3 | +#include <libdbc/dbc.hpp> |
| 4 | +#include <libdbc/utils/utils.hpp> |
| 5 | + |
| 6 | +#include <catch2/catch_approx.hpp> |
| 7 | +#include <catch2/catch_test_macros.hpp> |
| 8 | +#include <catch2/reporters/catch_reporter_event_listener.hpp> |
| 9 | +#include <catch2/reporters/catch_reporter_registrars.hpp> |
| 10 | + |
| 11 | +class testRunListener : public Catch::EventListenerBase { |
| 12 | +public: |
| 13 | + using Catch::EventListenerBase::EventListenerBase; |
| 14 | + |
| 15 | + void testRunStarting(Catch::TestRunInfo const&) override { |
| 16 | + prev_loc = std::setlocale(LC_ALL, nullptr); |
| 17 | + // Set the locale to something that has , instead of . for floats |
| 18 | + std::locale::global(std::locale("de_DE.UTF-8")); |
| 19 | + } |
| 20 | + |
| 21 | + void testCaseEnded(Catch::TestCaseStats const&) override { |
| 22 | + // Restore the old locale |
| 23 | + std::locale::global(std::locale(prev_loc)); |
| 24 | + } |
| 25 | + |
| 26 | +private: |
| 27 | + std::string prev_loc; |
| 28 | +}; |
| 29 | + |
| 30 | +CATCH_REGISTER_LISTENER(testRunListener) |
| 31 | + |
| 32 | +TEST_CASE("Should parse doubld string locale independently") { |
| 33 | + REQUIRE(Catch::Approx(utils::String::convert_to_double("6.82")) == 6.82); |
| 34 | +} |
| 35 | + |
| 36 | +TEST_CASE("Should process message with floats locale indpendently") { |
| 37 | + const auto* filename = std::tmpnam(NULL); |
| 38 | + |
| 39 | + create_tmp_dbc_with(filename, R"(BO_ 234 MSG1: 8 Vector__XXX |
| 40 | + SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX |
| 41 | + SG_ Sig2 : 39|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX |
| 42 | + SG_ Sig3 : 23|16@0- (10,0) [-3276.8|-3276.7] "C" Vector__XXX |
| 43 | + SG_ Sig4 : 7|16@0- (1,-10) [0|32767] "" Vector__XXX)"); |
| 44 | + |
| 45 | + auto parser = libdbc::DbcParser(); |
| 46 | + parser.parse_file(filename); |
| 47 | + |
| 48 | + REQUIRE(parser.get_messages().size() == 1); |
| 49 | + REQUIRE(parser.get_messages().at(0).name() == "MSG1"); |
| 50 | + REQUIRE(parser.get_messages().at(0).getSignals().size() == 4); |
| 51 | + |
| 52 | + SECTION("Evaluating first message") { |
| 53 | + const auto signal = parser.get_messages().at(0).getSignals().at(0); |
| 54 | + REQUIRE(signal.factor == 0.1); |
| 55 | + REQUIRE(signal.offset == 0); |
| 56 | + REQUIRE(signal.min == -3276.8); |
| 57 | + REQUIRE(signal.max == -3276.7); |
| 58 | + } |
| 59 | + SECTION("Evaluating second message") { |
| 60 | + const auto signal = parser.get_messages().at(0).getSignals().at(1); |
| 61 | + REQUIRE(signal.factor == 0.1); |
| 62 | + REQUIRE(signal.offset == 0); |
| 63 | + REQUIRE(signal.min == -3276.8); |
| 64 | + REQUIRE(signal.max == -3276.7); |
| 65 | + } |
| 66 | + SECTION("Evaluating third message") { |
| 67 | + const auto signal = parser.get_messages().at(0).getSignals().at(2); |
| 68 | + REQUIRE(signal.factor == 10); |
| 69 | + REQUIRE(signal.offset == 0); |
| 70 | + REQUIRE(signal.min == -3276.8); |
| 71 | + REQUIRE(signal.max == -3276.7); |
| 72 | + } |
| 73 | + SECTION("Evaluating fourth message") { |
| 74 | + const auto signal = parser.get_messages().at(0).getSignals().at(3); |
| 75 | + REQUIRE(signal.factor == 1); |
| 76 | + REQUIRE(signal.offset == -10); |
| 77 | + REQUIRE(signal.min == 0); |
| 78 | + REQUIRE(signal.max == 32767); |
| 79 | + } |
| 80 | +} |
0 commit comments