Skip to content

Commit dbf9a6c

Browse files
committed
Refactor Synth serialization tests
1 parent db5a1d1 commit dbf9a6c

4 files changed

Lines changed: 113 additions & 50 deletions

File tree

src/unit_tests/synth_test/synth_test.cpp

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void SynthTest::cleanupTestCase()
3535

3636
void SynthTest::test_defaultValues_shouldBeCorrect()
3737
{
38-
SynthDevice synth { "Test Synth" };
38+
const SynthDevice synth { "Test Synth" };
3939
QCOMPARE(synth.name(), std::string("Test Synth"));
4040
QCOMPARE(synth.vco1Octave(), 0);
4141
QCOMPARE(synth.vco1Pitch(), 0);
@@ -269,48 +269,6 @@ void SynthTest::test_reset_shouldRestoreDefaults()
269269
QCOMPARE(synth.lpfCutoff(), 1.0f);
270270
}
271271

272-
void SynthTest::test_serialization_shouldPreserveValues()
273-
{
274-
SynthDevice synth1 { "Test Synth 1" };
275-
synth1.setVco1Octave(1);
276-
synth1.setMixVco2(0.75f);
277-
synth1.setLpfCutoff(0.3f);
278-
synth1.setAmpAttack(0.2f);
279-
synth1.setMultiType(MultiEngine::Type::Decim);
280-
synth1.setMultiShape(0.42f);
281-
synth1.setMultiLevel(0.88f);
282-
synth1.setMultiKeyTrack(0.5f);
283-
synth1.setMasterPan(0.12f);
284-
285-
QByteArray data;
286-
QBuffer buffer(&data);
287-
buffer.open(QIODevice::WriteOnly);
288-
QXmlStreamWriter writer(&buffer);
289-
synth1.serializeToXml(writer);
290-
buffer.close();
291-
292-
SynthDevice synth2 { "Test Synth 2" };
293-
QBuffer readBuffer(&data);
294-
readBuffer.open(QIODevice::ReadOnly);
295-
QXmlStreamReader reader(&readBuffer);
296-
297-
while (!reader.atEnd() && !reader.isStartElement()) {
298-
reader.readNext();
299-
}
300-
301-
synth2.deserializeFromXml(reader);
302-
303-
QCOMPARE(synth2.vco1Octave(), 1);
304-
QCOMPARE(synth2.mixVco2(), 0.75f);
305-
QCOMPARE(synth2.lpfCutoff(), 0.3f);
306-
QCOMPARE(synth2.ampAttack(), 0.2f);
307-
QCOMPARE(synth2.multiType(), MultiEngine::Type::Decim);
308-
QCOMPARE(synth2.multiShape(), 0.42f);
309-
QCOMPARE(synth2.multiLevel(), 0.88f);
310-
QCOMPARE(synth2.multiKeyTrack(), 0.5f);
311-
QCOMPARE(synth2.masterPan(), 0.12f);
312-
}
313-
314272
void SynthTest::test_portamento_shouldGlideFrequency()
315273
{
316274
SynthDevice synth { "Test Synth" };
@@ -359,6 +317,45 @@ void SynthTest::test_portamento_shouldGlideFrequency()
359317
}
360318
}
361319

320+
void SynthTest::test_parameterDiscreteFlag_shouldReturnCorrectDiscreteState()
321+
{
322+
const SynthDevice synth { "Test Synth" };
323+
324+
// Test discrete parameters
325+
const auto vco1Wave = synth.parameter(Constants::NahdXml::xmlKeySynthVco1Waveform().toStdString());
326+
QVERIFY(vco1Wave.has_value());
327+
QVERIFY(vco1Wave->get().isDiscrete());
328+
329+
const auto vco1Octave = synth.parameter(Constants::NahdXml::xmlKeySynthVco1Octave().toStdString());
330+
QVERIFY(vco1Octave.has_value());
331+
QVERIFY(vco1Octave->get().isDiscrete());
332+
333+
const auto vco1Pitch = synth.parameter(Constants::NahdXml::xmlKeySynthVco1Pitch().toStdString());
334+
QVERIFY(vco1Pitch.has_value());
335+
QVERIFY(vco1Pitch->get().isDiscrete());
336+
337+
const auto modTarget = synth.parameter(Constants::NahdXml::xmlKeySynthModTarget().toStdString());
338+
QVERIFY(modTarget.has_value());
339+
QVERIFY(modTarget->get().isDiscrete());
340+
341+
const auto voiceMode = synth.parameter(Constants::NahdXml::xmlKeyVoiceMode().toStdString());
342+
QVERIFY(voiceMode.has_value());
343+
QVERIFY(voiceMode->get().isDiscrete());
344+
345+
// Test continuous parameters
346+
const auto lpfCutoff = synth.parameter(Constants::NahdXml::xmlKeySynthLpfCutoff().toStdString());
347+
QVERIFY(lpfCutoff.has_value());
348+
QVERIFY(!lpfCutoff->get().isDiscrete());
349+
350+
const auto ampAttack = synth.parameter(Constants::NahdXml::xmlKeySynthAmpAttack().toStdString());
351+
QVERIFY(ampAttack.has_value());
352+
QVERIFY(!ampAttack->get().isDiscrete());
353+
354+
const auto multiShape = synth.parameter(Constants::NahdXml::xmlKeySynthMultiShape().toStdString());
355+
QVERIFY(multiShape.has_value());
356+
QVERIFY(!multiShape->get().isDiscrete());
357+
}
358+
362359
} // namespace noteahead
363360

