File: filetest2.cpp

package info (click to toggle)
libdrumstick 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,876 kB
  • sloc: cpp: 25,685; xml: 122; sh: 14; makefile: 5
file content (242 lines) | stat: -rw-r--r-- 8,868 bytes parent folder | download
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
/*
    Copyright (C) 2008-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>

    This file is part of the Drumstick project, see https://sf.net/p/drumstick

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; If not, see <http://www.gnu.org/licenses/>.
*/

#include <QByteArray>
#include <QDataStream>
#include <QString>
#include <QtTest>
#include <QTextCodec>
#include <drumstick/qwrk.h>

DISABLE_WARNING_PUSH
DISABLE_WARNING_DEPRECATED_DECLARATIONS

using namespace drumstick::File;

class FileTest2 : public QObject
{
    Q_OBJECT

public:
    explicit FileTest2(QObject* parent = nullptr);

    static const char test_wrk[];
    static const int test_wrk_len;

public Q_SLOTS:
    void fileHeader(int verh, int verl);
    void trackHeader(const QString& name1, const QString& name2,
                     int trackno, int channel, int pitch,
                     int velocity, int port,
                     bool selected, bool muted, bool loop);
    void timeBase(int timebase);
    void noteEvent(int track, long time, int chan, int pitch, int vol, int dur);
    void timeSigEvent(int bar, int num, int den);
    void keySigEvent(int bar, int alt);
    void tempoEvent(long time, int tempo);
    void errorHandler(const QString& errorStr);
    void newTrackHeader(const QString& name,
                        int trackno, int channel, int pitch,
                        int velocity, int port,
                        bool selected, bool muted, bool loop);

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void testCaseReadWrkFile();

private:
    QWrk *m_engine;
    int m_timeBase;
    int m_numNotes;
    int m_lastNote;
    int m_tracks;
    int m_lastKeySig;
    float m_lastTempo;
    QByteArray m_testData;
    QString m_fileVersion;
    QString m_lastTimeSig;
    QString m_lastError;
};

FileTest2::FileTest2(QObject* parent): QObject(parent),
    m_engine(nullptr),
    m_timeBase(0),
    m_numNotes(0),
    m_lastNote(0),
    m_tracks(0),
    m_lastKeySig(0),
    m_lastTempo(0)
{
    m_engine = new QWrk(this);
    m_engine->setTextCodec(QTextCodec::codecForName("UTF-8"));

    connect(m_engine, &QWrk::signalWRKError, this, &FileTest2::errorHandler);
    connect(m_engine, &QWrk::signalWRKHeader, this, &FileTest2::fileHeader);
    connect(m_engine, &QWrk::signalWRKTrack, this, &FileTest2::trackHeader);
    connect(m_engine, &QWrk::signalWRKTimeBase, this, &FileTest2::timeBase);
    connect(m_engine, &QWrk::signalWRKNote, this, &FileTest2::noteEvent);
    connect(m_engine, &QWrk::signalWRKTimeSig, this, &FileTest2::timeSigEvent);
    connect(m_engine, &QWrk::signalWRKKeySig, this, &FileTest2::keySigEvent);
    connect(m_engine, &QWrk::signalWRKTempo, this, &FileTest2::tempoEvent);
    connect(m_engine, &QWrk::signalWRKNewTrack, this, &FileTest2::newTrackHeader);
}

const char FileTest2::test_wrk[] = {
    '\x43','\x41','\x4b','\x45','\x57','\x41','\x4c','\x4b','\x1a','\x00','\x02','\x0a','\x02','\x00','\x00','\x00',
    '\xc0','\x00','\x03','\x59','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00',
    '\x03','\x00','\x00','\x00','\x00','\x05','\x00','\x00','\x00','\x01','\x01','\x00','\x01','\x00','\x03','\x00',
    '\x00','\x01','\x00','\x00','\x00','\x00','\x00','\x01','\x01','\x00','\xff','\xff','\x01','\x7b','\x00','\x00',
    '\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d','\x0e','\x0f',
    '\x01','\x01','\x20','\x40','\x80','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00',
    '\x40','\x02','\x00','\x00','\xfe','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00',
    '\x16','\x16','\x00','\x00','\x00','\x03','\x00','\x06','\x4d','\x43','\x49','\x43','\x6d','\x64','\x01','\x04',
    '\x57','\x61','\x76','\x65','\x02','\x04','\x54','\x65','\x78','\x74','\x03','\x0f','\x14','\x00','\x00','\x00',
    '\x01','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\xe0','\x2e','\x00','\x00','\x00','\x00',
    '\x00','\x00','\x00','\x00','\x05','\x0e','\x00','\x00','\x00','\x01','\x00','\x00','\x00','\x00','\x00','\x01',
    '\x00','\x04','\x02','\x00','\x00','\x00','\x00','\x17','\x07','\x00','\x00','\x00','\x01','\x00','\x01','\x00',
    '\x04','\x02','\x00','\x0b','\x06','\x00','\x00','\x00','\x1e','\x00','\x00','\x00','\x00','\x00','\x11','\x0e',
    '\x00','\x00','\x00','\x00','\x00','\x09','\x00','\x25','\x00','\x6e','\x00','\x01','\x00','\x00','\x00','\x00',
    '\x00','\x10','\x0c','\x00','\x00','\x00','\x02','\x00','\x00','\x09','\x00','\x7f','\xff','\xff','\x00','\x00',
    '\x00','\x00','\x01','\x09','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x09','\x00','\x7f','\x00','\x00',
    '\x02','\x2c','\x00','\x00','\x00','\x00','\x00','\x05','\x00','\x00','\x00','\x00','\x90','\x58','\x64','\xc0',
    '\x00','\x00','\x00','\x00','\x90','\x25','\x64','\xc0','\x00','\xc0','\x00','\x00','\x90','\x25','\x64','\xc0',
    '\x00','\x80','\x01','\x00','\x90','\x25','\x64','\xc0','\x00','\x40','\x02','\x00','\x90','\x25','\x64','\xc0',
    '\x00','\x13','\x06','\x00','\x00','\x00','\x00','\x00','\x7f','\x00','\x40','\x00','\x19','\x42','\x00','\x00',
    '\x00','\x10','\x00','\x00','\x00','\x07','\x3f','\x01','\x00','\x07','\x3f','\x02','\x00','\x07','\x3f','\x03',
    '\x00','\x07','\x3f','\x04','\x00','\x07','\x3f','\x05','\x00','\x07','\x3f','\x06','\x00','\x07','\x3f','\x07',
    '\x00','\x07','\x3f','\x08','\x00','\x07','\x3f','\x09','\x00','\x07','\x3f','\x0a','\x00','\x07','\x3f','\x0b',
    '\x00','\x07','\x3f','\x0c','\x00','\x07','\x3f','\x0d','\x00','\x07','\x3f','\x0e','\x00','\x07','\x3f','\x0f',
    '\x00','\x07','\x3f','\xff'
};
const int FileTest2::test_wrk_len = sizeof(test_wrk); //388

