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.

353 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. #if JUCE_ENABLE_ALLOCATION_HOOKS
  19. #define JUCE_FAIL_ON_ALLOCATION_IN_SCOPE const UnitTestAllocationChecker checker (*this)
  20. #else
  21. #define JUCE_FAIL_ON_ALLOCATION_IN_SCOPE
  22. #endif
  23. namespace juce
  24. {
  25. namespace dsp
  26. {
  27. namespace
  28. {
  29. class ConstructCounts
  30. {
  31. auto tie() const noexcept { return std::tie (constructions, copies, moves, calls, destructions); }
  32. public:
  33. int constructions = 0;
  34. int copies = 0;
  35. int moves = 0;
  36. int calls = 0;
  37. int destructions = 0;
  38. ConstructCounts withConstructions (int i) const noexcept { auto c = *this; c.constructions = i; return c; }
  39. ConstructCounts withCopies (int i) const noexcept { auto c = *this; c.copies = i; return c; }
  40. ConstructCounts withMoves (int i) const noexcept { auto c = *this; c.moves = i; return c; }
  41. ConstructCounts withCalls (int i) const noexcept { auto c = *this; c.calls = i; return c; }
  42. ConstructCounts withDestructions (int i) const noexcept { auto c = *this; c.destructions = i; return c; }
  43. bool operator== (const ConstructCounts& other) const noexcept { return tie() == other.tie(); }
  44. bool operator!= (const ConstructCounts& other) const noexcept { return tie() != other.tie(); }
  45. };
  46. String& operator<< (String& str, const ConstructCounts& c)
  47. {
  48. return str << "{ constructions: " << c.constructions
  49. << ", copies: " << c.copies
  50. << ", moves: " << c.moves
  51. << ", calls: " << c.calls
  52. << ", destructions: " << c.destructions
  53. << " }";
  54. }
  55. class FixedSizeFunctionTest : public UnitTest
  56. {
  57. static void toggleBool (bool& b) { b = ! b; }
  58. struct ConstructCounter
  59. {
  60. explicit ConstructCounter (ConstructCounts& countsIn)
  61. : counts (countsIn) {}
  62. ConstructCounter (const ConstructCounter& c)
  63. : counts (c.counts)
  64. {
  65. counts.copies += 1;
  66. }
  67. ConstructCounter (ConstructCounter&& c) noexcept
  68. : counts (c.counts)
  69. {
  70. counts.moves += 1;
  71. }
  72. ~ConstructCounter() noexcept { counts.destructions += 1; }
  73. void operator()() const noexcept { counts.calls += 1; }
  74. ConstructCounts& counts;
  75. };
  76. public:
  77. FixedSizeFunctionTest()
  78. : UnitTest ("Fixed Size Function", UnitTestCategories::dsp)
  79. {}
  80. void runTest() override
  81. {
  82. beginTest ("Can be constructed and called from a lambda");
  83. {
  84. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  85. const auto result = 5;
  86. bool wasCalled = false;
  87. const auto lambda = [&] { wasCalled = true; return result; };
  88. const FixedSizeFunction<sizeof (lambda), int()> fn (lambda);
  89. const auto out = fn();
  90. expect (wasCalled);
  91. expectEquals (result, out);
  92. }
  93. beginTest ("void fn can be constructed from function with return value");
  94. {
  95. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  96. bool wasCalled = false;
  97. const auto lambda = [&] { wasCalled = true; return 5; };
  98. const FixedSizeFunction<sizeof (lambda), void()> fn (lambda);
  99. fn();
  100. expect (wasCalled);
  101. }
  102. beginTest ("Can be constructed and called from a function pointer");
  103. {
  104. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  105. bool state = false;
  106. const FixedSizeFunction<sizeof (void*), void (bool&)> fn (toggleBool);
  107. fn (state);
  108. expect (state);
  109. fn (state);
  110. expect (! state);
  111. fn (state);
  112. expect (state);
  113. }
  114. beginTest ("Default constructed functions throw if called");
  115. {
  116. const auto a = FixedSizeFunction<8, void()>();
  117. expectThrowsType (a(), std::bad_function_call)
  118. const auto b = FixedSizeFunction<8, void()> (nullptr);
  119. expectThrowsType (b(), std::bad_function_call)
  120. }
  121. beginTest ("Functions can be moved");
  122. {
  123. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  124. ConstructCounts counts;
  125. auto a = FixedSizeFunction<sizeof (ConstructCounter), void()> (ConstructCounter { counts });
  126. expectEquals (counts, ConstructCounts().withMoves (1).withDestructions (1)); // The temporary gets destroyed
  127. a();
  128. expectEquals (counts, ConstructCounts().withMoves (1).withDestructions (1).withCalls (1));
  129. const auto b = std::move (a);
  130. expectEquals (counts, ConstructCounts().withMoves (2).withDestructions (1).withCalls (1));
  131. b();
  132. expectEquals (counts, ConstructCounts().withMoves (2).withDestructions (1).withCalls (2));
  133. b();
  134. expectEquals (counts, ConstructCounts().withMoves (2).withDestructions (1).withCalls (3));
  135. }
  136. beginTest ("Functions are destructed properly");
  137. {
  138. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  139. ConstructCounts counts;
  140. const ConstructCounter toCopy { counts };
  141. {
  142. auto a = FixedSizeFunction<sizeof (ConstructCounter), void()> (toCopy);
  143. expectEquals (counts, ConstructCounts().withCopies (1));
  144. }
  145. expectEquals (counts, ConstructCounts().withCopies (1).withDestructions (1));
  146. }
  147. beginTest ("Avoid destructing functions that fail to construct");
  148. {
  149. struct BadConstructor
  150. {
  151. explicit BadConstructor (ConstructCounts& c)
  152. : counts (c)
  153. {
  154. counts.constructions += 1;
  155. throw std::runtime_error { "this was meant to happen" };
  156. }
  157. ~BadConstructor() noexcept { counts.destructions += 1; }
  158. void operator()() const noexcept { counts.calls += 1; }
  159. ConstructCounts& counts;
  160. };
  161. ConstructCounts counts;
  162. expectThrowsType ((FixedSizeFunction<sizeof (BadConstructor), void()> (BadConstructor { counts })),
  163. std::runtime_error)
  164. expectEquals (counts, ConstructCounts().withConstructions (1));
  165. }
  166. beginTest ("Equality checks work");
  167. {
  168. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  169. FixedSizeFunction<8, void()> a;
  170. expect (! bool (a));
  171. expect (a == nullptr);
  172. expect (nullptr == a);
  173. expect (! (a != nullptr));
  174. expect (! (nullptr != a));
  175. FixedSizeFunction<8, void()> b ([] {});
  176. expect (bool (b));
  177. expect (b != nullptr);
  178. expect (nullptr != b);
  179. expect (! (b == nullptr));
  180. expect (! (nullptr == b));
  181. }
  182. beginTest ("Functions can be cleared");
  183. {
  184. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  185. FixedSizeFunction<8, void()> fn ([] {});
  186. expect (bool (fn));
  187. fn = nullptr;
  188. expect (! bool (fn));
  189. }
  190. beginTest ("Functions can be assigned");
  191. {
  192. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  193. using Fn = FixedSizeFunction<8, void()>;
  194. int numCallsA = 0;
  195. int numCallsB = 0;
  196. Fn x;
  197. Fn y;
  198. expect (! bool (x));
  199. expect (! bool (y));
  200. x = [&] { numCallsA += 1; };
  201. y = [&] { numCallsB += 1; };
  202. expect (bool (x));
  203. expect (bool (y));
  204. x();
  205. expectEquals (numCallsA, 1);
  206. expectEquals (numCallsB, 0);
  207. y();
  208. expectEquals (numCallsA, 1);
  209. expectEquals (numCallsB, 1);
  210. x = std::move (y);
  211. expectEquals (numCallsA, 1);
  212. expectEquals (numCallsB, 1);
  213. x();
  214. expectEquals (numCallsA, 1);
  215. expectEquals (numCallsB, 2);
  216. }
  217. beginTest ("Functions may mutate internal state");
  218. {
  219. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  220. using Fn = FixedSizeFunction<64, void()>;
  221. Fn x;
  222. expect (! bool (x));
  223. int numCalls = 0;
  224. x = [&numCalls, counter = 0]() mutable { counter += 1; numCalls = counter; };
  225. expect (bool (x));
  226. expectEquals (numCalls, 0);
  227. x();
  228. expectEquals (numCalls, 1);
  229. x();
  230. expectEquals (numCalls, 2);
  231. }
  232. beginTest ("Functions can sink move-only parameters");
  233. {
  234. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  235. using Fn = FixedSizeFunction<64, int (std::unique_ptr<int>)>;
  236. auto value = 5;
  237. auto ptr = std::make_unique<int> (value);
  238. Fn fn = [] (std::unique_ptr<int> p) { return *p; };
  239. expect (value == fn (std::move (ptr)));
  240. }
  241. beginTest ("Functions be converted from smaller functions");
  242. {
  243. JUCE_FAIL_ON_ALLOCATION_IN_SCOPE;
  244. using SmallFn = FixedSizeFunction<20, void()>;
  245. using LargeFn = FixedSizeFunction<21, void()>;
  246. bool smallCalled = false;
  247. bool largeCalled = false;
  248. SmallFn small = [&smallCalled, a = std::array<char, 8>{}] { smallCalled = true; juce::ignoreUnused (a); };
  249. LargeFn large = [&largeCalled, a = std::array<char, 8>{}] { largeCalled = true; juce::ignoreUnused (a); };
  250. large = std::move (small);
  251. large();
  252. expect (smallCalled);
  253. expect (! largeCalled);
  254. }
  255. }
  256. };
  257. FixedSizeFunctionTest fixedSizedFunctionTest;
  258. }
  259. }
  260. }
  261. #undef JUCE_FAIL_ON_ALLOCATION_IN_SCOPE