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.

250 lines
7.4KB

  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. namespace juce
  19. {
  20. OSCArgument::OSCArgument (int32 v) : type (OSCTypes::int32), intValue (v) {}
  21. OSCArgument::OSCArgument (float v) : type (OSCTypes::float32), floatValue (v) {}
  22. OSCArgument::OSCArgument (const String& s) : type (OSCTypes::string), stringValue (s) {}
  23. OSCArgument::OSCArgument (MemoryBlock b) : type (OSCTypes::blob), blob (std::move (b)) {}
  24. OSCArgument::OSCArgument (OSCColour c) : type (OSCTypes::colour), intValue ((int32) c.toInt32()) {}
  25. //==============================================================================
  26. String OSCArgument::getString() const noexcept
  27. {
  28. if (isString())
  29. return stringValue;
  30. jassertfalse; // you must check the type of an argument before attempting to get its value!
  31. return {};
  32. }
  33. int32 OSCArgument::getInt32() const noexcept
  34. {
  35. if (isInt32())
  36. return intValue;
  37. jassertfalse; // you must check the type of an argument before attempting to get its value!
  38. return 0;
  39. }
  40. float OSCArgument::getFloat32() const noexcept
  41. {
  42. if (isFloat32())
  43. return floatValue;
  44. jassertfalse; // you must check the type of an argument before attempting to get its value!
  45. return 0.0f;
  46. }
  47. const MemoryBlock& OSCArgument::getBlob() const noexcept
  48. {
  49. // you must check the type of an argument before attempting to get its value!
  50. jassert (isBlob());
  51. return blob;
  52. }
  53. OSCColour OSCArgument::getColour() const noexcept
  54. {
  55. if (isColour())
  56. return OSCColour::fromInt32 ((uint32) intValue);
  57. jassertfalse; // you must check the type of an argument before attempting to get its value!
  58. return { 0, 0, 0, 0 };
  59. }
  60. //==============================================================================
  61. //==============================================================================
  62. #if JUCE_UNIT_TESTS
  63. class OSCArgumentTests : public UnitTest
  64. {
  65. public:
  66. OSCArgumentTests()
  67. : UnitTest ("OSCArgument class", UnitTestCategories::osc)
  68. {}
  69. MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
  70. {
  71. MemoryBlock block (numBytes);
  72. Random rng = getRandom();
  73. for (size_t i = 0; i < numBytes; ++i)
  74. block[i] = (char) rng.nextInt (256);
  75. return block;
  76. }
  77. void runTest()
  78. {
  79. runTestInitialisation();
  80. }
  81. void runTestInitialisation()
  82. {
  83. beginTest ("Int32");
  84. {
  85. int value = 123456789;
  86. OSCArgument arg (value);
  87. expect (arg.getType() == OSCTypes::int32);
  88. expect (arg.isInt32());
  89. expect (! arg.isFloat32());
  90. expect (! arg.isString());
  91. expect (! arg.isBlob());
  92. expect (! arg.isColour());
  93. expect (arg.getInt32() == value);
  94. }
  95. beginTest ("Float32");
  96. {
  97. float value = 12345.6789f;
  98. OSCArgument arg (value);
  99. expect (arg.getType() == OSCTypes::float32);
  100. expect (! arg.isInt32());
  101. expect (arg.isFloat32());
  102. expect (! arg.isString());
  103. expect (! arg.isBlob());
  104. expect (! arg.isColour());
  105. expect (arg.getFloat32() == value);
  106. }
  107. beginTest ("String");
  108. {
  109. String value = "Hello, World!";
  110. OSCArgument arg (value);
  111. expect (arg.getType() == OSCTypes::string);
  112. expect (! arg.isInt32());
  113. expect (! arg.isFloat32());
  114. expect (arg.isString());
  115. expect (! arg.isBlob());
  116. expect (! arg.isColour());
  117. expect (arg.getString() == value);
  118. }
  119. beginTest ("String (from C string)");
  120. {
  121. OSCArgument arg ("Hello, World!");
  122. expect (arg.getType() == OSCTypes::string);
  123. expect (! arg.isInt32());
  124. expect (! arg.isFloat32());
  125. expect (arg.isString());
  126. expect (! arg.isBlob());
  127. expect (! arg.isColour());
  128. expect (arg.getString() == "Hello, World!");
  129. }
  130. beginTest ("Blob");
  131. {
  132. auto blob = getMemoryBlockWithRandomData (413);
  133. OSCArgument arg (blob);
  134. expect (arg.getType() == OSCTypes::blob);
  135. expect (! arg.isInt32());
  136. expect (! arg.isFloat32());
  137. expect (! arg.isString());
  138. expect (arg.isBlob());
  139. expect (! arg.isColour());
  140. expect (arg.getBlob() == blob);
  141. }
  142. beginTest ("Colour");
  143. {
  144. Random rng = getRandom();
  145. for (int i = 100; --i >= 0;)
  146. {
  147. OSCColour col = { (uint8) rng.nextInt (256),
  148. (uint8) rng.nextInt (256),
  149. (uint8) rng.nextInt (256),
  150. (uint8) rng.nextInt (256) };
  151. OSCArgument arg (col);
  152. expect (arg.getType() == OSCTypes::colour);
  153. expect (! arg.isInt32());
  154. expect (! arg.isFloat32());
  155. expect (! arg.isString());
  156. expect (! arg.isBlob());
  157. expect (arg.isColour());
  158. expect (arg.getColour().toInt32() == col.toInt32());
  159. }
  160. }
  161. beginTest ("Copy, move and assignment");
  162. {
  163. {
  164. int value = -42;
  165. OSCArgument arg (value);
  166. OSCArgument copy = arg;
  167. expect (copy.getType() == OSCTypes::int32);
  168. expect (copy.getInt32() == value);
  169. OSCArgument assignment ("this will be overwritten!");
  170. assignment = copy;
  171. expect (assignment.getType() == OSCTypes::int32);
  172. expect (assignment.getInt32() == value);
  173. }
  174. {
  175. const size_t numBytes = 412;
  176. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  177. OSCArgument arg (blob);
  178. OSCArgument copy = arg;
  179. expect (copy.getType() == OSCTypes::blob);
  180. expect (copy.getBlob() == blob);
  181. OSCArgument assignment ("this will be overwritten!");
  182. assignment = copy;
  183. expect (assignment.getType() == OSCTypes::blob);
  184. expect (assignment.getBlob() == blob);
  185. }
  186. }
  187. }
  188. };
  189. static OSCArgumentTests OSCArgumentUnitTests;
  190. #endif
  191. } // namespace juce