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.

331 lines
8.9KB

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