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

Revision 452, 6.4 kB (checked in by ppalmers, 15 years ago)

libosc
- add type conversion functions to convert between the argument

type and the wanted data type


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     result &= TEST_SHOULD_RETURN_TRUE(m.getArgument(0).toInt()==1);
67    
68     return result;
69 }
70
71
72 static bool
73 test_OscNode_T1() {
74     bool result=true;
75     OscMessage m=OscMessage();
76
77     OscNode n1=OscNode("base1");
78     OscNode n2=OscNode("child1");
79     OscNode n3=OscNode("child2");
80     OscNode n4=OscNode("subchild1");
81     OscNode n5=OscNode("subchild2");
82
83     OscNode n6=OscNode("subsubchild1");
84     OscNode n7=OscNode("subsubchild2");
85
86     OscNode n8=OscNode("auto1");
87     OscNode n9=OscNode("auto2");
88
89     n1.addChildOscNode(&n2);
90     n1.addChildOscNode(&n3);
91     n2.addChildOscNode(&n4);
92     n2.addChildOscNode(&n5);
93
94     n1.addChildOscNode(&n6,"base1/child1/");
95     n1.addChildOscNode(&n7,"base1/child1/");
96
97     n1.addChildOscNode(&n8,"auto1/test/");
98     n1.addChildOscNode(&n9,"auto1/test2/");
99
100     n1.printOscNode();
101
102     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1",&m).isError());
103     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child1",&m).isError());
104     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child1/subchild1",&m).isError());
105     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child1/subchild2",&m).isError());
106     result &= TEST_SHOULD_RETURN_TRUE(n1.processOscMessage("base1/child1/subchild3",&m).isError());
107     result &= TEST_SHOULD_RETURN_FALSE(n1.processOscMessage("base1/child2",&m).isError());
108     result &= TEST_SHOULD_RETURN_TRUE(n1.processOscMessage("base1/child2/subchild1",&m).isError());
109
110     return result;
111 }
112
113 ////////////////////////////////
114
115 class OscTestNode
116     : public OscNode
117 {
118 public:
119     OscTestNode(std::string s)
120         : OscNode(s) {};
121     virtual ~OscTestNode() {};
122
123     virtual OscResponse processOscMessage(OscMessage *m) {
124         return OscResponse(*m);
125     };
126 };
127
128 static bool
129 test_OscServer_T1() {
130     bool result=true;
131
132     OscServer s=OscServer("17820");
133
134     if (!s.init()) {
135         printf("failed to init server");
136         return false;
137     }
138
139     OscNode n1=OscNode("base1");
140     OscNode n2=OscNode("child1");
141     OscNode n3=OscNode("child2");
142     OscNode n4=OscNode("subchild1");
143     OscNode n5=OscNode("subchild2");
144
145     OscNode n6=OscNode("subsubchild1");
146     OscNode n7=OscNode("subsubchild2");
147
148     OscNode n8=OscNode("auto1");
149     OscNode n9=OscNode("auto2");
150
151     n1.addChildOscNode(&n2);
152     n1.addChildOscNode(&n3);
153     n2.addChildOscNode(&n4);
154     n2.addChildOscNode(&n5);
155
156     n1.addChildOscNode(&n6,"base1/child1/");
157     n1.addChildOscNode(&n7,"base1/child1/");
158
159     n1.addChildOscNode(&n8,"auto1/test/");
160     n1.addChildOscNode(&n9,"auto1/test2/");
161
162     OscTestNode n10=OscTestNode("base2");
163
164     if (!s.registerAtRootNode(&n1)) {
165         printf("failed to register base1 at server");
166         return false;
167     }
168     if (!s.registerAtRootNode(&n10)) {
169         printf("failed to register base2 at server");
170         return false;
171     }
172
173     if (!s.start()) {
174         printf("failed to start server");
175         return false;
176     }
177
178     printf("server started\n");
179     printf("press ctrl-c to stop it & continue\n");
180
181     signal (SIGINT, sighandler);
182
183     run=1;
184     while(run) {
185         sleep(1);
186         fflush(stdout);
187         fflush(stderr);
188     }
189     signal (SIGINT, SIG_DFL);
190
191     if (!s.stop()) {
192         printf("failed to stop server");
193         return false;
194     }
195
196     if (!s.unregisterAtRootNode(&n1)) {
197         printf("failed to unregister base1 at server");
198         return false;
199     }
200     if (!s.unregisterAtRootNode(&n10)) {
201         printf("failed to unregister base2 at server");
202         return false;
203     }
204
205     return result;
206 }
207
208 /////////////////////////////////////
209 /////////////////////////////////////
210 /////////////////////////////////////
211
212
213 typedef bool ( *test_func ) ();
214 struct TestEntry {
215     const char* m_pName;
216     test_func m_pFunc;
217 };
218
219 TestEntry TestTable[] = {
220     { "Message test 1",test_OscMessage_T1 },
221     { "Node test 1",test_OscNode_T1 },
222     { "Server test 1",test_OscServer_T1 },
223 };
224
225 int
226 main( int argc,  char** argv )
227 {
228     int iNrOfPassedTests = 0;
229     int iNrOfFailedTests = 0;
230     int iNrOfTests = sizeof( TestTable )/sizeof( TestTable[0] );
231
232     for ( int i = 0; i < iNrOfTests ; ++i ) {
233         TestEntry* pEntry = &TestTable[i];
234
235         printf( "Test \"%s\"... ", pEntry->m_pName );
236         if ( pEntry->m_pFunc() ) {
237             puts( " passed" );
238             iNrOfPassedTests++;
239         } else {
240             puts( " failed" );
241             iNrOfFailedTests++;
242         }
243     }
244
245     printf( "passed: %d\n", iNrOfPassedTests );
246     printf( "failed: %d\n", iNrOfFailedTests );
247     printf( "total:  %d\n", iNrOfTests );
248
249     return 0;
250 }
Note: See TracBrowser for help on using the browser.