#include #include #include "LookupTable.h" #include "AudioMath.h" using namespace std; // test that we can call all the functions template static void test0() { LookupTableParams p; const int tableSize = 512; std::function f = [](double d) { return 0; }; LookupTable::init(p, tableSize, 0, 1, f); LookupTable::lookup(p, 0); } // test that simple lookup works template static void test1() { LookupTableParams p; const int tableSize = 512; std::function f = [](double d) { return 100; }; LookupTable::init(p, tableSize, 0, 1, f); assert(LookupTable::lookup(p, 0) == 100); assert(LookupTable::lookup(p, 1) == 100); assert(LookupTable::lookup(p, T(.342)) == 100); } // test that sin works template static void test2() { LookupTableParams p; const int tableSize = 512; std::function f = [](double d) { return std::sin(d); }; LookupTable::init(p, tableSize, 0, 1, f); const T tolerance = T(0.000001); for (double d = 0; d < 1; d += .0001) { T output = LookupTable::lookup(p, T(d)); const bool t = AudioMath::closeTo(output, std::sin(d), tolerance); if (!t) { cout << "failing with expected=" << std::sin(d) << " actual=" << output << " delta=" << std::abs(output - std::sin(d)); assert(false); } } } // test that sin works on domain 10..32 template static void test3() { LookupTableParams p; const int tableSize = 16; std::function f = [](double d) { const double s = (d - 10) / 3; return std::sin(s); }; LookupTable::init(p, tableSize, 10, 13, f); const T tolerance = T(0.01); for (double d = 10; d < 13; d += .0001) { const T output = LookupTable::lookup(p, T(d)); const T expected = (T) std::sin((d - 10.0) / 3); const bool t = AudioMath::closeTo(output, expected, tolerance); if (!t) { cout << "failing with d=" << d << " expected=" << expected << " actual=" << output << " delta=" << std::abs(output - std::sin(d)); assert(false); } } } // test that sin at extremes works template static void test4() { LookupTableParams exponential; const T xMin = -5; const T xMax = 5; std::function expFunc = AudioMath::makeFunc_Exp(-5, 5, 2, 2000); LookupTable::init(exponential, 128, -5, 5, expFunc); // Had to loosen tolerance to pass with windows gcc. Is there a problem // with precision, or is this expected with fast math? const T tolerance = T(0.0003); T outputLow = LookupTable::lookup(exponential, xMin); T outputHigh = LookupTable::lookup(exponential, xMax); bool t = AudioMath::closeTo(outputLow, 2, tolerance); if (!t) { cout << "failing l with expected=" << 2 << " actual=" << outputLow << " delta=" << std::abs(outputLow - 2) << std::endl; assert(false); } t = AudioMath::closeTo(outputHigh, 2000, tolerance); if (!t) { cout << "failing h with expected=" << 2000 << " actual=" << outputHigh << " delta=" << std::abs(outputHigh - 2000) << std::endl; assert(false); } } template static void test() { test0(); test1(); test2(); test3(); test4(); } void testLookupTable() { test(); test(); }