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.

234 lines
6.2KB

  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 value) noexcept
  22. : type (OSCTypes::int32), intValue (value)
  23. {
  24. }
  25. OSCArgument::OSCArgument (float value) noexcept
  26. : type (OSCTypes::float32), floatValue (value)
  27. {
  28. }
  29. OSCArgument::OSCArgument (const String& value) noexcept
  30. : type (OSCTypes::string), stringValue (value)
  31. {
  32. }
  33. OSCArgument::OSCArgument (const MemoryBlock& b)
  34. : type (OSCTypes::blob), blob (b)
  35. {
  36. }
  37. //==============================================================================
  38. String OSCArgument::getString() const noexcept
  39. {
  40. if (isString())
  41. return stringValue;
  42. jassertfalse; // you must check the type of an argument before attempting to get its value!
  43. return {};
  44. }
  45. int32 OSCArgument::getInt32() const noexcept
  46. {
  47. if (isInt32())
  48. return intValue;
  49. jassertfalse; // you must check the type of an argument before attempting to get its value!
  50. return 0;
  51. }
  52. float OSCArgument::getFloat32() const noexcept
  53. {
  54. if (isFloat32())
  55. return floatValue;
  56. jassertfalse; // you must check the type of an argument before attempting to get its value!
  57. return 0.0f;
  58. }
  59. const MemoryBlock& OSCArgument::getBlob() const noexcept
  60. {
  61. // you must check the type of an argument before attempting to get its value!
  62. jassert (isBlob());
  63. return blob;
  64. }
  65. //==============================================================================
  66. //==============================================================================
  67. #if JUCE_UNIT_TESTS
  68. class OSCArgumentTests : public UnitTest
  69. {
  70. public:
  71. OSCArgumentTests() : UnitTest ("OSCArgument class", "OSC") {}
  72. MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
  73. {
  74. MemoryBlock block (numBytes);
  75. Random rng = getRandom();
  76. for (size_t i = 0; i < numBytes; ++i)
  77. block[i] = (char) rng.nextInt (256);
  78. return block;
  79. }
  80. void runTest()
  81. {
  82. runTestInitialisation();
  83. }
  84. void runTestInitialisation()
  85. {
  86. beginTest ("Int32");
  87. {
  88. int value = 123456789;
  89. OSCArgument arg (value);
  90. expect (arg.getType() == OSCTypes::int32);
  91. expect (arg.isInt32());
  92. expect (! arg.isFloat32());
  93. expect (! arg.isString());
  94. expect (! arg.isBlob());
  95. expect (arg.getInt32() == value);
  96. }
  97. beginTest ("Float32");
  98. {
  99. float value = 12345.6789f;
  100. OSCArgument arg (value);
  101. expect (arg.getType() == OSCTypes::float32);
  102. expect (! arg.isInt32());
  103. expect (arg.isFloat32());
  104. expect (! arg.isString());
  105. expect (! arg.isBlob());
  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.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.getString() == String ("Hello, World!"));
  128. }
  129. beginTest ("Blob");
  130. {
  131. const size_t numBytes = 412;
  132. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  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.getBlob() == blob);
  140. }
  141. beginTest ("Copy, move and assignment");
  142. {
  143. {
  144. int value = -42;
  145. OSCArgument arg (value);
  146. OSCArgument copy = arg;
  147. expect (copy.getType() == OSCTypes::int32);
  148. expect (copy.getInt32() == value);
  149. OSCArgument assignment ("this will be overwritten!");
  150. assignment = copy;
  151. expect (assignment.getType() == OSCTypes::int32);
  152. expect (assignment.getInt32() == value);
  153. }
  154. {
  155. const size_t numBytes = 412;
  156. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  157. OSCArgument arg (blob);
  158. OSCArgument copy = arg;
  159. expect (copy.getType() == OSCTypes::blob);
  160. expect (copy.getBlob() == blob);
  161. OSCArgument assignment ("this will be overwritten!");
  162. assignment = copy;
  163. expect (assignment.getType() == OSCTypes::blob);
  164. expect (assignment.getBlob() == blob);
  165. }
  166. }
  167. }
  168. };
  169. static OSCArgumentTests OSCArgumentUnitTests;
  170. #endif // JUCE_UNIT_TESTS
  171. } // namespace juce