This commit is contained in:
deeaitch committed 2026-10-05 18:37:20 -04:00
1 parent 8d82b13dad
commit b91cf4aae5
14 files changed
+168 -149

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
+4 -2
View File
@@ -60,7 +60,8 @@ DecodedFrameValue DbcDecoder::Decode (const DecodeDatabase &database,
decoded.physicalValue = decoded.physicalValue =
std::visit([&signal](auto rawValue) { std::visit([&signal](auto rawValue) {
return (static_cast<double>(rawValue) * signal.factor) + signal.offset; return (static_cast<double>(rawValue) * signal.factor) + signal.offset;
}, decoded.rawValue); },
decoded.rawValue);
decoded.valid = true; decoded.valid = true;
result.signals.push_back(decoded); result.signals.push_back(decoded);
@@ -144,7 +145,8 @@ std::int64_t DbcDecoder::SignExtend (std::uint64_t value, std::uint32_t bitLengt
const std::uint64_t signMask = std::uint64_t(1U) << (bitLength - 1U); const std::uint64_t signMask = std::uint64_t(1U) << (bitLength - 1U);
const std::uint64_t valueMask = (bitLength == 64U) const std::uint64_t valueMask = (bitLength == 64U)
? ~std::uint64_t (0U) : (std::uint64_t (1U) << bitLength) - 1U; ? ~std::uint64_t(0U)
: (std::uint64_t(1U) << bitLength) - 1U;
value &= valueMask; value &= valueMask;
if ((value & signMask) == 0U) if ((value & signMask) == 0U)
+4 -11
View File
@@ -12,9 +12,9 @@
#ifndef DBC_DECODER_H #ifndef DBC_DECODER_H
#define DBC_DECODER_H #define DBC_DECODER_H
#include <vector>
#include <cstdint> #include <cstdint>
#include <variant> #include <variant>
#include <vector>
#include "decode_database.h" #include "decode_database.h"
@@ -27,9 +27,7 @@ struct RawCanFrame {
std::vector<std::uint8_t> data; /**< Payload bytes. */ std::vector<std::uint8_t> data; /**< Payload bytes. */
RawCanFrame() RawCanFrame()
: canId (0U) : canId(0U), isExtended(false), data() {
, isExtended (false)
, data() {
} }
}; };
@@ -43,10 +41,7 @@ struct DecodedSignalValue {
bool valid; /**< true if decoding succeeded. */ bool valid; /**< true if decoding succeeded. */
DecodedSignalValue() DecodedSignalValue()
: definition (nullptr) : definition(nullptr), rawValue(std::uint64_t(0U)), physicalValue(0.0), valid(false) {
, rawValue (std::uint64_t (0U))
, physicalValue (0.0)
, valid (false) {
} }
}; };
@@ -59,9 +54,7 @@ struct DecodedFrameValue {
bool valid; /**< true if frame was matched. */ bool valid; /**< true if frame was matched. */
DecodedFrameValue() DecodedFrameValue()
: definition (nullptr) : definition(nullptr), signals(), valid(false) {
, signals()
, valid (false) {
} }
}; };
+2 -2
View File
@@ -12,9 +12,9 @@
#include "dbc_parser.h" #include "dbc_parser.h"
#include "dbc_statement_handler.h" #include "dbc_statement_handler.h"
#include <cctype>
#include <fstream> #include <fstream>
#include <stdexcept> #include <stdexcept>
#include <cctype>
namespace { namespace {
/** /**
@@ -35,7 +35,7 @@ namespace {
return text.substr(begin, end - begin); return text.substr(begin, end - begin);
} }
} }// namespace
DbcDatabase DbcParser::ParseFile(const std::string &filePath) const { DbcDatabase DbcParser::ParseFile(const std::string &filePath) const {
std::ifstream input(filePath.c_str()); std::ifstream input(filePath.c_str());
+6 -6
View File
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#include "dbc_statement_handler.h" #include "dbc_statement_handler.h"
#include <sstream>
#include <stdexcept>
#include <cctype> #include <cctype>
#include <charconv> #include <charconv>
#include <cmath>
#include <limits> #include <limits>
#include <regex> #include <regex>
#include <cmath> #include <sstream>
#include <stdexcept>
namespace { namespace {
std::uint32_t ParseDecimal(const std::string &text, std::uint32_t ParseDecimal(const std::string &text,
@@ -40,7 +40,7 @@ namespace {
} }
FrameInfo *FindFrameById(DbcDatabase &, std::uint32_t, bool); FrameInfo *FindFrameById(DbcDatabase &, std::uint32_t, bool);
SignalInfo *FindSignalByName(FrameInfo &, const std::string &); SignalInfo *FindSignalByName(FrameInfo &, const std::string &);
} }// namespace
namespace { namespace {
std::vector<std::string> SplitReceivers(const std::string &text) { std::vector<std::string> SplitReceivers(const std::string &text) {
@@ -98,7 +98,7 @@ std::uint32_t TryExtractPgn (std::uint32_t canId, bool isExtended, bool &hasPgn)
return pgn; return pgn;
} }
} }// namespace
FrameInfo DbcFrameHandler::ParseFrameLine(const std::string &line) { FrameInfo DbcFrameHandler::ParseFrameLine(const std::string &line) {
/* /*
@@ -275,7 +275,7 @@ SignalInfo *FindSignalByName (FrameInfo &frame, const std::string &signalName) {
return nullptr; return nullptr;
} }
} }// namespace
void DbcFrameHandler::Handle(const std::string &line, DbcParseContext &context) const { void DbcFrameHandler::Handle(const std::string &line, DbcParseContext &context) const {
context.database.frames.push_back(ParseFrameLine(line)); context.database.frames.push_back(ParseFrameLine(line));
+3
View File
@@ -22,6 +22,7 @@ class DbcStatementHandler {
class DbcFrameHandler : public DbcStatementHandler { class DbcFrameHandler : public DbcStatementHandler {
public: public:
void Handle(const std::string &line, DbcParseContext &context) const override; void Handle(const std::string &line, DbcParseContext &context) const override;
private: private:
static FrameInfo ParseFrameLine(const std::string &line); static FrameInfo ParseFrameLine(const std::string &line);
}; };
@@ -29,6 +30,7 @@ class DbcFrameHandler : public DbcStatementHandler {
class DbcSignalHandler : public DbcStatementHandler { class DbcSignalHandler : public DbcStatementHandler {
public: public:
void Handle(const std::string &line, DbcParseContext &context) const override; void Handle(const std::string &line, DbcParseContext &context) const override;
private: private:
static SignalInfo ParseSignalLine(const std::string &line); static SignalInfo ParseSignalLine(const std::string &line);
}; };
@@ -36,6 +38,7 @@ class DbcSignalHandler : public DbcStatementHandler {
class DbcCommentHandler : public DbcStatementHandler { class DbcCommentHandler : public DbcStatementHandler {
public: public:
void Handle(const std::string &line, DbcParseContext &context) const override; void Handle(const std::string &line, DbcParseContext &context) const override;
private: private:
static void ParseCommentLine(const std::string &line, DbcDatabase &database); static void ParseCommentLine(const std::string &line, DbcDatabase &database);
}; };
+7 -28
View File
@@ -12,10 +12,10 @@
#ifndef DECODE_DATABASE_H #ifndef DECODE_DATABASE_H
#define DECODE_DATABASE_H #define DECODE_DATABASE_H
#include <string>
#include <vector>
#include <unordered_map>
#include <cstdint> #include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
/** /**
* @brief Signal byte order used for runtime decoding. * @brief Signal byte order used for runtime decoding.
@@ -51,18 +51,7 @@ struct DecodeSignal {
std::string comment; /**< Comment. */ std::string comment; /**< Comment. */
DecodeSignal() DecodeSignal()
: name() : name(), startBit(0U), length(0U), byteOrder(ByteOrder::Intel), valueType(ValueType::Unsigned), factor(1.0), offset(0.0), minimum(0.0), maximum(0.0), unit(), receivers(), comment() {
, startBit (0U)
, length (0U)
, byteOrder (ByteOrder::Intel)
, valueType (ValueType::Unsigned)
, factor (1.0)
, offset (0.0)
, minimum (0.0)
, maximum (0.0)
, unit()
, receivers()
, comment() {
} }
}; };
@@ -81,15 +70,7 @@ struct DecodeFrame {
std::vector<DecodeSignal> signals; /**< Signal definitions. */ std::vector<DecodeSignal> signals; /**< Signal definitions. */
DecodeFrame() DecodeFrame()
: name() : name(), canId(0U), isExtended(false), dlc(0U), pgn(0U), hasPgn(false), transmitter(), comment(), signals() {
, canId (0U)
, isExtended (false)
, dlc (0U)
, pgn (0U)
, hasPgn (false)
, transmitter()
, comment()
, signals() {
} }
}; };
@@ -101,13 +82,11 @@ struct FrameKey {
bool isExtended; bool isExtended;
FrameKey() FrameKey()
: canId (0U) : canId(0U), isExtended(false) {
, isExtended (false) {
} }
FrameKey(std::uint32_t id, bool extended) FrameKey(std::uint32_t id, bool extended)
: canId (id) : canId(id), isExtended(extended) {
, isExtended (extended) {
} }
bool operator==(const FrameKey &other) const { bool operator==(const FrameKey &other) const {
+2 -10
View File
@@ -12,9 +12,9 @@
#ifndef FRAME_INFO_H #ifndef FRAME_INFO_H
#define FRAME_INFO_H #define FRAME_INFO_H
#include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstdint>
#include "signal_info.h" #include "signal_info.h"
@@ -33,15 +33,7 @@ struct FrameInfo {
std::vector<SignalInfo> signals; /**< Signals contained in the frame. */ std::vector<SignalInfo> signals; /**< Signals contained in the frame. */
FrameInfo() FrameInfo()
: name() : name(), canId(0U), isExtended(false), pgn(0U), hasPgn(false), dlc(0U), transmitter(), comment(), signals() {
, canId (0U)
, isExtended (false)
, pgn (0U)
, hasPgn (false)
, dlc (0U)
, transmitter()
, comment()
, signals() {
} }
}; };
+4 -4
View File
@@ -9,16 +9,16 @@
* See LICENSE file in the project root for full license text. * See LICENSE file in the project root for full license text.
*/ */
#include <iostream> #include <cstddef>
#include <iomanip> #include <iomanip>
#include <iostream>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <cstddef>
#include "dbc_parser.h"
#include "dbc_tree_builder.h"
#include "dbc_decode_builder.h" #include "dbc_decode_builder.h"
#include "dbc_decoder.h" #include "dbc_decoder.h"
#include "dbc_parser.h"
#include "dbc_tree_builder.h"
static void PrintTree(const TreeNode *node, int indent) { static void PrintTree(const TreeNode *node, int indent) {
if (node == nullptr) if (node == nullptr)
+2 -13
View File
@@ -12,9 +12,9 @@
#ifndef SIGNAL_INFO_H #ifndef SIGNAL_INFO_H
#define SIGNAL_INFO_H #define SIGNAL_INFO_H
#include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstdint>
/** /**
* @brief Describes one signal inside a DBC frame. * @brief Describes one signal inside a DBC frame.
@@ -34,18 +34,7 @@ struct SignalInfo {
std::string comment; /**< Optional signal comment. */ std::string comment; /**< Optional signal comment. */
SignalInfo() SignalInfo()
: name() : name(), startBit(0U), length(0U), isLittleEndian(true), isSigned(false), factor(1.0), offset(0.0), minimum(0.0), maximum(0.0), unit(), receivers(), comment() {
, startBit (0U)
, length (0U)
, isLittleEndian (true)
, isSigned (false)
, factor (1.0)
, offset (0.0)
, minimum (0.0)
, maximum (0.0)
, unit()
, receivers()
, comment() {
} }
}; };
+19 -34
View File
@@ -20,41 +20,23 @@ struct DecodeCase {
int main() { int main() {
const DecodeCase cases[] = { const DecodeCase cases[] = {
{"Motorola reproduction", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {"Motorola reproduction", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
{0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0}, {"Motorola within byte", ByteOrder::Motorola, 5U, 4U, ValueType::Unsigned, {0xAC}, true, 11, 1.0, 0.0, 11.0},
{"Motorola within byte", ByteOrder::Motorola, 5U, 4U, ValueType::Unsigned, {"Motorola unaligned crossing", ByteOrder::Motorola, 3U, 12U, ValueType::Unsigned, {0xAB, 0xCD}, true, 0xBCD, 1.0, 0.0, 3021.0},
{0xAC}, true, 11, 1.0, 0.0, 11.0}, {"Motorola starts at bit zero", ByteOrder::Motorola, 0U, 2U, ValueType::Unsigned, {0x01, 0x80}, true, 3, 1.0, 0.0, 3.0},
{"Motorola unaligned crossing", ByteOrder::Motorola, 3U, 12U, ValueType::Unsigned, {"Motorola second byte", ByteOrder::Motorola, 15U, 16U, ValueType::Unsigned, {0xFF, 0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
{0xAB, 0xCD}, true, 0xBCD, 1.0, 0.0, 3021.0}, {"Motorola signed scaled", ByteOrder::Motorola, 7U, 16U, ValueType::Signed, {0xFF, 0xFE}, true, -2, 0.5, 10.0, 9.0},
{"Motorola starts at bit zero", ByteOrder::Motorola, 0U, 2U, ValueType::Unsigned, {"Motorola 64 bits", ByteOrder::Motorola, 7U, 64U, ValueType::Unsigned, {0, 0, 0, 0, 0, 0, 0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
{0x01, 0x80}, true, 3, 1.0, 0.0, 3.0}, {"Motorola 64 bits high bit", ByteOrder::Motorola, 7U, 64U, ValueType::Unsigned, {0x40, 0, 0, 0, 0, 0, 0, 0}, true, 0x4000000000000000LL, 1.0, 0.0, 4611686018427387904.0},
{"Motorola second byte", ByteOrder::Motorola, 15U, 16U, ValueType::Unsigned, {"Motorola short payload", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {0x12}, false, 0, 1.0, 0.0, 0.0},
{0xFF, 0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0}, {"Motorola empty payload", ByteOrder::Motorola, 7U, 8U, ValueType::Unsigned, {}, false, 0, 1.0, 0.0, 0.0},
{"Motorola signed scaled", ByteOrder::Motorola, 7U, 16U, ValueType::Signed, {"Motorola large start", ByteOrder::Motorola, std::numeric_limits<std::uint32_t>::max(), 8U, ValueType::Unsigned, {0xFF}, false, 0, 1.0, 0.0, 0.0},
{0xFF, 0xFE}, true, -2, 0.5, 10.0, 9.0}, {"Motorola zero length", ByteOrder::Motorola, 7U, 0U, ValueType::Unsigned, {0xFF}, false, 0, 1.0, 0.0, 0.0},
{"Motorola 64 bits", ByteOrder::Motorola, 7U, 64U, ValueType::Unsigned,
{0, 0, 0, 0, 0, 0, 0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
{"Motorola 64 bits high bit", ByteOrder::Motorola, 7U, 64U, ValueType::Unsigned,
{0x40, 0, 0, 0, 0, 0, 0, 0}, true, 0x4000000000000000LL,
1.0, 0.0, 4611686018427387904.0},
{"Motorola short payload", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned,
{0x12}, false, 0, 1.0, 0.0, 0.0},
{"Motorola empty payload", ByteOrder::Motorola, 7U, 8U, ValueType::Unsigned,
{}, false, 0, 1.0, 0.0, 0.0},
{"Motorola large start", ByteOrder::Motorola,
std::numeric_limits<std::uint32_t>::max(), 8U, ValueType::Unsigned,
{0xFF}, false, 0, 1.0, 0.0, 0.0},
{"Motorola zero length", ByteOrder::Motorola, 7U, 0U, ValueType::Unsigned,
{0xFF}, false, 0, 1.0, 0.0, 0.0},
{"Motorola excessive length", ByteOrder::Motorola, 7U, 65U, ValueType::Unsigned, {"Motorola excessive length", ByteOrder::Motorola, 7U, 65U, ValueType::Unsigned,
std::vector<std::uint8_t>(9U, 0xFF), false, 0, 1.0, 0.0, 0.0}, std::vector<std::uint8_t>(9U, 0xFF), false, 0, 1.0, 0.0, 0.0},
{"Intel regression", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {"Intel regression", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {0x12, 0x34}, true, 0x3412, 1.0, 0.0, 13330.0},
{0x12, 0x34}, true, 0x3412, 1.0, 0.0, 13330.0}, {"Intel signed regression", ByteOrder::Intel, 0U, 16U, ValueType::Signed, {0xFE, 0xFF}, true, -2, 0.5, 10.0, 9.0},
{"Intel signed regression", ByteOrder::Intel, 0U, 16U, ValueType::Signed, {"Intel short payload", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {0x12}, false, 0, 1.0, 0.0, 0.0}};
{0xFE, 0xFF}, true, -2, 0.5, 10.0, 9.0},
{"Intel short payload", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned,
{0x12}, false, 0, 1.0, 0.0, 0.0}
};
int failures = 0; int failures = 0;
for (const DecodeCase &test: cases) { for (const DecodeCase &test: cases) {
@@ -130,7 +112,10 @@ int main() {
? std::variant<std::int64_t, std::uint64_t>(bits) ? std::variant<std::int64_t, std::uint64_t>(bits)
: std::variant<std::int64_t, std::uint64_t>(signedExpected); : std::variant<std::int64_t, std::uint64_t>(signedExpected);
const double physical = (type == ValueType::Unsigned const double physical = (type == ValueType::Unsigned
? static_cast<double> (bits) : static_cast<double> (signedExpected)) * 0.5 + 10.0; ? static_cast<double>(bits)
: static_cast<double>(signedExpected)) *
0.5 +
10.0;
std::ostringstream actualText; std::ostringstream actualText;
std::ostringstream expectedText; std::ostringstream expectedText;
std::visit([&actualText](auto raw) { actualText << raw; }, decoded.rawValue); std::visit([&actualText](auto raw) { actualText << raw; }, decoded.rawValue);
+45 -22
View File
@@ -9,9 +9,11 @@
struct Fixture { struct Fixture {
std::filesystem::path path = std::filesystem::temp_directory_path() / std::filesystem::path path = std::filesystem::temp_directory_path() /
("dbc_parser_test_" + std::to_string ( ("dbc_parser_test_" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + ".dbc");
std::chrono::steady_clock::now().time_since_epoch().count()) + ".dbc"); ~Fixture() {
~Fixture() { std::error_code error; std::filesystem::remove (path, error); } std::error_code error;
std::filesystem::remove(path, error);
}
void Write(const std::string &text) const { void Write(const std::string &text) const {
std::ofstream output(path); std::ofstream output(path);
output << text; output << text;
@@ -37,9 +39,17 @@ int main() {
std::size_t count = 0U; std::size_t count = 0U;
const auto check = [&](bool passed, const std::string &name) { const auto check = [&](bool passed, const std::string &name) {
++count; ++count;
if (!passed) { ++failures; std::cerr << "FAIL: " << name << '\n'; } if (!passed) {
++failures;
std::cerr << "FAIL: " << name << '\n';
}
};
struct ValidCase {
const char *line;
std::uint32_t id;
bool extended;
unsigned length;
}; };
struct ValidCase { const char *line; std::uint32_t id; bool extended; unsigned length; };
const ValidCase valid[] = { const ValidCase valid[] = {
{"BO_ 0 Frame: 0 ECU", 0U, false, 0U}, {"BO_ 0 Frame: 0 ECU", 0U, false, 0U},
{"BO_ 2047 Frame: 8 ECU", 2047U, false, 8U}, {"BO_ 2047 Frame: 8 ECU", 2047U, false, 8U},
@@ -47,8 +57,7 @@ int main() {
{"BO_ 536870911 Frame: 255 ECU", 0x1FFFFFFFU, true, 255U}, {"BO_ 536870911 Frame: 255 ECU", 0x1FFFFFFFU, true, 255U},
{"BO_ 2147483649 Frame: 8 ECU", 1U, true, 8U}, {"BO_ 2147483649 Frame: 8 ECU", 1U, true, 8U},
{"BO_ 2684354559 Frame: 8 ECU", 0x1FFFFFFFU, true, 8U}, {"BO_ 2684354559 Frame: 8 ECU", 0x1FFFFFFFU, true, 8U},
{" BO_\t256\tFrame :\t8\tECU \r", 256U, false, 8U} {" BO_\t256\tFrame :\t8\tECU \r", 256U, false, 8U}};
};
for (const auto &test: valid) { for (const auto &test: valid) {
fixture.Write(std::string(test.line) + '\n'); fixture.Write(std::string(test.line) + '\n');
try { try {
@@ -57,7 +66,8 @@ int main() {
database.frames[0].canId == test.id && database.frames[0].canId == test.id &&
database.frames[0].isExtended == test.extended && database.frames[0].isExtended == test.extended &&
database.frames[0].dlc == test.length && database.frames[0].dlc == test.length &&
database.frames[0].transmitter == "ECU", test.line); database.frames[0].transmitter == "ECU",
test.line);
} catch (const std::exception &error) { check(false, error.what()); } } catch (const std::exception &error) { check(false, error.what()); }
} }
const char *invalid[] = { const char *invalid[] = {
@@ -70,8 +80,7 @@ int main() {
"BO_ 2684354560 Frame: 8 ECU", "BO_ 4294967295 Frame: 8 ECU", "BO_ 2684354560 Frame: 8 ECU", "BO_ 4294967295 Frame: 8 ECU",
"BO_ 1 Frame: -1 ECU", "BO_ 1 Frame: +8 ECU", "BO_ 1 Frame: 8x ECU", "BO_ 1 Frame: -1 ECU", "BO_ 1 Frame: +8 ECU", "BO_ 1 Frame: 8x ECU",
"BO_ 1 Frame: 8.5 ECU", "BO_ 1 Frame: 256 ECU", "BO_ 1 Frame: 8.5 ECU", "BO_ 1 Frame: 256 ECU",
"BO_ 1 Frame: 4294967296 ECU", "BO_\tinvalid" "BO_ 1 Frame: 4294967296 ECU", "BO_\tinvalid"};
};
for (const auto *line: invalid) { for (const auto *line: invalid) {
fixture.Write(std::string("VERSION \"test\"\n\n") + line + '\n'); fixture.Write(std::string("VERSION \"test\"\n\n") + line + '\n');
try { try {
@@ -80,7 +89,8 @@ int main() {
} catch (const std::runtime_error &error) { } catch (const std::runtime_error &error) {
const std::string prefix = fixture.path.string() + ":3: "; const std::string prefix = fixture.path.string() + ":3: ";
check(std::string(error.what()).find(prefix) == 0U && check(std::string(error.what()).find(prefix) == 0U &&
std::string (error.what()).size() > prefix.size(), line); std::string(error.what()).size() > prefix.size(),
line);
} }
} }
fixture.Write( fixture.Write(
@@ -91,7 +101,8 @@ int main() {
const auto database = DbcParser().ParseFile(fixture.path.string()); const auto database = DbcParser().ParseFile(fixture.path.string());
check(database.frames.size() == 2U && database.frames[0].signals.size() == 1U && check(database.frames.size() == 2U && database.frames[0].signals.size() == 1U &&
database.frames[0].comment == "Frame comment" && database.frames[0].comment == "Frame comment" &&
database.frames[0].signals[0].comment == "Signal comment", "Integration"); database.frames[0].signals[0].comment == "Signal comment",
"Integration");
fixture.Write("\nSG_ Speed : invalid\n"); fixture.Write("\nSG_ Speed : invalid\n");
try { try {
DbcParser().ParseFile(fixture.path.string()); DbcParser().ParseFile(fixture.path.string());
@@ -116,7 +127,8 @@ int main() {
} catch (const std::runtime_error &error) { } catch (const std::runtime_error &error) {
const std::string prefix = fixture.path.string() + ":3: "; const std::string prefix = fixture.path.string() + ":3: ";
check(std::string(error.what()).find(prefix) == 0U && check(std::string(error.what()).find(prefix) == 0U &&
std::string (error.what()).size() > prefix.size(), "Reject " + line); std::string(error.what()).size() > prefix.size(),
"Reject " + line);
} }
}; };
const std::string ordinary = signalLine("0", "8", "1+", "1", "0", "0", "255"); const std::string ordinary = signalLine("0", "8", "1+", "1", "0", "0", "255");
@@ -166,13 +178,21 @@ int main() {
factors.frames[0].signals[1].factor == -1.0, factors.frames[0].signals[1].factor == -1.0,
"Zero and negative factors and equal range boundaries"); "Zero and negative factors and equal range boundaries");
struct SignalCase { const char *start; const char *length; const char *order; unsigned payload; }; struct SignalCase {
const SignalCase validSignals[] = { const char *start;
{"0", "1", "1+", 1U}, {"7", "1", "0-", 1U}, const char *length;
{"0", "64", "1+", 8U}, {"7", "64", "0-", 8U}, const char *order;
{"63", "1", "1+", 8U}, {"56", "1", "0+", 8U}, unsigned payload;
{"3", "12", "0+", 2U}, {"2039", "1", "1+", 255U}
}; };
const SignalCase validSignals[] = {
{"0", "1", "1+", 1U},
{"7", "1", "0-", 1U},
{"0", "64", "1+", 8U},
{"7", "64", "0-", 8U},
{"63", "1", "1+", 8U},
{"56", "1", "0+", 8U},
{"3", "12", "0+", 2U},
{"2039", "1", "1+", 255U}};
for (const auto &test: validSignals) { for (const auto &test: validSignals) {
fixture.Write("BO_ 1 Frame: " + std::to_string(test.payload) + " ECU\n" + fixture.Write("BO_ 1 Frame: " + std::to_string(test.payload) + " ECU\n" +
signalLine(test.start, test.length, test.order, ".5e+1", "-2.5E-1", "-1.", "+2e2") + "\n"); signalLine(test.start, test.length, test.order, ".5e+1", "-2.5E-1", "-1.", "+2e2") + "\n");
@@ -204,11 +224,13 @@ int main() {
check(dispatcher.Dispatch("TEST_ first", context) && check(dispatcher.Dispatch("TEST_ first", context) &&
dispatcher.Dispatch("TEST_ second", context) && dispatcher.Dispatch("TEST_ second", context) &&
CountingHandler::instances == 1 && CountingHandler::calls == 2 && CountingHandler::instances == 1 && CountingHandler::calls == 2 &&
dispatchDatabase.frames.size() == 2U, "Factory dispatch and handler reuse"); dispatchDatabase.frames.size() == 2U,
"Factory dispatch and handler reuse");
check(!dispatcher.Dispatch("TEST_OTHER data", context) && check(!dispatcher.Dispatch("TEST_OTHER data", context) &&
!dispatcher.Dispatch("BO_TX_BU_ 1 : ECU;", context) && !dispatcher.Dispatch("BO_TX_BU_ 1 : ECU;", context) &&
!dispatcher.Dispatch("VAL_ 1 Speed 0 \"Stopped\";", context) && !dispatcher.Dispatch("VAL_ 1 Speed 0 \"Stopped\";", context) &&
CountingHandler::calls == 2, "Exact and unknown keyword dispatch"); CountingHandler::calls == 2,
"Exact and unknown keyword dispatch");
const DbcParser parser; const DbcParser parser;
fixture.Write("BO_ 1 First: 8 ECU\n"); fixture.Write("BO_ 1 First: 8 ECU\n");
check(parser.ParseFile(fixture.path.string()).frames.size() == 1U, "First parse"); check(parser.ParseFile(fixture.path.string()).frames.size() == 1U, "First parse");
@@ -218,7 +240,8 @@ int main() {
check(false, "Independent parse context"); check(false, "Independent parse context");
} catch (const std::runtime_error &error) { } catch (const std::runtime_error &error) {
check(std::string(error.what()).find("Signal found before any frame") != check(std::string(error.what()).find("Signal found before any frame") !=
std::string::npos, "Independent parse context"); std::string::npos,
"Independent parse context");
} }
fixture.Write("BO_ 2 Second: 8 ECU\n"); fixture.Write("BO_ 2 Second: 8 ECU\n");
const auto recovered = parser.ParseFile(fixture.path.string()); const auto recovered = parser.ParseFile(fixture.path.string());
+3 -15
View File
@@ -12,27 +12,15 @@
#include "tree_node.h" #include "tree_node.h"
TreeNode::TreeNode() TreeNode::TreeNode()
: m_type (NodeType::Root) : m_type(NodeType::Root), m_name("dbc"), m_children(), m_frame(), m_signal() {
, m_name ("dbc")
, m_children()
, m_frame()
, m_signal() {
} }
TreeNode::TreeNode(const FrameInfo &frame) TreeNode::TreeNode(const FrameInfo &frame)
: m_type (NodeType::Frame) : m_type(NodeType::Frame), m_name(frame.name), m_children(), m_frame(new FrameInfo(frame)), m_signal() {
, m_name (frame.name)
, m_children()
, m_frame (new FrameInfo (frame))
, m_signal() {
} }
TreeNode::TreeNode(const SignalInfo &signal) TreeNode::TreeNode(const SignalInfo &signal)
: m_type (NodeType::Signal) : m_type(NodeType::Signal), m_name(signal.name), m_children(), m_frame(), m_signal(new SignalInfo(signal)) {
, m_name (signal.name)
, m_children()
, m_frame()
, m_signal (new SignalInfo (signal)) {
} }
void TreeNode::AddChild(std::unique_ptr<TreeNode> child) { void TreeNode::AddChild(std::unique_ptr<TreeNode> child) {
+2 -2
View File
@@ -12,10 +12,10 @@
#ifndef TREE_NODE_H #ifndef TREE_NODE_H
#define TREE_NODE_H #define TREE_NODE_H
#include <cstddef>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include <cstddef>
#include "frame_info.h" #include "frame_info.h"
#include "signal_info.h" #include "signal_info.h"