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.

243 lines
7.2KB

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