|
| 1 | +#include "hal/interfaces/test_doubles/FlashMock.hpp" |
| 2 | +#include "infra/util/test_helper/MemoryRangeMatcher.hpp" |
| 3 | +#include "infra/util/test_helper/MockCallback.hpp" |
| 4 | +#include "postmaster/programmer/FlashAligner.hpp" |
| 5 | +#include "gtest/gtest.h" |
| 6 | + |
| 7 | +class FlashAlignerTest |
| 8 | + : public testing::Test |
| 9 | +{ |
| 10 | +public: |
| 11 | + testing::StrictMock<hal::CleanFlashMock> flash; |
| 12 | + services::FlashAligner::WithAlignment<4> aligner{ flash }; |
| 13 | + |
| 14 | + std::array<uint8_t, 1> size1{ 99 }; |
| 15 | + std::array<uint8_t, 4> size4{ 1, 2, 3, 4 }; |
| 16 | + std::array<uint8_t, 6> size6{ 5, 6, 7, 8, 9, 10 }; |
| 17 | + std::array<uint8_t, 8> size8{ 11, 12, 13, 14, 15, 16, 17, 18 }; |
| 18 | +}; |
| 19 | + |
| 20 | +TEST_F(FlashAlignerTest, write_one_block) |
| 21 | +{ |
| 22 | + EXPECT_CALL(flash, WriteBuffer(infra::MakeConstRange(size4), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 23 | + aligner.WriteBuffer(size4, 0, infra::MockFunction<void()>()); |
| 24 | + |
| 25 | + aligner.Flush(infra::MockFunction<void()>()); |
| 26 | +} |
| 27 | + |
| 28 | +TEST_F(FlashAlignerTest, write_small_block) |
| 29 | +{ |
| 30 | + aligner.WriteBuffer(size1, 0, infra::MockFunction<void()>()); |
| 31 | + |
| 32 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 99, 0, 0, 0 }), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 33 | + aligner.Flush(infra::MockFunction<void()>()); |
| 34 | +} |
| 35 | + |
| 36 | +TEST_F(FlashAlignerTest, write_large_block) |
| 37 | +{ |
| 38 | + EXPECT_CALL(flash, WriteBuffer(infra::Head(infra::MakeConstRange(size6), 4), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 39 | + aligner.WriteBuffer(size6, 0, infra::MockFunction<void()>()); |
| 40 | + |
| 41 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 9, 10, 0, 0}), 4, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 42 | + aligner.Flush(infra::MockFunction<void()>()); |
| 43 | +} |
| 44 | + |
| 45 | +TEST_F(FlashAlignerTest, write_double_block) |
| 46 | +{ |
| 47 | + EXPECT_CALL(flash, WriteBuffer(infra::MakeConstRange(size8), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 48 | + aligner.WriteBuffer(size8, 0, infra::MockFunction<void()>()); |
| 49 | + |
| 50 | + aligner.Flush(infra::MockFunction<void()>()); |
| 51 | +} |
| 52 | + |
| 53 | +TEST_F(FlashAlignerTest, write_one_block_at_offset) |
| 54 | +{ |
| 55 | + aligner.WriteBuffer(size1, 0, infra::MockFunction<void()>()); |
| 56 | + |
| 57 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 99, 1, 2, 3 }), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 58 | + aligner.WriteBuffer(size4, 1, infra::MockFunction<void()>()); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(FlashAlignerTest, write_small_block_at_offset) |
| 62 | +{ |
| 63 | + aligner.WriteBuffer(size1, 0, infra::MockFunction<void()>()); |
| 64 | + aligner.WriteBuffer(size1, 1, infra::MockFunction<void()>()); |
| 65 | + |
| 66 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 99, 99, 0, 0 }), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 67 | + aligner.Flush(infra::MockFunction<void()>()); |
| 68 | +} |
| 69 | + |
| 70 | +TEST_F(FlashAlignerTest, write_large_block_at_offset) |
| 71 | +{ |
| 72 | + aligner.WriteBuffer(size1, 0, infra::MockFunction<void()>()); |
| 73 | + |
| 74 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 99, 5, 6, 7 }), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 75 | + aligner.WriteBuffer(size6, 1, infra::MockFunction<void()>()); |
| 76 | + |
| 77 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 8, 9, 10, 0 }), 4, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 78 | + aligner.Flush(infra::MockFunction<void()>()); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_F(FlashAlignerTest, write_double_block_at_offset) |
| 82 | +{ |
| 83 | + aligner.WriteBuffer(size1, 0, infra::MockFunction<void()>()); |
| 84 | + |
| 85 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 99, 11, 12, 13 }), 0, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 86 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 14, 15, 16, 17 }), 4, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 87 | + aligner.WriteBuffer(size8, 1, infra::MockFunction<void()>()); |
| 88 | + |
| 89 | + EXPECT_CALL(flash, WriteBuffer(infra::ContentsEqual(std::vector<uint8_t>{ 18, 0, 0, 0 }), 8, testing::_)).WillOnce(testing::InvokeArgument<2>()); |
| 90 | + aligner.Flush(infra::MockFunction<void()>()); |
| 91 | +} |
0 commit comments