|
| 1 | +#include <stdlib.h> |
| 2 | +#include "testapi.h" |
| 3 | +#include "parser/dbclineparser.h" |
| 4 | +#include "helpers/formatter.h" |
| 5 | + |
| 6 | + |
| 7 | +TEST(TestSigLineParsing, test1) |
| 8 | +{ |
| 9 | + DbcLineParser parser; |
| 10 | + |
| 11 | + const std::string t2 = " SG_ FLT4_TEST_1 : 39|4@0+ (2.01,1E-002) [-0.01|30.14] \"\" BCM"; |
| 12 | + |
| 13 | + expect_true(parser.IsSignalLine(t2)); |
| 14 | + |
| 15 | + SignalDescriptor_t dsc; |
| 16 | + |
| 17 | + parser.ParseSignalLine(&dsc, t2); |
| 18 | + |
| 19 | + expect_true(dsc.IsDoubleSig); |
| 20 | + expect_true(dsc.Offset == 0.01); |
| 21 | + |
| 22 | + const std::string t3 = " SG_ FLT4_TEST_1 : 39|4@0+ (2, 1) [-0.01|30.14] \"\" BCM"; |
| 23 | + |
| 24 | + parser.ParseSignalLine(&dsc, t3); |
| 25 | + |
| 26 | + expect_false(dsc.IsDoubleSig); |
| 27 | + expect_true(dsc.Offset == 1.0); |
| 28 | + |
| 29 | + // last supported double values |
| 30 | + const std::string t2_ok_norm = " SG_ FLT4_TEST_1 : 39|4@0+ (2, 0.000000001) [-0.01|30.14] \"\" BCM"; |
| 31 | + const std::string t2_ok_scie = " SG_ FLT4_TEST_1 : 39|4@0+ (2, 1e-9) [-0.01|30.14] \"\" BCM"; |
| 32 | + // next values (and less than) are not currently supported |
| 33 | + const std::string t2_nok_norm = " SG_ FLT4_TEST_1 : 39|4@0+ (2, 0.00000000099) [-0.01|30.14] \"\" BCM"; |
| 34 | + const std::string t2_nok_scie = " SG_ FLT4_TEST_1 : 39|4@0+ (2, 1e-10) [-0.01|30.14] \"\" BCM"; |
| 35 | + const std::string t3_nok_scie = " SG_ FLT4_TEST_1 : 39|4@0+ (1e-10, 44) [-0.01|30.14] \"\" BCM"; |
| 36 | + const std::string t3_nok_norm = " SG_ FLT4_TEST_1 : 39|4@0+ (0.00000000099, 2) [-0.01|30.14] \"\" BCM"; |
| 37 | + |
| 38 | + parser.ParseSignalLine(&dsc, t2_ok_norm); |
| 39 | + |
| 40 | + expect_true(dsc.IsDoubleSig); |
| 41 | + expect_eq(dsc.Offset, 0.000000001); |
| 42 | + expect_eq(dsc.Factor, 2.0); |
| 43 | + |
| 44 | + parser.ParseSignalLine(&dsc, t2_ok_scie); |
| 45 | + |
| 46 | + expect_true(dsc.IsDoubleSig); |
| 47 | + expect_eq(dsc.Offset, 0.000000001); |
| 48 | + expect_eq(dsc.Factor, 2.0); |
| 49 | + |
| 50 | + parser.ParseSignalLine(&dsc, t3_nok_norm); |
| 51 | + |
| 52 | + expect_false(dsc.IsDoubleSig); |
| 53 | + expect_eq(dsc.Offset, 0.0); |
| 54 | + expect_eq(dsc.Factor, 1.0); |
| 55 | + |
| 56 | + parser.ParseSignalLine(&dsc, t3_nok_scie); |
| 57 | + |
| 58 | + expect_false(dsc.IsDoubleSig); |
| 59 | + expect_eq(dsc.Offset, 0.0); |
| 60 | + expect_eq(dsc.Factor, 1.0); |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +TEST(TestSigLineParsing, test_02) |
| 65 | +{ |
| 66 | + const std::string t3_ok = " SG_ FLT4_TEST_1 : 39|4@0+ (0.99, 0) [-0.01|30.14] \"\" BCM"; |
| 67 | + const std::string t4_ok = " SG_ FLT4_TEST_1 : 39|4@0+ (0, -0.11) [-0.01|30.14] \"\" BCM"; |
| 68 | + const std::string t5_notok = " SG_ FLT4_TEST_1 : 39|4@0+ (0.00000000099, 0) [-0.01|30.14] \"\" BCM"; |
| 69 | + const std::string t6_ok = " SG_ FLT4_TEST_1 : 39|4@0+ (0, 0.000000000000) [-0.01|30.14] \"\" BCM"; |
| 70 | + |
| 71 | + DbcLineParser parser; |
| 72 | + |
| 73 | + SignalDescriptor_t dsc; |
| 74 | + |
| 75 | + parser.ParseSignalLine(&dsc, t3_ok); |
| 76 | + |
| 77 | + expect_true(dsc.IsDoubleSig); |
| 78 | + |
| 79 | + parser.ParseSignalLine(&dsc, t4_ok); |
| 80 | + |
| 81 | + expect_true(dsc.IsDoubleSig); |
| 82 | + expect_eq(dsc.Factor, 0.0); |
| 83 | + expect_eq(dsc.Offset, -0.11); |
| 84 | + |
| 85 | + parser.ParseSignalLine(&dsc, t5_notok); |
| 86 | + |
| 87 | + expect_false(dsc.IsDoubleSig); |
| 88 | + expect_eq(dsc.Factor, 1.0); |
| 89 | + expect_eq(dsc.Offset, 0.0); |
| 90 | + |
| 91 | + parser.ParseSignalLine(&dsc, t6_ok); |
| 92 | + |
| 93 | + expect_true(dsc.IsDoubleSig); |
| 94 | + expect_eq(dsc.Factor, 0.0); |
| 95 | + expect_eq(dsc.Offset, 0.0); |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +TEST(TestSigLineParsing, test_prt_double) |
| 100 | +{ |
| 101 | + constexpr double v = -124.10001110002220; |
| 102 | + |
| 103 | + expect_eq((std::string)prt_double(v, 0, false), (std::string)"-124"); |
| 104 | + expect_eq((std::string)prt_double(v, 1, false), (std::string)"-124.1"); |
| 105 | + expect_eq((std::string)prt_double(v, 2, false), (std::string)"-124.1"); |
| 106 | + expect_eq((std::string)prt_double(v, 3, false), (std::string)"-124.1"); |
| 107 | + expect_eq((std::string)prt_double(v, 4, false), (std::string)"-124.1"); |
| 108 | + expect_eq((std::string)prt_double(v, 5, false), (std::string)"-124.10001"); |
| 109 | + expect_eq((std::string)prt_double(v, 6, false), (std::string)"-124.100011"); |
| 110 | + expect_eq((std::string)prt_double(v, 7, false), (std::string)"-124.1000111"); |
| 111 | + expect_eq((std::string)prt_double(v, 8, false), (std::string)"-124.1000111"); |
| 112 | + expect_eq((std::string)prt_double(v, 9, false), (std::string)"-124.1000111"); |
| 113 | + |
| 114 | + constexpr double vint = 123.0000; |
| 115 | + |
| 116 | + expect_eq((std::string)prt_double(vint, 3), (std::string)"123.0"); |
| 117 | + expect_eq((std::string)prt_double(vint, 2), (std::string)"123.0"); |
| 118 | + expect_eq((std::string)prt_double(vint, 1), (std::string)"123.0"); |
| 119 | + expect_eq((std::string)prt_double(vint, 0), (std::string)"123.0"); |
| 120 | + expect_eq((std::string)prt_double(vint, 100), (std::string)"123.0"); |
| 121 | + expect_eq((std::string)prt_double(vint, 1000), (std::string)"123.0"); |
| 122 | + |
| 123 | + constexpr double v2 = 0.0110022; |
| 124 | + |
| 125 | + expect_eq((std::string)prt_double(v2, 0), "0.0"); |
| 126 | +} |
0 commit comments