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.

287 lines
6.5KB

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