Skip to content

Commit faa7bad

Browse files
committed
cleanup: Dedupe RingBuffer implementation in mocks
1 parent a9e0db5 commit faa7bad

File tree

6 files changed

+15
-213
lines changed

6 files changed

+15
-213
lines changed

test/mocks/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ add_library(test-mocks STATIC
44
test-mocks.cpp
55
test-mocks.h
66
test-mocks_Namespace.h
7-
test-mocks_Defs.h
87
test-mocks_SerialMock.cpp
98
test-mocks_SerialMock.hpp
109
test-mocks_SerialMock.h
1110
)
11+
12+
target_link_libraries(test-mocks
13+
midi
14+
)

test/mocks/test-mocks_Defs.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/mocks/test-mocks_SerialMock.h

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11
#pragma once
22

33
#include "test-mocks.h"
4-
#include "test-mocks_Defs.h"
4+
#include <inttypes.h>
5+
#include <src/midi_RingBuffer.h>
56

67
BEGIN_TEST_MOCKS_NAMESPACE
78

8-
template<typename DataType, int Size>
9-
class RingBuffer
10-
{
11-
public:
12-
RingBuffer();
13-
~RingBuffer();
14-
15-
public:
16-
int getLength() const;
17-
bool isEmpty() const;
18-
19-
public:
20-
void write(DataType inData);
21-
void write(const DataType* inData, int inSize);
22-
void clear();
23-
24-
public:
25-
DataType peek() const;
26-
DataType read();
27-
void read(DataType* outData, int inSize);
28-
29-
private:
30-
DataType mData[Size];
31-
DataType* mWriteHead;
32-
DataType* mReadHead;
33-
};
34-
35-
// -----------------------------------------------------------------------------
36-
379
template<int BufferSize>
3810
class SerialMock
3911
{
@@ -44,14 +16,14 @@ class SerialMock
4416
public: // Arduino Serial API
4517
void begin(int inBaudrate);
4618
int available() const;
47-
void write(uint8 inData);
48-
uint8 read();
19+
void write(uint8_t inData);
20+
uint8_t read();
4921

5022
public: // Test Helpers API
5123
void moveTxToRx(); // Simulate loopback
5224

5325
public:
54-
typedef RingBuffer<uint8, BufferSize> Buffer;
26+
typedef midi::RingBuffer<uint8_t, BufferSize> Buffer;
5527
Buffer mTxBuffer;
5628
Buffer mRxBuffer;
5729
int mBaudrate;

test/mocks/test-mocks_SerialMock.hpp

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,6 @@
22

33
BEGIN_TEST_MOCKS_NAMESPACE
44

5-
template<typename DataType, int Size>
6-
RingBuffer<DataType, Size>::RingBuffer()
7-
: mWriteHead(mData)
8-
, mReadHead(mData)
9-
{
10-
memset(mData, DataType(0), Size * sizeof(DataType));
11-
}
12-
13-
template<typename DataType, int Size>
14-
RingBuffer<DataType, Size>::~RingBuffer()
15-
{
16-
}
17-
18-
// -----------------------------------------------------------------------------
19-
20-
template<typename DataType, int Size>
21-
int RingBuffer<DataType, Size>::getLength() const
22-
{
23-
if (mReadHead == mWriteHead)
24-
{
25-
return 0;
26-
}
27-
else if (mWriteHead > mReadHead)
28-
{
29-
return int(mWriteHead - mReadHead);
30-
}
31-
else
32-
{
33-
return int(mWriteHead - mData) + Size - int(mReadHead - mData);
34-
}
35-
}
36-
37-
template<typename DataType, int Size>
38-
bool RingBuffer<DataType, Size>::isEmpty() const
39-
{
40-
return mReadHead == mWriteHead;
41-
}
42-
43-
// -----------------------------------------------------------------------------
44-
45-
template<typename DataType, int Size>
46-
void RingBuffer<DataType, Size>::write(DataType inData)
47-
{
48-
*mWriteHead++ = inData;
49-
if (mWriteHead >= mData + Size)
50-
{
51-
mWriteHead = mData;
52-
}
53-
}
54-
55-
template<typename DataType, int Size>
56-
void RingBuffer<DataType, Size>::write(const DataType* inData, int inSize)
57-
{
58-
for (int i = 0; i < inSize; ++i)
59-
{
60-
write(inData[i]);
61-
}
62-
}
63-
64-
template<typename DataType, int Size>
65-
void RingBuffer<DataType, Size>::clear()
66-
{
67-
memset(mData, DataType(0), Size * sizeof(DataType));
68-
mReadHead = mData;
69-
mWriteHead = mData;
70-
}
71-
72-
// -----------------------------------------------------------------------------
73-
74-
template<typename DataType, int Size>
75-
DataType RingBuffer<DataType, Size>::peek() const
76-
{
77-
return *mReadHead;
78-
}
79-
80-
template<typename DataType, int Size>
81-
DataType RingBuffer<DataType, Size>::read()
82-
{
83-
const DataType data = *mReadHead++;
84-
if (mReadHead >= mData + Size)
85-
{
86-
mReadHead = mData;
87-
}
88-
return data;
89-
}
90-
91-
template<typename DataType, int Size>
92-
void RingBuffer<DataType, Size>::read(DataType* outData, int inSize)
93-
{
94-
for (int i = 0; i < inSize; ++i)
95-
{
96-
outData[i] = read();
97-
}
98-
}
99-
100-
// =============================================================================
101-
1025
template<int BufferSize>
1036
SerialMock<BufferSize>::SerialMock()
1047
{
@@ -126,13 +29,13 @@ int SerialMock<BufferSize>::available() const
12629
}
12730

12831
template<int BufferSize>
129-
void SerialMock<BufferSize>::write(uint8 inData)
32+
void SerialMock<BufferSize>::write(uint8_t inData)
13033
{
13134
mTxBuffer.write(inData);
13235
}
13336

13437
template<int BufferSize>
135-
uint8 SerialMock<BufferSize>::read()
38+
uint8_t SerialMock<BufferSize>::read()
13639
{
13740
return mRxBuffer.read();
13841
}

test/unit-tests/CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ include(CMakeToolsHelpers OPTIONAL)
33
project(unit-tests)
44

55
include_directories(
6-
${unit-tests_SOURCE_DIR}
7-
${gtest_SOURCE_DIR}/include
8-
${gmock_SOURCE_DIR}/include
9-
"../../external/midi-usb/src"
6+
"${unit-tests_SOURCE_DIR}"
7+
"${gtest_SOURCE_DIR}/include"
8+
"${gmock_SOURCE_DIR}/include"
9+
"${ROOT_SOURCE_DIR}/external/midi-usb/src"
1010
)
1111

1212
add_executable(unit-tests
13-
1413
unit-tests.cpp
1514
unit-tests.h
1615
unit-tests_Namespace.h
@@ -20,7 +19,6 @@ add_executable(unit-tests
2019
tests/unit-tests_Settings.h
2120
tests/unit-tests_SysExCodec.cpp
2221
tests/unit-tests_RingBuffer.cpp
23-
tests/unit-tests_SerialMock.cpp
2422
tests/unit-tests_MidiInput.cpp
2523
tests/unit-tests_MidiInputCallbacks.cpp
2624
tests/unit-tests_MidiOutput.cpp

test/unit-tests/tests/unit-tests_SerialMock.cpp

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)