The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

255 lines
7.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #if JUCE_UNIT_TESTS
  24. namespace FunctionTestsHelpers
  25. {
  26. void incrementArgument (int& x) { x++; };
  27. double multiply (double x, double a) noexcept { return a * x; };
  28. struct BigData
  29. {
  30. BigData()
  31. {
  32. for (auto i = 0; i < bigDataSize; ++i)
  33. content[i] = i + 1;
  34. }
  35. int sum() const
  36. {
  37. int result = 0;
  38. for (auto i = 0; i < bigDataSize; ++i)
  39. result += content[i];
  40. return result;
  41. }
  42. static const int bigDataSize = 32,
  43. bigDataSum = bigDataSize * (bigDataSize + 1) / 2;
  44. int content[bigDataSize];
  45. };
  46. struct FunctionObject
  47. {
  48. FunctionObject() {}
  49. FunctionObject (const FunctionObject& other)
  50. {
  51. bigData = new BigData (*other.bigData);
  52. }
  53. int operator()(int i) const { return bigData->sum() + i; }
  54. ScopedPointer<BigData> bigData { new BigData() };
  55. };
  56. }
  57. class FunctionTests : public UnitTest
  58. {
  59. public:
  60. FunctionTests() : UnitTest ("Function") {}
  61. void runTest() override
  62. {
  63. FunctionTestsHelpers::BigData bigData;
  64. {
  65. beginTest ("Functions");
  66. std::function<void(int&)> f1 (FunctionTestsHelpers::incrementArgument);
  67. auto x = 0;
  68. f1 (x);
  69. expectEquals (x, 1);
  70. std::function<double(double, double)> f2 (FunctionTestsHelpers::multiply);
  71. expectEquals (6.0, f2 (2.0, 3.0));
  72. }
  73. {
  74. beginTest ("Function objects");
  75. std::function<int(int)> f1 = FunctionTestsHelpers::FunctionObject();
  76. expectEquals (f1 (5), FunctionTestsHelpers::BigData::bigDataSum + 5);
  77. }
  78. {
  79. beginTest ("Lambdas");
  80. std::function<int()> fStack ([]() { return 3; });
  81. expectEquals (fStack(), 3);
  82. std::function<int()> fHeap ([=]() { return bigData.sum(); });
  83. expectEquals (fHeap(), FunctionTestsHelpers::BigData::bigDataSum);
  84. }
  85. {
  86. beginTest ("Boolean");
  87. std::function<void(int&)> f1;
  88. if (f1)
  89. expect (false);
  90. std::function<int()> f2 ([]() { return 3; });
  91. if (! f2)
  92. expect (false);
  93. }
  94. std::function<int()> fEmpty;
  95. std::function<int()> fStack ([]() { return 3; });
  96. std::function<int()> fHeap ([=]() { return bigData.sum(); });
  97. {
  98. beginTest ("copy constructor");
  99. std::function<int()> f1 (fStack);
  100. expectEquals (f1(), 3);
  101. std::function<int()> f2 (fHeap);
  102. expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
  103. std::function<int()> f3 (fEmpty);
  104. if (f3)
  105. expect (false);
  106. }
  107. {
  108. beginTest ("assignment");
  109. std::function<int()> f1;
  110. f1 = fStack;
  111. expectEquals (f1(), 3);
  112. std::function<int()> f2;
  113. f2 = fHeap;
  114. expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
  115. f1 = fHeap;
  116. expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum);
  117. f2 = fStack;
  118. expectEquals (f2(), 3);
  119. f1 = fEmpty;
  120. if (f1)
  121. expect (false);
  122. }
  123. {
  124. beginTest ("move constructor");
  125. ScopedPointer<std::function<int()>> fStackTmp (new std::function<int()> (fStack));
  126. std::function<int()> f1 (static_cast<std::function<int()>&&> (*fStackTmp));
  127. fStackTmp = nullptr;
  128. expectEquals (f1(), 3);
  129. ScopedPointer<std::function<int()>> fHeapTmp (new std::function<int()> (fHeap));
  130. std::function<int()> f2 (static_cast<std::function<int()>&&> (*fHeapTmp));
  131. if (*fHeapTmp)
  132. expect (false);
  133. fHeapTmp = nullptr;
  134. expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
  135. ScopedPointer<std::function<int()>> fEmptyTmp (new std::function<int()>());
  136. std::function<int()> f3 (static_cast<std::function<int()>&&> (*fEmptyTmp));
  137. fEmptyTmp = nullptr;
  138. if (f3)
  139. expect (false);
  140. }
  141. {
  142. beginTest ("move assignment");
  143. std::function<int()> f1 (fHeap);
  144. ScopedPointer<std::function<int()>> fStackTmp (new std::function<int()> (fStack));
  145. f1 = static_cast<std::function<int()>&&> (*fStackTmp);
  146. fStackTmp = nullptr;
  147. expectEquals (f1(), 3);
  148. std::function<int()> f2 (fStack);
  149. ScopedPointer<std::function<int()>> fHeapTmp (new std::function<int()> (fHeap));
  150. f2 = static_cast<std::function<int()>&&> (*fHeapTmp);
  151. if (*fHeapTmp)
  152. expect (false);
  153. fHeapTmp = nullptr;
  154. expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
  155. std::function<int()> f3 (fHeap);
  156. ScopedPointer<std::function<int()>> fEmptyTmp (new std::function<int()>());
  157. f3 = static_cast<std::function<int()>&&> (*fEmptyTmp);
  158. fEmptyTmp = nullptr;
  159. if (f3)
  160. expect (false);
  161. }
  162. {
  163. beginTest ("nullptr");
  164. std::function<int()> f1 (nullptr);
  165. if (f1)
  166. expect (false);
  167. std::function<int()> f2 ([]() { return 11; });
  168. f2 = nullptr;
  169. if (f2)
  170. expect (false);
  171. }
  172. {
  173. beginTest ("Swap");
  174. std::function<int()> f1;
  175. std::function<int()> f2 (fStack);
  176. f2.swap (f1);
  177. expectEquals (f1(), 3);
  178. if (f2)
  179. expect (false);
  180. std::function<int()> f3 (fHeap);
  181. f3.swap (f1);
  182. expectEquals (f3(), 3);
  183. expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum);
  184. }
  185. }
  186. };
  187. static FunctionTests functionTests;
  188. #endif