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.

509 lines
15KB

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