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