Skip to content

Commit 579de6d

Browse files
committed
Add isolated unit tests for RingBuffer
1 parent 99bf27e commit 579de6d

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

src/unit_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_subdirectory(note_column_line_container_helper_test)
2424
add_subdirectory(note_column_model_test)
2525
add_subdirectory(note_column_model_handler_test)
2626
add_subdirectory(recent_files_model_test)
27+
add_subdirectory(ring_buffer_test)
2728
add_subdirectory(midi_exporter_test)
2829
add_subdirectory(midi_importer_test)
2930
add_subdirectory(player_worker_test)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
2+
set(NAME ring_buffer_test)
3+
set(SRC
4+
${NAME}.cpp
5+
${NAME}.hpp
6+
)
7+
qt_add_executable(${NAME} ${SRC})
8+
set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${UNIT_TEST_BASE_DIR})
9+
add_test(${NAME} ${UNIT_TEST_BASE_DIR}/${NAME})
10+
target_link_libraries(${NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Test)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// This file is part of Noteahead.
2+
// Copyright (C) 2026 Jussi Lind <jussi.lind@iki.fi>
3+
//
4+
// Noteahead is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
// Noteahead is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Noteahead. If not, see <http://www.gnu.org/licenses/>.
15+
16+
#include "ring_buffer_test.hpp"
17+
#include "../../infra/audio/ring_buffer.hpp"
18+
19+
#include <QtTest>
20+
#include <vector>
21+
22+
namespace noteahead {
23+
24+
void RingBufferTest::test_pushPop_basic()
25+
{
26+
RingBuffer<int> rb(10);
27+
const std::vector<int> input = { 1, 2, 3, 4, 5 };
28+
29+
QVERIFY(rb.push(input.data(), input.size()));
30+
QCOMPARE(rb.readAvailable(), 5u);
31+
32+
std::vector<int> output(5);
33+
QCOMPARE(rb.pop(output.data(), 5), 5u);
34+
QCOMPARE(rb.readAvailable(), 0u);
35+
36+
for (size_t i = 0; i < input.size(); ++i) {
37+
QCOMPARE(output[i], input[i]);
38+
}
39+
}
40+
41+
void RingBufferTest::test_capacity_and_available()
42+
{
43+
// Note: RingBuffer keeps one slot empty to distinguish full from empty
44+
RingBuffer<int> rb(5);
45+
QCOMPARE(rb.writeAvailable(), 4u);
46+
QCOMPARE(rb.readAvailable(), 0u);
47+
48+
int val = 42;
49+
rb.push(&val, 1);
50+
QCOMPARE(rb.writeAvailable(), 3u);
51+
QCOMPARE(rb.readAvailable(), 1u);
52+
}
53+
54+
void RingBufferTest::test_wrapping()
55+
{
56+
RingBuffer<int> rb(5); // Internal capacity is 5, usable is 4
57+
58+
std::vector<int> data = { 1, 2, 3 };
59+
rb.push(data.data(), 3);
60+
61+
std::vector<int> dummy(2);
62+
rb.pop(dummy.data(), 2); // tail is now at 2, head at 3
63+
64+
// Now push 2 more items, this should wrap around the end
65+
std::vector<int> wrapData = { 4, 5 };
66+
QVERIFY(rb.push(wrapData.data(), 2));
67+
68+
std::vector<int> finalOutput(3);
69+
QCOMPARE(rb.pop(finalOutput.data(), 3), 3u);
70+
71+
QCOMPARE(finalOutput[0], 3);
72+
QCOMPARE(finalOutput[1], 4);
73+
QCOMPARE(finalOutput[2], 5);
74+
}
75+
76+
void RingBufferTest::test_overflow()
77+
{
78+
RingBuffer<int> rb(5); // usable 4
79+
std::vector<int> data = { 1, 2, 3, 4, 5 };
80+
81+
QVERIFY(!rb.push(data.data(), 5));
82+
QVERIFY(rb.push(data.data(), 4));
83+
}
84+
85+
void RingBufferTest::test_clear()
86+
{
87+
RingBuffer<int> rb(10);
88+
int data[3] = { 1, 2, 3 };
89+
rb.push(data, 3);
90+
QCOMPARE(rb.readAvailable(), 3u);
91+
92+
rb.clear();
93+
QCOMPARE(rb.readAvailable(), 0u);
94+
QCOMPARE(rb.writeAvailable(), 9u);
95+
}
96+
97+
} // namespace noteahead
98+
99+
QTEST_GUILESS_MAIN(noteahead::RingBufferTest)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of Noteahead.
2+
// Copyright (C) 2026 Jussi Lind <jussi.lind@iki.fi>
3+
//
4+
// Noteahead is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
// Noteahead is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Noteahead. If not, see <http://www.gnu.org/licenses/>.
15+
16+
#ifndef RING_BUFFER_TEST_HPP
17+
#define RING_BUFFER_TEST_HPP
18+
19+
#include <QObject>
20+
21+
namespace noteahead {
22+
23+
class RingBufferTest : public QObject
24+
{
25+
Q_OBJECT
26+
27+
private slots:
28+
void test_pushPop_basic();
29+
void test_capacity_and_available();
30+
void test_wrapping();
31+
void test_overflow();
32+
void test_clear();
33+
};
34+
35+
} // namespace noteahead
36+
37+
#endif // RING_BUFFER_TEST_HPP

0 commit comments

Comments
 (0)