Resurrect profiling code.
[ardour.git] / libs / ardour / test / test_util.cc
1 #include <fstream>
2 #include <sstream>
3 #include "pbd/xml++.h"
4 #include "pbd/textreceiver.h"
5 #include "ardour/session.h"
6 #include "ardour/audioengine.h"
7 #include <cppunit/extensions/HelperMacros.h>
8
9 using namespace std;
10 using namespace ARDOUR;
11 using namespace PBD;
12
13 void
14 check_xml (XMLNode* node, string ref_file)
15 {
16         system ("rm -f libs/ardour/test/test.xml");
17         ofstream f ("libs/ardour/test/test.xml");
18         node->dump (f);
19         f.close ();
20
21         stringstream cmd;
22         cmd << "diff -u libs/ardour/test/test.xml " << ref_file;
23         CPPUNIT_ASSERT_EQUAL (0, system (cmd.str().c_str ()));
24 }
25
26 class TestReceiver : public Receiver 
27 {
28 protected:
29         void receive (Transmitter::Channel chn, const char * str) {
30                 const char *prefix = "";
31                 
32                 switch (chn) {
33                 case Transmitter::Error:
34                         prefix = ": [ERROR]: ";
35                         break;
36                 case Transmitter::Info:
37                         /* ignore */
38                         return;
39                 case Transmitter::Warning:
40                         prefix = ": [WARNING]: ";
41                         break;
42                 case Transmitter::Fatal:
43                         prefix = ": [FATAL]: ";
44                         break;
45                 case Transmitter::Throw:
46                         /* this isn't supposed to happen */
47                         abort ();
48                 }
49                 
50                 /* note: iostreams are already thread-safe: no external
51                    lock required.
52                 */
53                 
54                 cout << prefix << str << endl;
55                 
56                 if (chn == Transmitter::Fatal) {
57                         exit (9);
58                 }
59         }
60 };
61
62 TestReceiver test_receiver;
63
64 Session *
65 load_session (string dir, string state)
66 {
67         SessionEvent::create_per_thread_pool ("test", 512);
68
69         test_receiver.listen_to (error);
70         test_receiver.listen_to (info);
71         test_receiver.listen_to (fatal);
72         test_receiver.listen_to (warning);
73
74         /* We can't use VSTs here as we have a stub instead of the
75            required bits in gtk2_ardour.
76         */
77         Config->set_use_lxvst (false);
78
79         AudioEngine* engine = new AudioEngine ("test", "");
80         init_post_engine ();
81
82         CPPUNIT_ASSERT (engine->start () == 0);
83
84         Session* session = new Session (*engine, dir, state);
85         engine->set_session (session);
86         return session;
87 }