summaryrefslogtreecommitdiffstats
path: root/src/base/test/test.cpp
blob: 6c7aabf0ae9e5c8497f0c9108ec88bb37d5a80f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#define NDEBUG

// This does some rather shoddy tests on a small selection of core classes.

#include "Event.h"
#include "Segment.h"
#include "Composition.h"
//#include "Sets.h"

#define TEST_NOTATION_TYPES 1
#define TEST_SPEED 1

#ifdef TEST_NOTATION_TYPES
#include "NotationTypes.h"
#include "SegmentNotationHelper.h"
#include "SegmentPerformanceHelper.h"
#endif

#include "MidiTypes.h"

#include <cstdio>

#include <sys/times.h>
#include <iostream>

using namespace std;
using namespace Rosegarden;

static const PropertyName DURATION_PROPERTY = "duration";
static const PropertyName SOME_INT_PROPERTY = "someIntProp";
static const PropertyName SOME_BOOL_PROPERTY = "someBoolProp";
static const PropertyName SOME_STRING_PROPERTY = "someStringProp";
static const PropertyName NONEXISTENT_PROPERTY = "nonexistentprop";
static const PropertyName ANNOTATION_PROPERTY = "annotation";

#if 0
// Some attempts at reproducing the func-template-within-template problem
// 
enum FooType {A, B, C};

class Foo
{
public:
    template<FooType T> void func();
};

template<class T>
void Foo::func()
{
    // dummy code
    T j = 0;
    for(T i = 0; i < 100; ++i) j += i;
}

//template void Foo::func<int>();

template <class R>
class FooR
{
public:
    void rfunc();
};

template<class R>
void FooR<R>::rfunc()
{
    // this won't compile
    Foo* foo;
    foo->func<A>();
}

void templateTest()
{
    Foo foo;
    foo.func<A>();

//     FooR<float> foor;
//     foor.rfunc();
}


template <class Element, class Container>
class GenericSet // abstract base
{
public:
    typedef typename Container::iterator Iterator;

    /// Return true if this element, known to test() true, is a set member
    virtual bool sample(const Iterator &i);
};


template <class Element, class Container>
bool
GenericSet<Element, Container>::sample(const Iterator &i)
{
    Event *e;
    long p = e->get<Int>("blah");
}

#endif

