1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// ------------------------------------------------------------------------
// eca-object-factory_test.h: Unit test for ECA_OBJECT_FACTORY
// Copyright (C) 2002,2008,2009,2012 Kai Vehmanen
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ------------------------------------------------------------------------
#include <cmath> /* std::fabs() */
#include <iostream>
#include <string>
#include <typeinfo> /* typeid() */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "kvu_numtostr.h"
#include "audioio.h"
#include "audioio-device.h"
#include "eca-chainop.h"
#include "audiofx_ladspa.h"
#include "audiofx_lv2.h"
#include "preset.h"
#include "generic-controller.h"
#include "midiio.h"
#include "eca-operator.h"
#include "eca-object-factory.h"
#include "eca-object-map.h"
#include "eca-preset-map.h"
#include "eca-test-case.h"
using namespace std;
/**
* Unit test for ECA_OBJECT_FACTORY.
*
* 1. Checks that all factory object maps have
* at least one registered object.
* 2. Creates one instance of each object and
* verifies that DYNAMIC_OBJECT::new_expr() and
* DYNAMIC_OBJECT::clone() member functions
* are correctly defined.
* 3. Checks that LADSPA id and unique_name
* maps have same number of registered objects.
*/
class ECA_OBJECT_FACTORY_TEST : public ECA_TEST_CASE {
protected:
virtual string do_name(void) const { return("ECA_OBJECT_FACTORY_TEST"); }
virtual void do_run(void);
public:
virtual ~ECA_OBJECT_FACTORY_TEST(void) { }
private:
template<class T> void test_map(const ECA_OBJECT_MAP& objmap);
template<class T> void test_object_types(const T* source, const T* target, const string& description);
};
template<class T>
void ECA_OBJECT_FACTORY_TEST::test_object_types(const T* source, const T* target, const string& description)
{
if (target == 0) {
ECA_TEST_FAILURE("Unable to create object (" + description + ") of type \"" + source->name() + "\"");
}
else if (target->name() != source->name()) {
ECA_TEST_FAILURE("Type mismatch (name) between (" + description + ") and original object; type name \"" + source->name() + "\"");
}
else if (typeid(target) != typeid(source)) {
ECA_TEST_FAILURE("Type mismatch (type_id) between (" + description + ") and original object; type name \"" + source->name() + "\"");
}
}
template<class T>
void ECA_OBJECT_FACTORY_TEST::test_map(const ECA_OBJECT_MAP& objmap)
{
const list<string>& regobjs = objmap.registered_objects();
list<string>::const_iterator p = regobjs.begin();
while(p != regobjs.end()) {
const T* obj = dynamic_cast<const T*>(objmap.object(*p));
if (obj == 0) {
ECA_TEST_FAILURE("Unable to create object from keyword \"" + *p + "\"");
}
else {
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"libecasound_tester: Object type \"" +
obj->name() +
"\" successfully created.");
T* target = dynamic_cast<T*>(obj->new_expr());
test_object_types<T>(obj, target, "new_expr");
if (target != 0) delete target;
target = dynamic_cast<T*>(obj->clone());
test_object_types<T>(obj, target, "clone");
if (target != 0) {
if (obj->number_of_params() != target->number_of_params() &&
target->variable_params() != true) {
ECA_TEST_FAILURE("Cloned object has different number of arguments "
"than original object; type name \"" +
obj->name() + "\".");
}
else {
const OPERATOR* operator_source = dynamic_cast<const OPERATOR*>(obj);
OPERATOR* operator_target = dynamic_cast<OPERATOR*>(target);
const AUDIO_IO* audioio_source = dynamic_cast<const AUDIO_IO*>(obj);
AUDIO_IO* audioio_target = dynamic_cast<AUDIO_IO*>(target);
for(int n = 0; n < obj->number_of_params(); n++) {
if (obj->get_parameter_name(n + 1) !=
target->get_parameter_name(n + 1)) {
ECA_TEST_FAILURE("Cloned object has different parameter name \"" +
target->get_parameter_name(n + 1) +
"\" than that of original object \"" +
obj->get_parameter_name(n + 1) +
"\"; type name \"" +
obj->name() + "\".");
}
else {
if (operator_source != 0 && operator_target != 0) {
/* OPERATOR binds parameter type to 'SAMPLE_SPECS::sample_t' (float) */
SAMPLE_SPECS::sample_t diffval =
std::fabs(operator_source->get_parameter(n + 1) -
operator_target->get_parameter(n + 1));
if (diffval > 0.1f) {
ECA_TEST_FAILURE("Cloned object has different parameter value \"" +
kvu_numtostr(operator_target->get_parameter(n + 1)) +
"\" than that of the original object \"" +
kvu_numtostr(operator_source->get_parameter(n + 1)) +
"\", diff " +
kvu_numtostr(diffval) + "; type name \"" +
operator_source->name() + "\".");
}
}
else if (audioio_source != 0 && audioio_target != 0) {
/* AUDIO_IO binds parameter type to 'std::string' */
if (audioio_source->get_parameter(n + 1) !=
audioio_target->get_parameter(n + 1)) {
ECA_TEST_FAILURE("Cloned object has different parameter value \"" +
audioio_target->get_parameter(n + 1) +
"\" than that of the original object \"" +
audioio_source->get_parameter(n + 1) +
"\"; type name \"" +
audioio_source->name() + "\".");
}
}
}
}
}
}
delete target;
}
++p;
}
}
void ECA_OBJECT_FACTORY_TEST::do_run(void)
{
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - rt-map");
ECA_OBJECT_MAP& rt_map = ECA_OBJECT_FACTORY::audio_io_rt_map();
test_map<AUDIO_IO_DEVICE>(rt_map);
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - nonrt-map");
ECA_OBJECT_MAP& nonrt_map = ECA_OBJECT_FACTORY::audio_io_nonrt_map();
test_map<AUDIO_IO>(nonrt_map);
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - chainop-map");
ECA_OBJECT_MAP& chainop_map = ECA_OBJECT_FACTORY::chain_operator_map();
test_map<CHAIN_OPERATOR>(chainop_map);
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - LADSPA-map");
ECA_OBJECT_MAP& ladspa_map = ECA_OBJECT_FACTORY::ladspa_plugin_map();
test_map<EFFECT_LADSPA>(ladspa_map);
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - LADSPA-id-map");
ECA_OBJECT_MAP& ladspa_id_map = ECA_OBJECT_FACTORY::ladspa_plugin_id_map();
test_map<EFFECT_LADSPA>(ladspa_id_map);
if (ladspa_map.registered_objects().size() !=
ladspa_id_map.registered_objects().size()) {
ECA_TEST_FAILURE("LADSPA plugin name and id maps have different number of plugins!");
}
#if ECA_USE_LIBLILV
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - LV2-map");
ECA_OBJECT_MAP& lv2_map = ECA_OBJECT_FACTORY::lv2_plugin_map();
test_map<EFFECT_LV2>(lv2_map);
#endif
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factories - preset-map");
ECA_PRESET_MAP& preset_map = ECA_OBJECT_FACTORY::preset_map();
test_map<PRESET>(preset_map);
ECA_LOG_MSG(ECA_LOGGER::info, "libecasound_tester: object factory test done");
}
|