3 * Copyright (C) 2013-2015 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
19 # error Build this file with debug ON please
22 #include "CarlaUtils.hpp"
23 #include "CarlaMathUtils.hpp"
25 // #include "CarlaBackendUtils.hpp"
26 // #include "CarlaBridgeUtils.hpp"
27 // #include "CarlaEngineUtils.hpp"
29 // --------------------------------------------------------------------------------------------------------------------
31 static void test_CarlaUtils()
33 // ----------------------------------------------------------------------------------------------------------------
44 char* const strBuf2(strBuf
+1);
48 // ----------------------------------------------------------------------------------------------------------------
49 // string print functions
53 carla_stdout("STDOUT %s", bool2str(true));
54 carla_stderr("STDERR %s", bool2str(false));
55 carla_stderr2("STDERR2 " P_UINT64
, 0xffffffff); // 4294967295
58 // ----------------------------------------------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------------------------------------------
70 carla_setenv("THIS", "THAT");
71 assert(std::strcmp(std::getenv("THIS"), "THAT") == 0);
73 carla_unsetenv("THIS");
74 assert(std::getenv("THIS") == nullptr);
77 // ----------------------------------------------------------------------------------------------------------------
82 const char* const str1(carla_strdup("stringN"));
83 const char* const strF(carla_strdup_free(strdup("stringF")));
84 const char* const str2(carla_strdup_safe("stringS"));
90 delete[] carla_strdup("string_normal");
91 delete[] carla_strdup_free(strdup("string_free"));
92 delete[] carla_strdup_safe("string_safe");
96 // common use case in Carla code
99 const char* strNormal
;
105 strNormal(carla_strdup("strNormal")),
106 strFree(carla_strdup_free(strdup("strFree"))),
107 strSafe(carla_strdup_safe("strSafe")) {}
109 ~TestStruct() noexcept
111 if (strNull
!= nullptr)
117 if (strNormal
!= nullptr)
123 if (strFree
!= nullptr)
129 if (strSafe
!= nullptr)
136 CARLA_DECLARE_NON_COPY_STRUCT(TestStruct
)
142 // ----------------------------------------------------------------------------------------------------------------
146 int a1
[] = { 4, 3, 2, 1 };
147 int a2
[] = { 4, 3, 2, 1 };
148 int b1
[] = { 1, 2, 3, 4 };
149 int b2
[] = { 1, 2, 3, 4 };
151 carla_add(a1
, b1
, 4);
157 carla_add(b1
, a2
, 4);
162 assert(a1
[0] == b1
[0]);
163 assert(a1
[1] == b1
[1]);
164 assert(a1
[2] == b1
[2]);
165 assert(a1
[3] == b1
[3]);
167 carla_copy(a1
, b2
, 4);
168 assert(a1
[0] != b1
[0]);
169 assert(a1
[1] != b1
[1]);
170 assert(a1
[2] != b1
[2]);
171 assert(a1
[3] != b1
[3]);
172 assert(a1
[0] == b2
[0]);
173 assert(a1
[1] == b2
[1]);
174 assert(a1
[2] == b2
[2]);
175 assert(a1
[3] == b2
[3]);
177 carla_copy(a1
, b1
, 4);
178 assert(a1
[0] == b1
[0]);
179 assert(a1
[1] == b1
[1]);
180 assert(a1
[2] == b1
[2]);
181 assert(a1
[3] == b1
[3]);
183 carla_copy(a1
, b2
, 2);
184 assert(a1
[0] != b1
[0]);
185 assert(a1
[1] != b1
[1]);
186 assert(a1
[2] == b1
[2]);
187 assert(a1
[3] == b1
[3]);
189 carla_copy(a1
+2, b2
+2, 2);
190 assert(a1
[0] != b1
[0]);
191 assert(a1
[1] != b1
[1]);
192 assert(a1
[2] != b1
[2]);
193 assert(a1
[3] != b1
[3]);
195 carla_copy(a1
, b1
, 2);
196 assert(a1
[0] == b1
[0]);
197 assert(a1
[1] == b1
[1]);
198 assert(a1
[2] != b1
[2]);
199 assert(a1
[3] != b1
[3]);
201 carla_copy(a1
+2, b1
+2, 2);
202 assert(a1
[0] == b1
[0]);
203 assert(a1
[1] == b1
[1]);
204 assert(a1
[2] == b1
[2]);
205 assert(a1
[3] == b1
[3]);
207 carla_fill(a1
, 0, 4);
213 carla_fill(a1
, -11, 4);
214 assert(a1
[0] == -11);
215 assert(a1
[1] == -11);
216 assert(a1
[2] == -11);
217 assert(a1
[3] == -11);
219 carla_fill(a1
, 1791, 2);
220 assert(a1
[0] == 1791);
221 assert(a1
[1] == 1791);
222 assert(a1
[2] == -11);
223 assert(a1
[3] == -11);
225 carla_fill(a1
+2, 1791, 2);
226 assert(a1
[0] == 1791);
227 assert(a1
[1] == 1791);
228 assert(a1
[2] == 1791);
229 assert(a1
[3] == 1791);
231 int16_t d
= 1527, d2
= d
, e
= 0;
233 carla_add(&d
, &d2
, 1);
236 carla_add(&d
, &d2
, 1);
239 carla_add(&d
, &e
, 1);
243 carla_add(&e
, &d
, 1);
246 carla_add(&e
, &d
, 1);
250 carla_add(&d
, &e
, 1);
256 const bool f
= false, t
= true;
258 carla_copy(&x
, &t
, 1);
261 carla_copy(&x
, &f
, 1);
264 carla_fill(&x
, true, 1);
267 carla_fill(&x
, false, 1);
272 uint8_t a
[] = { 3, 2, 1, 0 };
273 carla_zeroBytes(a
, 1);
279 carla_zeroBytes(a
+1, 2);
289 for (int i
=500; --i
>=0;)
292 carla_zeroChars(a
, 501);
294 for (int i
=501; --i
>=0;)
295 assert(a
[i
] == '\0');
297 for (int i
=500; --i
>=0;)
300 assert(std::strlen(a
) == 500);
302 carla_fill(a
+200, '\0', 1);
303 assert(std::strlen(a
) == 200);
308 carla_zeroPointers(a
, 33);
310 for (int i
=33; --i
>=0;)
311 assert(a
[i
] == nullptr);
320 bool operator==(const Thing
& t
) const noexcept
322 return (t
.c
== c
&& t
.i
== i
&& t
.h
== h
);
325 bool operator!=(const Thing
& t
) const noexcept
327 return !operator==(t
);
343 carla_copyStruct(a
, b
);
349 carla_copyStruct(c
, b
);
357 carla_zeroStructs(d
, 3);
362 carla_copyStructs(d
, &a
, 1);
367 carla_copyStructs(&c
, d
+2, 1);
375 // --------------------------------------------------------------------------------------------------------------------
377 static void test_CarlaMathUtils() noexcept
379 // ----------------------------------------------------------------------------------------------------------------
380 // math functions (base)
382 // Return the lower of 2 values, with 'min' as the minimum possible value.
383 assert(carla_min(0, 0, 0) == 0);
384 assert(carla_min(0, 3, 0) == 0);
385 assert(carla_min(3, 0, 0) == 0);
387 assert(carla_min(0, 0, 3) == 3);
388 assert(carla_min(0, 1, 3) == 3);
389 assert(carla_min(0, 2, 3) == 3);
390 assert(carla_min(0, 3, 3) == 3);
391 assert(carla_min(0, 4, 3) == 3);
393 assert(carla_min(5, 0, 3) == 3);
394 assert(carla_min(5, 1, 3) == 3);
395 assert(carla_min(5, 2, 3) == 3);
396 assert(carla_min(5, 3, 3) == 3);
397 assert(carla_min(5, 4, 3) == 4);
398 assert(carla_min(5, 5, 3) == 5);
399 assert(carla_min(5, 6, 3) == 5);
401 assert(carla_min(2, -2, 0) == 0);
402 assert(carla_min(2, -1, 0) == 0);
403 assert(carla_min(2, 0, 0) == 0);
404 assert(carla_min(2, 1, 0) == 1);
405 assert(carla_min(2, 2, 0) == 2);
407 assert(carla_min(5, -6, 3) == 3);
408 assert(carla_min(5, -5, 3) == 3);
409 assert(carla_min(5, -4, 3) == 3);
410 assert(carla_min(5, -3, 3) == 3);
411 assert(carla_min(5, -2, 3) == 3);
412 assert(carla_min(5, -1, 3) == 3);
413 assert(carla_min(5, 0, 3) == 3);
414 assert(carla_min(5, 1, 3) == 3);
415 assert(carla_min(5, 2, 3) == 3);
416 assert(carla_min(5, 3, 3) == 3);
417 assert(carla_min(5, 4, 3) == 4);
418 assert(carla_min(5, 5, 3) == 5);
419 assert(carla_min(5, 6, 3) == 5);
421 assert(carla_min(5, -6, -3) == -3);
422 assert(carla_min(5, -5, -3) == -3);
423 assert(carla_min(5, -4, -3) == -3);
424 assert(carla_min(5, -3, -3) == -3);
425 assert(carla_min(5, -2, -3) == -2);
426 assert(carla_min(5, -1, -3) == -1);
427 assert(carla_min(5, 0, -3) == 0);
428 assert(carla_min(5, 1, -3) == 1);
429 assert(carla_min(5, 2, -3) == 2);
430 assert(carla_min(5, 3, -3) == 3);
431 assert(carla_min(5, 4, -3) == 4);
432 assert(carla_min(5, 5, -3) == 5);
433 assert(carla_min(5, 6, -3) == 5);
435 // Return the lower positive of 2 values.
436 assert(carla_minPositive(0, 0) == 0);
437 assert(carla_minPositive(0, 1) == 0);
438 assert(carla_minPositive(0, 2) == 0);
439 assert(carla_minPositive(0, 3) == 0);
441 assert(carla_minPositive(1, 0) == 0);
442 assert(carla_minPositive(1, 1) == 1);
443 assert(carla_minPositive(1, 2) == 1);
444 assert(carla_minPositive(1, 3) == 1);
446 assert(carla_minPositive(3, 0) == 0);
447 assert(carla_minPositive(3, 1) == 1);
448 assert(carla_minPositive(3, 2) == 2);
449 assert(carla_minPositive(3, 3) == 3);
451 assert(carla_minPositive(-1, 0) == 0);
452 assert(carla_minPositive(-1, 1) == 1);
453 assert(carla_minPositive(-1, 2) == 2);
454 assert(carla_minPositive(-1, 3) == 3);
456 // Return the higher of 2 values, with 'max' as the maximum possible value.
457 assert(carla_max(0, 0, 0) == 0);
458 assert(carla_max(0, 3, 0) == 0);
459 assert(carla_max(3, 0, 0) == 0);
461 assert(carla_max(0, 0, 3) == 0);
462 assert(carla_max(0, 1, 3) == 1);
463 assert(carla_max(0, 2, 3) == 2);
464 assert(carla_max(0, 3, 3) == 3);
465 assert(carla_max(0, 4, 3) == 3);
467 assert(carla_max(5, 0, 3) == 3);
468 assert(carla_max(5, 1, 3) == 3);
469 assert(carla_max(5, 2, 3) == 3);
470 assert(carla_max(5, 3, 3) == 3);
471 assert(carla_max(5, 4, 3) == 3);
472 assert(carla_max(5, 5, 3) == 3);
473 assert(carla_max(5, 6, 3) == 3);
475 // Fix bounds of 'value' between 'min' and 'max'.
476 assert(carla_fixedValue(0, 1, -1) == 0);
477 assert(carla_fixedValue(0, 1, 0) == 0);
478 assert(carla_fixedValue(0, 1, 1) == 1);
479 assert(carla_fixedValue(0, 1, 2) == 1);
481 assert(carla_fixedValue(0.0, 1.0, -1.0) == 0.0);
482 assert(carla_fixedValue(0.0, 1.0, 0.0) == 0.0);
483 assert(carla_fixedValue(0.0, 1.0, 1.0) == 1.0);
484 assert(carla_fixedValue(0.0, 1.0, 2.0) == 1.0);
486 assert(carla_fixedValue(0.0, 1.0, -0.1) == 0.0);
487 assert(carla_fixedValue(0.0, 1.0, 1.1) == 1.0);
489 // Get next power of 2.
490 assert(carla_nextPowerOf2(0) == 0);
491 assert(carla_nextPowerOf2(1) == 1);
492 assert(carla_nextPowerOf2(2) == 2);
493 assert(carla_nextPowerOf2(4) == 4);
494 assert(carla_nextPowerOf2(5) == 8);
495 assert(carla_nextPowerOf2(6) == 8);
496 assert(carla_nextPowerOf2(7) == 8);
497 assert(carla_nextPowerOf2(8) == 8);
498 assert(carla_nextPowerOf2(9) == 16);
501 assert((power
= carla_nextPowerOf2(power
+1)) == 2);
502 assert((power
= carla_nextPowerOf2(power
+1)) == 4);
503 assert((power
= carla_nextPowerOf2(power
+1)) == 8);
504 assert((power
= carla_nextPowerOf2(power
+1)) == 16);
505 assert((power
= carla_nextPowerOf2(power
+1)) == 32);
506 assert((power
= carla_nextPowerOf2(power
+1)) == 64);
507 assert((power
= carla_nextPowerOf2(power
+1)) == 128);
508 assert((power
= carla_nextPowerOf2(power
+1)) == 256);
509 assert((power
= carla_nextPowerOf2(power
+1)) == 512);
510 assert((power
= carla_nextPowerOf2(power
+1)) == 1024);
511 assert((power
= carla_nextPowerOf2(power
+1)) == 2048);
512 assert((power
= carla_nextPowerOf2(power
+1)) == 4096);
513 assert((power
= carla_nextPowerOf2(power
+1)) == 8192);
514 assert((power
= carla_nextPowerOf2(power
+1)) == 16384);
515 assert((power
= carla_nextPowerOf2(power
+1)) == 32768);
516 assert((power
= carla_nextPowerOf2(power
+1)) == 65536);
517 assert((power
= carla_nextPowerOf2(power
+1)) == 131072);
519 // ----------------------------------------------------------------------------------------------------------------
520 // math functions (extended)
522 // carla_addFloat, carla_copyFloat & carla_zeroFloat tests skipped
523 // mostly unused due to water::FloatVectorOperations
528 // --------------------------------------------------------------------------------------------------------------------
530 static void test_CarlaBackendUtils() noexcept
532 CARLA_BACKEND_USE_NAMESPACE
533 carla_stdout(PluginOption2Str(PLUGIN_OPTION_FIXED_BUFFERS
));
534 carla_stdout(BinaryType2Str(BINARY_NONE
));
535 carla_stdout(PluginType2Str(PLUGIN_NONE
));
536 carla_stdout(PluginCategory2Str(PLUGIN_CATEGORY_NONE
));
537 carla_stdout(ParameterType2Str(PARAMETER_UNKNOWN
));
538 carla_stdout(InternalParameterIndex2Str(PARAMETER_NULL
));
539 carla_stdout(EngineCallbackOpcode2Str(ENGINE_CALLBACK_DEBUG
));
540 carla_stdout(EngineOption2Str(ENGINE_OPTION_DEBUG
));
541 carla_stdout(EngineProcessMode2Str(ENGINE_PROCESS_MODE_SINGLE_CLIENT
));
542 carla_stdout(EngineTransportMode2Str(ENGINE_TRANSPORT_MODE_INTERNAL
));
543 carla_stdout(FileCallbackOpcode2Str(FILE_CALLBACK_DEBUG
));
544 carla_stdout(getPluginTypeAsString(PLUGIN_INTERNAL
));
545 carla_stdout(PatchbayIcon2Str(PATCHBAY_ICON_APPLICATION
));
546 getPluginTypeFromString("none");
547 getPluginCategoryFromName("cat");
550 // --------------------------------------------------------------------------------------------------------------------
552 static void test_CarlaBridgeUtils() noexcept
554 carla_stdout(PluginBridgeOscInfoType2str(kPluginBridgeOscPong
));
555 carla_stdout(PluginBridgeRtOpcode2str(kPluginBridgeRtNull
));
556 carla_stdout(PluginBridgeNonRtOpcode2str(kPluginBridgeNonRtNull
));
559 // --------------------------------------------------------------------------------------------------------------------
561 static void test_CarlaEngineUtils() noexcept
563 CARLA_BACKEND_USE_NAMESPACE
564 carla_stdout(EngineType2Str(kEngineTypeNull
));
565 carla_stdout(EnginePortType2Str(kEnginePortTypeNull
));
566 carla_stdout(EngineEventType2Str(kEngineEventTypeNull
));
567 carla_stdout(EngineControlEventType2Str(kEngineControlEventTypeNull
));
571 // --------------------------------------------------------------------------------------------------------------------
577 //test_CarlaMathUtils();
579 //test_CarlaBackendUtils();
580 //test_CarlaBridgeUtils();
581 //test_CarlaEngineUtils();
586 // --------------------------------------------------------------------------------------------------------------------