1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
OSCArgument(int32 v)29 OSCArgument::OSCArgument (int32 v)              : type (OSCTypes::int32),   intValue (v) {}
OSCArgument(float v)30 OSCArgument::OSCArgument (float v)              : type (OSCTypes::float32), floatValue (v) {}
OSCArgument(const String & s)31 OSCArgument::OSCArgument (const String& s)      : type (OSCTypes::string),  stringValue (s) {}
OSCArgument(MemoryBlock b)32 OSCArgument::OSCArgument (MemoryBlock b)        : type (OSCTypes::blob),    blob (std::move (b)) {}
OSCArgument(OSCColour c)33 OSCArgument::OSCArgument (OSCColour c)          : type (OSCTypes::colour),  intValue ((int32) c.toInt32()) {}
34 
35 //==============================================================================
getString() const36 String OSCArgument::getString() const noexcept
37 {
38     if (isString())
39         return stringValue;
40 
41     jassertfalse; // you must check the type of an argument before attempting to get its value!
42     return {};
43 }
44 
getInt32() const45 int32 OSCArgument::getInt32() const noexcept
46 {
47     if (isInt32())
48         return intValue;
49 
50     jassertfalse; // you must check the type of an argument before attempting to get its value!
51     return 0;
52 }
53 
getFloat32() const54 float OSCArgument::getFloat32() const noexcept
55 {
56     if (isFloat32())
57         return floatValue;
58 
59     jassertfalse; // you must check the type of an argument before attempting to get its value!
60     return 0.0f;
61 }
62 
getBlob() const63 const MemoryBlock& OSCArgument::getBlob() const noexcept
64 {
65     // you must check the type of an argument before attempting to get its value!
66     jassert (isBlob());
67 
68     return blob;
69 }
70 
getColour() const71 OSCColour OSCArgument::getColour() const noexcept
72 {
73     if (isColour())
74         return OSCColour::fromInt32 ((uint32) intValue);
75 
76     jassertfalse; // you must check the type of an argument before attempting to get its value!
77     return { 0, 0, 0, 0 };
78 }
79 
80 
81 //==============================================================================
82 //==============================================================================
83 #if JUCE_UNIT_TESTS
84 
85 class OSCArgumentTests  : public UnitTest
86 {
87 public:
OSCArgumentTests()88     OSCArgumentTests()
89          : UnitTest ("OSCArgument class", UnitTestCategories::osc)
90     {}
91 
92 
getMemoryBlockWithRandomData(size_t numBytes)93     MemoryBlock getMemoryBlockWithRandomData (size_t numBytes)
94     {
95         MemoryBlock block (numBytes);
96 
97         Random rng = getRandom();
98 
99         for (size_t i = 0; i < numBytes; ++i)
100             block[i] = (char) rng.nextInt (256);
101 
102         return block;
103     }
104 
runTest()105     void runTest()
106     {
107         runTestInitialisation();
108     }
109 
runTestInitialisation()110     void runTestInitialisation()
111     {
112         beginTest ("Int32");
113         {
114             int value = 123456789;
115 
116             OSCArgument arg (value);
117 
118             expect (arg.getType() == OSCTypes::int32);
119             expect (arg.isInt32());
120             expect (! arg.isFloat32());
121             expect (! arg.isString());
122             expect (! arg.isBlob());
123             expect (! arg.isColour());
124 
125             expect (arg.getInt32() == value);
126         }
127 
128         beginTest ("Float32");
129         {
130             float value = 12345.6789f;
131 
132             OSCArgument arg (value);
133 
134             expect (arg.getType() == OSCTypes::float32);
135             expect (! arg.isInt32());
136             expect (arg.isFloat32());
137             expect (! arg.isString());
138             expect (! arg.isBlob());
139             expect (! arg.isColour());
140 
141             expect (arg.getFloat32() == value);
142         }
143 
144         beginTest ("String");
145         {
146             String value = "Hello, World!";
147             OSCArgument arg (value);
148 
149             expect (arg.getType() == OSCTypes::string);
150             expect (! arg.isInt32());
151             expect (! arg.isFloat32());
152             expect (arg.isString());
153             expect (! arg.isBlob());
154             expect (! arg.isColour());
155 
156             expect (arg.getString() == value);
157         }
158 
159         beginTest ("String (from C string)");
160         {
161             OSCArgument arg ("Hello, World!");
162 
163             expect (arg.getType() == OSCTypes::string);
164             expect (! arg.isInt32());
165             expect (! arg.isFloat32());
166             expect (arg.isString());
167             expect (! arg.isBlob());
168             expect (! arg.isColour());
169 
170             expect (arg.getString() == "Hello, World!");
171         }
172 
173         beginTest ("Blob");
174         {
175             auto blob = getMemoryBlockWithRandomData (413);
176             OSCArgument arg (blob);
177 
178             expect (arg.getType() == OSCTypes::blob);
179             expect (! arg.isInt32());
180             expect (! arg.isFloat32());
181             expect (! arg.isString());
182             expect (arg.isBlob());
183             expect (! arg.isColour());
184 
185             expect (arg.getBlob() == blob);
186         }
187 
188         beginTest ("Colour");
189         {
190             Random rng = getRandom();
191 
192             for (int i = 100; --i >= 0;)
193             {
194                 OSCColour col = { (uint8) rng.nextInt (256),
195                                   (uint8) rng.nextInt (256),
196                                   (uint8) rng.nextInt (256),
197                                   (uint8) rng.nextInt (256) };
198 
199                 OSCArgument arg (col);
200 
201                 expect (arg.getType() == OSCTypes::colour);
202                 expect (! arg.isInt32());
203                 expect (! arg.isFloat32());
204                 expect (! arg.isString());
205                 expect (! arg.isBlob());
206                 expect (arg.isColour());
207 
208                 expect (arg.getColour().toInt32() == col.toInt32());
209             }
210         }
211 
212         beginTest ("Copy, move and assignment");
213         {
214             {
215                 int value = -42;
216                 OSCArgument arg (value);
217 
218                 OSCArgument copy = arg;
219                 expect (copy.getType() == OSCTypes::int32);
220                 expect (copy.getInt32() == value);
221 
222                 OSCArgument assignment ("this will be overwritten!");
223                 assignment = copy;
224                 expect (assignment.getType() == OSCTypes::int32);
225                 expect (assignment.getInt32() == value);
226            }
227            {
228                 const size_t numBytes = 412;
229                 MemoryBlock blob = getMemoryBlockWithRandomData (numBytes);
230                 OSCArgument arg (blob);
231 
232                 OSCArgument copy = arg;
233                 expect (copy.getType() == OSCTypes::blob);
234                 expect (copy.getBlob() == blob);
235 
236                 OSCArgument assignment ("this will be overwritten!");
237                 assignment = copy;
238                 expect (assignment.getType() == OSCTypes::blob);
239                 expect (assignment.getBlob() == blob);
240            }
241         }
242     }
243 };
244 
245 static OSCArgumentTests OSCArgumentUnitTests;
246 
247 #endif
248 
249 } // namespace juce
250