1 /*
2  * Carla 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 #include "../backend/engine/CarlaEngineInternal.hpp"
19 
20 #include "CarlaPlugin.hpp"
21 
22 #include "CarlaHost.h"
23 #include "CarlaUtils.hpp"
24 
25 // -----------------------------------------------------------------------
26 
27 CARLA_BACKEND_START_NAMESPACE
28 
29 class CarlaEngineDummy : public CarlaEngine
30 {
31 public:
CarlaEngineDummy()32     CarlaEngineDummy()
33         : CarlaEngine(),
34           fIsRunning(false)
35     {
36     }
37 
init(const char * const clientName)38     bool init(const char* const clientName) override
39     {
40         fIsRunning = true;
41         pData->bufferSize = 512;
42         pData->sampleRate = 44100.0;
43         return CarlaEngine::init(clientName);
44     }
45 
close()46     bool close() override
47     {
48         fIsRunning = false;
49         return CarlaEngine::close();
50     }
51 
isRunning() const52     bool isRunning() const noexcept override
53     {
54         return fIsRunning;
55     }
56 
isOffline() const57     bool isOffline() const noexcept override
58     {
59         return false;
60     }
61 
getType() const62     EngineType getType() const noexcept override
63     {
64         return kEngineTypePlugin;
65     }
66 
getCurrentDriverName() const67     const char* getCurrentDriverName() const noexcept override
68     {
69         return "Dummy";
70     }
71 
72 private:
73     bool fIsRunning;
74 };
75 
76 CARLA_BACKEND_END_NAMESPACE
77 
78 // -----------------------------------------------------------------------
79 
80 CARLA_BACKEND_USE_NAMESPACE
81 
82 #define TEST_NAME "TestName"
83 
testEngine(CarlaEngine * const eng)84 void testEngine(CarlaEngine* const eng)
85 {
86     assert(eng->getMaxClientNameSize() != 0);
87     assert(eng->getMaxPortNameSize() != 0);
88     assert(eng->getCurrentPluginCount() == 0);
89     assert(eng->getMaxPluginNumber() == 0);
90     assert(! eng->isRunning());
91     assert(eng->getPlugin(0) == nullptr);
92 
93     const char* name1 = eng->getUniquePluginName(TEST_NAME);
94     const char* name2 = eng->getUniquePluginName(TEST_NAME);
95     const char* name3 = eng->getUniquePluginName(TEST_NAME);
96 
97     assert(name1 != nullptr);
98     assert(name2 != nullptr);
99     assert(name3 != nullptr);
100     assert(std::strcmp(name1, TEST_NAME) == 0);
101     assert(std::strcmp(name2, TEST_NAME) == 0);
102     assert(std::strcmp(name3, TEST_NAME) == 0);
103     delete[] name1;
104     delete[] name2;
105     delete[] name3;
106 
107     eng->init("test");
108 
109     assert(eng->getCurrentPluginCount() == 0);
110     assert(eng->getMaxPluginNumber() != 0);
111     assert(eng->isRunning());
112     assert(eng->getPlugin(0) == nullptr);
113 
114     name1 = eng->getUniquePluginName(TEST_NAME);
115     name2 = eng->getUniquePluginName(TEST_NAME);
116     name3 = eng->getUniquePluginName(TEST_NAME);
117 
118     assert(name1 != nullptr);
119     assert(name2 != nullptr);
120     assert(name3 != nullptr);
121     assert(std::strcmp(name1, TEST_NAME) == 0);
122     assert(std::strcmp(name2, TEST_NAME) == 0);
123     assert(std::strcmp(name3, TEST_NAME) == 0);
124     delete[] name1;
125     delete[] name2;
126     delete[] name3;
127 
128     eng->close();
129 
130     // test quick init & close 3 times
131     eng->init("test1");
132     eng->close();
133     eng->init("test2");
134     eng->close();
135     eng->init("test3");
136     eng->close();
137 
138     // leave it open
139     eng->init("test");
140 
141     // add as much plugins as possible
142     for (;;)
143     {
144         if (! eng->addPlugin(PLUGIN_INTERNAL, nullptr, TEST_NAME, "bypass"))
145             break;
146     }
147     assert(eng->getCurrentPluginCount() != 0);
148     assert(eng->getCurrentPluginCount() == eng->getMaxPluginNumber());
149     assert(eng->getCurrentPluginCount() == MAX_DEFAULT_PLUGINS);
150 
151     eng->close();
152 }
153 
main()154 int main()
155 {
156     assert(CarlaEngine::getDriverCount() != 0);
157 
158 #if 0
159     for (uint i=0, count=CarlaEngine::getDriverCount(); i < count && i < 4; ++i)
160     {
161         if (i == 1 || i == 2) continue;
162         carla_stdout("driver %i/%i: %s", i+1, count, CarlaEngine::getDriverName(i));
163 
164         if (const char* const* const devNames = CarlaEngine::getDriverDeviceNames(i))
165         {
166             for (uint j=0; devNames[j] != nullptr; ++j)
167             {
168                 CarlaEngine::getDriverDeviceInfo(i, devNames[j]);
169             }
170         }
171     }
172 #endif
173 
174     assert(CarlaPlugin::getNativePluginCount() != 0);
175 
176     for (size_t i=0, count=CarlaPlugin::getNativePluginCount(); i < count; ++i)
177     {
178         assert(CarlaPlugin::getNativePluginDescriptor(i) != nullptr);
179     }
180 
181     CarlaEngineDummy e;
182     testEngine(&e);
183 
184 //     if (CarlaEngine* const eng = CarlaEngine::newDriverByName("PulseAudio"))
185 //     {
186 //         testEngine(eng);
187 //         delete eng;
188 //     }
189 
190     return 0;
191 }
192