364361
QTEST_GUILESS_MAIN(noteahead::SynthTest)

src/unit_tests/synth_test/synth_test.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ private slots:
3838
void test_voiceStealing_shouldStealQuietestVoice();
3939
void test_softClipper_shouldPreventClipping();
4040
void test_reset_shouldRestoreDefaults();
41-
void test_serialization_shouldPreserveValues();
4241
void test_portamento_shouldGlideFrequency();
42+
void test_parameterDiscreteFlag_shouldReturnCorrectDiscreteState();
4343
};
4444

4545
} // namespace noteahead

src/unit_tests/xml_serialization_test/xml_serialization_test.cpp

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "../../application/service/side_chain_service.hpp"
3030
#include "../../domain/column_settings.hpp"
3131
#include "../../domain/devices/sampler_device.hpp"
32+
#include "../../domain/devices/synth_device.hpp"
3233
#include "../../domain/instrument.hpp"
3334
#include "../../domain/note_data.hpp"
3435
#include "../../domain/song.hpp"
@@ -709,9 +710,9 @@ void XmlSerializationTest::test_toXmlFromXml_samplerDevice_shouldLoadSamplerDevi
709710
const std::string fileName = "test.wav";
710711
const auto samplerName = Constants::samplerDeviceName().toStdString();
711712

712-
auto engine = std::make_shared<AudioEngine>();
713+
const auto engine = std::make_shared<AudioEngine>();
713714
DeviceService deviceServiceOut { engine };
714-
auto samplerOut = std::make_shared<SamplerDevice>(samplerName, std::make_unique<MockAudioFileReader>());
715+
const auto samplerOut = std::make_shared<SamplerDevice>(samplerName, std::make_unique<MockAudioFileReader>());
715716
samplerOut->setId(42);
716717
samplerOut->loadSample(60, fileName);
717718
samplerOut->setSamplePan(60, 0.75f);
@@ -724,12 +725,12 @@ void XmlSerializationTest::test_toXmlFromXml_samplerDevice_shouldLoadSamplerDevi
724725

725726
const auto xml = editorServiceOut.toXml();
726727

727-
DeviceService deviceServiceIn { std::make_shared<AudioEngine>() };
728-
auto samplerIn = std::make_shared<SamplerDevice>(samplerName, std::make_unique<MockAudioFileReader>());
729-
deviceServiceIn.registerDevice(samplerIn);
728+
const auto deviceServiceIn = std::make_shared<DeviceService>(std::make_shared<AudioEngine>());
729+
const auto samplerIn = std::make_shared<SamplerDevice>(samplerName, std::make_unique<MockAudioFileReader>());
730+
deviceServiceIn->registerDevice(samplerIn);
730731

731732
EditorService editorServiceIn { std::make_shared<SelectionService>(), std::make_shared<SettingsService>(), std::make_shared<AutomationService>(std::make_shared<PropertyService>()) };
732-
connect(&editorServiceIn, &EditorService::devicesDeserializationRequested, &deviceServiceIn, &DeviceService::deserializeFromXml);
733+
connect(&editorServiceIn, &EditorService::devicesDeserializationRequested, deviceServiceIn.get(), &DeviceService::deserializeFromXml);
733734

734735
editorServiceIn.fromXml(xml);
735736

@@ -742,6 +743,70 @@ void XmlSerializationTest::test_toXmlFromXml_samplerDevice_shouldLoadSamplerDevi
742743
QCOMPARE(samplerIn->sampleCutoff(60), 0.4f);
743744
}
744745

