LCOV - code coverage report
Current view: top level - unit_tests/tests/util - test_buffered_writer.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 41 41 100.0 %
Date: 2024-04-25 02:23:43 Functions: 28 28 100.0 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "buffered_writer.h"
       4             : 
       5             : using ::testing::_;
       6             : using ::testing::Return;
       7             : using ::testing::StrictMock;
       8             : 
       9             : template <int TBufferSize>
      10             : struct MockBufferedWriter : public BufferedWriter<TBufferSize>
      11             : {
      12          14 :         MOCK_METHOD(size_t, writeInternal, (const char*, size_t), (override));
      13             : };
      14             : 
      15             : static const char* testBuffer = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      16             : 
      17           4 : TEST(BufferedWriter, WriteSmall) {
      18             :         // No calls to dut expected
      19           1 :         StrictMock<MockBufferedWriter<10>> dut;
      20             : 
      21           1 :         EXPECT_EQ(0, dut.write(testBuffer, 5));
      22           2 : }
      23             : 
      24           4 : TEST(BufferedWriter, WriteSmallFlush) {
      25           1 :         StrictMock<MockBufferedWriter<10>> dut;
      26           1 :         EXPECT_CALL(dut, writeInternal(_, 5)).WillOnce(Return(5));
      27             : 
      28           1 :         ASSERT_EQ(0, dut.write(testBuffer, 5));
      29             : 
      30           1 :         EXPECT_EQ(dut.flush(), 5);
      31           1 : }
      32             : 
      33           4 : TEST(BufferedWriter, WriteMultipleSmall) {
      34           1 :         StrictMock<MockBufferedWriter<10>> dut;
      35             :         {
      36           1 :                 EXPECT_CALL(dut, writeInternal(_, 10)).WillOnce(Return(10));
      37           1 :                 EXPECT_CALL(dut, writeInternal(_, 2)).WillOnce(Return(2));
      38             :         }
      39             : 
      40           1 :         EXPECT_EQ(0, dut.write(testBuffer, 3));
      41           1 :         EXPECT_EQ(0, dut.write(testBuffer, 3));
      42           1 :         EXPECT_EQ(0, dut.write(testBuffer, 3));
      43           1 :         EXPECT_EQ(10, dut.write(testBuffer, 3));        // <- this one should trigger a flush
      44             : 
      45             :         // Flush the remainder
      46           1 :         EXPECT_EQ(dut.flush(), 2);
      47           2 : }
      48             : 
      49           4 : TEST(BufferedWriter, WriteSingleFullBufferSize) {
      50           1 :         StrictMock<MockBufferedWriter<50>> dut;
      51             : 
      52           1 :         EXPECT_CALL(dut, writeInternal(_, 50)).WillOnce(Return(50));
      53             : 
      54           1 :         EXPECT_EQ(50, dut.write(testBuffer, 50));
      55             : 
      56             :         // Nothing left to flush!
      57           1 :         EXPECT_EQ(0, dut.flush());
      58           2 : }
      59             : 
      60           4 : TEST(BufferedWriter, WriteLarge) {
      61           1 :         StrictMock<MockBufferedWriter<10>> dut;
      62             : 
      63             :         {
      64           1 :                 EXPECT_CALL(dut, writeInternal(_, 45)).WillOnce(Return(45));
      65             :         }
      66             : 
      67           1 :         EXPECT_EQ(45, dut.write(testBuffer, 45));
      68             : 
      69           1 :         EXPECT_EQ(0, dut.flush());
      70           2 : }
      71             : 
      72           4 : TEST(BufferedWriter, WriteLargeAfterSmall) {
      73           1 :         StrictMock<MockBufferedWriter<10>> dut;
      74             : 
      75             :         {
      76           1 :                 EXPECT_CALL(dut, writeInternal(_, 10)).WillOnce(Return(10));
      77           1 :                 EXPECT_CALL(dut, writeInternal(_, 36)).WillOnce(Return(36));
      78             :         }
      79             : 
      80           1 :         EXPECT_EQ(0, dut.write(testBuffer, 1));
      81             : 
      82           1 :         EXPECT_EQ(46, dut.write(testBuffer, 45));
      83             : 
      84           1 :         EXPECT_EQ(0, dut.flush());
      85           2 : }

Generated by: LCOV version 1.14