diff options
author | André Nusser <andre.nusser@googlemail.com> | 2017-01-17 14:06:53 +0100 |
---|---|---|
committer | André Nusser <andre.nusser@googlemail.com> | 2017-01-17 14:27:25 +0100 |
commit | c03bd6049a635ae42c0ca2cc3bc88fc1f489b477 (patch) | |
tree | 9dc136410b1fcbdaa255c7fc79202cd36794c04c /test | |
parent | b328df4dea35d4d4c1713bd3fb6bda48d30b9e1b (diff) |
Make DrumkitCreator clean up the created files in the destructor.
Diffstat (limited to 'test')
-rw-r--r-- | test/audiocachefiletest.cc | 6 | ||||
-rw-r--r-- | test/audiocachetest.cc | 10 | ||||
-rw-r--r-- | test/drumkit_creator.cc | 96 | ||||
-rw-r--r-- | test/drumkit_creator.h | 153 | ||||
-rw-r--r-- | test/drumkitcreatortest.cc | 6 | ||||
-rw-r--r-- | test/enginetest.cc | 6 | ||||
-rw-r--r-- | test/lv2.cc | 14 | ||||
-rw-r--r-- | test/memcheckertest.cc | 7 | ||||
-rw-r--r-- | test/resource_test.cc | 4 |
9 files changed, 170 insertions, 132 deletions
diff --git a/test/audiocachefiletest.cc b/test/audiocachefiletest.cc index 7ae2f95..83aece8 100644 --- a/test/audiocachefiletest.cc +++ b/test/audiocachefiletest.cc @@ -61,6 +61,8 @@ class AudioCacheFileTest CPPUNIT_TEST(noFileTest); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -68,7 +70,7 @@ public: void refTest() { // Create the audio file - auto filename = drumkit_creator::createSingleChannelWav("single_channel.wav"); + auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav"); // Conduct tests TestableAudioCacheFiles audiofiles; @@ -92,7 +94,7 @@ public: printf("Test buffer size: %d samples\n", (int)buffer_size); // Create the audio file - auto filename = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav"); // Conduct tests AudioFile* ref_file[13]; diff --git a/test/audiocachetest.cc b/test/audiocachetest.cc index a3d62c9..546a5c4 100644 --- a/test/audiocachetest.cc +++ b/test/audiocachetest.cc @@ -44,6 +44,8 @@ class AudioCacheTest CPPUNIT_TEST(multiChannelThreaded); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -140,7 +142,7 @@ public: { printf("\nsinglechannel_nonthreaded()\n"); - auto filename = drumkit_creator::createSingleChannelWav("single_channel.wav"); + auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav"); // Conduct test int channel = 0; @@ -152,7 +154,7 @@ public: { printf("\nsinglechannel_threaded()\n"); - auto filename = drumkit_creator::createSingleChannelWav("single_channel.wav"); + auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav"); // Conduct test int channel = 0; @@ -164,7 +166,7 @@ public: { printf("\nmultichannel_nonthreaded()\n"); - auto filename = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav"); // Conduct test int channel = 0; @@ -178,7 +180,7 @@ public: { printf("\nmultichannel_threaded()\n"); - auto filename = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav"); // Conduct test int channel = 0; diff --git a/test/drumkit_creator.cc b/test/drumkit_creator.cc index f773c6f..9fae6e3 100644 --- a/test/drumkit_creator.cc +++ b/test/drumkit_creator.cc @@ -26,6 +26,8 @@ */ #include "drumkit_creator.h" +#include <unistd.h> + #include "../src/random.h" #include <sndfile.h> @@ -35,25 +37,30 @@ #include <cstdlib> #include <algorithm> -// FIXME: unix-centric paths, automatically clean up the drumkit directory - -namespace drumkit_creator +DrumkitCreator::~DrumkitCreator() { + for (const auto& file: created_files) + { + auto error = unlink(file.c_str()); + + if (error) { + std::cerr << "File could not be deleted in DrumkitCreator destructor" + << std::endl; + } + } -std::vector<Sample> createData(const WavInfo& wav_info, - std::size_t number_of_channels); -std::string createTemporaryDirectory(const std::string& name); -void createInstrument(const InstrumentData& data, std::size_t number_of_channels, - const std::string& dir); -std::string createDrumkitFile(const DrumkitData& data, const std::string& dir); + for (const auto& dir: created_directories) + { + auto error = rmdir(dir.c_str()); -bool is_valid(const DrumkitData& data) -{ - // TODO Check the consistency of the data. - return true; + if (error) { + std::cerr << "Directory could not be deleted in DrumkitCreator destructor" + << std::endl; + } + } } -std::string create(const DrumkitData& data) +std::string DrumkitCreator::create(const DrumkitData& data) { std::string drumkit_filename; @@ -73,14 +80,13 @@ std::string create(const DrumkitData& data) } else { - // TODO report error - std::cout << "ERROR: create" << std::endl; + throw "DrumkitData not valid"; } return drumkit_filename; } -void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir) +void DrumkitCreator::createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir) { SF_INFO sfinfo; sfinfo.samplerate = 44100; @@ -90,21 +96,19 @@ void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const st std::string filename = dir + "/" + wav_info.filename; auto sndfile = sf_open(filename.c_str(), SFM_WRITE, &sfinfo); if (!sndfile) { - std::cout << "ERROR: Wav file couldn't be created: " - << sf_strerror(sndfile) << std::endl; - return; + throw "The wav file could not be created"; } + created_files.push_back(filename); + auto data_vec = createData(wav_info, number_of_channels); - - auto written_count = sf_write_raw(sndfile, data_vec.data(), 2*data_vec.size()); - std::cout << "Written: " << written_count << std::endl; + sf_write_raw(sndfile, data_vec.data(), 2*data_vec.size()); sf_write_sync(sndfile); sf_close(sndfile); } -std::string createStdKit(const std::string& name) +std::string DrumkitCreator::createStdKit(const std::string& name) { std::vector<WavInfo> wav_infos = { WavInfo("1011.wav", 1, 0x1110), @@ -127,7 +131,7 @@ std::string createStdKit(const std::string& name) return create(kit_data); } -std::string createSmallKit(const std::string& name) +std::string DrumkitCreator::createSmallKit(const std::string& name) { std::vector<WavInfo> wav_infos = { WavInfo("small_instr.wav", 549833) @@ -149,7 +153,7 @@ std::string createSmallKit(const std::string& name) return create(kit_data); } -std::string createHugeKit(const std::string& name) +std::string DrumkitCreator::createHugeKit(const std::string& name) { std::vector<WavInfo> wav_infos = { WavInfo("huge_instr.wav", 549833) @@ -177,7 +181,7 @@ std::string createHugeKit(const std::string& name) return create(kit_data); } -std::string createSingleChannelWav(const std::string& name) +std::string DrumkitCreator::createSingleChannelWav(const std::string& name) { auto dir = createTemporaryDirectory("wavfiles"); @@ -189,7 +193,7 @@ std::string createSingleChannelWav(const std::string& name) return dir + "/" + name; } -std::string createMultiChannelWav(const std::string& name) +std::string DrumkitCreator::createMultiChannelWav(const std::string& name) { auto dir = createTemporaryDirectory("wavfiles"); @@ -201,7 +205,7 @@ std::string createMultiChannelWav(const std::string& name) return dir + "/" + name; } -std::string create0000Wav(const std::string& name) +std::string DrumkitCreator::create0000Wav(const std::string& name) { auto dir = createTemporaryDirectory("wavfiles"); @@ -213,7 +217,7 @@ std::string create0000Wav(const std::string& name) return dir + "/" + name; } -std::string createStdMidimap(const std::string& name) +std::string DrumkitCreator::createStdMidimap(const std::string& name) { auto dir = createTemporaryDirectory("midimap"); @@ -228,12 +232,12 @@ std::string createStdMidimap(const std::string& name) file.open(filename); if (file.is_open()) { + created_files.push_back(filename); file << content; } else { - // TODO print error - std::cout << "ERROR: instrument" << std::endl; + throw "File could not be opened"; } file.close(); @@ -241,23 +245,29 @@ std::string createStdMidimap(const std::string& name) } // -// Helper functions +// private member functions // -std::string createTemporaryDirectory(const std::string& name) +bool DrumkitCreator::is_valid(const DrumkitData& data) +{ + // TODO Check the consistency of the data. + return true; +} + +std::string DrumkitCreator::createTemporaryDirectory(const std::string& name) { - // FIXME Use cross-platform temporary directory std::string dir_template = "/tmp/drumgizmo_" + name + "XXXXXX"; const auto dir_name = mkdtemp(&dir_template[0]); if (dir_name) { + created_directories.push_back(dir_name); return std::string(dir_name); } return ""; } -std::vector<Sample> createData(const WavInfo& wav_info, std::size_t number_of_channels) +auto DrumkitCreator::createData(const WavInfo& wav_info, std::size_t number_of_channels) -> std::vector<Sample> { std::vector<Sample> data_vec(number_of_channels * wav_info.length, wav_info.sample); if (wav_info.is_random) @@ -274,7 +284,7 @@ std::vector<Sample> createData(const WavInfo& wav_info, std::size_t number_of_ch return data_vec; } -void createInstrument(const InstrumentData& data, std::size_t number_of_channels, +void DrumkitCreator::createInstrument(const InstrumentData& data, std::size_t number_of_channels, const std::string& dir) { std::string prefix = "<?xml version='1.0' encoding='UTF-8'?>\n" @@ -307,17 +317,17 @@ void createInstrument(const InstrumentData& data, std::size_t number_of_channels file.open(filename); if (file.is_open()) { + created_files.push_back(filename); file << prefix + samples + postfix; } else { - // TODO print error - std::cout << "ERROR: instrument" << std::endl; + throw "File could not be opened"; } file.close(); } -std::string createDrumkitFile(const DrumkitData& data, const std::string& dir) +std::string DrumkitCreator::createDrumkitFile(const DrumkitData& data, const std::string& dir) { // Pre- and postfix string std::string prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" @@ -355,16 +365,14 @@ std::string createDrumkitFile(const DrumkitData& data, const std::string& dir) file.open(filename); if (file.is_open()) { + created_files.push_back(filename); file << prefix + channels + instruments + postfix; } else { - // TODO print error - std::cout << "ERROR: drumkit" << std::endl; + throw "File could not be opened"; } file.close(); return filename; } - -} // end drumkit_creator diff --git a/test/drumkit_creator.h b/test/drumkit_creator.h index 93a6f36..7c63e6a 100644 --- a/test/drumkit_creator.h +++ b/test/drumkit_creator.h @@ -30,72 +30,91 @@ #include <vector> #include <cstdint> -namespace drumkit_creator +class DrumkitCreator { - -using Sample = uint16_t; - -//! If is_random is true then this overrules the sample member. If is_random -//! is false however, every sample is chosen as the one in the member variable. -struct WavInfo -{ - const std::string filename; - const std::size_t length; - - const bool is_random; - const Sample sample; - - WavInfo(const std::string& filename, std::size_t length) - : filename(filename), length(length), is_random(true), sample(0) {} - - WavInfo(const std::string& filename, std::size_t length, Sample sample) - : filename(filename), length(length), is_random(false), sample(sample) {} -}; - -struct Audiofile -{ - WavInfo* wav_info; - std::size_t filechannel; -}; - -struct SampleData -{ - std::string name; - // Vector of non-owning pointers and therefore it is raw. - std::vector<Audiofile> audiofiles; +public: + using Sample = uint16_t; + + //! If is_random is true then this overrules the sample member. If is_random + //! is false however, every sample is chosen as the one in the member variable. + struct WavInfo + { + const std::string filename; + const std::size_t length; + + const bool is_random; + const Sample sample; + + WavInfo(const std::string& filename, std::size_t length) + : filename(filename), length(length), is_random(true), sample(0) {} + + WavInfo(const std::string& filename, std::size_t length, Sample sample) + : filename(filename), length(length), is_random(false), sample(sample) {} + }; + + struct Audiofile + { + WavInfo* wav_info; + std::size_t filechannel; + }; + + struct SampleData + { + std::string name; + // Vector of non-owning pointers and therefore it is raw. + std::vector<Audiofile> audiofiles; + }; + + struct InstrumentData + { + std::string name; + std::string filename; + std::vector<SampleData> sample_data; + }; + + struct DrumkitData + { + std::string name; + std::size_t number_of_channels; + std::vector<InstrumentData> instruments; + std::vector<WavInfo> wav_infos; + }; + + DrumkitCreator() = default; + + //! This destructor removes all the created temporary files and directories. + ~DrumkitCreator(); + + //! Creates a drumkit from data in the temporary directory of the OS and + //! returns its filename. + std::string create(const DrumkitData& data); + + //! Creates a single wav file + void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir); + + //! Those functions create some special wav files, drumkits, and midimaps + //@{ + std::string createStdKit(const std::string& name); + std::string createSmallKit(const std::string& name); + std::string createHugeKit(const std::string& name); + + std::string createSingleChannelWav(const std::string& name); + std::string createMultiChannelWav(const std::string& name); + std::string create0000Wav(const std::string& name); + + std::string createStdMidimap(const std::string& name); + //@} + +private: + std::vector<std::string> created_files; + std::vector<std::string> created_directories; + + bool is_valid(const DrumkitData& data); + + std::string createTemporaryDirectory(const std::string& name); + std::vector<Sample> createData(const WavInfo& wav_info, + std::size_t number_of_channels); + void createInstrument(const InstrumentData& data, std::size_t number_of_channels, + const std::string& dir); + std::string createDrumkitFile(const DrumkitData& data, const std::string& dir); }; - -struct InstrumentData -{ - std::string name; - std::string filename; - std::vector<SampleData> sample_data; -}; - -struct DrumkitData -{ - std::string name; - std::size_t number_of_channels; - std::vector<InstrumentData> instruments; - std::vector<WavInfo> wav_infos; -}; - -//! Creates a drumkit from data in the temporary directory of the OS and -//! returns its filename. -std::string create(const DrumkitData& data); - -//! Creates a single wav file -void createWav(const WavInfo& wav_info, std::size_t number_of_channels, const std::string& dir); - -//! Those functions create some special wav files, drumkits, and midimaps -std::string createStdKit(const std::string& name); -std::string createSmallKit(const std::string& name); -std::string createHugeKit(const std::string& name); - -std::string createSingleChannelWav(const std::string& name); -std::string createMultiChannelWav(const std::string& name); -std::string create0000Wav(const std::string& name); - -std::string createStdMidimap(const std::string& name); - -} // end drumkit_creator diff --git a/test/drumkitcreatortest.cc b/test/drumkitcreatortest.cc index 852f407..1b6321b 100644 --- a/test/drumkitcreatortest.cc +++ b/test/drumkitcreatortest.cc @@ -28,8 +28,6 @@ #include "drumkit_creator.h" -using namespace drumkit_creator; - class DrumkitcreatorTest : public CppUnit::TestFixture { @@ -37,6 +35,8 @@ class DrumkitcreatorTest CPPUNIT_TEST(testTest); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() { @@ -50,7 +50,7 @@ public: //! This just creates some drumkit. void testTest() { - drumkit_creator::createStdKit("stdkit"); + drumkit_creator.createStdKit("stdkit"); } }; diff --git a/test/enginetest.cc b/test/enginetest.cc index 7079bad..5e33fe2 100644 --- a/test/enginetest.cc +++ b/test/enginetest.cc @@ -71,6 +71,8 @@ class test_engine : public CppUnit::TestFixture CPPUNIT_TEST(loading); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -84,8 +86,8 @@ public: dg.setFrameSize(100); // Create drumkits - auto kit1_file = drumkit_creator::createStdKit("kit1"); - auto kit2_file = drumkit_creator::createStdKit("kit2"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); + auto kit2_file = drumkit_creator.createStdKit("kit2"); // Switch kits emmidiately without giving the loader time to work: for(int i = 0; i < 100; ++i) diff --git a/test/lv2.cc b/test/lv2.cc index 4ad6d94..78bf342 100644 --- a/test/lv2.cc +++ b/test/lv2.cc @@ -62,6 +62,8 @@ class test_lv2 : public CppUnit::TestFixture CPPUNIT_TEST(test1); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} @@ -110,9 +112,9 @@ public: "</config>"; // Create drumkit - auto kit1_file = drumkit_creator::createStdKit("kit1"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); - auto midimapfile = drumkit_creator::createStdMidimap("midimap"); + auto midimapfile = drumkit_creator.createStdMidimap("midimap"); bool enable_velocity_modifier = true; float velocity_modifier_falloff = 0.5; float velocity_modifier_weight = 0.25; @@ -177,9 +179,9 @@ public: "</config>"; // Create drumkit - auto kit1_file = drumkit_creator::createStdKit("kit1"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); - auto midimapfile = drumkit_creator::createStdMidimap("midimap"); + auto midimapfile = drumkit_creator.createStdMidimap("midimap"); bool enable_velocity_modifier = true; float velocity_modifier_falloff = 0.5; float velocity_modifier_weight = 0.25; @@ -256,9 +258,9 @@ public: "</config>"; // Create drumkit - auto kit1_file = drumkit_creator::createStdKit("kit1"); + auto kit1_file = drumkit_creator.createStdKit("kit1"); - auto midimapfile = drumkit_creator::createStdMidimap("midimap"); + auto midimapfile = drumkit_creator.createStdMidimap("midimap"); bool enable_velocity_modifier = true; float velocity_modifier_falloff = 0.5; float velocity_modifier_weight = 0.25; diff --git a/test/memcheckertest.cc b/test/memcheckertest.cc index 125ff6c..da5eae6 100644 --- a/test/memcheckertest.cc +++ b/test/memcheckertest.cc @@ -57,10 +57,11 @@ private: Settings settings; DrumKit kit; Random random; + DrumkitCreator drumkit_creator; - const std::string small_kit_path = drumkit_creator::createSmallKit("small_kit"); - const std::string huge_kit_path = drumkit_creator::createHugeKit("huge_kit"); - const std::string audiofile = drumkit_creator::createMultiChannelWav("multi_channel.wav"); + const std::string small_kit_path = drumkit_creator.createSmallKit("small_kit"); + const std::string huge_kit_path = drumkit_creator.createHugeKit("huge_kit"); + const std::string audiofile = drumkit_creator.createMultiChannelWav("multi_channel.wav"); public: void setUp() { diff --git a/test/resource_test.cc b/test/resource_test.cc index c8650a5..d6d9221 100644 --- a/test/resource_test.cc +++ b/test/resource_test.cc @@ -49,13 +49,15 @@ class ResourceTest : public CppUnit::TestFixture CPPUNIT_TEST(internalReadTest); CPPUNIT_TEST_SUITE_END(); + DrumkitCreator drumkit_creator; + public: void setUp() {} void tearDown() {} void externalReadTest() { - auto filename = drumkit_creator::create0000Wav("0000.wav"); + auto filename = drumkit_creator.create0000Wav("0000.wav"); ResourceTester rc(filename); CPPUNIT_ASSERT(!rc.probeIsInternal()); |