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.

349 lines
11KB

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