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.

497 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. #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. CARLA_DECLARE_NON_COPY_STRUCT(TestStruct)
  103. };
  104. TestStruct a, b, c;
  105. }
  106. // -------------------------------------------------------------------
  107. // memory functions
  108. {
  109. int a[] = { 4, 3, 2, 1 };
  110. int b[] = { 1, 2, 3, 4 };
  111. carla_add(a, b, 4);
  112. assert(a[0] == 5);
  113. assert(a[1] == 5);
  114. assert(a[2] == 5);
  115. assert(a[3] == 5);
  116. int c[] = { 4, 3, 2, 1 };
  117. carla_add(b, c, 4);
  118. assert(a[0] == b[0]);
  119. assert(a[1] == b[1]);
  120. assert(a[2] == b[2]);
  121. assert(a[3] == b[3]);
  122. carla_copy(a, c, 2);
  123. carla_copy(b+2, c+2, 2);
  124. assert(a[0] == c[0]);
  125. assert(a[1] == c[1]);
  126. assert(b[2] == c[2]);
  127. assert(b[3] == c[3]);
  128. carla_copy(c, a, 4);
  129. assert(a[0] == c[0]);
  130. assert(a[1] == c[1]);
  131. assert(a[2] == c[2]);
  132. assert(a[3] == c[3]);
  133. carla_fill(a, 0, 4);
  134. assert(a[0] == 0);
  135. assert(a[1] == 0);
  136. assert(a[2] == 0);
  137. assert(a[3] == 0);
  138. carla_fill(a, -11, 4);
  139. assert(a[0] == -11);
  140. assert(a[1] == -11);
  141. assert(a[2] == -11);
  142. assert(a[3] == -11);
  143. carla_fill(a+0, 17, 2);
  144. carla_fill(a+2, 23, 2);
  145. assert(a[0] == 17);
  146. assert(a[1] == 17);
  147. assert(a[2] == 23);
  148. assert(a[3] == 23);
  149. carla_zeroBytes(a, sizeof(int)*4);
  150. assert(a[0] == 0);
  151. assert(a[1] == 0);
  152. assert(a[2] == 0);
  153. assert(a[3] == 0);
  154. char strBuf[501];
  155. strBuf[0] = strBuf[499] = strBuf[500] = '!';
  156. carla_zeroChar(strBuf, 501);
  157. for (int i=0; i<501; ++i)
  158. assert(strBuf[i] == '\0');
  159. int d = 1527, e = 0;
  160. carla_add(&d, &d, 1);
  161. carla_add(&d, &e, 1);
  162. carla_add(&e, &d, 1);
  163. assert(d == 1527*2);
  164. assert(d == e);
  165. e = -e;
  166. carla_add(&d, &e, 1);
  167. assert(d == 0);
  168. carla_copy(&d, &e, 1);
  169. assert(d == -1527*2);
  170. assert(d == e);
  171. carla_fill(&d, 9999, 1);
  172. carla_fill(&e, 123, 1);
  173. assert(d == 9999);
  174. assert(e == 123);
  175. carla_zeroStruct(d);
  176. assert(d == 0);
  177. carla_copyStruct(d, e);
  178. assert(d != 0);
  179. assert(d == e);
  180. carla_fill(a+0, 1, 1);
  181. carla_fill(a+1, 2, 1);
  182. carla_fill(a+2, 3, 1);
  183. carla_fill(a+3, 4, 1);
  184. assert(a[0] == 1);
  185. assert(a[1] == 2);
  186. assert(a[2] == 3);
  187. assert(a[3] == 4);
  188. carla_copyStruct(c, a, 4);
  189. assert(c[0] == a[0]);
  190. assert(c[1] == a[1]);
  191. assert(c[2] == a[2]);
  192. assert(c[3] == a[3]);
  193. carla_zeroStruct(c, 4);
  194. assert(c[0] == 0);
  195. assert(c[1] == 0);
  196. assert(c[2] == 0);
  197. assert(c[3] == 0);
  198. float fl[5];
  199. carla_fill(fl, 1.11f, 5);
  200. assert(fl[0] == 1.11f);
  201. assert(fl[1] == 1.11f);
  202. assert(fl[2] == 1.11f);
  203. assert(fl[3] == 1.11f);
  204. assert(fl[4] == 1.11f);
  205. carla_add(fl, fl, 5);
  206. assert(fl[0] == 1.11f*2);
  207. assert(fl[1] == 1.11f*2);
  208. assert(fl[2] == 1.11f*2);
  209. assert(fl[3] == 1.11f*2);
  210. assert(fl[4] == 1.11f*2);
  211. carla_add(fl, fl, 4);
  212. assert(fl[0] == 1.11f*4);
  213. assert(fl[1] == 1.11f*4);
  214. assert(fl[2] == 1.11f*4);
  215. assert(fl[3] == 1.11f*4);
  216. assert(fl[4] == 1.11f*2);
  217. carla_add(fl, fl, 3);
  218. assert(fl[0] == 1.11f*8);
  219. assert(fl[1] == 1.11f*8);
  220. assert(fl[2] == 1.11f*8);
  221. assert(fl[3] == 1.11f*4);
  222. assert(fl[4] == 1.11f*2);
  223. carla_add(fl, fl, 2);
  224. assert(fl[0] == 1.11f*16);
  225. assert(fl[1] == 1.11f*16);
  226. assert(fl[2] == 1.11f*8);
  227. assert(fl[3] == 1.11f*4);
  228. assert(fl[4] == 1.11f*2);
  229. carla_add(fl, fl, 1);
  230. assert(fl[0] == 1.11f*32);
  231. assert(fl[1] == 1.11f*16);
  232. assert(fl[2] == 1.11f*8);
  233. assert(fl[3] == 1.11f*4);
  234. assert(fl[4] == 1.11f*2);
  235. }
  236. }
  237. // -----------------------------------------------------------------------
  238. static void test_CarlaMathUtils() noexcept
  239. {
  240. // -------------------------------------------------------------------
  241. // math functions (base)
  242. // Return the lower of 2 values, with 'min' as the minimum possible value.
  243. assert(carla_min(0, 0, 0) == 0);
  244. assert(carla_min(0, 3, 0) == 0);
  245. assert(carla_min(3, 0, 0) == 0);
  246. assert(carla_min(0, 0, 3) == 3);
  247. assert(carla_min(0, 1, 3) == 3);
  248. assert(carla_min(0, 2, 3) == 3);
  249. assert(carla_min(0, 3, 3) == 3);
  250. assert(carla_min(0, 4, 3) == 3);
  251. assert(carla_min(5, 0, 3) == 3);
  252. assert(carla_min(5, 1, 3) == 3);
  253. assert(carla_min(5, 2, 3) == 3);
  254. assert(carla_min(5, 3, 3) == 3);
  255. assert(carla_min(5, 4, 3) == 4);
  256. assert(carla_min(5, 5, 3) == 5);
  257. assert(carla_min(5, 6, 3) == 5);
  258. assert(carla_min(2, -2, 0) == 0);
  259. assert(carla_min(2, -1, 0) == 0);
  260. assert(carla_min(2, 0, 0) == 0);
  261. assert(carla_min(2, 1, 0) == 1);
  262. assert(carla_min(2, 2, 0) == 2);
  263. assert(carla_min(5, -6, 3) == 3);
  264. assert(carla_min(5, -5, 3) == 3);
  265. assert(carla_min(5, -4, 3) == 3);
  266. assert(carla_min(5, -3, 3) == 3);
  267. assert(carla_min(5, -2, 3) == 3);
  268. assert(carla_min(5, -1, 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(5, -6, -3) == -3);
  277. assert(carla_min(5, -5, -3) == -3);
  278. assert(carla_min(5, -4, -3) == -3);
  279. assert(carla_min(5, -3, -3) == -3);
  280. assert(carla_min(5, -2, -3) == -2);
  281. assert(carla_min(5, -1, -3) == -1);
  282. assert(carla_min(5, 0, -3) == 0);
  283. assert(carla_min(5, 1, -3) == 1);
  284. assert(carla_min(5, 2, -3) == 2);
  285. assert(carla_min(5, 3, -3) == 3);
  286. assert(carla_min(5, 4, -3) == 4);
  287. assert(carla_min(5, 5, -3) == 5);
  288. assert(carla_min(5, 6, -3) == 5);
  289. // Return the lower positive of 2 values.
  290. assert(carla_minPositive(0, 0) == 0);
  291. assert(carla_minPositive(0, 1) == 0);
  292. assert(carla_minPositive(0, 2) == 0);
  293. assert(carla_minPositive(0, 3) == 0);
  294. assert(carla_minPositive(1, 0) == 0);
  295. assert(carla_minPositive(1, 1) == 1);
  296. assert(carla_minPositive(1, 2) == 1);
  297. assert(carla_minPositive(1, 3) == 1);
  298. assert(carla_minPositive(3, 0) == 0);
  299. assert(carla_minPositive(3, 1) == 1);
  300. assert(carla_minPositive(3, 2) == 2);
  301. assert(carla_minPositive(3, 3) == 3);
  302. assert(carla_minPositive(-1, 0) == 0);
  303. assert(carla_minPositive(-1, 1) == 1);
  304. assert(carla_minPositive(-1, 2) == 2);
  305. assert(carla_minPositive(-1, 3) == 3);
  306. // Return the higher of 2 values, with 'max' as the maximum possible value.
  307. assert(carla_max(0, 0, 0) == 0);
  308. assert(carla_max(0, 3, 0) == 0);
  309. assert(carla_max(3, 0, 0) == 0);
  310. assert(carla_max(0, 0, 3) == 0);
  311. assert(carla_max(0, 1, 3) == 1);
  312. assert(carla_max(0, 2, 3) == 2);
  313. assert(carla_max(0, 3, 3) == 3);
  314. assert(carla_max(0, 4, 3) == 3);
  315. assert(carla_max(5, 0, 3) == 3);
  316. assert(carla_max(5, 1, 3) == 3);
  317. assert(carla_max(5, 2, 3) == 3);
  318. assert(carla_max(5, 3, 3) == 3);
  319. assert(carla_max(5, 4, 3) == 3);
  320. assert(carla_max(5, 5, 3) == 3);
  321. assert(carla_max(5, 6, 3) == 3);
  322. // Fix bounds of 'value' between 'min' and 'max'.
  323. assert(carla_fixValue(0, 1, -1) == 0);
  324. assert(carla_fixValue(0, 1, 0) == 0);
  325. assert(carla_fixValue(0, 1, 1) == 1);
  326. assert(carla_fixValue(0, 1, 2) == 1);
  327. assert(carla_fixValue(0.0, 1.0, -1.0) == 0.0);
  328. assert(carla_fixValue(0.0, 1.0, 0.0) == 0.0);
  329. assert(carla_fixValue(0.0, 1.0, 1.0) == 1.0);
  330. assert(carla_fixValue(0.0, 1.0, 2.0) == 1.0);
  331. assert(carla_fixValue(0.0, 1.0, -0.1) == 0.0);
  332. assert(carla_fixValue(0.0, 1.0, 1.1) == 1.0);
  333. // Get next power of 2.
  334. assert(carla_nextPowerOf2(0) == 0);
  335. assert(carla_nextPowerOf2(1) == 1);
  336. assert(carla_nextPowerOf2(2) == 2);
  337. assert(carla_nextPowerOf2(4) == 4);
  338. assert(carla_nextPowerOf2(5) == 8);
  339. assert(carla_nextPowerOf2(6) == 8);
  340. assert(carla_nextPowerOf2(7) == 8);
  341. assert(carla_nextPowerOf2(8) == 8);
  342. assert(carla_nextPowerOf2(9) == 16);
  343. uint32_t power = 1;
  344. assert((power = carla_nextPowerOf2(power+1)) == 2);
  345. assert((power = carla_nextPowerOf2(power+1)) == 4);
  346. assert((power = carla_nextPowerOf2(power+1)) == 8);
  347. assert((power = carla_nextPowerOf2(power+1)) == 16);
  348. assert((power = carla_nextPowerOf2(power+1)) == 32);
  349. assert((power = carla_nextPowerOf2(power+1)) == 64);
  350. assert((power = carla_nextPowerOf2(power+1)) == 128);
  351. assert((power = carla_nextPowerOf2(power+1)) == 256);
  352. assert((power = carla_nextPowerOf2(power+1)) == 512);
  353. assert((power = carla_nextPowerOf2(power+1)) == 1024);
  354. assert((power = carla_nextPowerOf2(power+1)) == 2048);
  355. assert((power = carla_nextPowerOf2(power+1)) == 4096);
  356. assert((power = carla_nextPowerOf2(power+1)) == 8192);
  357. assert((power = carla_nextPowerOf2(power+1)) == 16384);
  358. assert((power = carla_nextPowerOf2(power+1)) == 32768);
  359. assert((power = carla_nextPowerOf2(power+1)) == 65536);
  360. assert((power = carla_nextPowerOf2(power+1)) == 131072);
  361. // -------------------------------------------------------------------
  362. // math functions (extended)
  363. // carla_addFloat, carla_copyFloat & carla_zeroFloat tests skipped
  364. // mostly unused due to juce::FloatVectorOperations
  365. }
  366. // -----------------------------------------------------------------------
  367. static void test_CarlaBackendUtils() noexcept
  368. {
  369. CARLA_BACKEND_USE_NAMESPACE
  370. carla_stdout(PluginOption2Str(PLUGIN_OPTION_FIXED_BUFFERS));
  371. carla_stdout(BinaryType2Str(BINARY_NONE));
  372. carla_stdout(PluginType2Str(PLUGIN_NONE));
  373. carla_stdout(PluginCategory2Str(PLUGIN_CATEGORY_NONE));
  374. carla_stdout(ParameterType2Str(PARAMETER_UNKNOWN));
  375. carla_stdout(InternalParameterIndex2Str(PARAMETER_NULL));
  376. carla_stdout(EngineCallbackOpcode2Str(ENGINE_CALLBACK_DEBUG));
  377. carla_stdout(EngineOption2Str(ENGINE_OPTION_DEBUG));
  378. carla_stdout(EngineProcessMode2Str(ENGINE_PROCESS_MODE_SINGLE_CLIENT));
  379. carla_stdout(EngineTransportMode2Str(ENGINE_TRANSPORT_MODE_INTERNAL));
  380. carla_stdout(FileCallbackOpcode2Str(FILE_CALLBACK_DEBUG));
  381. carla_stdout(getPluginTypeAsString(PLUGIN_INTERNAL));
  382. carla_stdout(PatchbayIcon2Str(PATCHBAY_ICON_APPLICATION));
  383. getPluginTypeFromString("none");
  384. getPluginCategoryFromName("cat");
  385. }
  386. // -----------------------------------------------------------------------
  387. static void test_CarlaBridgeUtils() noexcept
  388. {
  389. carla_stdout(PluginBridgeOscInfoType2str(kPluginBridgeOscPong));
  390. carla_stdout(PluginBridgeRtOpcode2str(kPluginBridgeRtNull));
  391. carla_stdout(PluginBridgeNonRtOpcode2str(kPluginBridgeNonRtNull));
  392. }
  393. // -----------------------------------------------------------------------
  394. static void test_CarlaEngineUtils() noexcept
  395. {
  396. CARLA_BACKEND_USE_NAMESPACE
  397. carla_stdout(EngineType2Str(kEngineTypeNull));
  398. carla_stdout(EnginePortType2Str(kEnginePortTypeNull));
  399. carla_stdout(EngineEventType2Str(kEngineEventTypeNull));
  400. carla_stdout(EngineControlEventType2Str(kEngineControlEventTypeNull));
  401. }
  402. // -----------------------------------------------------------------------
  403. // main
  404. int main()
  405. {
  406. // already tested, skip for now
  407. test_CarlaUtils();
  408. test_CarlaMathUtils();
  409. test_CarlaBackendUtils();
  410. test_CarlaBridgeUtils();
  411. test_CarlaEngineUtils();
  412. return 0;
  413. }
  414. // -----------------------------------------------------------------------