tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_ConstrainedCachedValue.cpp
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14#if TRACKTION_UNIT_TESTS && ENGINE_UNIT_TESTS_CONSTRAINED_CACHED_VALUE
15
16class ConstrainedCachedValueTests : public juce::UnitTest
17{
18public:
19 ConstrainedCachedValueTests()
20 : juce::UnitTest ("ConstrainedCachedValue ", "Tracktion") {}
21
22 //==============================================================================
23 void runTest() override
24 {
25 beginTest ("Odd/even tests");
26 {
27 juce::ValueTree state ("TREE");
28 ConstrainedCachedValue<int> value;
29 value.setConstrainer ([] (int v) { return v % 2 == 0 ? v : v + 1; });
30 value.referTo (state, "value", nullptr);
31
32 expectEquals (value.get(), 0);
33 value = 1;
34 expectEquals (value.get(), 2);
35 value = 2;
36 expectEquals (value.get(), 2);
37 value = 17;
38 expectEquals (value.get(), 18);
39 }
40
41 beginTest ("ceil/floor tests");
42 {
43 juce::ValueTree state ("TREE");
44 ConstrainedCachedValue<float> value;
45 bool useCeil = true;
46 value.setConstrainer ([&useCeil] (float v) { return useCeil ? std::ceil (v) : std::floor (v); });
47 value.referTo (state, "value", nullptr);
48
49 expectWithinAbsoluteError (value.getDefault(), 0.0f, 0.00000000000001f);
50 expectWithinAbsoluteError (value.get(), 0.0f, 0.00000000000001f);
51 value = 1.1f;
52 expectWithinAbsoluteError (value.get(), 2.0f, 0.00000000000001f);
53 value = 2.7f;
54 expectWithinAbsoluteError (value.get(), 3.0f, 0.00000000000001f);
55
56 useCeil = false;
57 value = 2.7f;
58 expectWithinAbsoluteError (value.get(), 2.0f, 0.00000000000001f);
59 value = 1.1f;
60 expectWithinAbsoluteError (value.get(), 1.0f, 0.00000000000001f);
61 value = -3.0f;
62 expectWithinAbsoluteError (value.get(), -3.0f, 0.00000000000001f);
63
64 value.referTo (state, "value", nullptr, 1.2f);
65 expectWithinAbsoluteError (value.getDefault(), 1.0f, 0.00000000000001f);
66 expectWithinAbsoluteError (value.get(), -3.0f, 0.00000000000001f);
67 }
68 }
69};
70
71static ConstrainedCachedValueTests constrainedCachedValueTests;
72
73#endif // TRACKTION_UNIT_TESTS
74
75}} // namespace tracktion { inline namespace engine
T ceil(T... args)
floor