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.

239 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. OSCArgument::OSCArgument (int32 value) noexcept
  18. : type (OSCTypes::int32), intValue (value)
  19. {
  20. }
  21. OSCArgument::OSCArgument (float value) noexcept
  22. : type (OSCTypes::float32), floatValue (value)
  23. {
  24. }
  25. OSCArgument::OSCArgument (const String& value) noexcept
  26. : type (OSCTypes::string), stringValue (value)
  27. {
  28. }
  29. OSCArgument::OSCArgument (const MemoryBlock& b)
  30. : type (OSCTypes::blob), blob (b)
  31. {
  32. }
  33. //==============================================================================
  34. String OSCArgument::getString() const noexcept
  35. {
  36. if (isString())
  37. return stringValue;
  38. jassertfalse; // you must check the type of an argument before attempting to get its value!
  39. return String();
  40. }
  41. int32 OSCArgument::getInt32() const noexcept
  42. {
  43. if (isInt32())
  44. return intValue;
  45. jassertfalse; // you must check the type of an argument before attempting to get its value!
  46. return 0;
  47. }
  48. float OSCArgument::getFloat32() const noexcept
  49. {
  50. if (isFloat32())
  51. return floatValue;
  52. jassertfalse; // you must check the type of an argument before attempting to get its value!
  53. return 0.0f;
  54. }
  55. const MemoryBlock& OSCArgument::getBlob() const noexcept
  56. {
  57. // you must check the type of an argument before attempting to get its value!
  58. jassert (isBlob());
  59. return blob;
  60. }
  61. //==============================================================================
  62. //==============================================================================
  63. #if JUCE_UNIT_TESTS
  64. class OSCArgumentTests : public UnitTest
  65. {
  66. public:
  67. OSCArgumentTests() : UnitTest ("OSCArgument class") {}
  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.getInt32() == value);
  92. }
  93. beginTest ("Float32");
  94. {
  95. float value = 12345.6789f;
  96. OSCArgument arg (value);
  97. expect (arg.getType() == OSCTypes::float32);
  98. expect (! arg.isInt32());
  99. expect (arg.isFloat32());
  100. expect (! arg.isString());
  101. expect (! arg.isBlob());
  102. expect (arg.getFloat32() == value);
  103. }
  104. beginTest ("String");
  105. {
  106. String value = "Hello, World!";
  107. OSCArgument arg (value);
  108. expect (arg.getType() == OSCTypes::string);
  109. expect (! arg.isInt32());
  110. expect (! arg.isFloat32());
  111. expect (arg.isString());
  112. expect (! arg.isBlob());
  113. expect (arg.getString() == value);
  114. }
  115. beginTest ("String (from C string)");
  116. {
  117. OSCArgument arg ("Hello, World!");
  118. expect (arg.getType() == OSCTypes::string);
  119. expect (! arg.isInt32());
  120. expect (! arg.isFloat32());
  121. expect (arg.isString());
  122. expect (! arg.isBlob());
  123. expect (arg.getString() == String ("Hello, World!"));
  124. }
  125. beginTest ("Blob");
  126. {
  127. const size_t numBytes = 412;
  128. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  129. OSCArgument arg (blob);
  130. expect (arg.getType() == OSCTypes::blob);
  131. expect (! arg.isInt32());
  132. expect (! arg.isFloat32());
  133. expect (! arg.isString());
  134. expect (arg.isBlob());
  135. expect (arg.getBlob() == blob);
  136. }
  137. beginTest ("Copy, move and assignment");
  138. {
  139. {
  140. int value = -42;
  141. OSCArgument arg (value);
  142. OSCArgument copy = arg;
  143. expect (copy.getType() == OSCTypes::int32);
  144. expect (copy.getInt32() == value);
  145. OSCArgument assignment ("this will be overwritten!");
  146. assignment = copy;
  147. expect (assignment.getType() == OSCTypes::int32);
  148. expect (assignment.getInt32() == value);
  149. }
  150. {
  151. const size_t numBytes = 412;
  152. MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
  153. OSCArgument arg (blob);
  154. OSCArgument copy = arg;
  155. expect (copy.getType() == OSCTypes::blob);
  156. expect (copy.getBlob() == blob);
  157. OSCArgument assignment ("this will be overwritten!");
  158. assignment = copy;
  159. expect (assignment.getType() == OSCTypes::blob);
  160. expect (assignment.getBlob() == blob);
  161. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  162. OSCArgument move = std::move (arg);
  163. expect (move.getType() == OSCTypes::blob);
  164. expect (move.getBlob() == blob);
  165. OSCArgument moveAssignment ("this will be overwritten!");
  166. moveAssignment = std::move (copy);
  167. expect (moveAssignment.getType() == OSCTypes::blob);
  168. expect (moveAssignment.getBlob() == blob);
  169. #endif
  170. }
  171. }
  172. }
  173. };
  174. static OSCArgumentTests OSCArgumentUnitTests;
  175. #endif // JUCE_UNIT_TESTS