|
| 1 | +#include "loopsidecar.h" |
| 2 | + |
| 3 | +#include <QtTest/QTest> |
| 4 | + |
| 5 | +class test_LoopSidecar : public QObject |
| 6 | +{ |
| 7 | + Q_OBJECT |
| 8 | + |
| 9 | +private: |
| 10 | + // 44100 Hz, 16-bit stereo: 1 second = 44100 samples = 176400 bytes. |
| 11 | + // The parser's legacy form takes sample counts, so a value of 44100 → 1000 ms. |
| 12 | + static constexpr int SAMPLE_RATE = 44100; |
| 13 | + |
| 14 | + static std::function<int()> fixedRate(int rate) |
| 15 | + { |
| 16 | + return [rate]() { |
| 17 | + return rate; |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | +private Q_SLOTS: |
| 22 | + void emptyInput() |
| 23 | + { |
| 24 | + LoopPoints lp = parseLoopSidecarText("", fixedRate(SAMPLE_RATE)); |
| 25 | + QCOMPARE(lp.start_ms, qint64(0)); |
| 26 | + QCOMPARE(lp.end_ms, qint64(0)); |
| 27 | + } |
| 28 | + |
| 29 | + void onlyWhitespace() |
| 30 | + { |
| 31 | + LoopPoints lp = parseLoopSidecarText("\n\n \n", fixedRate(SAMPLE_RATE)); |
| 32 | + QCOMPARE(lp.start_ms, qint64(0)); |
| 33 | + QCOMPARE(lp.end_ms, qint64(0)); |
| 34 | + } |
| 35 | + |
| 36 | + void secondsFormStartEnd() |
| 37 | + { |
| 38 | + LoopPoints lp = parseLoopSidecarText("seconds=true\nloop_start=2.5\nloop_end=10", fixedRate(SAMPLE_RATE)); |
| 39 | + QCOMPARE(lp.start_ms, qint64(2500)); |
| 40 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 41 | + } |
| 42 | + |
| 43 | + void secondsFormStartLength() |
| 44 | + { |
| 45 | + LoopPoints lp = parseLoopSidecarText("seconds=true\nloop_start=2.5\nloop_length=7.5", fixedRate(SAMPLE_RATE)); |
| 46 | + QCOMPARE(lp.start_ms, qint64(2500)); |
| 47 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 48 | + } |
| 49 | + |
| 50 | + void legacyFormStartEnd() |
| 51 | + { |
| 52 | + // 44100 samples = 1 second at 44.1 kHz; 88200 samples = 2 seconds. |
| 53 | + LoopPoints lp = parseLoopSidecarText("loop_start=44100\nloop_end=88200", fixedRate(SAMPLE_RATE)); |
| 54 | + QCOMPARE(lp.start_ms, qint64(1000)); |
| 55 | + QCOMPARE(lp.end_ms, qint64(2000)); |
| 56 | + } |
| 57 | + |
| 58 | + void legacyFormStartLength() |
| 59 | + { |
| 60 | + LoopPoints lp = parseLoopSidecarText("loop_start=22050\nloop_length=22050", fixedRate(SAMPLE_RATE)); |
| 61 | + QCOMPARE(lp.start_ms, qint64(500)); |
| 62 | + QCOMPARE(lp.end_ms, qint64(1000)); |
| 63 | + } |
| 64 | + |
| 65 | + void explicitSecondsFalseIsLegacy() |
| 66 | + { |
| 67 | + LoopPoints lp = parseLoopSidecarText("seconds=false\nloop_start=44100\nloop_end=88200", fixedRate(SAMPLE_RATE)); |
| 68 | + QCOMPARE(lp.start_ms, qint64(1000)); |
| 69 | + QCOMPARE(lp.end_ms, qint64(2000)); |
| 70 | + } |
| 71 | + |
| 72 | + void secondsFormSkipsProbe() |
| 73 | + { |
| 74 | + int probeCalls = 0; |
| 75 | + auto provider = [&]() { |
| 76 | + ++probeCalls; |
| 77 | + return SAMPLE_RATE; |
| 78 | + }; |
| 79 | + LoopPoints lp = parseLoopSidecarText("seconds=true\nloop_start=1\nloop_end=2", provider); |
| 80 | + QCOMPARE(lp.start_ms, qint64(1000)); |
| 81 | + QCOMPARE(lp.end_ms, qint64(2000)); |
| 82 | + QCOMPARE(probeCalls, 0); |
| 83 | + } |
| 84 | + |
| 85 | + void legacyFormProbesAtMostOnce() |
| 86 | + { |
| 87 | + int probeCalls = 0; |
| 88 | + auto provider = [&]() { |
| 89 | + ++probeCalls; |
| 90 | + return SAMPLE_RATE; |
| 91 | + }; |
| 92 | + LoopPoints lp = parseLoopSidecarText("loop_start=44100\nloop_length=44100\nloop_end=132300", provider); |
| 93 | + QCOMPARE(lp.start_ms, qint64(1000)); |
| 94 | + QCOMPARE(lp.end_ms, qint64(3000)); // loop_end overrides the loop_length computation |
| 95 | + QCOMPARE(probeCalls, 1); |
| 96 | + } |
| 97 | + |
| 98 | + void probeFailureSilencesLegacyEntries() |
| 99 | + { |
| 100 | + LoopPoints lp = parseLoopSidecarText("loop_start=44100\nloop_end=88200", fixedRate(0)); |
| 101 | + QCOMPARE(lp.start_ms, qint64(0)); |
| 102 | + QCOMPARE(lp.end_ms, qint64(0)); |
| 103 | + } |
| 104 | + |
| 105 | + void nullProviderTreatedAsFailure() |
| 106 | + { |
| 107 | + LoopPoints lp = parseLoopSidecarText("loop_start=44100\nloop_end=88200", nullptr); |
| 108 | + QCOMPARE(lp.start_ms, qint64(0)); |
| 109 | + QCOMPARE(lp.end_ms, qint64(0)); |
| 110 | + } |
| 111 | + |
| 112 | + void malformedLinesSkipped() |
| 113 | + { |
| 114 | + QString input = "this is not a kv line\n" |
| 115 | + "seconds=true\n" |
| 116 | + "loop_start=2.5\n" |
| 117 | + "garbage_without_equals\n" |
| 118 | + "loop_end=10\n"; |
| 119 | + LoopPoints lp = parseLoopSidecarText(input, fixedRate(SAMPLE_RATE)); |
| 120 | + QCOMPARE(lp.start_ms, qint64(2500)); |
| 121 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 122 | + } |
| 123 | + |
| 124 | + void unknownKeysIgnored() |
| 125 | + { |
| 126 | + QString input = "seconds=true\n" |
| 127 | + "loop_start=2.5\n" |
| 128 | + "something_else=42\n" |
| 129 | + "loop_end=10\n"; |
| 130 | + LoopPoints lp = parseLoopSidecarText(input, fixedRate(SAMPLE_RATE)); |
| 131 | + QCOMPARE(lp.start_ms, qint64(2500)); |
| 132 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 133 | + } |
| 134 | + |
| 135 | + void whitespaceAroundKvIsTrimmed() |
| 136 | + { |
| 137 | + QString input = " seconds = true \n" |
| 138 | + " loop_start = 2.5\n" |
| 139 | + "loop_end= 10 \n"; |
| 140 | + LoopPoints lp = parseLoopSidecarText(input, fixedRate(SAMPLE_RATE)); |
| 141 | + QCOMPARE(lp.start_ms, qint64(2500)); |
| 142 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 143 | + } |
| 144 | + |
| 145 | + void lastLoopStartWins() |
| 146 | + { |
| 147 | + QString input = "seconds=true\n" |
| 148 | + "loop_start=1\n" |
| 149 | + "loop_start=5\n" |
| 150 | + "loop_end=10\n"; |
| 151 | + LoopPoints lp = parseLoopSidecarText(input, fixedRate(SAMPLE_RATE)); |
| 152 | + QCOMPARE(lp.start_ms, qint64(5000)); |
| 153 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 154 | + } |
| 155 | + |
| 156 | + void secondsToggleMidStreamApplies() |
| 157 | + { |
| 158 | + // Mode change mid-file: loop_start uses legacy (sample count at 44.1k), |
| 159 | + // then `seconds=true` flips the mode, and loop_end uses seconds. |
| 160 | + QString input = "loop_start=44100\n" |
| 161 | + "seconds=true\n" |
| 162 | + "loop_end=10\n"; |
| 163 | + LoopPoints lp = parseLoopSidecarText(input, fixedRate(SAMPLE_RATE)); |
| 164 | + QCOMPARE(lp.start_ms, qint64(1000)); |
| 165 | + QCOMPARE(lp.end_ms, qint64(10000)); |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +#include "test/test_loopsidecar.moc" |
| 170 | + |
| 171 | +QTEST_APPLESS_MAIN(test_LoopSidecar) |
0 commit comments