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.

251 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()
  68. : UnitTest ("OSCArgument class", UnitTestCategories::osc)
  69. {}
  70. MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
  71. {
  72. MemoryBlock block (numBytes);
  73. Random rng = getRandom();
  74. for (size_t i = 0; i < numBytes; ++i)
  75. block[i] = (char) rng.nextInt (256);
  76. return block;
  77. }
  78. void runTest()
  79. {
  80. runTestInitialisation();
  81. }
  82. void runTestInitialisation()
  83. {
  84. beginTest ("Int32");
  85. {
  86. int value = 123456789;
  87. OSCArgument arg (value);
  88. expect (arg.getType() == OSCTypes::int32);
  89. expect (arg.isInt32());
  90. expect (! arg.isFloat32());
  91. expect (! arg.isString());
  92. expect (! arg.isBlob());
  93. expect (! arg.isColour());
  94. expect (arg.getInt32() == value);
  95. }
  96. beginTest ("Float32");
  97. {
  98. float value = 12345.6789f;
  99. OSCArgument arg (value);
  100. expect (arg.getType() == OSCTypes::float32);
  101. expect (! arg.isInt32());
  102. expect (arg.isFloat32());
  103. expect (! arg.isString());
  104. expect (! arg.isBlob());
  105. expect (! arg.isColour());
  106. expect (arg.getFloat32() == value);
  107. }
  108. beginTest ("String");
  109. {
  110. String value = "Hello, World!";
  111. OSCArgument arg (value);
  112. expect (arg.getType() == OSCTypes::string);
  113. expect (! arg.isInt32());
  114. expect (! arg.isFloat32());
  115. expect (arg.isString());
  116. expect (! arg.isBlob());
  117. expect (! arg.isColour());
  118. expect (arg.getString() == value);
  119. }
  120. beginTest ("String (from C string)");
  121. {
  122. OSCArgument arg ("Hello, World!");
  123. expect (arg.getType() == OSCTypes::string);
  124. expect (! arg.isInt32());
  125. expect (! arg.isFloat32());
  126. expect (arg.isString());
  127. expect (! arg.isBlob());
  128. expect (! arg.isColour());
  129. expect (arg.getString() == "Hello, World!");
  130. }
  131. beginTest ("Blob");
  132. {
  133. auto blob = getMemoryBlockWithRandomData (413);
  134. OSCArgument arg (blob);
  135. expect (arg.getType() == OSCTypes::blob);
  136. expect (! arg.isInt32());
  137. expect (! arg.isFloat32());
  138. expect (! arg.isString());
  139. expect (arg.isBlob());
  140. expect (! arg.isColour());
  141. expect (arg.getBlob() == blob);
  142. }
  143. beginTest ("Colour");
  144. {
  145. Random rng = getRandom();
  146. for (int i = 100; --i >= 0;)
  147. {
  148. OSCColour col = { (uint8) rng.nextInt (256),
  149. (uint8) rng.nextInt (256),
  150. (uint8) rng.nextInt (256),
  151. (uint8) rng.nextInt (256) };
  152. OSCArgument arg (col);
  153. expect (arg.getType() == OSCTypes::colour);
  154. expect (! arg.isInt32());
  155. expect (! arg.isFloat32());
  156. expect (! arg.isString());
  157. expect (! arg.isBlob());
  158. expect (arg.isColour());
  159. expect (arg.getColour().toInt32() == col.toInt32());
  160. }
  161. }
  162. beginTest ("Copy, move and assignment");
  163. {
  164. {
  165. int value = -42;
  166. OSCArgument arg (value);
  167. OSCArgument copy = arg;
  168. expect (copy.getType() == OSCTypes::int32);
  169. expect (copy.getInt32() == value);
  170. OSCArgument assignment ("this will be overwritten!");
  171. assignment = copy;
  172. expect (assignment.getType() == OSCTypes::int32);
  173. expect (assignment.getInt32() == value);
  174. }
  175. {
  176. const size_t numBytes = 412;
  177. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  178. OSCArgument arg (blob);
  179. OSCArgument copy = arg;
  180. expect (copy.getType() == OSCTypes::blob);
  181. expect (copy.getBlob() == blob);
  182. OSCArgument assignment ("this will be overwritten!");
  183. assignment = copy;
  184. expect (assignment.getType() == OSCTypes::blob);
  185. expect (assignment.getBlob() == blob);
  186. }
  187. }
  188. }
  189. };
  190. static OSCArgumentTests OSCArgumentUnitTests;
  191. #endif
  192. } // namespace juce