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