diff options
author | Hans Baier <hansfbaier@googlemail.com> | 2012-10-08 04:32:12 +0000 |
---|---|---|
committer | Hans Baier <hansfbaier@googlemail.com> | 2012-10-08 04:32:12 +0000 |
commit | 0bb2227eb875060e30b57c171c497e70d94b3527 (patch) | |
tree | 23f93b399a29711311ceaaf9922f08641019523a /libs/midi++2 | |
parent | 1a9d9b058c74c6e73da85ee90334a087844a36b2 (diff) |
add unit tests to midi++ and assorted bugfixes for midnam_patch.cc
git-svn-id: svn://localhost/ardour2/branches/3.0@13213 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/midi++2')
-rw-r--r-- | libs/midi++2/midi++/midnam_patch.h | 4 | ||||
-rw-r--r-- | libs/midi++2/midnam_patch.cc | 10 | ||||
-rw-r--r-- | libs/midi++2/test/MidnamTest.cpp | 125 | ||||
-rw-r--r-- | libs/midi++2/test/MidnamTest.hpp | 48 | ||||
-rw-r--r-- | libs/midi++2/test/testrunner.cpp | 31 | ||||
-rw-r--r-- | libs/midi++2/wscript | 17 |
6 files changed, 231 insertions, 4 deletions
diff --git a/libs/midi++2/midi++/midnam_patch.h b/libs/midi++2/midi++/midnam_patch.h index f3ff434a9f..a6b2b00985 100644 --- a/libs/midi++2/midi++/midnam_patch.h +++ b/libs/midi++2/midi++/midnam_patch.h @@ -1,5 +1,6 @@ /* - Copyright (C) 2008 Hans Baier + Copyright (C) 2012 Paul Davis + Author: Hans Baier 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 @@ -267,6 +268,7 @@ public: XMLNode& get_state (void); int set_state (const XMLTree&, const XMLNode&); + /// Note: channel here is 0-based while in the MIDNAM-file it's 1-based std::string channel_name_set_name_by_channel(uint8_t channel) { assert(channel <= 15); return _channel_name_set_assignments[channel]; diff --git a/libs/midi++2/midnam_patch.cc b/libs/midi++2/midnam_patch.cc index 2b32fee42e..59fba49ded 100644 --- a/libs/midi++2/midnam_patch.cc +++ b/libs/midi++2/midnam_patch.cc @@ -404,7 +404,8 @@ CustomDeviceMode::get_state(void) boost::shared_ptr<CustomDeviceMode> MasterDeviceNames::custom_device_mode_by_name(std::string mode_name) { - assert(mode_name != ""); + // can't assert this, since in many of the patch files the mode name is empty + //assert(mode_name != ""); return _custom_device_modes[mode_name]; } @@ -428,7 +429,7 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode& a_node) // Manufacturer boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer"); assert(manufacturer->size() == 1); - _manufacturer = manufacturer->front()->content(); + _manufacturer = manufacturer->front()->children().front()->content(); // Models boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model"); @@ -549,7 +550,10 @@ MIDINameDocument::set_state (const XMLTree& tree, const XMLNode& a_node) error << "No author information in MIDNAM file" << endmsg; return -1; } - _author = author->front()->content(); + + if (author->front()->children().size() > 0) { + _author = author->front()->children().front()->content(); + } // MasterDeviceNames diff --git a/libs/midi++2/test/MidnamTest.cpp b/libs/midi++2/test/MidnamTest.cpp new file mode 100644 index 0000000000..b993f2d65e --- /dev/null +++ b/libs/midi++2/test/MidnamTest.cpp @@ -0,0 +1,125 @@ +#include "MidnamTest.hpp" + +#include <glibmm/fileutils.h> + +#include "pbd/xml++.h" +#include "pbd/file_utils.h" +#include "midi++/midnam_patch.h" + +using namespace std; +using namespace MIDI::Name; + +CPPUNIT_TEST_SUITE_REGISTRATION( MidnamTest ); + +static string const prefix = "../../../patchfiles/"; + +void MidnamTest::protoolsPatchFileTest() { + XMLTree xmldoc(prefix + "ProtoolsPatchFile.midnam"); + boost::shared_ptr<XMLSharedNodeList> result = xmldoc.find( + "//MIDINameDocument"); + CPPUNIT_ASSERT(result->size() == 1); + + result = xmldoc.find("//ChannelNameSet"); + CPPUNIT_ASSERT(result->size() == 2); + + MIDINameDocument doc(prefix + "ProtoolsPatchFile.midnam"); + CPPUNIT_ASSERT(doc.all_models().size() == 1); + CPPUNIT_ASSERT(doc.author().find("Mark of the Unicorn") == 0); + + const string model = doc.all_models().front(); + CPPUNIT_ASSERT_EQUAL(string("SC-88 Pro"), model); + boost::shared_ptr<MasterDeviceNames> masterDeviceNames = + doc.master_device_names_by_model().find(model)->second; + CPPUNIT_ASSERT_EQUAL(string("Roland"), masterDeviceNames->manufacturer()); + + string modename = masterDeviceNames->custom_device_mode_names().front(); + CPPUNIT_ASSERT_EQUAL(string("Mode 1"), modename); + + boost::shared_ptr<CustomDeviceMode> mode = + masterDeviceNames->custom_device_mode_by_name(modename); + + CPPUNIT_ASSERT_EQUAL(modename, mode->name()); + + string ns1 = string("Name Set 1"); + string ns2 = string("Name Set 2"); + + for (uint8_t i = 0; i <= 15; i++) { + if (i != 9) + CPPUNIT_ASSERT_EQUAL(ns1, + mode->channel_name_set_name_by_channel(i)); + else + CPPUNIT_ASSERT_EQUAL(ns2, + mode->channel_name_set_name_by_channel(i)); + } + + boost::shared_ptr<ChannelNameSet> nameSet1 = + masterDeviceNames->channel_name_set_by_device_mode_and_channel( + modename, 0); + boost::shared_ptr<ChannelNameSet> nameSet2 = + masterDeviceNames->channel_name_set_by_device_mode_and_channel( + modename, 9); + + CPPUNIT_ASSERT_EQUAL(ns1, nameSet1->name()); + CPPUNIT_ASSERT_EQUAL(ns2, nameSet2->name()); + + const ChannelNameSet::PatchBanks& banks1 = nameSet1->patch_banks(); + const ChannelNameSet::PatchBanks& banks2 = nameSet2->patch_banks(); + CPPUNIT_ASSERT(banks1.size() == 16); + CPPUNIT_ASSERT(banks2.size() == 1); + + boost::shared_ptr<PatchBank> bank = banks1.front(); + CPPUNIT_ASSERT_EQUAL(string("Piano"), bank->name()); + const PatchBank::PatchNameList& plist1 = bank->patch_name_list(); + CPPUNIT_ASSERT(plist1.size() == 110); + + bank = banks2.front(); + CPPUNIT_ASSERT_EQUAL(string("Drum sets"), bank->name()); + const PatchBank::PatchNameList& plist2 = bank->patch_name_list(); + CPPUNIT_ASSERT(plist2.size() == 49); +} + +void +MidnamTest::loadAllMidnamsTest () +{ + assert (Glib::file_test (prefix, Glib::FILE_TEST_IS_DIR)); + + Glib::PatternSpec pattern(string("*.midnam")); + vector<std::string> result; + + PBD::find_matching_files_in_directory (prefix, pattern, result); + + cout << "Loading " << result.size() << " MIDI patches from " << prefix << endl; + + for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) { + cout << "Processing file " << *i << endl; + boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(*i)); + + XMLTree xmldoc(*i); + boost::shared_ptr<XMLSharedNodeList> result = xmldoc.find("//MIDINameDocument"); + CPPUNIT_ASSERT(result->size() == 1); + + result = xmldoc.find("//MasterDeviceNames"); + CPPUNIT_ASSERT(result->size() == 1); + + result = xmldoc.find("//ChannelNameSet"); + CPPUNIT_ASSERT(result->size() >= 1); + + result = xmldoc.find("//PatchBank"); + int banks = result->size(); + + + result = xmldoc.find("//CustomDeviceMode[1]"); + string deviceModeName = result->front()->property("Name")->value(); + + MIDINameDocument::MasterDeviceNamesList::const_iterator device = + document->master_device_names_by_model().begin(); + + string modename = device->second->custom_device_mode_names().front(); + cerr << "modename:" << modename << endl; + boost::shared_ptr<CustomDeviceMode> mode = device->second->custom_device_mode_by_name(modename); + CPPUNIT_ASSERT_EQUAL(deviceModeName, mode->name()); + + boost::shared_ptr<ChannelNameSet> nameSet = device->second->channel_name_set_by_device_mode_and_channel(modename, 0); + } +} + diff --git a/libs/midi++2/test/MidnamTest.hpp b/libs/midi++2/test/MidnamTest.hpp new file mode 100644 index 0000000000..b04c12ed99 --- /dev/null +++ b/libs/midi++2/test/MidnamTest.hpp @@ -0,0 +1,48 @@ +/* + Copyright (C) 2012 Paul Davis + Author: Hans Baier + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id$ +*/ + +#include <cassert> +#include <stdint.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +class MidnamTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(MidnamTest); + CPPUNIT_TEST(protoolsPatchFileTest); + CPPUNIT_TEST(loadAllMidnamsTest); + CPPUNIT_TEST_SUITE_END(); + +public: + typedef double Time; + + void setUp() { + } + + void tearDown() { + } + + void protoolsPatchFileTest(); + void loadAllMidnamsTest(); + +private: +}; + diff --git a/libs/midi++2/test/testrunner.cpp b/libs/midi++2/test/testrunner.cpp new file mode 100644 index 0000000000..b1c63172c4 --- /dev/null +++ b/libs/midi++2/test/testrunner.cpp @@ -0,0 +1,31 @@ +#include <cppunit/CompilerOutputter.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <cppunit/TestResult.h> +#include <cppunit/TestResultCollector.h> +#include <cppunit/TestRunner.h> +#include <cppunit/BriefTestProgressListener.h> + +#include <glibmm.h> + +int +main() +{ + Glib::thread_init(); + + CppUnit::TestResult testresult; + + CppUnit::TestResultCollector collectedresults; + testresult.addListener (&collectedresults); + + CppUnit::BriefTestProgressListener progress; + testresult.addListener (&progress); + + CppUnit::TestRunner testrunner; + testrunner.addTest (CppUnit::TestFactoryRegistry::getRegistry ().makeTest ()); + testrunner.run (testresult); + + CppUnit::CompilerOutputter compileroutputter (&collectedresults, std::cerr); + compileroutputter.write (); + + return collectedresults.wasSuccessful () ? 0 : 1; +} diff --git a/libs/midi++2/wscript b/libs/midi++2/wscript index d4f71124aa..9f2f297a4b 100644 --- a/libs/midi++2/wscript +++ b/libs/midi++2/wscript @@ -27,12 +27,15 @@ path_prefix = 'libs/midi++2/' def options(opt): autowaf.set_options(opt) + opt.add_option('--test', action='store_true', default=False, dest='build_tests', + help="Build unit tests") def configure(conf): conf.load('compiler_cxx') autowaf.build_version_files(path_prefix+'midi++/version.h', path_prefix+'version.cc', 'midipp', MAJOR, MINOR, MICRO) autowaf.configure(conf) + autowaf.check_pkg(conf, 'cppunit', uselib_store='CPPUNIT', atleast_version='1.12.0', mandatory=False) autowaf.check_pkg(conf, 'jack', uselib_store='JACK', atleast_version='0.118.2') autowaf.check_pkg(conf, 'libxml-2.0', uselib_store='XML') autowaf.check_pkg(conf, 'sigc++-2.0', uselib_store='SIGCPP', atleast_version='2.0') @@ -68,5 +71,19 @@ def build(bld): obj.vnum = LIBMIDIPP_LIB_VERSION obj.install_path = os.path.join(bld.env['LIBDIR'], 'ardour3') + if bld.env['BUILD_TESTS'] and bld.is_defined('HAVE_CPPUNIT'): + # Unit tests + obj = bld(features = 'cxx cxxprogram') + obj.source = ''' + test/MidnamTest.cpp + test/testrunner.cpp + ''' + obj.includes = ['.', './src'] + obj.use = 'libmidipp' + obj.uselib = 'CPPUNIT XML' + obj.target = 'run-tests' + obj.name = 'libmidipp-tests' + obj.install_path = '' + def shutdown(): autowaf.shutdown() |