| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Created by kifir on 1/6/25. | |||
| 3 | // | |||
| 4 | ||||
| 5 | #include "pch.h" | |||
| 6 | ||||
| 7 | #include <array> | |||
| 8 | ||||
| 9 | #include "mlg_field.h" | |||
| 10 | ||||
| 11 | namespace { | |||
| 12 | class BitLoggerFieldTest : public ::testing::Test { | |||
| 13 | static constexpr uint8_t COIL_STATE_12_BIT_NUMBER = 11; | |||
| 14 | protected: | |||
| 15 | void SetUp() override; | |||
| 16 | ||||
| 17 | void checkBitLoggerField(bool expected, const char* context); | |||
| 18 | void updateTestBit(bool value); | |||
| 19 | ||||
| 20 | std::unique_ptr<output_channels_s> m_testOutputChannels; | |||
| 21 | std::unique_ptr<MLG::Entries::Field> m_logField; | |||
| 22 | std::array<char, 6> m_buffer; | |||
| 23 | }; | |||
| 24 | ||||
| 25 | 1 | void BitLoggerFieldTest::SetUp() { | ||
| 26 |
1/1✓ Branch 2 taken 1 time.
|
1 | m_testOutputChannels = std::make_unique<output_channels_s>(); | |
| 27 | ||||
| 28 | 1 | const uint32_t testCoilStateBlockOffset = ( | ||
| 29 | 1 | reinterpret_cast<const char*>(&m_testOutputChannels->outputRequestPeriod) | ||
| 30 | 1 | - reinterpret_cast<const char*>(m_testOutputChannels.get()) | ||
| 31 | 1 | ) - sizeof(uint32_t); | ||
| 32 |
1/1✓ Branch 2 taken 1 time.
|
3 | m_logField = std::make_unique<MLG::Entries::Field>( | |
| 33 | 1 | *m_testOutputChannels, | ||
| 34 | testCoilStateBlockOffset, | |||
| 35 | COIL_STATE_12_BIT_NUMBER, | |||
| 36 | "coilState12", | |||
| 37 | "" | |||
| 38 | 1 | ); | ||
| 39 | ||||
| 40 |
1/1✓ Branch 2 taken 1 time.
|
1 | m_buffer.fill(0xAA); | |
| 41 |
5/10✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 time.
✗ Branch 31 not taken.
✗ Branch 36 not taken.
✗ Branch 39 not taken.
✓ Branch 46 taken 1 time.
✗ Branch 47 not taken.
|
1 | ASSERT_THAT(m_buffer, ::testing::ElementsAre(0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA)); | |
| 42 | } | |||
| 43 | ||||
| 44 | 3 | void BitLoggerFieldTest::checkBitLoggerField(const bool expected, const char* const context) { | ||
| 45 |
3/8✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 3 times.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 25 not taken.
✗ Branch 28 not taken.
|
6 | EXPECT_EQ(1u, m_logField->writeData(m_buffer.data(), nullptr)) << context; | |
| 46 |
6/10✓ Branch 9 taken 1 time.
✓ Branch 10 taken 2 times.
✓ Branch 12 taken 3 times.
✓ Branch 15 taken 3 times.
✓ Branch 18 taken 3 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 3 times.
✗ Branch 33 not taken.
✗ Branch 38 not taken.
✗ Branch 41 not taken.
|
3 | EXPECT_THAT(m_buffer, ::testing::ElementsAre((expected ? 0x01 : 0x00), 0xAA, 0xAA, 0xAA, 0xAA, 0xAA)); | |
| 47 | 3 | } | ||
| 48 | ||||
| 49 | 2 | void BitLoggerFieldTest::updateTestBit(const bool value) { | ||
| 50 | 2 | m_testOutputChannels->coilState12 = value; | ||
| 51 | 2 | } | ||
| 52 | ||||
| 53 | // If the following test fails, at first please check that in `output_channels_generated.h` header: | |||
| 54 | // 1. bits block with `coilState12` bit precedes `outputRequestPeriod` field | |||
| 55 | // 2. `coilState12` bit number equals `BitLoggerFieldTest::COIL_STATE_12_BIT_NUMBER` constant | |||
| 56 | 4 | TEST_F(BitLoggerFieldTest, checkBitSwitching) { | ||
| 57 | 1 | checkBitLoggerField(false, "default"); | ||
| 58 | ||||
| 59 | 1 | updateTestBit(true); | ||
| 60 | 1 | checkBitLoggerField(true, "bit is on"); | ||
| 61 | ||||
| 62 | 1 | updateTestBit(false); | ||
| 63 | 1 | checkBitLoggerField(false, "bit is off"); | ||
| 64 | 1 | } | ||
| 65 | } | |||
| 66 |