void FileTest2::fileHeader(int verh, int verl)
{
    m_fileVersion = QString("%1.%2").arg(verh).arg(verl);
}

void FileTest2::errorHandler(const QString& errorStr)
{
    m_lastError = errorStr;
    qWarning() << errorStr;
}

void FileTest2::trackHeader( const QString& name1, const QString& name2,
                           int trackno, int channel, int pitch,
                           int velocity, int port,
                           bool selected, bool muted, bool loop )
{
    Q_UNUSED(name1)
    Q_UNUSED(name2)
    Q_UNUSED(trackno)
    Q_UNUSED(channel)
    Q_UNUSED(pitch)
    Q_UNUSED(velocity)
    Q_UNUSED(port)
    Q_UNUSED(selected)
    Q_UNUSED(muted)
    Q_UNUSED(loop)
    m_tracks++;
}

void FileTest2::timeBase(int timebase)
{
    m_timeBase = timebase;
}

void FileTest2::noteEvent(int track, long time, int chan, int pitch, int vol, int dur)
{
    Q_UNUSED(track)
    Q_UNUSED(time)
    Q_UNUSED(chan)
    Q_UNUSED(vol)
    Q_UNUSED(dur)
    m_numNotes++;
    m_lastNote = pitch;
}

void FileTest2::timeSigEvent(int bar, int num, int den)
{
    Q_UNUSED(bar)
    m_lastTimeSig = QString("%1/%2").arg(num).arg(den);
}

void FileTest2::keySigEvent(int bar, int alt)
{
    Q_UNUSED(bar)
    m_lastKeySig = alt;
}

void FileTest2::tempoEvent(long time, int tempo)
{
    Q_UNUSED(time)
    double bpm = tempo / 100.0;
    m_lastTempo = bpm;
}

void FileTest2::newTrackHeader( const QString& name,
                              int trackno, int channel, int pitch,
                              int velocity, int port,
                              bool selected, bool muted, bool loop )
{
    Q_UNUSED(name)
    Q_UNUSED(trackno)
    Q_UNUSED(channel)
    Q_UNUSED(pitch)
    Q_UNUSED(velocity)
    Q_UNUSED(port)
    Q_UNUSED(selected)
    Q_UNUSED(muted)
    Q_UNUSED(loop)
    m_tracks++;
}

void FileTest2::initTestCase()
{
    m_testData = QByteArray::fromRawData(test_wrk, test_wrk_len);
}

void FileTest2::cleanupTestCase()
{
    m_testData.clear();
}

void FileTest2::testCaseReadWrkFile()
{
    QDataStream stream(&m_testData,  QIODevice::ReadWrite);
    m_engine->readFromStream(&stream);
    if (!m_lastError.isEmpty()) {
        QFAIL(m_lastError.toLocal8Bit());
    }
    QCOMPARE(m_fileVersion, QString("2.0"));
    QCOMPARE(m_timeBase, 192);
    QCOMPARE(m_tracks, 1);
    QCOMPARE(m_lastTempo, 120.0);
    QCOMPARE(m_lastTimeSig, QString("4/4"));
    QCOMPARE(m_lastKeySig, 0);
    QCOMPARE(m_numNotes, 5);
    QCOMPARE(m_lastNote, 37);
}

QTEST_APPLESS_MAIN(FileTest2)

#include "filetest2.moc"

DISABLE_WARNING_POP