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.9KB

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