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