Audio plugin host https://kx.studio/carla
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.

CarlaUtils1.cpp 17KB

6 years ago
6 years ago
6 years ago
6 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Carla Utility Tests
  3. * Copyright (C) 2013-2015 Filipe Coelho <falktx@falktx.com>
  4. *
  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.
  9. *
  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.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifdef NDEBUG
  18. # error Build this file with debug ON please
  19. #endif
  20. #include "CarlaUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. // #include "CarlaBackendUtils.hpp"
  23. // #include "CarlaBridgeUtils.hpp"
  24. // #include "CarlaEngineUtils.hpp"
  25. // --------------------------------------------------------------------------------------------------------------------
  26. static void test_CarlaUtils()
  27. {
  28. // ----------------------------------------------------------------------------------------------------------------
  29. // misc functions
  30. {
  31. bool2str(false);
  32. bool2str(true);
  33. pass();
  34. char strBuf[2];
  35. nullStrBuf(strBuf);
  36. char* const strBuf2(strBuf+1);
  37. nullStrBuf(strBuf2);
  38. }
  39. // ----------------------------------------------------------------------------------------------------------------
  40. // string print functions
  41. {
  42. carla_debug("DEBUG");
  43. carla_stdout("STDOUT %s", bool2str(true));
  44. carla_stderr("STDERR %s", bool2str(false));
  45. carla_stderr2("STDERR2 " P_UINT64, 0xffffffff); // 4294967295
  46. }
  47. // ----------------------------------------------------------------------------------------------------------------
  48. // carla_*sleep
  49. {
  50. carla_sleep(1);
  51. carla_msleep(1);
  52. }
  53. // ----------------------------------------------------------------------------------------------------------------
  54. // carla_setenv
  55. {
  56. carla_setenv("THIS", "THAT");
  57. assert(std::strcmp(std::getenv("THIS"), "THAT") == 0);
  58. carla_unsetenv("THIS");
  59. assert(std::getenv("THIS") == nullptr);
  60. }
  61. // ----------------------------------------------------------------------------------------------------------------
  62. // carla_strdup
  63. {
  64. // with variables
  65. const char* const str1(carla_strdup("stringN"));
  66. const char* const strF(carla_strdup_free(strdup("stringF")));
  67. const char* const str2(carla_strdup_safe("stringS"));
  68. delete[] str1;
  69. delete[] str2;
  70. delete[] strF;
  71. // without variables
  72. delete[] carla_strdup("string_normal");
  73. delete[] carla_strdup_free(strdup("string_free"));
  74. delete[] carla_strdup_safe("string_safe");
  75. }
  76. {
  77. // common use case in Carla code
  78. struct TestStruct {
  79. const char* strNull;
  80. const char* strNormal;
  81. const char* strFree;
  82. const char* strSafe;
  83. TestStruct()
  84. : strNull(nullptr),
  85. strNormal(carla_strdup("strNormal")),
  86. strFree(carla_strdup_free(strdup("strFree"))),
  87. strSafe(carla_strdup_safe("strSafe")) {}
  88. ~TestStruct() noexcept
  89. {
  90. if (strNull != nullptr)
  91. {
  92. delete[] strNull;
  93. strNull = nullptr;
  94. }
  95. if (strNormal != nullptr)
  96. {
  97. delete[] strNormal;
  98. strNormal = nullptr;
  99. }
  100. if (strFree != nullptr)
  101. {
  102. delete[] strFree;
  103. strFree = nullptr;
  104. }
  105. if (strSafe != nullptr)
  106. {
  107. delete[] strSafe;
  108. strSafe = nullptr;
  109. }
  110. }
  111. CARLA_DECLARE_NON_COPY_STRUCT(TestStruct)
  112. };
  113. TestStruct a, b, c;
  114. }
  115. // ----------------------------------------------------------------------------------------------------------------
  116. // memory functions
  117. {
  118. int a1[] = { 4, 3, 2, 1 };
  119. int a2[] = { 4, 3, 2, 1 };
  120. int b1[] = { 1, 2, 3, 4 };
  121. int b2[] = { 1, 2, 3, 4 };
  122. carla_add(a1, b1, 4);
  123. assert(a1[0] == 5);
  124. assert(a1[1] == 5);
  125. assert(a1[2] == 5);
  126. assert(a1[3] == 5);
  127. carla_add(b1, a2, 4);
  128. assert(b1[0] == 5);
  129. assert(b1[1] == 5);
  130. assert(b1[2] == 5);
  131. assert(b1[3] == 5);
  132. assert(a1[0] == b1[0]);
  133. assert(a1[1] == b1[1]);
  134. assert(a1[2] == b1[2]);
  135. assert(a1[3] == b1[3]);
  136. carla_copy(a1, b2, 4);
  137. assert(a1[0] != b1[0]);
  138. assert(a1[1] != b1[1]);
  139. assert(a1[2] != b1[2]);
  140. assert(a1[3] != b1[3]);
  141. assert(a1[0] == b2[0]);
  142. assert(a1[1] == b2[1]);
  143. assert(a1[2] == b2[2]);
  144. assert(a1[3] == b2[3]);
  145. carla_copy(a1, b1, 4);
  146. assert(a1[0] == b1[0]);
  147. assert(a1[1] == b1[1]);
  148. assert(a1[2] == b1[2]);
  149. assert(a1[3] == b1[3]);
  150. carla_copy(a1, b2, 2);
  151. assert(a1[0] != b1[0]);
  152. assert(a1[1] != b1[1]);
  153. assert(a1[2] == b1[2]);
  154. assert(a1[3] == b1[3]);
  155. carla_copy(a1+2, b2+2, 2);
  156. assert(a1[0] != b1[0]);
  157. assert(a1[1] != b1[1]);
  158. assert(a1[2] != b1[2]);
  159. assert(a1[3] != b1[3]);
  160. carla_copy(a1, b1, 2);
  161. assert(a1[0] == b1[0]);
  162. assert(a1[1] == b1[1]);
  163. assert(a1[2] != b1[2]);
  164. assert(a1[3] != b1[3]);
  165. carla_copy(a1+2, b1+2, 2);
  166. assert(a1[0] == b1[0]);
  167. assert(a1[1] == b1[1]);
  168. assert(a1[2] == b1[2]);
  169. assert(a1[3] == b1[3]);
  170. carla_fill(a1, 0, 4);
  171. assert(a1[0] == 0);
  172. assert(a1[1] == 0);
  173. assert(a1[2] == 0);
  174. assert(a1[3] == 0);
  175. carla_fill(a1, -11, 4);
  176. assert(a1[0] == -11);
  177. assert(a1[1] == -11);
  178. assert(a1[2] == -11);
  179. assert(a1[3] == -11);
  180. carla_fill(a1, 1791, 2);
  181. assert(a1[0] == 1791);
  182. assert(a1[1] == 1791);
  183. assert(a1[2] == -11);
  184. assert(a1[3] == -11);
  185. carla_fill(a1+2, 1791, 2);
  186. assert(a1[0] == 1791);
  187. assert(a1[1] == 1791);
  188. assert(a1[2] == 1791);
  189. assert(a1[3] == 1791);
  190. int16_t d = 1527, d2 = d, e = 0;
  191. carla_add(&d, &d2, 1);
  192. assert(d == 1527*2);
  193. carla_add(&d, &d2, 1);
  194. assert(d == 1527*3);
  195. carla_add(&d, &e, 1);
  196. assert(d == 1527*3);
  197. assert(e == 0);
  198. carla_add(&e, &d, 1);
  199. assert(d == e);
  200. carla_add(&e, &d, 1);
  201. assert(e == d*2);
  202. d = -e;
  203. carla_add(&d, &e, 1);
  204. assert(d == 0);
  205. }
  206. {
  207. bool x;
  208. const bool f = false, t = true;
  209. carla_copy(&x, &t, 1);
  210. assert(x);
  211. carla_copy(&x, &f, 1);
  212. assert(! x);
  213. carla_fill(&x, true, 1);
  214. assert(x);
  215. carla_fill(&x, false, 1);
  216. assert(! x);
  217. }
  218. {
  219. uint8_t a[] = { 3, 2, 1, 0 };
  220. carla_zeroBytes(a, 1);
  221. assert(a[0] == 0);
  222. assert(a[1] == 2);
  223. assert(a[2] == 1);
  224. assert(a[3] == 0);
  225. carla_zeroBytes(a+1, 2);
  226. assert(a[0] == 0);
  227. assert(a[1] == 0);
  228. assert(a[2] == 0);
  229. assert(a[3] == 0);
  230. }
  231. {
  232. char a[501];
  233. for (int i=500; --i>=0;)
  234. a[i] = 'a';
  235. carla_zeroChars(a, 501);
  236. for (int i=501; --i>=0;)
  237. assert(a[i] == '\0');
  238. for (int i=500; --i>=0;)
  239. a[i] = 'a';
  240. assert(std::strlen(a) == 500);
  241. carla_fill(a+200, '\0', 1);
  242. assert(std::strlen(a) == 200);
  243. }
  244. {
  245. void* a[33];
  246. carla_zeroPointers(a, 33);
  247. for (int i=33; --i>=0;)
  248. assert(a[i] == nullptr);
  249. }
  250. {
  251. struct Thing {
  252. char c;
  253. int i;
  254. int64_t h;
  255. bool operator==(const Thing& t) const noexcept
  256. {
  257. return (t.c == c && t.i == i && t.h == h);
  258. }
  259. bool operator!=(const Thing& t) const noexcept
  260. {
  261. return !operator==(t);
  262. }
  263. };
  264. Thing a, b, c;
  265. a.c = 0;
  266. a.i = 0;
  267. a.h = 0;
  268. b.c = 64;
  269. b.i = 64;
  270. b.h = 64;
  271. c = a;
  272. carla_copyStruct(a, b);
  273. assert(a == b);
  274. carla_zeroStruct(a);
  275. assert(a == c);
  276. carla_copyStruct(c, b);
  277. assert(a != c);
  278. // make it non-zero
  279. a.c = 1;
  280. Thing d[3];
  281. carla_zeroStructs(d, 3);
  282. assert(d[0] != a);
  283. assert(d[1] != b);
  284. assert(d[2] != c);
  285. carla_copyStructs(d, &a, 1);
  286. assert(d[0] == a);
  287. assert(d[1] != b);
  288. assert(d[2] != c);
  289. carla_copyStructs(&c, d+2, 1);
  290. assert(d[0] == a);
  291. assert(d[1] != b);
  292. assert(d[2] == c);
  293. }
  294. }
  295. #if 0
  296. // --------------------------------------------------------------------------------------------------------------------
  297. static void test_CarlaMathUtils() noexcept
  298. {
  299. // ----------------------------------------------------------------------------------------------------------------
  300. // math functions (base)
  301. // Return the lower of 2 values, with 'min' as the minimum possible value.
  302. assert(carla_min(0, 0, 0) == 0);
  303. assert(carla_min(0, 3, 0) == 0);
  304. assert(carla_min(3, 0, 0) == 0);
  305. assert(carla_min(0, 0, 3) == 3);
  306. assert(carla_min(0, 1, 3) == 3);
  307. assert(carla_min(0, 2, 3) == 3);
  308. assert(carla_min(0, 3, 3) == 3);
  309. assert(carla_min(0, 4, 3) == 3);
  310. assert(carla_min(5, 0, 3) == 3);
  311. assert(carla_min(5, 1, 3) == 3);
  312. assert(carla_min(5, 2, 3) == 3);
  313. assert(carla_min(5, 3, 3) == 3);
  314. assert(carla_min(5, 4, 3) == 4);
  315. assert(carla_min(5, 5, 3) == 5);
  316. assert(carla_min(5, 6, 3) == 5);
  317. assert(carla_min(2, -2, 0) == 0);
  318. assert(carla_min(2, -1, 0) == 0);
  319. assert(carla_min(2, 0, 0) == 0);
  320. assert(carla_min(2, 1, 0) == 1);
  321. assert(carla_min(2, 2, 0) == 2);
  322. assert(carla_min(5, -6, 3) == 3);
  323. assert(carla_min(5, -5, 3) == 3);
  324. assert(carla_min(5, -4, 3) == 3);
  325. assert(carla_min(5, -3, 3) == 3);
  326. assert(carla_min(5, -2, 3) == 3);
  327. assert(carla_min(5, -1, 3) == 3);
  328. assert(carla_min(5, 0, 3) == 3);
  329. assert(carla_min(5, 1, 3) == 3);
  330. assert(carla_min(5, 2, 3) == 3);
  331. assert(carla_min(5, 3, 3) == 3);
  332. assert(carla_min(5, 4, 3) == 4);
  333. assert(carla_min(5, 5, 3) == 5);
  334. assert(carla_min(5, 6, 3) == 5);
  335. assert(carla_min(5, -6, -3) == -3);
  336. assert(carla_min(5, -5, -3) == -3);
  337. assert(carla_min(5, -4, -3) == -3);
  338. assert(carla_min(5, -3, -3) == -3);
  339. assert(carla_min(5, -2, -3) == -2);
  340. assert(carla_min(5, -1, -3) == -1);
  341. assert(carla_min(5, 0, -3) == 0);
  342. assert(carla_min(5, 1, -3) == 1);
  343. assert(carla_min(5, 2, -3) == 2);
  344. assert(carla_min(5, 3, -3) == 3);
  345. assert(carla_min(5, 4, -3) == 4);
  346. assert(carla_min(5, 5, -3) == 5);
  347. assert(carla_min(5, 6, -3) == 5);
  348. // Return the lower positive of 2 values.
  349. assert(carla_minPositive(0, 0) == 0);
  350. assert(carla_minPositive(0, 1) == 0);
  351. assert(carla_minPositive(0, 2) == 0);
  352. assert(carla_minPositive(0, 3) == 0);
  353. assert(carla_minPositive(1, 0) == 0);
  354. assert(carla_minPositive(1, 1) == 1);
  355. assert(carla_minPositive(1, 2) == 1);
  356. assert(carla_minPositive(1, 3) == 1);
  357. assert(carla_minPositive(3, 0) == 0);
  358. assert(carla_minPositive(3, 1) == 1);
  359. assert(carla_minPositive(3, 2) == 2);
  360. assert(carla_minPositive(3, 3) == 3);
  361. assert(carla_minPositive(-1, 0) == 0);
  362. assert(carla_minPositive(-1, 1) == 1);
  363. assert(carla_minPositive(-1, 2) == 2);
  364. assert(carla_minPositive(-1, 3) == 3);
  365. // Return the higher of 2 values, with 'max' as the maximum possible value.
  366. assert(carla_max(0, 0, 0) == 0);
  367. assert(carla_max(0, 3, 0) == 0);
  368. assert(carla_max(3, 0, 0) == 0);
  369. assert(carla_max(0, 0, 3) == 0);
  370. assert(carla_max(0, 1, 3) == 1);
  371. assert(carla_max(0, 2, 3) == 2);
  372. assert(carla_max(0, 3, 3) == 3);
  373. assert(carla_max(0, 4, 3) == 3);
  374. assert(carla_max(5, 0, 3) == 3);
  375. assert(carla_max(5, 1, 3) == 3);
  376. assert(carla_max(5, 2, 3) == 3);
  377. assert(carla_max(5, 3, 3) == 3);
  378. assert(carla_max(5, 4, 3) == 3);
  379. assert(carla_max(5, 5, 3) == 3);
  380. assert(carla_max(5, 6, 3) == 3);
  381. // Fix bounds of 'value' between 'min' and 'max'.
  382. assert(carla_fixedValue(0, 1, -1) == 0);
  383. assert(carla_fixedValue(0, 1, 0) == 0);
  384. assert(carla_fixedValue(0, 1, 1) == 1);
  385. assert(carla_fixedValue(0, 1, 2) == 1);
  386. assert(carla_fixedValue(0.0, 1.0, -1.0) == 0.0);
  387. assert(carla_fixedValue(0.0, 1.0, 0.0) == 0.0);
  388. assert(carla_fixedValue(0.0, 1.0, 1.0) == 1.0);
  389. assert(carla_fixedValue(0.0, 1.0, 2.0) == 1.0);
  390. assert(carla_fixedValue(0.0, 1.0, -0.1) == 0.0);
  391. assert(carla_fixedValue(0.0, 1.0, 1.1) == 1.0);
  392. // Get next power of 2.
  393. assert(carla_nextPowerOf2(0) == 0);
  394. assert(carla_nextPowerOf2(1) == 1);
  395. assert(carla_nextPowerOf2(2) == 2);
  396. assert(carla_nextPowerOf2(4) == 4);
  397. assert(carla_nextPowerOf2(5) == 8);
  398. assert(carla_nextPowerOf2(6) == 8);
  399. assert(carla_nextPowerOf2(7) == 8);
  400. assert(carla_nextPowerOf2(8) == 8);
  401. assert(carla_nextPowerOf2(9) == 16);
  402. uint32_t power = 1;
  403. assert((power = carla_nextPowerOf2(power+1)) == 2);
  404. assert((power = carla_nextPowerOf2(power+1)) == 4);
  405. assert((power = carla_nextPowerOf2(power+1)) == 8);
  406. assert((power = carla_nextPowerOf2(power+1)) == 16);
  407. assert((power = carla_nextPowerOf2(power+1)) == 32);
  408. assert((power = carla_nextPowerOf2(power+1)) == 64);
  409. assert((power = carla_nextPowerOf2(power+1)) == 128);
  410. assert((power = carla_nextPowerOf2(power+1)) == 256);
  411. assert((power = carla_nextPowerOf2(power+1)) == 512);
  412. assert((power = carla_nextPowerOf2(power+1)) == 1024);
  413. assert((power = carla_nextPowerOf2(power+1)) == 2048);
  414. assert((power = carla_nextPowerOf2(power+1)) == 4096);
  415. assert((power = carla_nextPowerOf2(power+1)) == 8192);
  416. assert((power = carla_nextPowerOf2(power+1)) == 16384);
  417. assert((power = carla_nextPowerOf2(power+1)) == 32768);
  418. assert((power = carla_nextPowerOf2(power+1)) == 65536);
  419. assert((power = carla_nextPowerOf2(power+1)) == 131072);
  420. // ----------------------------------------------------------------------------------------------------------------
  421. // math functions (extended)
  422. // carla_addFloat, carla_copyFloat & carla_zeroFloat tests skipped
  423. // mostly unused due to water::FloatVectorOperations
  424. }
  425. #endif
  426. #if 0
  427. // --------------------------------------------------------------------------------------------------------------------
  428. static void test_CarlaBackendUtils() noexcept
  429. {
  430. CARLA_BACKEND_USE_NAMESPACE
  431. carla_stdout(PluginOption2Str(PLUGIN_OPTION_FIXED_BUFFERS));
  432. carla_stdout(BinaryType2Str(BINARY_NONE));
  433. carla_stdout(PluginType2Str(PLUGIN_NONE));
  434. carla_stdout(PluginCategory2Str(PLUGIN_CATEGORY_NONE));
  435. carla_stdout(ParameterType2Str(PARAMETER_UNKNOWN));
  436. carla_stdout(InternalParameterIndex2Str(PARAMETER_NULL));
  437. carla_stdout(EngineCallbackOpcode2Str(ENGINE_CALLBACK_DEBUG));
  438. carla_stdout(EngineOption2Str(ENGINE_OPTION_DEBUG));
  439. carla_stdout(EngineProcessMode2Str(ENGINE_PROCESS_MODE_SINGLE_CLIENT));
  440. carla_stdout(EngineTransportMode2Str(ENGINE_TRANSPORT_MODE_INTERNAL));
  441. carla_stdout(FileCallbackOpcode2Str(FILE_CALLBACK_DEBUG));
  442. carla_stdout(getPluginTypeAsString(PLUGIN_INTERNAL));
  443. carla_stdout(PatchbayIcon2Str(PATCHBAY_ICON_APPLICATION));
  444. getPluginTypeFromString("none");
  445. getPluginCategoryFromName("cat");
  446. }
  447. // --------------------------------------------------------------------------------------------------------------------
  448. static void test_CarlaBridgeUtils() noexcept
  449. {
  450. carla_stdout(PluginBridgeOscInfoType2str(kPluginBridgeOscPong));
  451. carla_stdout(PluginBridgeRtOpcode2str(kPluginBridgeRtNull));
  452. carla_stdout(PluginBridgeNonRtOpcode2str(kPluginBridgeNonRtNull));
  453. }
  454. // --------------------------------------------------------------------------------------------------------------------
  455. static void test_CarlaEngineUtils() noexcept
  456. {
  457. CARLA_BACKEND_USE_NAMESPACE
  458. carla_stdout(EngineType2Str(kEngineTypeNull));
  459. carla_stdout(EnginePortType2Str(kEnginePortTypeNull));
  460. carla_stdout(EngineEventType2Str(kEngineEventTypeNull));
  461. carla_stdout(EngineControlEventType2Str(kEngineControlEventTypeNull));
  462. }
  463. #endif
  464. // --------------------------------------------------------------------------------------------------------------------
  465. // main
  466. int main()
  467. {
  468. test_CarlaUtils();
  469. //test_CarlaMathUtils();
  470. //test_CarlaBackendUtils();
  471. //test_CarlaBridgeUtils();
  472. //test_CarlaEngineUtils();
  473. return 0;
  474. }
  475. // --------------------------------------------------------------------------------------------------------------------