Audio plugin host https://kx.studio/carla
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.

174 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. struct LinearAlgebraUnitTest : public UnitTest
  23. {
  24. LinearAlgebraUnitTest()
  25. : UnitTest ("Linear Algebra UnitTests", UnitTestCategories::dsp)
  26. {}
  27. struct AdditionTest
  28. {
  29. template <typename ElementType>
  30. static void run (LinearAlgebraUnitTest& u)
  31. {
  32. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  33. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  34. const ElementType data3[] = { 2, 1, 6, 3, 10, 5, 14, 7 };
  35. Matrix<ElementType> mat1 (2, 4, data1);
  36. Matrix<ElementType> mat2 (2, 4, data2);
  37. Matrix<ElementType> mat3 (2, 4, data3);
  38. u.expect((mat1 + mat2) == mat3);
  39. }
  40. };
  41. struct DifferenceTest
  42. {
  43. template <typename ElementType>
  44. static void run (LinearAlgebraUnitTest& u)
  45. {
  46. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  47. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  48. const ElementType data3[] = { 0, 3, 0, 5, 0, 7, 0, 9 };
  49. Matrix<ElementType> mat1 (2, 4, data1);
  50. Matrix<ElementType> mat2 (2, 4, data2);
  51. Matrix<ElementType> mat3 (2, 4, data3);
  52. u.expect((mat1 - mat2) == mat3);
  53. }
  54. };
  55. struct ScalarMultiplicationTest
  56. {
  57. template <typename ElementType>
  58. static void run (LinearAlgebraUnitTest& u)
  59. {
  60. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  61. const ElementType scalar = 2.0;
  62. const ElementType data2[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  63. Matrix<ElementType> x (2, 4, data1);
  64. Matrix<ElementType> expected (2, 4, data2);
  65. u.expect ((x * scalar) == expected);
  66. }
  67. };
  68. struct HadamardProductTest
  69. {
  70. template <typename ElementType>
  71. static void run (LinearAlgebraUnitTest& u)
  72. {
  73. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  74. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  75. const ElementType data3[] = { 1, -2, 9, -4, 25, -6, 49, -8 };
  76. Matrix<ElementType> mat1 (2, 4, data1);
  77. Matrix<ElementType> mat2 (2, 4, data2);
  78. Matrix<ElementType> mat3 (2, 4, data3);
  79. u.expect (Matrix<ElementType>::hadarmard (mat1, mat2) == mat3);
  80. }
  81. };
  82. struct MultiplicationTest
  83. {
  84. template <typename ElementType>
  85. static void run (LinearAlgebraUnitTest& u)
  86. {
  87. const ElementType data1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  88. const ElementType data2[] = { 1, -1, 3, -1, 5, -1, 7, -1 };
  89. const ElementType data3[] = { 50, -10, 114, -26 };
  90. Matrix<ElementType> mat1 (2, 4, data1);
  91. Matrix<ElementType> mat2 (4, 2, data2);
  92. Matrix<ElementType> mat3 (2, 2, data3);
  93. u.expect((mat1 * mat2) == mat3);
  94. }
  95. };
  96. struct IdentityMatrixTest
  97. {
  98. template <typename ElementType>
  99. static void run (LinearAlgebraUnitTest& u)
  100. {
  101. const ElementType data1[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  102. u.expect (Matrix<ElementType>::identity (4) == Matrix<ElementType> (4, 4, data1));
  103. }
  104. };
  105. struct SolvingTest
  106. {
  107. template <typename ElementType>
  108. static void run (LinearAlgebraUnitTest& u)
  109. {
  110. const ElementType data1[] = { 1, -1, 2, -2 };
  111. const ElementType data2[] = { -1, 0, -1, -7 };
  112. const ElementType data3[] = { 1, 4, 2, 1, -1, 1, 4, 3, -2, -1, 1, 1, -1, 0, 1, 4 };
  113. Matrix<ElementType> X (4, 1, data1);
  114. Matrix<ElementType> B (4, 1, data2);
  115. Matrix<ElementType> A (4, 4, data3);
  116. u.expect (A.solve (B));
  117. u.expect (Matrix<ElementType>::compare (X, B, (ElementType) 1e-4));
  118. }
  119. };
  120. template <class TheTest>
  121. void runTestForAllTypes (const char* unitTestName)
  122. {
  123. beginTest (unitTestName);
  124. TheTest::template run<float> (*this);
  125. TheTest::template run<double> (*this);
  126. }
  127. void runTest() override
  128. {
  129. runTestForAllTypes<AdditionTest> ("AdditionTest");
  130. runTestForAllTypes<DifferenceTest> ("DifferenceTest");
  131. runTestForAllTypes<ScalarMultiplicationTest> ("ScalarMultiplication");
  132. runTestForAllTypes<HadamardProductTest> ("HadamardProductTest");
  133. runTestForAllTypes<MultiplicationTest> ("MultiplicationTest");
  134. runTestForAllTypes<IdentityMatrixTest> ("IdentityMatrixTest");
  135. runTestForAllTypes<SolvingTest> ("SolvingTest");
  136. }
  137. };
  138. static LinearAlgebraUnitTest linearAlgebraUnitTest;
  139. } // namespace dsp
  140. } // namespace juce