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.

CarlaUtils3.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "CarlaJuceUtils.hpp"
  21. #include "CarlaLibCounter.hpp"
  22. #include "CarlaOscUtils.hpp"
  23. #include "CarlaPatchbayUtils.hpp"
  24. #include "CarlaShmUtils.hpp"
  25. // -----------------------------------------------------------------------
  26. class LeakTestClass
  27. {
  28. public:
  29. LeakTestClass()noexcept
  30. : i(0) {}
  31. private:
  32. int i;
  33. CARLA_LEAK_DETECTOR(LeakTestClass)
  34. };
  35. static void test_CarlaJuceUtils()
  36. {
  37. LeakTestClass a, b;
  38. LeakTestClass* e;
  39. LeakTestClass* f = nullptr;
  40. e = new LeakTestClass;
  41. f = new LeakTestClass;
  42. delete e; delete f;
  43. delete new LeakTestClass;
  44. int x = 1;
  45. {
  46. assert(x == 1);
  47. ScopedValueSetter<int> s(x, 2);
  48. assert(x == 2);
  49. }
  50. assert(x == 1);
  51. {
  52. assert(x == 1);
  53. ScopedValueSetter<int> s(x, 3, 4);
  54. assert(x == 3);
  55. }
  56. assert(x == 4);
  57. }
  58. // -----------------------------------------------------------------------
  59. static void test_CarlaLibUtils() noexcept
  60. {
  61. void* const libNot = lib_open("/libzzzzz...");
  62. assert(libNot == nullptr);
  63. carla_stdout("Force lib_open fail error results in: %s", lib_error("/libzzzzz..."));
  64. void* const lib = lib_open("/usr/lib/liblo.so");
  65. CARLA_SAFE_ASSERT_RETURN(lib != nullptr,);
  66. void* const libS = lib_symbol(lib, "lo_server_new");
  67. CARLA_SAFE_ASSERT(libS != nullptr);
  68. const bool closed = lib_close(lib);
  69. CARLA_SAFE_ASSERT(closed);
  70. LibCounter lc;
  71. void* const test1 = lc.open("/usr/lib/liblo.so");
  72. void* const test2 = lc.open("/usr/lib/liblo.so");
  73. void* const test3 = lc.open("/usr/lib/liblo.so");
  74. assert(test1 == test2);
  75. assert(test2 == test3);
  76. lc.close(test1); lc.close(test2); lc.close(test3);
  77. // test if the pointer changes after all closed
  78. void* const test1b = lc.open("/usr/lib/liblo.so");
  79. assert(test1 != test1b);
  80. lc.close(test1b);
  81. // test non-delete flag
  82. void* const test4 = lc.open("/usr/lib/liblrdf.so.0", false);
  83. lc.close(test4);
  84. void* const test5 = lc.open("/usr/lib/liblrdf.so.0");
  85. assert(test4 == test5);
  86. lc.close(test5);
  87. // open non-delete a few times, tests for cleanup on destruction
  88. lc.open("/usr/lib/liblrdf.so.0");
  89. lc.open("/usr/lib/liblrdf.so.0");
  90. lc.open("/usr/lib/liblrdf.so.0");
  91. }
  92. // -----------------------------------------------------------------------
  93. static void test_CarlaOscUtils() noexcept
  94. {
  95. }
  96. // -----------------------------------------------------------------------
  97. static void test_CarlaPatchbayUtils() noexcept
  98. {
  99. GroupNameToId g1, g2, g3;
  100. // only clear #1
  101. g1.clear();
  102. // set initial data
  103. g1.setData(1, "1");
  104. g2.setData(2, "2");
  105. g3.setData(3, "3");
  106. // should not match
  107. assert(g1 != g2);
  108. assert(g1 != g3);
  109. // set data equal to #1, test match
  110. g3.setData(1, "0");
  111. g3.rename("1");
  112. assert(g1 == g3);
  113. // set data back
  114. g3.setData(3, "3");
  115. PatchbayGroupList glist;
  116. glist.list.append(g1); ++glist.lastId;
  117. glist.list.append(g3); ++glist.lastId;
  118. glist.list.append(g2); ++glist.lastId;
  119. assert(glist.getGroupId("1") == 1);
  120. assert(glist.getGroupId("2") == 2);
  121. assert(glist.getGroupId("3") == 3);
  122. assert(std::strcmp(glist.getGroupName(1), "1") == 0);
  123. assert(std::strcmp(glist.getGroupName(2), "2") == 0);
  124. assert(std::strcmp(glist.getGroupName(3), "3") == 0);
  125. glist.clear();
  126. PortNameToId p11, p12, p21, p31;
  127. // only clear #11
  128. p11.clear();
  129. // set initial data
  130. p11.setData(1, 1, "1", "1:1");
  131. p12.setData(1, 2, "2", "1:2");
  132. p21.setData(2, 1, "1", "2:1");
  133. p31.setData(3, 1, "1", "3:1");
  134. // should not match
  135. assert(p11 != p12);
  136. assert(p11 != p21);
  137. assert(p11 != p31);
  138. // set data equal to #1, test match
  139. p31.setData(1, 2, "0", "0:0");
  140. p31.rename("2", "1:2");
  141. assert(p12 == p31);
  142. // set data back
  143. p31.setData(3, 1, "1", "3:1");
  144. PatchbayPortList plist;
  145. plist.list.append(p11); ++plist.lastId;
  146. plist.list.append(p12); ++plist.lastId;
  147. plist.list.append(p21); ++plist.lastId;
  148. plist.list.append(p31); ++plist.lastId;
  149. assert(std::strcmp(plist.getFullPortName(1, 1), "1:1") == 0);
  150. assert(std::strcmp(plist.getFullPortName(1, 2), "1:2") == 0);
  151. assert(std::strcmp(plist.getFullPortName(2, 1), "2:1") == 0);
  152. assert(std::strcmp(plist.getFullPortName(3, 1), "3:1") == 0);
  153. assert(p11 == plist.getPortNameToId("1:1"));
  154. assert(p12 == plist.getPortNameToId("1:2"));
  155. assert(p21 == plist.getPortNameToId("2:1"));
  156. assert(p31 == plist.getPortNameToId("3:1"));
  157. plist.clear();
  158. // no tests here, just usage
  159. ConnectionToId c1, c2;
  160. c1.clear();
  161. c2.setData(0, 0, 0, 0, 0);
  162. assert(c1 == c2);
  163. c2.setData(0, 0, 0, 0, 1);
  164. assert(c1 != c2);
  165. PatchbayConnectionList clist;
  166. clist.list.append(c1); ++clist.lastId;
  167. clist.list.append(c2); ++clist.lastId;
  168. clist.clear();
  169. }
  170. // -----------------------------------------------------------------------
  171. struct ShmStruct {
  172. char stringStart[255];
  173. bool boolean;
  174. int integer;
  175. float floating;
  176. char stringEnd[255];
  177. };
  178. static void test_CarlaShmUtils() noexcept
  179. {
  180. shm_t shm, shma;
  181. ShmStruct* shmStruct1;
  182. ShmStruct* shmStruct2;
  183. // base tests first
  184. carla_shm_init(shm);
  185. assert(! carla_is_shm_valid(shm));
  186. shm = carla_shm_create("/carla-shm-test1");
  187. carla_stdout("test %i", shm);
  188. assert(carla_is_shm_valid(shm));
  189. carla_shm_close(shm);
  190. assert(! carla_is_shm_valid(shm));
  191. shm = carla_shm_create("/carla-shm-test1");
  192. assert(carla_is_shm_valid(shm));
  193. shma = carla_shm_attach("/carla-shm-test1");
  194. assert(carla_is_shm_valid(shma));
  195. carla_shm_close(shm);
  196. carla_shm_close(shma);
  197. assert(! carla_is_shm_valid(shm));
  198. assert(! carla_is_shm_valid(shma));
  199. // test attach invalid
  200. shma = carla_shm_attach("/carla-shm-test-NOT");
  201. assert(! carla_is_shm_valid(shma));
  202. // test memory, start
  203. shm = carla_shm_create("/carla-shm-test1");
  204. assert(carla_is_shm_valid(shm));
  205. shma = carla_shm_attach("/carla-shm-test1");
  206. assert(carla_is_shm_valid(shma));
  207. // test memory, check valid
  208. shmStruct1 = carla_shm_map<ShmStruct>(shm);
  209. assert(shmStruct1 != nullptr);
  210. shmStruct2 = carla_shm_map<ShmStruct>(shma);
  211. assert(shmStruct2 != nullptr);
  212. carla_shm_unmap(shma, shmStruct2);
  213. assert(shmStruct2 == nullptr);
  214. carla_shm_unmap(shm, shmStruct1);
  215. assert(shmStruct1 == nullptr);
  216. // test memory, check if write data matches
  217. shmStruct1 = carla_shm_map<ShmStruct>(shm);
  218. assert(shmStruct1 != nullptr);
  219. shmStruct2 = carla_shm_map<ShmStruct>(shma);
  220. assert(shmStruct2 != nullptr);
  221. carla_zeroStruct(*shmStruct1);
  222. assert(shmStruct1->stringStart[0] == '\0');
  223. assert(shmStruct2->stringStart[0] == '\0');
  224. assert(shmStruct1->stringEnd[0] == '\0');
  225. assert(shmStruct2->stringEnd[0] == '\0');
  226. assert(! shmStruct1->boolean);
  227. assert(! shmStruct2->boolean);
  228. shmStruct1->boolean = true;
  229. shmStruct1->integer = 232312;
  230. assert(shmStruct1->boolean == shmStruct2->boolean);
  231. assert(shmStruct1->integer == shmStruct2->integer);
  232. shmStruct2->floating = 2342.231f;
  233. std::strcpy(shmStruct2->stringStart, "test1start");
  234. std::strcpy(shmStruct2->stringEnd, "test2end");
  235. assert(shmStruct1->floating == shmStruct2->floating);
  236. assert(std::strcmp(shmStruct1->stringStart, "test1start") == 0);
  237. assert(std::strcmp(shmStruct1->stringStart, shmStruct2->stringStart) == 0);
  238. assert(std::strcmp(shmStruct1->stringEnd, "test2end") == 0);
  239. assert(std::strcmp(shmStruct1->stringEnd, shmStruct2->stringEnd) == 0);
  240. carla_shm_unmap(shma, shmStruct2);
  241. assert(shmStruct2 == nullptr);
  242. carla_shm_unmap(shm, shmStruct1);
  243. assert(shmStruct1 == nullptr);
  244. // test memory, done
  245. carla_shm_close(shm);
  246. carla_shm_close(shma);
  247. assert(! carla_is_shm_valid(shm));
  248. assert(! carla_is_shm_valid(shma));
  249. }
  250. // -----------------------------------------------------------------------
  251. // main
  252. int main()
  253. {
  254. test_CarlaJuceUtils();
  255. test_CarlaLibUtils();
  256. test_CarlaOscUtils();
  257. test_CarlaPatchbayUtils();
  258. test_CarlaShmUtils();
  259. return 0;
  260. }
  261. // -----------------------------------------------------------------------