|
| 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) |
0 commit comments