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.

248 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. OSCArgument::OSCArgument (int32 v) : type (OSCTypes::int32), intValue (v) {}
  22. OSCArgument::OSCArgument (float v) : type (OSCTypes::float32), floatValue (v) {}
  23. OSCArgument::OSCArgument (const String& s) : type (OSCTypes::string), stringValue (s) {}
  24. OSCArgument::OSCArgument (MemoryBlock b) : type (OSCTypes::blob), blob (std::move (b)) {}
  25. OSCArgument::OSCArgument (OSCColour c) : type (OSCTypes::colour), intValue ((int32) c.toInt32()) {}
  26. //==============================================================================
  27. String OSCArgument::getString() const noexcept
  28. {
  29. if (isString())
  30. return stringValue;
  31. jassertfalse; // you must check the type of an argument before attempting to get its value!
  32. return {};
  33. }
  34. int32 OSCArgument::getInt32() const noexcept
  35. {
  36. if (isInt32())
  37. return intValue;
  38. jassertfalse; // you must check the type of an argument before attempting to get its value!
  39. return 0;
  40. }
  41. float OSCArgument::getFloat32() const noexcept
  42. {
  43. if (isFloat32())
  44. return floatValue;
  45. jassertfalse; // you must check the type of an argument before attempting to get its value!
  46. return 0.0f;
  47. }
  48. const MemoryBlock& OSCArgument::getBlob() const noexcept
  49. {
  50. // you must check the type of an argument before attempting to get its value!
  51. jassert (isBlob());
  52. return blob;
  53. }
  54. OSCColour OSCArgument::getColour() const noexcept
  55. {
  56. if (isColour())
  57. return OSCColour::fromInt32 ((uint32) intValue);
  58. jassertfalse; // you must check the type of an argument before attempting to get its value!
  59. return { 0, 0, 0, 0 };
  60. }
  61. //==============================================================================
  62. //==============================================================================
  63. #if JUCE_UNIT_TESTS
  64. class OSCArgumentTests : public UnitTest
  65. {
  66. public:
  67. OSCArgumentTests() : UnitTest ("OSCArgument class", "OSC") {}
  68. MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
  69. {
  70. MemoryBlock block (numBytes);
  71. Random rng = getRandom();
  72. for (size_t i = 0; i < numBytes; ++i)
  73. block[i] = (char) rng.nextInt (256);
  74. return block;
  75. }
  76. void runTest()
  77. {
  78. runTestInitialisation();
  79. }
  80. void runTestInitialisation()
  81. {
  82. beginTest ("Int32");
  83. {
  84. int value = 123456789;
  85. OSCArgument arg (value);
  86. expect (arg.getType() == OSCTypes::int32);
  87. expect (arg.isInt32());
  88. expect (! arg.isFloat32());
  89. expect (! arg.isString());
  90. expect (! arg.isBlob());
  91. expect (! arg.isColour());
  92. expect (arg.getInt32() == value);
  93. }
  94. beginTest ("Float32");
  95. {
  96. float value = 12345.6789f;
  97. OSCArgument arg (value);
  98. expect (arg.getType() == OSCTypes::float32);
  99. expect (! arg.isInt32());
  100. expect (arg.isFloat32());
  101. expect (! arg.isString());
  102. expect (! arg.isBlob());
  103. expect (! arg.isColour());
  104. expect (arg.getFloat32() == value);
  105. }
  106. beginTest ("String");
  107. {
  108. String value = "Hello, World!";
  109. OSCArgument arg (value);
  110. expect (arg.getType() == OSCTypes::string);
  111. expect (! arg.isInt32());
  112. expect (! arg.isFloat32());
  113. expect (arg.isString());
  114. expect (! arg.isBlob());
  115. expect (! arg.isColour());
  116. expect (arg.getString() == value);
  117. }
  118. beginTest ("String (from C string)");
  119. {
  120. OSCArgument arg ("Hello, World!");
  121. expect (arg.getType() == OSCTypes::string);
  122. expect (! arg.isInt32());
  123. expect (! arg.isFloat32());
  124. expect (arg.isString());
  125. expect (! arg.isBlob());
  126. expect (! arg.isColour());
  127. expect (arg.getString() == "Hello, World!");
  128. }
  129. beginTest ("Blob");
  130. {
  131. auto blob = getMemoryBlockWithRandomData (413);
  132. OSCArgument arg (blob);
  133. expect (arg.getType() == OSCTypes::blob);
  134. expect (! arg.isInt32());
  135. expect (! arg.isFloat32());
  136. expect (! arg.isString());
  137. expect (arg.isBlob());
  138. expect (! arg.isColour());
  139. expect (arg.getBlob() == blob);
  140. }
  141. beginTest ("Colour");
  142. {
  143. Random rng = getRandom();
  144. for (int i = 100; --i >= 0;)
  145. {
  146. OSCColour col = { (uint8) rng.nextInt (256),
  147. (uint8) rng.nextInt (256),
  148. (uint8) rng.nextInt (256),
  149. (uint8) rng.nextInt (256) };
  150. OSCArgument arg (col);
  151. expect (arg.getType() == OSCTypes::colour);
  152. expect (! arg.isInt32());
  153. expect (! arg.isFloat32());
  154. expect (! arg.isString());
  155. expect (! arg.isBlob());
  156. expect (arg.isColour());
  157. expect (arg.getColour().toInt32() == col.toInt32());
  158. }
  159. }
  160. beginTest ("Copy, move and assignment");
  161. {
  162. {
  163. int value = -42;
  164. OSCArgument arg (value);
  165. OSCArgument copy = arg;
  166. expect (copy.getType() == OSCTypes::int32);
  167. expect (copy.getInt32() == value);
  168. OSCArgument assignment ("this will be overwritten!");
  169. assignment = copy;
  170. expect (assignment.getType() == OSCTypes::int32);
  171. expect (assignment.getInt32() == value);
  172. }
  173. {
  174. const size_t numBytes = 412;
  175. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  176. OSCArgument arg (blob);
  177. OSCArgument copy = arg;
  178. expect (copy.getType() == OSCTypes::blob);
  179. expect (copy.getBlob() == blob);
  180. OSCArgument assignment ("this will be overwritten!");
  181. assignment = copy;
  182. expect (assignment.getType() == OSCTypes::blob);
  183. expect (assignment.getBlob() == blob);
  184. }
  185. }
  186. }
  187. };
  188. static OSCArgumentTests OSCArgumentUnitTests;
  189. #endif // JUCE_UNIT_TESTS
  190. } // namespace juce