746+
void XmlSerializationTest::test_toXmlFromXml_synthDevice_shouldPreserveValuesAndDiscreteFlags()
747+
{
748+
const auto synthName = "Test Synth";
749+
750+
const auto engine = std::make_shared<AudioEngine>();
751+
DeviceService deviceServiceOut { engine };
752+
const auto synthOut = std::make_shared<SynthDevice>(synthName);
753+
synthOut->setId(66);
754+
synthOut->setVco1Waveform(PolyBLEPOscillator::Waveform::Saw);
755+
synthOut->setVco1Octave(1);
756+
synthOut->setMixVco2(0.75f);
757+
synthOut->setLpfCutoff(0.3f);
758+
synthOut->setAmpAttack(0.2f);
759+
synthOut->setMultiType(MultiEngine::Type::Decim);
760+
synthOut->setMultiShape(0.42f);
761+
synthOut->setMultiLevel(0.88f);
762+
synthOut->setMultiKeyTrack(0.5f);
763+
synthOut->setMasterPan(0.12f);
764+
deviceServiceOut.registerDevice(synthOut);
765+
766+
EditorService editorServiceOut { std::make_shared<SelectionService>(), std::make_shared<SettingsService>(), std::make_shared<AutomationService>(std::make_shared<PropertyService>()) };
767+
connect(&editorServiceOut, &EditorService::devicesSerializationRequested, &deviceServiceOut, &DeviceService::serializeToXml);
768+
769+
const auto xml = editorServiceOut.toXml();
770+
771+
const auto deviceServiceIn = std::make_shared<DeviceService>(std::make_shared<AudioEngine>());
772+
const auto synthIn = std::make_shared<SynthDevice>(synthName);
773+
deviceServiceIn->registerDevice(synthIn);
774+
775+
EditorService editorServiceIn { std::make_shared<SelectionService>(), std::make_shared<SettingsService>(), std::make_shared<AutomationService>(std::make_shared<PropertyService>()) };
776+
connect(&editorServiceIn, &EditorService::devicesDeserializationRequested, deviceServiceIn.get(), &DeviceService::deserializeFromXml);
777+
778+
editorServiceIn.fromXml(xml);
779+
780+
QCOMPARE(synthIn->id(), 66ull);
781+
QCOMPARE(synthIn->vco1Waveform(), PolyBLEPOscillator::Waveform::Saw);
782+
QCOMPARE(synthIn->vco1Octave(), 1);
783+
QCOMPARE(synthIn->mixVco2(), 0.75f);
784+
QCOMPARE(synthIn->lpfCutoff(), 0.3f);
785+
QCOMPARE(synthIn->ampAttack(), 0.2f);
786+
QCOMPARE(synthIn->multiType(), MultiEngine::Type::Decim);
787+
QCOMPARE(synthIn->multiShape(), 0.42f);
788+
QCOMPARE(synthIn->multiLevel(), 0.88f);
789+
QCOMPARE(synthIn->multiKeyTrack(), 0.5f);
790+
QCOMPARE(synthIn->masterPan(), 0.12f);
791+
792+
// Verify discrete flags
793+
const auto vco1Wave = synthIn->parameter(Constants::NahdXml::xmlKeySynthVco1Waveform().toStdString());
794+
QVERIFY(vco1Wave.has_value());
795+
QVERIFY(vco1Wave->get().isDiscrete());
796+
797+
const auto vco1Octave = synthIn->parameter(Constants::NahdXml::xmlKeySynthVco1Octave().toStdString());
798+
QVERIFY(vco1Octave.has_value());
799+
QVERIFY(vco1Octave->get().isDiscrete());
800+
801+
const auto lpfCutoff = synthIn->parameter(Constants::NahdXml::xmlKeySynthLpfCutoff().toStdString());
802+
QVERIFY(lpfCutoff.has_value());
803+
QVERIFY(!lpfCutoff->get().isDiscrete());
804+
805+
const auto multiShape = synthIn->parameter(Constants::NahdXml::xmlKeySynthMultiShape().toStdString());
806+
QVERIFY(multiShape.has_value());
807+
QVERIFY(!multiShape->get().isDiscrete());
808+
}
809+
745810
void XmlSerializationTest::test_fromXml_samplerDevice_missingId_shouldNotThrow()
746811
{
747812
const auto xml = QString(R"XML(

src/unit_tests/xml_serialization_test/xml_serialization_test.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ private slots:
5454
void test_toXmlFromXml_trackName_shouldLoadTrackName();
5555
void test_toXmlFromXml_trackDrumTrack_shouldLoadTrackDrumTrack();
5656
void test_toXmlFromXml_samplerDevice_shouldLoadSamplerDevice();
57+
void test_toXmlFromXml_synthDevice_shouldPreserveValuesAndDiscreteFlags();
5758
void test_fromXml_samplerDevice_missingId_shouldNotThrow();
5859

5960
void test_toXmlFromXml_differentSongs_shouldLoadSongs();

0 commit comments

Comments
 (0)