GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 79.4% 50 / 0 / 63
Functions: 100.0% 4 / 0 / 4
Branches: 76.9% 20 / 0 / 26
Decisions: 75.0% 6 / - / 8

firmware/controllers/system/buffered_writer.h
Line Branch Decision Exec Source
1 /**
2 * @file buffered_writer.h
3 */
4
5 #pragma once
6
7 #include <cstring>
8
9 #include "writer.h"
10
11 template <int TBufferSize>
12 class BufferedWriter : public Writer {
13 public:
14 390765124 size_t write(const char* buffer, size_t count) override {
15 390765124 size_t bytesFlushed = 0;
16
17
6/6
BufferedWriter<10>::write(char const*, unsigned long):
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
BufferedWriter<50>::write(char const*, unsigned long):
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
BufferedWriter<512>::write(char const*, unsigned long):
✓ Branch 0 taken 391915795 times.
✓ Branch 1 taken 390765114 times.
2/2
✓ Decision 'true' taken 391915795 times.
✓ Decision 'false' taken 390765114 times.
782680931 while (count) {
18
9/12
BufferedWriter<10>::write(char const*, unsigned long):
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
BufferedWriter<50>::write(char const*, unsigned long):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
BufferedWriter<512>::write(char const*, unsigned long):
✓ Branch 0 taken 1696002 times.
✓ Branch 1 taken 390219793 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1696002 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 391915795 times.
391915807 if (m_bytesUsed == 0 && count >= TBufferSize) {
19 // special case: write-thru, skip the copy
20 3 bytesFlushed += writeInternal(buffer, count);
21 3 count = 0;
22
4/6
BufferedWriter<10>::write(char const*, unsigned long):
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
BufferedWriter<50>::write(char const*, unsigned long):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
BufferedWriter<512>::write(char const*, unsigned long):
✓ Branch 0 taken 390220369 times.
✓ Branch 1 taken 1695426 times.
2/2
✓ Decision 'true' taken 390220369 times.
✓ Decision 'false' taken 1695426 times.
391915804 } else if (m_bytesUsed + count < TBufferSize) {
23 // Write that will fit in the buffer, just copy to intermediate buffer
24 390220376 memcpy(m_buffer + m_bytesUsed, buffer, count);
25 390220376 m_bytesUsed += count;
26 390220376 count = 0;
27 } else {
28 // Need to write partial, then flush buffer
29 1695428 size_t bytesToWrite = TBufferSize - m_bytesUsed;
30 // Copy this block in to place
31 1695428 memcpy(m_buffer + m_bytesUsed, buffer, bytesToWrite);
32 1695428 m_bytesUsed += bytesToWrite;
33
34 // Flush to underlying
35 1695428 bytesFlushed += flush();
36 // Step the read pointer ahead
37 1695428 buffer += bytesToWrite;
38 // Decrement remaining bytes
39 1695428 count -= bytesToWrite;
40 }
41 }
42
43 390765124 return bytesFlushed;
44 }
45
46 // Flush the internal buffer to the underlying interface.
47 1696009 size_t flush() override {
48 1696009 size_t bytesToWrite = m_bytesUsed;
49
50
1/2
✓ Branch 0 taken 1696002 times.
✗ Branch 1 not taken.
1/2
✓ Decision 'true' taken 1696002 times.
✗ Decision 'false' not taken.
1696009 if (bytesToWrite > 0) {
51 1696006 m_bytesUsed = 0;
52 1696006 return writeInternal(m_buffer, bytesToWrite);
53 } else {
54 3 return 0;
55 }
56 }
57
58 protected:
59 virtual size_t writeInternal(const char* buffer, size_t count) = 0;
60
61 private:
62 char m_buffer[TBufferSize];
63 size_t m_bytesUsed = 0;
64 };
65