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.

332 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. // nothing for now
  97. }
  98. // -----------------------------------------------------------------------
  99. static void test_CarlaPatchbayUtils() noexcept
  100. {
  101. GroupNameToId g1, g2, g3;
  102. // only clear #1
  103. g1.clear();
  104. // set initial data
  105. g1.setData(1, "1");
  106. g2.setData(2, "2");
  107. g3.setData(3, "3");
  108. // should not match
  109. assert(g1 != g2);
  110. assert(g1 != g3);
  111. // set data equal to #1, test match
  112. g3.setData(1, "0");
  113. g3.rename("1");
  114. assert(g1 == g3);
  115. // set data back
  116. g3.setData(3, "3");
  117. PatchbayGroupList glist;
  118. glist.list.append(g1); ++glist.lastId;
  119. glist.list.append(g3); ++glist.lastId;
  120. glist.list.append(g2); ++glist.lastId;
  121. assert(glist.getGroupId("1") == 1);
  122. assert(glist.getGroupId("2") == 2);
  123. assert(glist.getGroupId("3") == 3);
  124. assert(std::strcmp(glist.getGroupName(1), "1") == 0);
  125. assert(std::strcmp(glist.getGroupName(2), "2") == 0);
  126. assert(std::strcmp(glist.getGroupName(3), "3") == 0);
  127. glist.clear();
  128. PortNameToId p11, p12, p21, p31;
  129. // only clear #11
  130. p11.clear();
  131. // set initial data
  132. p11.setData(1, 1, "1", "1:1");
  133. p12.setData(1, 2, "2", "1:2");
  134. p21.setData(2, 1, "1", "2:1");
  135. p31.setData(3, 1, "1", "3:1");
  136. // should not match
  137. assert(p11 != p12);
  138. assert(p11 != p21);
  139. assert(p11 != p31);
  140. // set data equal to #1, test match
  141. p31.setData(1, 2, "0", "0:0");
  142. p31.rename("2", "1:2");
  143. assert(p12 == p31);
  144. // set data back
  145. p31.setData(3, 1, "1", "3:1");
  146. PatchbayPortList plist;
  147. plist.list.append(p11); ++plist.lastId;
  148. plist.list.append(p12); ++plist.lastId;
  149. plist.list.append(p21); ++plist.lastId;
  150. plist.list.append(p31); ++plist.lastId;
  151. assert(std::strcmp(plist.getFullPortName(1, 1), "1:1") == 0);
  152. assert(std::strcmp(plist.getFullPortName(1, 2), "1:2") == 0);
  153. assert(std::strcmp(plist.getFullPortName(2, 1), "2:1") == 0);
  154. assert(std::strcmp(plist.getFullPortName(3, 1), "3:1") == 0);
  155. assert(p11 == plist.getPortNameToId("1:1"));
  156. assert(p12 == plist.getPortNameToId("1:2"));
  157. assert(p21 == plist.getPortNameToId("2:1"));
  158. assert(p31 == plist.getPortNameToId("3:1"));
  159. plist.clear();
  160. // no tests here, just usage
  161. ConnectionToId c1, c2;
  162. c1.clear();
  163. c2.setData(0, 0, 0, 0, 0);
  164. assert(c1 == c2);
  165. c2.setData(0, 0, 0, 0, 1);
  166. assert(c1 != c2);
  167. PatchbayConnectionList clist;
  168. clist.list.append(c1); ++clist.lastId;
  169. clist.list.append(c2); ++clist.lastId;
  170. clist.clear();
  171. }
  172. // -----------------------------------------------------------------------
  173. struct ShmStruct {
  174. char stringStart[255];
  175. bool boolean;
  176. int integer;
  177. float floating;
  178. char stringEnd[255];
  179. };
  180. static void test_CarlaShmUtils() noexcept
  181. {
  182. shm_t shm, shma;
  183. ShmStruct* shmStruct1;
  184. ShmStruct* shmStruct2;
  185. // base tests first
  186. carla_shm_init(shm);
  187. assert(! carla_is_shm_valid(shm));
  188. shm = carla_shm_create("/carla-shm-test1");
  189. carla_stdout("test %i", shm);
  190. assert(carla_is_shm_valid(shm));
  191. carla_shm_close(shm);
  192. assert(! carla_is_shm_valid(shm));
  193. shm = carla_shm_create("/carla-shm-test1");
  194. assert(carla_is_shm_valid(shm));
  195. shma = carla_shm_attach("/carla-shm-test1");
  196. assert(carla_is_shm_valid(shma));
  197. carla_shm_close(shm);
  198. carla_shm_close(shma);
  199. assert(! carla_is_shm_valid(shm));
  200. assert(! carla_is_shm_valid(shma));
  201. // test attach invalid
  202. shma = carla_shm_attach("/carla-shm-test-NOT");
  203. assert(! carla_is_shm_valid(shma));
  204. // test memory, start
  205. shm = carla_shm_create("/carla-shm-test1");
  206. assert(carla_is_shm_valid(shm));
  207. shma = carla_shm_attach("/carla-shm-test1");
  208. assert(carla_is_shm_valid(shma));
  209. // test memory, check valid
  210. shmStruct1 = carla_shm_map<ShmStruct>(shm);
  211. assert(shmStruct1 != nullptr);
  212. shmStruct2 = carla_shm_map<ShmStruct>(shma);
  213. assert(shmStruct2 != nullptr);
  214. carla_shm_unmap(shma, shmStruct2);
  215. assert(shmStruct2 == nullptr);
  216. carla_shm_unmap(shm, shmStruct1);
  217. assert(shmStruct1 == nullptr);
  218. // test memory, check if write data matches
  219. shmStruct1 = carla_shm_map<ShmStruct>(shm);
  220. assert(shmStruct1 != nullptr);
  221. shmStruct2 = carla_shm_map<ShmStruct>(shma);
  222. assert(shmStruct2 != nullptr);
  223. carla_zeroStruct(*shmStruct1);
  224. assert(shmStruct1->stringStart[0] == '\0');
  225. assert(shmStruct2->stringStart[0] == '\0');
  226. assert(shmStruct1->stringEnd[0] == '\0');
  227. assert(shmStruct2->stringEnd[0] == '\0');
  228. assert(! shmStruct1->boolean);
  229. assert(! shmStruct2->boolean);
  230. shmStruct1->boolean = true;
  231. shmStruct1->integer = 232312;
  232. assert(shmStruct1->boolean == shmStruct2->boolean);
  233. assert(shmStruct1->integer == shmStruct2->integer);
  234. shmStruct2->floating = 2342.231f;
  235. std::strcpy(shmStruct2->stringStart, "test1start");
  236. std::strcpy(shmStruct2->stringEnd, "test2end");
  237. assert(shmStruct1->floating == shmStruct2->floating);
  238. assert(std::strcmp(shmStruct1->stringStart, "test1start") == 0);
  239. assert(std::strcmp(shmStruct1->stringStart, shmStruct2->stringStart) == 0);
  240. assert(std::strcmp(shmStruct1->stringEnd, "test2end") == 0);
  241. assert(std::strcmp(shmStruct1->stringEnd, shmStruct2->stringEnd) == 0);
  242. carla_shm_unmap(shma, shmStruct2);
  243. assert(shmStruct2 == nullptr);
  244. carla_shm_unmap(shm, shmStruct1);
  245. assert(shmStruct1 == nullptr);
  246. // test memory, done
  247. carla_shm_close(shm);
  248. carla_shm_close(shma);
  249. assert(! carla_is_shm_valid(shm));
  250. assert(! carla_is_shm_valid(shma));
  251. }
  252. // -----------------------------------------------------------------------
  253. // main
  254. int main()
  255. {
  256. test_CarlaJuceUtils();
  257. test_CarlaLibUtils();
  258. test_CarlaOscUtils();
  259. test_CarlaPatchbayUtils();
  260. test_CarlaShmUtils();
  261. return 0;
  262. }
  263. // -----------------------------------------------------------------------