root/trunk/libffado/src/libosc/unittests.cpp

Revision 445, 6.3 kB (checked in by pieterpalmers, 17 years ago)

* name change from FreeBoB to FFADO
* replaced tabs by 4 spaces
* got rid of end-of-line spaces
* made all license and copyrights conform

library becomes LGPL, apps become GPL
explicitly state LGPL v2.1 and GPL v2 (don't like v3 draft)

copyrights are 2005-2007 Daniel & Pieter
except for the MotU stuff (C) Jonathan, Pieter

Line 
1 /*
2  * Copyright (C) 2005-2007 by Daniel Wagner
3  * Copyright (C) 2005-2007 by Pieter Palmers
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB.
9  *
10  * This is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * FFADO is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with FFADO; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA.
23  */
24
25 #include "OscServer.h"
26 #include "OscArgument.h"
27 #include "OscMessage.h"
28 #include "OscResponse.h"
29 #include "OscNode.h"
30
31 #include <signal.h>
32 #include <stdio.h>
33 #include <string>
34
35 using namespace OSC;
36
37 #define TEST_SHOULD_RETURN_TRUE(test) \
38     (test ? true : printf( "'" #test "' should return true\n") && false )
39 #define TEST_SHOULD_RETURN_FALSE(test) \
40     (test ? printf( "'" #test "' should return true\n") && false : true )
41
42 static int run=0;
43
44 static void sighandler (int sig)
45 {
46         run = 0;
47 }
48
49 ///////////////////////////////////////
50
51 static bool
52 test_OscMessage_T1() {
53     bool result=true;
54     OscMessage m=OscMessage();
55
56     m.addArgument((float)1.1);
57     m.addArgument("teststring");
58     m.addArgument(1);
59     m.print();
60
61     result &= TEST_SHOULD_RETURN_TRUE(m.nbArguments()==3);
62     result &= TEST_SHOULD_RETURN_TRUE(m.getArgument(0).getFloat()==(float)1.1);
63     result &= TEST_SHOULD_RETURN_TRUE(m.getArgument(1).getString()==string("teststring"));
64     result &= TEST_SHOULD_RETURN_TRUE(m.getArgument(2).getInt()==1);
65
66     return result;
67 }
68
69
70 static bool
71 test_OscNode_T1() {
72     bool result=true;
73     OscMessage m=OscMessage();
74
75     OscNode n1=OscNode("base1");
76     OscNode n2=OscNode("child1");
77     OscNode n3=OscNode("child2");
78     OscNode n4=OscNode("subchild1");
79     OscNode n5=OscNode("subchild2");
80
81     OscNode n6=OscNode("subsubchild1");
82     OscNode n7=OscNode("subsubchild2");
83
84     OscNode n8=OscNode("auto1");
85     OscNode n9=OscNode("auto2");
86
87     n1.addChildOscNode(&n2);
88     n1.addChildOscNode(&n3);
89     n2.addChildOscNode(&n4);
90     n2.addChildOscNode(&n5);
91
92     n1.addChildOscNode(&n6,"base1/child1/");
93     n1.addChildOscNode(&n7,"base1/child1/");
94
95     n1.addChildOscNode(&n8,"auto1/test/");
96     n1.addChildOscNode(&n9,"auto1/test2/");
97
98     n1.printOscNode();
99
100     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1",&m).isError());
101     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child1",&m).isError());
102     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child1/subchild1",&m).isError());
103     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child1/subchild2",&m).isError());
104     result &= TEST_SHOULD_RETURN_TRUE(n1.processOscMessage("base1/child1/subchild3",&m).isError());
105     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child2",&m).isError());
106     result &= TEST_SHOULD_RETURN_TRUE(n1.processOscMessage("base1/child2/subchild1",&m).isError());
107
108     return result;
109 }
110
111 ////////////////////////////////
112
113 class OscTestNode
114     : public OscNode
115 {
116 public:
117     OscTestNode(std::string s)
118         : OscNode(s) {};
119     virtual ~OscTestNode() {};
120
121     virtual OscResponse processOscMessage(OscMessage *m) {
122         return OscResponse(*m);
123     };
124 };
125
126 static bool
127 test_OscServer_T1() {
128     bool result=true;
129
130     OscServer s=OscServer("17820");
131
132     if (!s.init()) {
133         printf("failed to init server");
134         return false;
135     }
136
137     OscNode n1=OscNode("base1");
138     OscNode n2=OscNode("child1");
139     OscNode n3=OscNode("child2");
140     OscNode n4=OscNode("subchild1");
141     OscNode n5=OscNode("subchild2");
142
143     OscNode n6=OscNode("subsubchild1");
144     OscNode n7=OscNode("subsubchild2");
145
146     OscNode n8=OscNode("auto1");
147     OscNode n9=OscNode("auto2");
148
149     n1.addChildOscNode(&n2);
150     n1.addChildOscNode(&n3);
151     n2.addChildOscNode(&n4);
152     n2.addChildOscNode(&n5);
153
154     n1.addChildOscNode(&n6,"base1/child1/");
155     n1.addChildOscNode(&n7,"base1/child1/");
156
157     n1.addChildOscNode(&n8,"auto1/test/");
158     n1.addChildOscNode(&n9,"auto1/test2/");
159
160     OscTestNode n10=OscTestNode("base2");
161
162     if (!s.registerAtRootNode(&n1)) {
163         printf("failed to register base1 at server");
164         return false;
165     }
166     if (!s.registerAtRootNode(&n10)) {
167         printf("failed to register base2 at server");
168         return false;
169     }
170
171     if (!s.start()) {
172         printf("failed to start server");
173         return false;
174     }
175
176     printf("server started\n");
177     printf("press ctrl-c to stop it & continue\n");
178
179     signal (SIGINT, sighandler);
180
181     run=1;
182     while(run) {
183         sleep(1);
184         fflush(stdout);
185         fflush(stderr);
186     }
187     signal (SIGINT, SIG_DFL);
188
189     if (!s.stop()) {
190         printf("failed to stop server");
191         return false;
192     }
193
194     if (!s.unregisterAtRootNode(&n1)) {
195         printf("failed to unregister base1 at server");
196         return false;
197     }
198     if (!s.unregisterAtRootNode(&n10)) {
199         printf("failed to unregister base2 at server");
200         return false;
201     }
202
203     return result;
204 }
205
206 /////////////////////////////////////
207 /////////////////////////////////////
208 /////////////////////////////////////
209
210
211 typedef bool ( *test_func ) ();
212 struct TestEntry {
213     const char* m_pName;
214     test_func m_pFunc;
215 };
216
217 TestEntry TestTable[] = {
218     { "Message test 1",test_OscMessage_T1 },
219     { "Node test 1",test_OscNode_T1 },
220     { "Server test 1",test_OscServer_T1 },
221 };
222
223 int
224 main( int argc,  char** argv )
225 {
226     int iNrOfPassedTests = 0;
227     int iNrOfFailedTests = 0;
228     int iNrOfTests = sizeof( TestTable )/sizeof( TestTable[0] );
229
230     for ( int i = 0; i < iNrOfTests ; ++i ) {
231         TestEntry* pEntry = &TestTable[i];
232
233         printf( "Test \"%s\"... ", pEntry->m_pName );
234         if ( pEntry->m_pFunc() ) {
235             puts( " passed" );
236             iNrOfPassedTests++;
237         } else {
238             puts( " failed" );
239             iNrOfFailedTests++;
240         }
241     }
242
243     printf( "passed: %d\n", iNrOfPassedTests );
244     printf( "failed: %d\n", iNrOfFailedTests );
245     printf( "total:  %d\n", iNrOfTests );
246
247     return 0;
248 }
Note: See TracBrowser for help on using the browser.