int main(int argc, char **argv)
{
    typedef std::vector<int> intvect;
    
//     intvect foo;

//     GenericSet<int, intvect> genset;
//     genset.sample(foo.begin());

        clock_t st, et;
        struct tms spare;

#ifdef TEST_WIDE_STRING
	basic_string<wchar_t> widestring(L"This is a test");
	widestring += L" of wide character strings";
	for (unsigned int i = 0; i < widestring.length(); ++i) {
	    if (widestring[i] == L'w' ||
		widestring[i] == L'c') {
		widestring[i] = toupper(widestring[i]);
	    }
	}
	cout << "Testing wide string: string value is \"" << widestring << "\""
	     << endl;
	cout << "String's length is " << widestring.length() << endl;
	cout << "and storage space is " 
	     << (widestring.length() * sizeof(widestring[0]))
	     << endl;
	cout << "Characters are: ";
	for (unsigned int i = 0; i < widestring.length(); ++i) {
	    cout << widestring[i];
	    if (i < widestring.length()-1) cout << " ";
	    else cout << endl;
	}
#endif

        cout << "\nTesting Event..." << endl
             << "sizeof Event : " << sizeof(Event) << endl;

        Event e("note", 0);
        e.set<Int>(DURATION_PROPERTY, 20);
        cout << "duration is " << e.get<Int>(DURATION_PROPERTY) << endl;

        e.set<Bool>(SOME_BOOL_PROPERTY, true);
        e.set<String>(SOME_STRING_PROPERTY, "foobar");

        cout << "sizeof event after some properties set : "
             << sizeof e << endl;

        try {
                cout << "duration is " << e.get<String>(DURATION_PROPERTY) << endl;
        } catch (Event::BadType bt) {
                cout << "Correctly caught BadType when trying to get<String> of duration" << endl;
        }

        string s;
    
        if (!e.get<String>(DURATION_PROPERTY, s)) {
                cout << "Correctly got error when trying to get<String> of duration" << endl;
        } else {
                cerr << "ERROR AT " << __LINE__ << endl;
        }

        try {
                cout << "dummy prop is " << e.get<String>(NONEXISTENT_PROPERTY) << endl;
        } catch (Event::NoData bt) {
                cout << "Correctly caught NoData when trying to get non existent property" << endl;
        }

        if (!e.get<String>(NONEXISTENT_PROPERTY, s)) {
                cout << "Correctly got error when trying to get<String> of non existent property" << endl;
        } else {
                cerr << "ERROR AT " << __LINE__ << endl;
        }


        e.setFromString<Int>(DURATION_PROPERTY, "30");
        cout << "duration is " << e.get<Int>(DURATION_PROPERTY) << endl;

        e.setFromString<String>(ANNOTATION_PROPERTY, "This is my house");
        cout << "annotation is " << e.get<String>(ANNOTATION_PROPERTY) << endl;

        long durationVal;
        if (e.get<Int>(DURATION_PROPERTY, durationVal))
                cout << "duration is " << durationVal << endl;
        else
                cerr << "ERROR AT " << __LINE__ << endl;

        if (e.get<String>(ANNOTATION_PROPERTY, s))
                cout << "annotation is " << s << endl;
        else
                cerr << "ERROR AT " << __LINE__ << endl;

	cout << "\nTesting persistence & setMaybe..." << endl;

	e.setMaybe<Int>(SOME_INT_PROPERTY, 1);
	if (e.get<Int>(SOME_INT_PROPERTY) == 1) {
	    cout << "a. Correct: 1" << endl;
	} else {
	    cout << "a. ERROR: " << e.get<Int>(SOME_INT_PROPERTY) << endl;
	}

	e.set<Int>(SOME_INT_PROPERTY, 2, false);
	e.setMaybe<Int>(SOME_INT_PROPERTY, 3);
	if (e.get<Int>(SOME_INT_PROPERTY) == 3) {
	    cout << "b. Correct: 3" << endl;
	} else {
	    cout << "b. ERROR: " << e.get<Int>(SOME_INT_PROPERTY) << endl;
	}

	e.set<Int>(SOME_INT_PROPERTY, 4);
	e.setMaybe<Int>(SOME_INT_PROPERTY, 5);
	if (e.get<Int>(SOME_INT_PROPERTY) == 4) {
	    cout << "c. Correct: 4" << endl;
	} else {
	    cout << "c. ERROR: " << e.get<Int>(SOME_INT_PROPERTY) << endl;
	}

        cout << "\nTesting debug dump : " << endl;
        e.dump(cout);
        cout << endl << "dump finished" << endl;

#if TEST_SPEED
        cout << "Testing speed of Event..." << endl;
        int i;
        long j;

        char b[20];
        strcpy(b, "test");

#define NAME_COUNT 500

        PropertyName names[NAME_COUNT];
        for (i = 0; i < NAME_COUNT; ++i) {
            sprintf(b+4, "%d", i);
            names[i] = b;
        }

        Event e1("note", 0);
        int gsCount = 200000;

        st = times(&spare);
        for (i = 0; i < gsCount; ++i) {
                e1.set<Int>(names[i % NAME_COUNT], i);
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " setInts: " << (et-st)*10 << "ms\n";

        st = times(&spare);
        j = 0;
        for (i = 0; i < gsCount; ++i) {
                if (i%4==0) sprintf(b+4, "%d", i);
                j += e1.get<Int>(names[i % NAME_COUNT]);
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " getInts: " << (et-st)*10 << "ms (result: " << j << ")\n";
        
        st = times(&spare);
        for (i = 0; i < 1000; ++i) {
                Event e11(e1);
                (void)e11.get<Int>(names[i % NAME_COUNT]);
        }
        et = times(&spare);
        cout << "Event: 1000 copy ctors of " << e1.getStorageSize() << "-byte element: "
             << (et-st)*10 << "ms\n";

//        gsCount = 100000;

        for (i = 0; i < NAME_COUNT; ++i) {
            sprintf(b+4, "%ds", i);
            names[i] = b;
        }

        st = times(&spare);
        for (i = 0; i < gsCount; ++i) {
                e1.set<String>(names[i % NAME_COUNT], b);
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " setStrings: " << (et-st)*10 << "ms\n";
        
        st = times(&spare);
        j = 0;
        for (i = 0; i < gsCount; ++i) {
                if (i%4==0) sprintf(b+4, "%ds", i);
                j += e1.get<String>(names[i % NAME_COUNT]).size();
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " getStrings: " << (et-st)*10 << "ms (result: " << j << ")\n";
        
        st = times(&spare);
        for (i = 0; i < 1000; ++i) {
                Event e11(e1);
                (void)e11.get<String>(names[i % NAME_COUNT]);
        }
        et = times(&spare);
        cout << "Event: 1000 copy ctors of " << e1.getStorageSize() << "-byte element: "
             << (et-st)*10 << "ms\n";

        st = times(&spare);
        for (i = 0; i < 1000; ++i) {
                Event e11(e1);
                (void)e11.get<String>(names[i % NAME_COUNT]);
		(void)e11.set<String>(names[i % NAME_COUNT], "blah");
        }
        et = times(&spare);
        cout << "Event: 1000 copy ctors plus set<String> of " << e1.getStorageSize() << "-byte element: "
             << (et-st)*10 << "ms\n";

//	gsCount = 1000000;

        st = times(&spare);
        for (i = 0; i < gsCount; ++i) {
	        Event e21("dummy", i, 0, MIN_SUBORDERING);
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " event ctors alone: "
             << (et-st)*10 << "ms\n";
	
        st = times(&spare);
        for (i = 0; i < gsCount; ++i) {
	    std::string s0("dummy");
	    std::string s1 = s0;
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " string ctors+assignents: "
             << (et-st)*10 << "ms\n";
	
        st = times(&spare);
        for (i = 0; i < gsCount; ++i) {
	        Event e21("dummy", i, 0, MIN_SUBORDERING);
                (void)e21.getAbsoluteTime();
                (void)e21.getDuration();
                (void)e21.getSubOrdering();
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " event ctors plus getAbsTime/Duration/SubOrdering: "
             << (et-st)*10 << "ms\n";
	
        st = times(&spare);
        for (i = 0; i < gsCount; ++i) {
	        Event e21("dummy", i, 0, MIN_SUBORDERING);
                (void)e21.getAbsoluteTime();
                (void)e21.getDuration();
                (void)e21.getSubOrdering();
		e21.set<Int>(names[0], 40);
		(void)e21.get<Int>(names[0]);
        }
        et = times(&spare);
        cout << "Event: " << gsCount << " event ctors plus one get/set and getAbsTime/Duration/SubOrdering: "
             << (et-st)*10 << "ms\n";
	

#else
        cout << "Skipping test speed of Event\n";
#endif // TEST_SPEED

#ifdef NOT_DEFINED
        cout << "Testing segment shrinking\n";
        
        Segment segment(5, 0);
        unsigned int nbBars = segment.getNbBars();

        cout << "Segment nbBars : " << nbBars << endl;
        if (nbBars != 5) {
                cerr << "%%%ERROR : segment nbBars should be 5\n";
        }

        Segment::iterator iter = segment.end();
        --iter;
        cout << "Last segment el. time : " << (*iter)->getAbsoluteTime() << endl;

        cout << "Shrinking segment to 3 bars : \n";
        segment.setNbBars(3);
        nbBars = segment.getNbBars();

        cout << "Segment new nbBars : " << nbBars << endl;
        if (nbBars != 3) {
                cerr << "%%%ERROR : segment new nbBars should be 3\n";
        }
#endif // NOT_DEFINED

#ifdef TEST_NOTATION_TYPES
        cout << "Testing duration-list stuff\n";

        cout << "2/4..." << endl;
        TimeSignature ts(2,4);
        DurationList dlist;
        ts.getDurationListForInterval
                (dlist, 1209,
                 ts.getBarDuration() - Note(Note::Semiquaver, true).getDuration());
        int acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;



        cout << "4/4 96/96..." << endl;
        ts = TimeSignature(4,4);
        dlist = DurationList();
        ts.getDurationListForInterval(dlist, 96, 96);
        acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;
        


        cout << "6/8..." << endl;
        ts = TimeSignature(6,8);
        dlist = DurationList();
        ts.getDurationListForInterval
                (dlist, 1209,
                 ts.getBarDuration() - Note(Note::Semiquaver, true).getDuration());
        acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;

        cout << "3/4..." << endl;
        ts = TimeSignature(3,4);
        dlist = DurationList();
        ts.getDurationListForInterval
                (dlist, 1209,
                 ts.getBarDuration() - Note(Note::Semiquaver, true).getDuration());
        acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;

        cout << "4/4..." << endl;
        ts = TimeSignature(4,4);
        dlist = DurationList();
        ts.getDurationListForInterval
                (dlist, 1209,
                 ts.getBarDuration() - Note(Note::Semiquaver, true).getDuration());
        acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;

        cout << "3/8..." << endl;
        ts = TimeSignature(3,8);
        dlist = DurationList();
        ts.getDurationListForInterval
                (dlist, 1209,
                 ts.getBarDuration() - Note(Note::Semiquaver, true).getDuration());
        acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;

        cout << "4/4 wacky placement..." << endl;
        ts = TimeSignature(4,4);
        dlist = DurationList();
        ts.getDurationListForInterval(dlist, 160, 1280);
        acc = 0;
        for (DurationList::iterator i = dlist.begin(); i != dlist.end(); ++i) {
                cout << "duration: " << *i << endl;
                acc += *i;
        }
        cout << "total: " << acc << " (on bar duration of " << ts.getBarDuration() << ")" << endl;

        cout << "Testing Segment::splitIntoTie() - splitting 384 -> 2*192\n";

	Composition c;
	Segment *ht = new Segment();
	c.addSegment(ht);
        Segment &t(*ht);
        SegmentNotationHelper nh(t);
        SegmentPerformanceHelper ph(t);

        Event *ev = new Event("note", 0, 384);
        ev->set<Int>("pitch", 60);
        t.insert(ev);

	Segment::iterator sb(t.begin());
        nh.splitIntoTie(sb, 384/2);

        for(Segment::iterator i = t.begin(); i != t.end(); ++i) {
                cout << "Event at " << (*i)->getAbsoluteTime()
                     << " - duration : " << (*i)->getDuration()
                     << endl;
        }

        Segment::iterator half2 = t.begin(); ++half2;
        
        cout << "Splitting 192 -> (48 + 144) : \n";

	sb = t.begin();
        nh.splitIntoTie(sb, 48);

        for(Segment::iterator i = t.begin(); i != t.end(); ++i) {
                cout << "Event at " << (*i)->getAbsoluteTime()
                     << " - duration : " << (*i)->getDuration()
                     << endl;
        }
        
        cout << "Splitting 192 -> (144 + 48) : \n";

        nh.splitIntoTie(half2, 144);


        for(Segment::iterator i = t.begin(); i != t.end(); ++i) {
                cout << "Event at " << (*i)->getAbsoluteTime()
                     << " - duration : " << (*i)->getDuration()
                     << " - performance duration : " <<
                    ph.getSoundingDuration(i) << endl;

		cout << endl;
		(*i)->dump(cout);
		cout << endl;
        }

	nh.autoBeam(t.begin(), t.end(), "beamed");

#endif // TEST_NOTATION_TYPES
};