You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.3KB

  1. #include "asserts.h"
  2. #include "ObjectCache.h"
  3. extern int _numLookupParams;
  4. template <typename T>
  5. static void testBipolar()
  6. {
  7. assertEQ(_numLookupParams, 0);
  8. auto test = ObjectCache<T>::getBipolarAudioTaper();
  9. assertEQ(_numLookupParams, 1);
  10. auto test2 = ObjectCache<T>::getBipolarAudioTaper();
  11. assertEQ(_numLookupParams, 1);
  12. test.reset();
  13. assertEQ(_numLookupParams, 1);
  14. test2.reset();
  15. assertEQ(_numLookupParams, 0);
  16. {
  17. // simple test that bipolar audio scalers use cached lookups, and they work
  18. AudioMath::ScaleFun<float> f = AudioMath::makeBipolarAudioScaler(3, 4);
  19. assertEQ(f(0, -5, 0), 3.);
  20. assertEQ(_numLookupParams, 1);
  21. }
  22. assertEQ(_numLookupParams, 0);
  23. // make again
  24. test = ObjectCache<T>::getBipolarAudioTaper();
  25. assertEQ(_numLookupParams, 1);
  26. }
  27. template <typename T>
  28. static void testSin()
  29. {
  30. assertEQ(_numLookupParams, 0);
  31. auto test = ObjectCache<T>::getSinLookup();
  32. assertEQ(_numLookupParams, 1);
  33. auto test2 = ObjectCache<T>::getSinLookup();
  34. assertEQ(_numLookupParams, 1);
  35. test.reset();
  36. assertEQ(_numLookupParams, 1);
  37. test2.reset();
  38. assertEQ(_numLookupParams, 0);
  39. {
  40. // // simple test that bipolar audio scalers use cached lookups, and they work
  41. AudioMath::ScaleFun<float> f = AudioMath::makeBipolarAudioScaler(3, 4);
  42. assertEQ(f(0, -5, 0), 3.);
  43. assertEQ(_numLookupParams, 1);
  44. }
  45. assertEQ(_numLookupParams, 0);
  46. // make again
  47. test = ObjectCache<T>::getSinLookup();
  48. assertEQ(_numLookupParams, 1);
  49. }
  50. template <typename T>
  51. static void testExp2()
  52. {
  53. assertEQ(_numLookupParams, 0);
  54. auto test = ObjectCache<T>::getExp2();
  55. assertEQ(_numLookupParams, 1);
  56. auto test2 = ObjectCache<T>::getExp2();
  57. assertEQ(_numLookupParams, 1);
  58. test.reset();
  59. assertEQ(_numLookupParams, 1);
  60. test2.reset();
  61. assertEQ(_numLookupParams, 0);
  62. {
  63. auto test3 = ObjectCache<T>::getExp2();
  64. const double x = LookupTable<T>::lookup(*test3, (T)3.2);
  65. const double y = std::pow(2, 3.2);
  66. assertClose(x, y, .001);
  67. assertEQ(_numLookupParams, 1);
  68. }
  69. assertEQ(_numLookupParams, 0);
  70. // make again
  71. test = ObjectCache<T>::getExp2();
  72. assertEQ(_numLookupParams, 1);
  73. }
  74. template <typename T>
  75. static void testExp2b()
  76. {
  77. {
  78. // make sure exp2 is really 1V/octave
  79. auto ex2 = ObjectCache<T>::getExp2();
  80. const T a = LookupTable<T>::lookup(*ex2, 5);
  81. const T b = LookupTable<T>::lookup(*ex2, 6);
  82. assertClose(b / a, 2, .001);
  83. }
  84. }
  85. template <typename T>
  86. static void testDb2Gain()
  87. {
  88. assertEQ(_numLookupParams, 0);
  89. auto test = ObjectCache<T>::getDb2Gain();
  90. assertEQ(_numLookupParams, 1);
  91. auto test2 = ObjectCache<T>::getDb2Gain();
  92. assertEQ(_numLookupParams, 1);
  93. test.reset();
  94. assertEQ(_numLookupParams, 1);
  95. test2.reset();
  96. assertEQ(_numLookupParams, 0);
  97. {
  98. auto test3 = ObjectCache<T>::getDb2Gain();
  99. const double x = LookupTable<T>::lookup(*test3, (T) -12);
  100. const double y = AudioMath::gainFromDb(-12);
  101. assertClose(x, y, .1);
  102. assertEQ(_numLookupParams, 1);
  103. }
  104. assertEQ(_numLookupParams, 0);
  105. // make again
  106. test = ObjectCache<T>::getDb2Gain();
  107. assertEQ(_numLookupParams, 1);
  108. }
  109. template <typename T>
  110. static void testDb2Gain2()
  111. {
  112. assertEQ(_numLookupParams, 0);
  113. auto test = ObjectCache<T>::getDb2Gain();
  114. // .1 db from -80 to +20
  115. for (double db = -80; db < 20; db += .1) {
  116. const double x = LookupTable<T>::lookup(*test, (T) db);
  117. const double y = AudioMath::gainFromDb(db);
  118. assertClose(x, y, .2);
  119. }
  120. }
  121. template <typename T>
  122. static void testTanh5()
  123. {
  124. auto test = ObjectCache<T>::getTanh5();
  125. auto test2 = ObjectCache<T>::getTanh5();
  126. assertEQ(_numLookupParams, 1);
  127. for (double x = -5; x <= 5; x += .1) {
  128. const double y = LookupTable<T>::lookup(*test, (T) x);
  129. const double y0 = std::tanh(x);
  130. assertClose(y, y0, .01);
  131. }
  132. }
  133. template <typename T>
  134. static void test()
  135. {
  136. testBipolar<T>();
  137. testSin<T>();
  138. testExp2<T>();
  139. testExp2b<T>();
  140. testDb2Gain<T>();
  141. testDb2Gain2<T>();
  142. testTanh5<T>();
  143. }
  144. void testObjectCache()
  145. {
  146. assertEQ(_numLookupParams, 0);
  147. test<float>();
  148. test<double>();
  149. assertEQ(_numLookupParams, 0);
  150. }