Skip to content

Commit

Permalink
Adding a couple of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Dec 2, 2024
1 parent 946ebcd commit 76a5633
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 13 deletions.
76 changes: 63 additions & 13 deletions tests/plugin_tests/chowdsp_plugin_state_test/ParamHolderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

TEST_CASE ("ParamHolder Test", "[plugin][state]")
{
SECTION ("add()")
{
chowdsp::ParamHolder params;
std::array<chowdsp::PercentParameter::Ptr, 2> floatParams {
chowdsp::PercentParameter::Ptr { "param3", "Param", 0.5f },
chowdsp::PercentParameter::Ptr { "param4", "Param", 0.5f },
};

chowdsp::BoolParameter::Ptr boolNested { "param1", "Param", false };
chowdsp::ChoiceParameter::Ptr choiceNested { "param2", "Param", juce::StringArray { "One", "Two" }, 0 };
chowdsp::ParamHolder nestedParams { &params };
nestedParams.add (boolNested, choiceNested);
std::array<chowdsp::PercentParameter::Ptr, 2> floatParams {
chowdsp::PercentParameter::Ptr { "param3", "Param", 0.5f },
chowdsp::PercentParameter::Ptr { "param4", "Param", 0.5f },
};
chowdsp::BoolParameter::Ptr boolNested { "param1", "Param", false };
chowdsp::ChoiceParameter::Ptr choiceNested { "param2", "Param", juce::StringArray { "One", "Two" }, 0 };

params.add (nestedParams, floatParams);
chowdsp::ParamHolder params;
chowdsp::ParamHolder nestedParams { &params };
nestedParams.add (boolNested, choiceNested);
params.add (nestedParams, floatParams);

SECTION ("add()")
{
auto allParamIDs = [&params]
{
juce::StringArray allIDs;
Expand All @@ -31,4 +30,55 @@ TEST_CASE ("ParamHolder Test", "[plugin][state]")
REQUIRE (allParamIDs.contains (floatParams[0]->paramID));
REQUIRE (allParamIDs.contains (floatParams[1]->paramID));
}

SECTION ("Serialize JSON")
{
using namespace chowdsp::ParameterTypeHelpers;
setValue (0.0f, *floatParams[0]);
setValue (1.0f, *floatParams[1]);
setValue (true, *boolNested);
setValue (1, *choiceNested);

const auto json_state = chowdsp::ParamHolder::serialize_json (params);
params.doForAllParameters ([] (auto& param, size_t) { setValue (getDefaultValue (param), param); });

REQUIRE (getValue (*floatParams[0]) == 0.5f);
REQUIRE (getValue (*floatParams[1]) == 0.5f);
REQUIRE (getValue (*boolNested) == false);
REQUIRE (getValue (*choiceNested) == 0);

chowdsp::ParamHolder::deserialize_json (json_state, params);
REQUIRE (getValue (*floatParams[0]) == 0.0f);
REQUIRE (getValue (*floatParams[1]) == 1.0f);
REQUIRE (getValue (*boolNested) == true);
REQUIRE (getValue (*choiceNested) == 1);
}

SECTION ("Serialize Bytes")
{
using namespace chowdsp::ParameterTypeHelpers;
setValue (0.0f, *floatParams[0]);
setValue (1.0f, *floatParams[1]);
setValue (true, *boolNested);
setValue (1, *choiceNested);

chowdsp::ChainedArenaAllocator arena { 128 };
chowdsp::ParamHolder::serialize (arena, params);
juce::MemoryBlock state {};
chowdsp::dump_serialized_bytes (state, arena);

params.doForAllParameters ([] (auto& param, size_t) { setValue (getDefaultValue (param), param); });

REQUIRE (getValue (*floatParams[0]) == 0.5f);
REQUIRE (getValue (*floatParams[1]) == 0.5f);
REQUIRE (getValue (*boolNested) == false);
REQUIRE (getValue (*choiceNested) == 0);

nonstd::span state_data = { (const std::byte*) state.getData(), state.getSize() };
chowdsp::ParamHolder::deserialize (state_data, params);
REQUIRE (getValue (*floatParams[0]) == 0.0f);
REQUIRE (getValue (*floatParams[1]) == 1.0f);
REQUIRE (getValue (*boolNested) == true);
REQUIRE (getValue (*choiceNested) == 1);
}
}
47 changes: 47 additions & 0 deletions tests/plugin_tests/chowdsp_presets_v2_test/PresetManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ TEST_CASE ("Preset Manager Test", "[plugin][presets][state]")
REQUIRE (presetMgr->getIsPresetDirty());
}

SECTION ("Null State JSON Test")
{
static constexpr float otherValue = 0.15f;

chowdsp::json json_state;
{
ScopedPresetManager presetMgr {};

presetMgr.setFloatParam (otherValue);
REQUIRE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue));

json_state = decltype (presetMgr.state)::serialize (presetMgr.state);
}

ScopedPresetManager presetMgr {};
decltype (presetMgr.state)::deserialize (json_state, presetMgr.state);
REQUIRE_MESSAGE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue), "Preset state is overriding parameter state!");
REQUIRE (presetMgr->getCurrentPreset() == nullptr);
REQUIRE (presetMgr->getIsPresetDirty());
}

SECTION ("Preset State Test")
{
static constexpr float testValue = 0.05f;
Expand All @@ -267,6 +288,32 @@ TEST_CASE ("Preset Manager Test", "[plugin][presets][state]")
REQUIRE (presetMgr->getIsPresetDirty());
}

SECTION ("Preset State JSON Test")
{
static constexpr float testValue = 0.05f;
static constexpr float otherValue = 0.15f;
auto preset = saveUserPreset ("test.preset", testValue);

chowdsp::json json_state;
{
ScopedPresetManager presetMgr {};
presetMgr->addPresets ({ chowdsp::presets::Preset { preset } });
presetMgr->loadPreset (presetMgr->getPresetTree().getRootNode().first_child->value.leaf());
REQUIRE (juce::approximatelyEqual (presetMgr.getFloatParam(), testValue));

presetMgr.setFloatParam (otherValue);
REQUIRE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue));

json_state = decltype (presetMgr.state)::serialize (presetMgr.state);
}

ScopedPresetManager presetMgr {};
decltype (presetMgr.state)::deserialize (json_state, presetMgr.state);
REQUIRE_MESSAGE (juce::approximatelyEqual (presetMgr.getFloatParam(), otherValue), "Preset state is overriding parameter state!");
REQUIRE (*presetMgr->getCurrentPreset() == preset);
REQUIRE (presetMgr->getIsPresetDirty());
}

SECTION ("Preset Undo/Redo Test")
{
static constexpr float testValue = 0.05f;
Expand Down

0 comments on commit 76a5633

Please sign in to comment.