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 15KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Carla Utility Tests
  3. * Copyright (C) 2013-2014 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[1];
  35. nullStrBuf(strBuf);
  36. }
  37. // -------------------------------------------------------------------
  38. // string print functions
  39. {
  40. carla_debug("DEBUG");
  41. carla_stdout("STDOUT %s", bool2str(true));
  42. carla_stderr("STDERR %s", bool2str(false));
  43. carla_stderr2("STDERR2 " P_UINT64, 0xffffffff); // 4294967295
  44. }
  45. // -------------------------------------------------------------------
  46. // carla_*sleep
  47. {
  48. carla_sleep(1);
  49. carla_msleep(1);
  50. }
  51. // -------------------------------------------------------------------
  52. // carla_setenv
  53. {
  54. carla_setenv("THIS", "THAT");
  55. assert(std::strcmp(std::getenv("THIS"), "THAT") == 0);
  56. }
  57. // -------------------------------------------------------------------
  58. // carla_strdup
  59. {
  60. // with variables
  61. const char* const str1(carla_strdup("string1"));
  62. const char* const strF(carla_strdup_free(strdup("string2")));
  63. delete[] str1;
  64. delete[] strF;
  65. // without variables
  66. delete[] carla_strdup("string3");
  67. delete[] carla_strdup_free(strdup("string4"));
  68. // common use case in Carla code
  69. struct TestStruct {
  70. const char* str1;
  71. const char* str2;
  72. const char* str3;
  73. const char* str4;
  74. TestStruct()
  75. : str1(carla_strdup("str1")),
  76. str2(carla_strdup("str2")),
  77. str3(nullptr),
  78. str4(carla_strdup("str4")) {}
  79. ~TestStruct() noexcept
  80. {
  81. if (str1 != nullptr)
  82. {
  83. delete[] str1;
  84. str1 = nullptr;
  85. }
  86. if (str2 != nullptr)
  87. {
  88. delete[] str2;
  89. str2 = nullptr;
  90. }
  91. if (str3 != nullptr)
  92. {
  93. delete[] str3;
  94. str3 = nullptr;
  95. }
  96. if (str4 != nullptr)
  97. {
  98. delete[] str4;
  99. str4 = nullptr;
  100. }
  101. }
  102. };
  103. TestStruct a, b, c;
  104. }
  105. // -------------------------------------------------------------------
  106. // memory functions
  107. {
  108. int a[] = { 4, 3, 2, 1 };
  109. int b[] = { 1, 2, 3, 4 };
  110. carla_add(a, b, 4);
  111. assert(a[0] == 5);
  112. assert(a[1] == 5);
  113. assert(a[2] == 5);
  114. assert(a[3] == 5);
  115. int c[] = { 4, 3, 2, 1 };
  116. carla_add(b, c, 4);
  117. assert(a[0] == b[0]);
  118. assert(a[1] == b[1]);
  119. assert(a[2] == b[2]);
  120. assert(a[3] == b[3]);
  121. carla_copy(a, c, 2);
  122. carla_copy(b+2, c+2, 2);
  123. assert(a[0] == c[0]);
  124. assert(a[1] == c[1]);
  125. assert(b[2] == c[2]);
  126. assert(b[3] == c[3]);
  127. carla_copy(c, a, 4);
  128. assert(a[0] == c[0]);
  129. assert(a[1] == c[1]);
  130. assert(a[2] == c[2]);
  131. assert(a[3] == c[3]);
  132. carla_fill(a, 0, 4);
  133. assert(a[0] == 0);
  134. assert(a[1] == 0);
  135. assert(a[2] == 0);
  136. assert(a[3] == 0);
  137. carla_fill(a, -11, 4);
  138. assert(a[0] == -11);
  139. assert(a[1] == -11);
  140. assert(a[2] == -11);
  141. assert(a[3] == -11);
  142. carla_fill(a+0, 17, 2);
  143. carla_fill(a+2, 23, 2);
  144. assert(a[0] == 17);
  145. assert(a[1] == 17);
  146. assert(a[2] == 23);
  147. assert(a[3] == 23);
  148. carla_zeroBytes(a, sizeof(int)*4);
  149. assert(a[0] == 0);
  150. assert(a[1] == 0);
  151. assert(a[2] == 0);
  152. assert(a[3] == 0);
  153. char strBuf[501];
  154. strBuf[0] = strBuf[499] = strBuf[500] = '!';
  155. carla_zeroChar(strBuf, 501);
  156. for (int i=0; i<501; ++i)
  157. assert(strBuf[i] == '\0');
  158. int d = 1527, e = 0;
  159. carla_add(&d, &d, 1);
  160. carla_add(&d, &e, 1);
  161. carla_add(&e, &d, 1);
  162. assert(d == 1527*2);
  163. assert(d == e);
  164. e = -e;
  165. carla_add(&d, &e, 1);
  166. assert(d == 0);
  167. carla_copy(&d, &e, 1);
  168. assert(d == -1527*2);
  169. assert(d == e);
  170. carla_fill(&d, 9999, 1);
  171. carla_fill(&e, 123, 1);
  172. assert(d == 9999);
  173. assert(e == 123);
  174. carla_zeroStruct(d);
  175. assert(d == 0);
  176. carla_copyStruct(d, e);
  177. assert(d != 0);
  178. assert(d == e);
  179. carla_fill(a+0, 1, 1);
  180. carla_fill(a+1, 2, 1);
  181. carla_fill(a+2, 3, 1);
  182. carla_fill(a+3, 4, 1);
  183. assert(a[0] == 1);
  184. assert(a[1] == 2);
  185. assert(a[2] == 3);
  186. assert(a[3] == 4);
  187. carla_copyStruct(c, a, 4);
  188. assert(c[0] == a[0]);
  189. assert(c[1] == a[1]);
  190. assert(c[2] == a[2]);
  191. assert(c[3] == a[3]);
  192. carla_zeroStruct(c, 4);
  193. assert(c[0] == 0);
  194. assert(c[1] == 0);
  195. assert(c[2] == 0);
  196. assert(c[3] == 0);
  197. float fl[5];
  198. carla_fill(fl, 1.11f, 5);
  199. assert(fl[0] == 1.11f);
  200. assert(fl[1] == 1.11f);
  201. assert(fl[2] == 1.11f);
  202. assert(fl[3] == 1.11f);
  203. assert(fl[4] == 1.11f);
  204. carla_add(fl, fl, 5);
  205. assert(fl[0] == 1.11f*2);
  206. assert(fl[1] == 1.11f*2);
  207. assert(fl[2] == 1.11f*2);
  208. assert(fl[3] == 1.11f*2);
  209. assert(fl[4] == 1.11f*2);
  210. carla_add(fl, fl, 4);
  211. assert(fl[0] == 1.11f*4);
  212. assert(fl[1] == 1.11f*4);
  213. assert(fl[2] == 1.11f*4);
  214. assert(fl[3] == 1.11f*4);
  215. assert(fl[4] == 1.11f*2);
  216. carla_add(fl, fl, 3);
  217. assert(fl[0] == 1.11f*8);
  218. assert(fl[1] == 1.11f*8);
  219. assert(fl[2] == 1.11f*8);
  220. assert(fl[3] == 1.11f*4);
  221. assert(fl[4] == 1.11f*2);
  222. carla_add(fl, fl, 2);
  223. assert(fl[0] == 1.11f*16);
  224. assert(fl[1] == 1.11f*16);
  225. assert(fl[2] == 1.11f*8);
  226. assert(fl[3] == 1.11f*4);
  227. assert(fl[4] == 1.11f*2);
  228. carla_add(fl, fl, 1);
  229. assert(fl[0] == 1.11f*32);
  230. assert(fl[1] == 1.11f*16);
  231. assert(fl[2] == 1.11f*8);
  232. assert(fl[3] == 1.11f*4);
  233. assert(fl[4] == 1.11f*2);
  234. }
  235. }
  236. // -----------------------------------------------------------------------
  237. static void test_CarlaMathUtils() noexcept
  238. {
  239. // -------------------------------------------------------------------
  240. // math functions (base)
  241. // Return the lower of 2 values, with 'min' as the minimum possible value.
  242. assert(carla_min(0, 0, 0) == 0);
  243. assert(carla_min(0, 3, 0) == 0);
  244. assert(carla_min(3, 0, 0) == 0);
  245. assert(carla_min(0, 0, 3) == 3);
  246. assert(carla_min(0, 1, 3) == 3);
  247. assert(carla_min(0, 2, 3) == 3);
  248. assert(carla_min(0, 3, 3) == 3);
  249. assert(carla_min(0, 4, 3) == 3);
  250. assert(carla_min(5, 0, 3) == 3);
  251. assert(carla_min(5, 1, 3) == 3);
  252. assert(carla_min(5, 2, 3) == 3);
  253. assert(carla_min(5, 3, 3) == 3);
  254. assert(carla_min(5, 4, 3) == 4);
  255. assert(carla_min(5, 5, 3) == 5);
  256. assert(carla_min(5, 6, 3) == 5);
  257. assert(carla_min(2, -2, 0) == 0);
  258. assert(carla_min(2, -1, 0) == 0);
  259. assert(carla_min(2, 0, 0) == 0);
  260. assert(carla_min(2, 1, 0) == 1);
  261. assert(carla_min(2, 2, 0) == 2);
  262. assert(carla_min(5, -6, 3) == 3);
  263. assert(carla_min(5, -5, 3) == 3);
  264. assert(carla_min(5, -4, 3) == 3);
  265. assert(carla_min(5, -3, 3) == 3);
  266. assert(carla_min(5, -2, 3) == 3);
  267. assert(carla_min(5, -1, 3) == 3);
  268. assert(carla_min(5, 0, 3) == 3);
  269. assert(carla_min(5, 1, 3) == 3);
  270. assert(carla_min(5, 2, 3) == 3);
  271. assert(carla_min(5, 3, 3) == 3);
  272. assert(carla_min(5, 4, 3) == 4);
  273. assert(carla_min(5, 5, 3) == 5);
  274. assert(carla_min(5, 6, 3) == 5);
  275. assert(carla_min(5, -6, -3) == -3);
  276. assert(carla_min(5, -5, -3) == -3);
  277. assert(carla_min(5, -4, -3) == -3);
  278. assert(carla_min(5, -3, -3) == -3);
  279. assert(carla_min(5, -2, -3) == -2);
  280. assert(carla_min(5, -1, -3) == -1);
  281. assert(carla_min(5, 0, -3) == 0);
  282. assert(carla_min(5, 1, -3) == 1);
  283. assert(carla_min(5, 2, -3) == 2);
  284. assert(carla_min(5, 3, -3) == 3);
  285. assert(carla_min(5, 4, -3) == 4);
  286. assert(carla_min(5, 5, -3) == 5);
  287. assert(carla_min(5, 6, -3) == 5);
  288. // Return the lower positive of 2 values.
  289. assert(carla_minPositive(0, 0) == 0);
  290. assert(carla_minPositive(0, 1) == 0);
  291. assert(carla_minPositive(0, 2) == 0);
  292. assert(carla_minPositive(0, 3) == 0);
  293. assert(carla_minPositive(1, 0) == 0);
  294. assert(carla_minPositive(1, 1) == 1);
  295. assert(carla_minPositive(1, 2) == 1);
  296. assert(carla_minPositive(1, 3) == 1);
  297. assert(carla_minPositive(3, 0) == 0);
  298. assert(carla_minPositive(3, 1) == 1);
  299. assert(carla_minPositive(3, 2) == 2);
  300. assert(carla_minPositive(3, 3) == 3);
  301. assert(carla_minPositive(-1, 0) == 0);
  302. assert(carla_minPositive(-1, 1) == 1);
  303. assert(carla_minPositive(-1, 2) == 2);
  304. assert(carla_minPositive(-1, 3) == 3);
  305. // Return the higher of 2 values, with 'max' as the maximum possible value.
  306. assert(carla_max(0, 0, 0) == 0);
  307. assert(carla_max(0, 3, 0) == 0);
  308. assert(carla_max(3, 0, 0) == 0);
  309. assert(carla_max(0, 0, 3) == 0);
  310. assert(carla_max(0, 1, 3) == 1);
  311. assert(carla_max(0, 2, 3) == 2);
  312. assert(carla_max(0, 3, 3) == 3);
  313. assert(carla_max(0, 4, 3) == 3);
  314. assert(carla_max(5, 0, 3) == 3);
  315. assert(carla_max(5, 1, 3) == 3);
  316. assert(carla_max(5, 2, 3) == 3);
  317. assert(carla_max(5, 3, 3) == 3);
  318. assert(carla_max(5, 4, 3) == 3);
  319. assert(carla_max(5, 5, 3) == 3);
  320. assert(carla_max(5, 6, 3) == 3);
  321. // Fix bounds of 'value' between 'min' and 'max'.
  322. assert(carla_fixValue(0, 1, -1) == 0);
  323. assert(carla_fixValue(0, 1, 0) == 0);
  324. assert(carla_fixValue(0, 1, 1) == 1);
  325. assert(carla_fixValue(0, 1, 2) == 1);
  326. assert(carla_fixValue(0.0, 1.0, -1.0) == 0.0);
  327. assert(carla_fixValue(0.0, 1.0, 0.0) == 0.0);
  328. assert(carla_fixValue(0.0, 1.0, 1.0) == 1.0);
  329. assert(carla_fixValue(0.0, 1.0, 2.0) == 1.0);
  330. assert(carla_fixValue(0.0, 1.0, -0.1) == 0.0);
  331. assert(carla_fixValue(0.0, 1.0, 1.1) == 1.0);
  332. // Get next power of 2.
  333. assert(carla_nextPowerOf2(0) == 0);
  334. assert(carla_nextPowerOf2(1) == 1);
  335. assert(carla_nextPowerOf2(2) == 2);
  336. assert(carla_nextPowerOf2(4) == 4);
  337. assert(carla_nextPowerOf2(5) == 8);
  338. assert(carla_nextPowerOf2(6) == 8);
  339. assert(carla_nextPowerOf2(7) == 8);
  340. assert(carla_nextPowerOf2(8) == 8);
  341. assert(carla_nextPowerOf2(9) == 16);
  342. uint32_t power = 1;
  343. assert((power = carla_nextPowerOf2(power+1)) == 2);
  344. assert((power = carla_nextPowerOf2(power+1)) == 4);
  345. assert((power = carla_nextPowerOf2(power+1)) == 8);
  346. assert((power = carla_nextPowerOf2(power+1)) == 16);
  347. assert((power = carla_nextPowerOf2(power+1)) == 32);
  348. assert((power = carla_nextPowerOf2(power+1)) == 64);
  349. assert((power = carla_nextPowerOf2(power+1)) == 128);
  350. assert((power = carla_nextPowerOf2(power+1)) == 256);
  351. assert((power = carla_nextPowerOf2(power+1)) == 512);
  352. assert((power = carla_nextPowerOf2(power+1)) == 1024);
  353. assert((power = carla_nextPowerOf2(power+1)) == 2048);
  354. assert((power = carla_nextPowerOf2(power+1)) == 4096);
  355. assert((power = carla_nextPowerOf2(power+1)) == 8192);
  356. assert((power = carla_nextPowerOf2(power+1)) == 16384);
  357. assert((power = carla_nextPowerOf2(power+1)) == 32768);
  358. assert((power = carla_nextPowerOf2(power+1)) == 65536);
  359. assert((power = carla_nextPowerOf2(power+1)) == 131072);
  360. // -------------------------------------------------------------------
  361. // math functions (extended)
  362. // carla_addFloat, carla_copyFloat & carla_zeroFloat tests skipped
  363. // mostly unused due to juce::FloatVectorOperations
  364. }
  365. // -----------------------------------------------------------------------
  366. static void test_CarlaBackendUtils() noexcept
  367. {
  368. CARLA_BACKEND_USE_NAMESPACE
  369. carla_stdout(PluginOption2Str(PLUGIN_OPTION_FIXED_BUFFERS));
  370. carla_stdout(BinaryType2Str(BINARY_NONE));
  371. carla_stdout(PluginType2Str(PLUGIN_NONE));
  372. carla_stdout(PluginCategory2Str(PLUGIN_CATEGORY_NONE));
  373. carla_stdout(ParameterType2Str(PARAMETER_UNKNOWN));
  374. carla_stdout(InternalParameterIndex2Str(PARAMETER_NULL));
  375. carla_stdout(EngineCallbackOpcode2Str(ENGINE_CALLBACK_DEBUG));
  376. carla_stdout(EngineOption2Str(ENGINE_OPTION_DEBUG));
  377. carla_stdout(EngineProcessMode2Str(ENGINE_PROCESS_MODE_SINGLE_CLIENT));
  378. carla_stdout(EngineTransportMode2Str(ENGINE_TRANSPORT_MODE_INTERNAL));
  379. carla_stdout(FileCallbackOpcode2Str(FILE_CALLBACK_DEBUG));
  380. carla_stdout(getPluginTypeAsString(PLUGIN_INTERNAL));
  381. carla_stdout(PatchbayIcon2Str(PATCHBAY_ICON_APPLICATION));
  382. getPluginTypeFromString("none");
  383. getPluginCategoryFromName("cat");
  384. }
  385. // -----------------------------------------------------------------------
  386. static void test_CarlaBridgeUtils() noexcept
  387. {
  388. carla_stdout(PluginBridgeInfoType2str(kPluginBridgePong));
  389. carla_stdout(PluginBridgeOpcode2str(kPluginBridgeOpcodeNull));
  390. }
  391. // -----------------------------------------------------------------------
  392. static void test_CarlaEngineUtils() noexcept
  393. {
  394. CARLA_BACKEND_USE_NAMESPACE
  395. carla_stdout(EngineType2Str(kEngineTypeNull));
  396. carla_stdout(EnginePortType2Str(kEnginePortTypeNull));
  397. carla_stdout(EngineEventType2Str(kEngineEventTypeNull));
  398. carla_stdout(EngineControlEventType2Str(kEngineControlEventTypeNull));
  399. }
  400. // -----------------------------------------------------------------------
  401. // main
  402. int main()
  403. {
  404. // already tested, skip for now
  405. test_CarlaUtils();
  406. test_CarlaMathUtils();
  407. test_CarlaBackendUtils();
  408. test_CarlaBridgeUtils();
  409. test_CarlaEngineUtils();
  410. return 0;
  411. }
  412. // -----------------------------------------------------------------------