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.

Utils.cpp 7.0KB

11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include "CarlaUtils.hpp"
  18. #include "CarlaBackendUtils.hpp"
  19. #include "CarlaBridgeUtils.hpp"
  20. // #include "CarlaDssiUtils.hpp"
  21. #include "CarlaJuceUtils.hpp"
  22. #include "CarlaLadspaUtils.hpp"
  23. // #include "CarlaLibUtils.hpp"
  24. // #include "CarlaLv2Utils.hpp"
  25. #include "CarlaMathUtils.hpp"
  26. #include "CarlaOscUtils.hpp"
  27. #include "CarlaPipeUtils.hpp"
  28. // #include "CarlaShmUtils.hpp"
  29. // #include "CarlaStateUtils.hpp"
  30. // #include "CarlaVstUtils.hpp"
  31. // #include "CarlaLibCounter.hpp"
  32. // #include "CarlaLogThread.hpp"
  33. // #include "CarlaMutex.hpp"
  34. // #include "CarlaRingBuffer.hpp"
  35. // #include "CarlaString.hpp"
  36. // #include "CarlaThread.hpp"
  37. // #include "List.hpp"
  38. // #include "Lv2AtomQueue.hpp"
  39. // #include "RtList.hpp"
  40. // #include "JucePluginWindow.hpp"
  41. struct MyStruct {
  42. char pad[100];
  43. int i;
  44. double d;
  45. void* ptr;
  46. };
  47. class MyLeakCheckedClass
  48. {
  49. public:
  50. MyLeakCheckedClass() noexcept {}
  51. ~MyLeakCheckedClass() noexcept {}
  52. private:
  53. char pad[100];
  54. int i;
  55. double d;
  56. void* ptr;
  57. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MyLeakCheckedClass)
  58. };
  59. #if 0
  60. class MyThread : public CarlaThread
  61. {
  62. public:
  63. MyThread(CarlaMutex* const m)
  64. : CarlaThread("myThread"),
  65. fMu(m)
  66. {
  67. }
  68. protected:
  69. void run() override
  70. {
  71. carla_stderr("Thread started");
  72. carla_stderr("Thread lock");
  73. fMu->lock();
  74. carla_stderr("Thread sleeping");
  75. carla_sleep(3);
  76. carla_stderr("Thread unlock");
  77. fMu->unlock();
  78. carla_stderr("Thread sleep-waiting for stop");
  79. while (! shouldExit())
  80. carla_sleep(1);
  81. carla_stderr("Thread finished");
  82. return;
  83. vstPluginCanDo(nullptr, "something");
  84. }
  85. private:
  86. CarlaMutex* const fMu;
  87. };
  88. #endif
  89. int main()
  90. {
  91. // misc functions
  92. bool2str(false);
  93. bool2str(true);
  94. pass();
  95. // string print functions
  96. carla_debug("DEBUG");
  97. carla_stdout("STDOUT");
  98. carla_stderr("STDERR");
  99. carla_stderr2("STDERR2");
  100. // carla_*sleep
  101. carla_sleep(1);
  102. carla_msleep(1);
  103. // carla_setenv
  104. carla_setenv("THIS", "THAT");
  105. assert(std::strcmp(std::getenv("THIS"), "THAT") == 0);
  106. // carla_strdup
  107. const char* const str1(carla_strdup("string1"));
  108. const char* const strF(carla_strdup_free(strdup("stringFree")));
  109. delete[] str1;
  110. delete[] strF;
  111. {
  112. struct TestStruct {
  113. const char* str1;
  114. const char* str2;
  115. const char* str3;
  116. const char* str4;
  117. TestStruct()
  118. : str1(carla_strdup("str1")),
  119. str2(carla_strdup("str2")),
  120. str3(nullptr),
  121. str4(carla_strdup("str4")) {}
  122. ~TestStruct()
  123. {
  124. if (str1 != nullptr)
  125. {
  126. delete[] str1;
  127. str1 = nullptr;
  128. }
  129. if (str2 != nullptr)
  130. {
  131. delete[] str2;
  132. str2 = nullptr;
  133. }
  134. if (str3 != nullptr)
  135. {
  136. delete[] str3;
  137. str3 = nullptr;
  138. }
  139. if (str4 != nullptr)
  140. {
  141. delete[] str4;
  142. str4 = nullptr;
  143. }
  144. }
  145. };
  146. TestStruct a, b, c;
  147. }
  148. // math/memory functions
  149. {
  150. assert(carla_min<int32_t>(0, -5, 8) == 8);
  151. assert(carla_max<int32_t>(0, -5, 8) == 0);
  152. assert(carla_fixValue<float>(0.0f, 1.0f, 1.1f) == 1.0f);
  153. int v1 = 6;
  154. int v2 = 8;
  155. const int v3 = 9;
  156. assert(v1 == 6 && v2 == 8 && v3 == 9);
  157. carla_copy<int>(&v1, &v2, 1);
  158. assert(v1 == 8 && v2 == 8 && v3 == 9);
  159. carla_copy<int>(&v2, &v3, 1);
  160. assert(v1 == 8 && v2 == 9 && v3 == 9);
  161. float fl[5];
  162. carla_fill(fl, 5, 1.11f);
  163. assert(fl[0] == 1.11f);
  164. assert(fl[1] == 1.11f);
  165. assert(fl[2] == 1.11f);
  166. assert(fl[3] == 1.11f);
  167. assert(fl[4] == 1.11f);
  168. carla_add(fl, fl, 5);
  169. assert(fl[0] == 1.11f*2);
  170. assert(fl[1] == 1.11f*2);
  171. assert(fl[2] == 1.11f*2);
  172. assert(fl[3] == 1.11f*2);
  173. assert(fl[4] == 1.11f*2);
  174. carla_add(fl, fl, 4);
  175. assert(fl[0] == 1.11f*4);
  176. assert(fl[1] == 1.11f*4);
  177. assert(fl[2] == 1.11f*4);
  178. assert(fl[3] == 1.11f*4);
  179. assert(fl[4] == 1.11f*2);
  180. carla_add(fl, fl, 3);
  181. assert(fl[0] == 1.11f*8);
  182. assert(fl[1] == 1.11f*8);
  183. assert(fl[2] == 1.11f*8);
  184. assert(fl[3] == 1.11f*4);
  185. assert(fl[4] == 1.11f*2);
  186. carla_add(fl, fl, 2);
  187. assert(fl[0] == 1.11f*16);
  188. assert(fl[1] == 1.11f*16);
  189. assert(fl[2] == 1.11f*8);
  190. assert(fl[3] == 1.11f*4);
  191. assert(fl[4] == 1.11f*2);
  192. carla_add(fl, fl, 1);
  193. assert(fl[0] == 1.11f*32);
  194. assert(fl[1] == 1.11f*16);
  195. assert(fl[2] == 1.11f*8);
  196. assert(fl[3] == 1.11f*4);
  197. assert(fl[4] == 1.11f*2);
  198. char ch[500];
  199. carla_zeroChar(ch, 500);
  200. for (int i=0; i<500; ++i)
  201. assert(ch[i] == '\0');
  202. }
  203. {
  204. MyStruct a, b, c[2], d[2];
  205. carla_zeroMem(&a, sizeof(MyStruct));
  206. carla_zeroStruct<MyStruct>(b);
  207. carla_zeroStruct<MyStruct>(c, 2);
  208. carla_copyStruct<MyStruct>(b, a);
  209. carla_copyStruct<MyStruct>(d, c, 2);
  210. }
  211. // Leak check
  212. {
  213. MyLeakCheckedClass a;
  214. MyLeakCheckedClass* const b(new MyLeakCheckedClass);
  215. delete b;
  216. }
  217. #if 0
  218. // Mutex
  219. {
  220. CarlaMutex m;
  221. assert(! m.wasTryLockCalled());
  222. assert(m.tryLock());
  223. assert(m.wasTryLockCalled());
  224. assert(! m.wasTryLockCalled()); // once
  225. m.unlock();
  226. m.lock();
  227. assert(! m.wasTryLockCalled());
  228. { CarlaMutex::ScopedUnlocker su(m); }
  229. assert(! m.wasTryLockCalled());
  230. m.unlock();
  231. { CarlaMutex::ScopedLocker sl(m); }
  232. }
  233. // String
  234. {
  235. CarlaString a, b(2), c("haha"), d((uint)0x999, true), e(0.7f), f(0.9), g('u');
  236. assert(g == "u");
  237. }
  238. // Thread
  239. {
  240. CarlaMutex m;
  241. MyThread t(&m);
  242. carla_stdout("Thread init started");
  243. t.start();
  244. carla_stdout("Thread init finished, lock waiting...");
  245. m.lock();
  246. carla_stdout("Thread lock wait done");
  247. m.unlock();
  248. t.stop(-1);
  249. }
  250. #endif
  251. return 0;
  252. }