allow to run unit-test under wine from srcdir.
[ardour.git] / libs / evoral / test / SMFTest.cpp
1 #include "SMFTest.hpp"
2
3 #include <glibmm/fileutils.h>
4 #include <glibmm/miscutils.h>
5
6 #include "pbd/file_utils.h"
7
8 using namespace std;
9
10 CPPUNIT_TEST_SUITE_REGISTRATION( SMFTest );
11
12 void
13 SMFTest::createNewFileTest ()
14 {
15         TestSMF smf;
16
17         string output_dir_path = PBD::tmp_writable_directory (PACKAGE, "createNewFileTest");
18         string new_file_path = Glib::build_filename (output_dir_path, "NewFile.mid");
19         smf.create(new_file_path);
20         smf.close();
21         CPPUNIT_ASSERT(Glib::file_test (new_file_path, Glib::FILE_TEST_IS_REGULAR));
22 }
23
24 PBD::Searchpath
25 test_search_path ()
26 {
27 #ifdef PLATFORM_WINDOWS
28         if (!getenv("EVORAL_TEST_PATH")) {
29                 string wsp(g_win32_get_package_installation_directory_of_module(NULL));
30                 return Glib::build_filename (wsp,  "evoral_testdata");
31         }
32 #endif
33         return Glib::getenv("EVORAL_TEST_PATH");
34 }
35
36 void
37 SMFTest::takeFiveTest ()
38 {
39         TestSMF smf;
40         string testdata_path;
41         CPPUNIT_ASSERT (find_file (test_search_path (), "TakeFive.mid", testdata_path));
42         smf.open(testdata_path);
43         CPPUNIT_ASSERT(!smf.is_empty());
44
45         seq->start_write();
46         smf.seek_to_start();
47
48         uint64_t time = 0; /* in SMF ticks */
49         Evoral::Event<Evoral::Beats> ev;
50
51         uint32_t delta_t = 0;
52         uint32_t size    = 0;
53         uint8_t* buf     = NULL;
54         int ret;
55         while ((ret = smf.read_event(&delta_t, &size, &buf)) >= 0) {
56                 ev.set(buf, size, Evoral::Beats());
57                 time += delta_t;
58
59                 if (ret > 0) { // didn't skip (meta) event
60                         //cerr << "read smf event type " << hex << int(buf[0]) << endl;
61                         ev.set_time(Evoral::Beats::ticks_at_rate(time, smf.ppqn()));
62                         ev.set_event_type(type_map->midi_event_type(buf[0]));
63                         seq->append(ev, next_event_id ());
64                 }
65         }
66
67         seq->end_write (Sequence<Time>::Relax,
68                         Evoral::Beats::ticks_at_rate(time, smf.ppqn()));
69         CPPUNIT_ASSERT(!seq->empty());
70 }