diff --git a/test/can_log_based/can_logs/24_RjxzsBms_ov_cov.txt b/test/can_log_based/can_logs/24_RjxzsBms_ov_cov.txt new file mode 100644 index 00000000..70b09575 --- /dev/null +++ b/test/can_log_based/can_logs/24_RjxzsBms_ov_cov.txt @@ -0,0 +1,5 @@ +# 65278V pack (overvoltage) +(123.893) RX0 f5 [8] 03 fe fe 00 00 00 00 00 + +# 5V max cell (cell overvoltage) +(124.893) RX0 f5 [8] 51 01 01 01 01 13 88 00 diff --git a/test/can_log_based/can_logs/24_RjxzsBms_overvoltage.txt b/test/can_log_based/can_logs/24_RjxzsBms_overvoltage.txt deleted file mode 100644 index 8d7c133d..00000000 --- a/test/can_log_based/can_logs/24_RjxzsBms_overvoltage.txt +++ /dev/null @@ -1 +0,0 @@ -(123.893) RX0 f5 [8] 03 ff ff 00 00 00 00 00 diff --git a/test/can_log_based/can_logs/37_MgHsPhev_good.txt b/test/can_log_based/can_logs/37_MgHsPhev_base.txt similarity index 100% rename from test/can_log_based/can_logs/37_MgHsPhev_good.txt rename to test/can_log_based/can_logs/37_MgHsPhev_base.txt diff --git a/test/can_log_based/can_logs/37_MgHsPhev_ov_cov.txt b/test/can_log_based/can_logs/37_MgHsPhev_ov_cov.txt new file mode 100644 index 00000000..404115c7 --- /dev/null +++ b/test/can_log_based/can_logs/37_MgHsPhev_ov_cov.txt @@ -0,0 +1,5 @@ +# 450V pack (overvoltage) +(13148.893) RX0 3ac [8] 01 d4 01 6f 07 08 4d d0 + +# 5V max cell (cell overvoltage) +(38245.429) RX0 173 [8] 00 00 c8 c3 13 88 0e 18 diff --git a/test/can_log_based/can_logs/37_MgHsPhev_overvoltage.txt b/test/can_log_based/can_logs/37_MgHsPhev_overvoltage.txt deleted file mode 100644 index fc1a81be..00000000 --- a/test/can_log_based/can_logs/37_MgHsPhev_overvoltage.txt +++ /dev/null @@ -1,2 +0,0 @@ -# 450V (overvoltage) -(13148.893) RX0 3ac [8] 01 d4 01 6f 07 08 4d d0 diff --git a/test/can_log_based/can_logs/5_BydAtto3_good.txt b/test/can_log_based/can_logs/5_BydAtto3_base.txt similarity index 100% rename from test/can_log_based/can_logs/5_BydAtto3_good.txt rename to test/can_log_based/can_logs/5_BydAtto3_base.txt diff --git a/test/can_log_based/canlog_safety_tests.cpp b/test/can_log_based/canlog_safety_tests.cpp index 489ebbce..6f758c15 100644 --- a/test/can_log_based/canlog_safety_tests.cpp +++ b/test/can_log_based/canlog_safety_tests.cpp @@ -67,6 +67,9 @@ class CanLogTestFixture : public testing::Test { } update_machineryprotection(); + + // When debugging, uncomment this to see the parsed values + // PrintValues(); } void PrintValues() { @@ -85,15 +88,13 @@ class CanLogTestFixture : public testing::Test { fs::path path_; }; -// Check that the parsed logs populate the required datalayer values for Battery -// Emulator to function. -class AllValuesPresentTest : public CanLogTestFixture { +// Check that the parsed logs populate the minimum required datalayer values for +// Battery Emulator to function. +class BaseValuesPresentTest : public CanLogTestFixture { public: - explicit AllValuesPresentTest(fs::path path) : CanLogTestFixture(path) {} + explicit BaseValuesPresentTest(fs::path path) : CanLogTestFixture(path) {} void TestBody() override { ProcessLog(); - // When debugging, uncomment this to see the parsed values - //PrintValues(); EXPECT_NE(datalayer.battery.status.voltage_dV, 0); // TODO: Current isn't actually a requirement? check power instead? @@ -115,33 +116,79 @@ class OverVoltageTest : public CanLogTestFixture { explicit OverVoltageTest(fs::path path) : CanLogTestFixture(path) {} void TestBody() override { ProcessLog(); - // When debugging, uncomment this to see the parsed values - //PrintValues(); EXPECT_EQ(get_event_pointer(EVENT_BATTERY_OVERVOLTAGE)->occurences, 1); } }; +// Check that the parsed logs correctly trigger a cell overvoltage event. +class CellOverVoltageTest : public CanLogTestFixture { + public: + explicit CellOverVoltageTest(fs::path path) : CanLogTestFixture(path) {} + void TestBody() override { + ProcessLog(); + + EXPECT_EQ(get_event_pointer(EVENT_CELL_OVER_VOLTAGE)->occurences, 1); + EXPECT_EQ(get_event_pointer(EVENT_CELL_CRITICAL_OVER_VOLTAGE)->occurences, 1); + } +}; + +// Check that the parsed logs correctly trigger a cell undervoltage event. +class CellUnderVoltageTest : public CanLogTestFixture { + public: + explicit CellUnderVoltageTest(fs::path path) : CanLogTestFixture(path) {} + void TestBody() override { + ProcessLog(); + + EXPECT_EQ(get_event_pointer(EVENT_CELL_UNDER_VOLTAGE)->occurences, 1); + EXPECT_EQ(get_event_pointer(EVENT_CELL_CRITICAL_UNDER_VOLTAGE)->occurences, 1); + } +}; + void RegisterCanLogTests() { // The logs should be named as follows: - // __good.txt (all values present) - // __overvoltage.txt (triggers overvoltage event) - // where battery_type is the integer corresponding to the BatteryType enum + // + // ___.txt + // + // where: + // battery_type is the integer in the BatteryType enum + // flag1/flag2... are flags that indicate which tests to run: + // base: test that the minimmum required values are populated (and no events triggered) + // ov: test that an overvoltage event is triggered + // cov: test that normal and critical cell overvoltage events are triggered + // cuv: test that normal and critical cell undervoltage events are triggered std::string directoryPath = "../can_log_based/can_logs"; for (const auto& entry : fs::directory_iterator(directoryPath)) { - if (entry.is_regular_file() && ends_with(entry.path(), "_good.txt")) { - - testing::RegisterTest("CanLogTestFixture", ("TestAllValuesPresent_" + entry.path().filename().string()).c_str(), - nullptr, entry.path().filename().string().c_str(), __FILE__, __LINE__, - [=]() -> CanLogTestFixture* { return new AllValuesPresentTest(entry.path()); }); + if (!entry.is_regular_file() || entry.path().extension().string() != ".txt") { + continue; } - if (entry.is_regular_file() && ends_with(entry.path(), "_overvoltage.txt")) { + auto bits = split(entry.path().stem(), '_'); + auto has_flag = [&bits](const std::string& flag) -> bool { + return std::find(bits.begin() + 2, bits.end(), flag) != bits.end(); + }; + + if (has_flag("base")) { + testing::RegisterTest("CanLogTestFixture", ("TestBaseValuesPresent_" + entry.path().filename().string()).c_str(), + nullptr, entry.path().filename().string().c_str(), __FILE__, __LINE__, + [=]() -> CanLogTestFixture* { return new BaseValuesPresentTest(entry.path()); }); + } + if (has_flag("ov")) { testing::RegisterTest("CanLogTestFixture", ("TestOverVoltage_" + entry.path().filename().string()).c_str(), nullptr, entry.path().filename().string().c_str(), __FILE__, __LINE__, [=]() -> CanLogTestFixture* { return new OverVoltageTest(entry.path()); }); } + if (has_flag("cov")) { + testing::RegisterTest("CanLogTestFixture", ("TestCellOverVoltage_" + entry.path().filename().string()).c_str(), + nullptr, entry.path().filename().string().c_str(), __FILE__, __LINE__, + [=]() -> CanLogTestFixture* { return new CellOverVoltageTest(entry.path()); }); + } + if (has_flag("cuv")) { + testing::RegisterTest("CanLogTestFixture", ("TestCellUnderVoltage_" + entry.path().filename().string()).c_str(), + nullptr, entry.path().filename().string().c_str(), __FILE__, __LINE__, + [=]() -> CanLogTestFixture* { return new CellUnderVoltageTest(entry.path()); }); + } } } diff --git a/test/can_log_based/utils.cpp b/test/can_log_based/utils.cpp index 4fb36e9f..339d69c2 100644 --- a/test/can_log_based/utils.cpp +++ b/test/can_log_based/utils.cpp @@ -8,6 +8,17 @@ bool ends_with(const std::string& str, const std::string& suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } +std::vector split(const std::string& text, char sep) { + std::vector tokens; + std::size_t start = 0, end = 0; + while ((end = text.find(sep, start)) != std::string::npos) { + tokens.push_back(text.substr(start, end - start)); + start = end + 1; + } + tokens.push_back(text.substr(start)); + return tokens; +} + void print_frame(const CAN_frame& frame) { std::cout << "ID: " << std::hex << frame.ID << ", DLC: " << (int)frame.DLC << ", Data: "; for (int i = 0; i < frame.DLC; ++i) { diff --git a/test/can_log_based/utils.h b/test/can_log_based/utils.h index 9de74a98..5e54b39c 100644 --- a/test/can_log_based/utils.h +++ b/test/can_log_based/utils.h @@ -6,5 +6,6 @@ namespace fs = std::filesystem; bool ends_with(const std::string& str, const std::string& suffix); +std::vector split(const std::string& text, char sep); std::vector parse_can_log_file(const fs::path& filePath);