Chris@1611
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1611
|
2
|
Chris@1611
|
3 /*
|
Chris@1611
|
4 Sonic Visualiser
|
Chris@1611
|
5 An audio file viewer and annotation editor.
|
Chris@1611
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1611
|
7
|
Chris@1611
|
8 This program is free software; you can redistribute it and/or
|
Chris@1611
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1611
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1611
|
11 License, or (at your option) any later version. See the file
|
Chris@1611
|
12 COPYING included with this distribution for more information.
|
Chris@1611
|
13 */
|
Chris@1611
|
14
|
Chris@1611
|
15 #ifndef TEST_SPARSE_MODELS_H
|
Chris@1611
|
16 #define TEST_SPARSE_MODELS_H
|
Chris@1611
|
17
|
Chris@1611
|
18 #include "../SparseOneDimensionalModel.h"
|
Chris@1611
|
19 #include "../NoteModel.h"
|
Chris@1611
|
20
|
Chris@1611
|
21 #include <QObject>
|
Chris@1611
|
22 #include <QtTest>
|
Chris@1611
|
23
|
Chris@1611
|
24 #include <iostream>
|
Chris@1611
|
25
|
Chris@1611
|
26 using namespace std;
|
Chris@1611
|
27
|
Chris@1611
|
28 class TestSparseModels : public QObject
|
Chris@1611
|
29 {
|
Chris@1611
|
30 Q_OBJECT
|
Chris@1611
|
31
|
Chris@1611
|
32 private slots:
|
Chris@1611
|
33 void s1d_empty() {
|
Chris@1611
|
34 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1611
|
35 QCOMPARE(m.isEmpty(), true);
|
Chris@1611
|
36 QCOMPARE(m.getPointCount(), 0);
|
Chris@1611
|
37 QCOMPARE(m.getPoints().begin(), m.getPoints().end());
|
Chris@1611
|
38 QCOMPARE(m.getStartFrame(), 0);
|
Chris@1611
|
39 QCOMPARE(m.getEndFrame(), 0);
|
Chris@1611
|
40 QCOMPARE(m.getSampleRate(), 100);
|
Chris@1611
|
41 QCOMPARE(m.getResolution(), 10);
|
Chris@1611
|
42 QCOMPARE(m.isSparse(), true);
|
Chris@1611
|
43
|
Chris@1611
|
44 SparseOneDimensionalModel::Point p(10);
|
Chris@1611
|
45 m.addPoint(p);
|
Chris@1611
|
46 m.clear();
|
Chris@1611
|
47 QCOMPARE(m.isEmpty(), true);
|
Chris@1611
|
48 QCOMPARE(m.getPointCount(), 0);
|
Chris@1611
|
49 QCOMPARE(m.getPoints().begin(), m.getPoints().end());
|
Chris@1611
|
50 QCOMPARE(m.getStartFrame(), 0);
|
Chris@1611
|
51 QCOMPARE(m.getEndFrame(), 0);
|
Chris@1611
|
52
|
Chris@1611
|
53 m.addPoint(p);
|
Chris@1611
|
54 m.deletePoint(p);
|
Chris@1611
|
55 QCOMPARE(m.isEmpty(), true);
|
Chris@1611
|
56 QCOMPARE(m.getPointCount(), 0);
|
Chris@1611
|
57 QCOMPARE(m.getPoints().begin(), m.getPoints().end());
|
Chris@1611
|
58 QCOMPARE(m.getStartFrame(), 0);
|
Chris@1611
|
59 QCOMPARE(m.getEndFrame(), 0);
|
Chris@1611
|
60 }
|
Chris@1611
|
61
|
Chris@1611
|
62 void s1d_extents() {
|
Chris@1611
|
63 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1611
|
64 SparseOneDimensionalModel::Point p1(20);
|
Chris@1611
|
65 m.addPoint(p1);
|
Chris@1611
|
66 QCOMPARE(m.isEmpty(), false);
|
Chris@1611
|
67 QCOMPARE(m.getPointCount(), 1);
|
Chris@1611
|
68 SparseOneDimensionalModel::Point p2(50);
|
Chris@1611
|
69 m.addPoint(p2);
|
Chris@1611
|
70 QCOMPARE(m.isEmpty(), false);
|
Chris@1611
|
71 QCOMPARE(m.getPointCount(), 2);
|
Chris@1611
|
72 QCOMPARE(m.getPoints().size(), 2);
|
Chris@1611
|
73 QCOMPARE(*m.getPoints().begin(), p1);
|
Chris@1611
|
74 QCOMPARE(*m.getPoints().rbegin(), p2);
|
Chris@1611
|
75 QCOMPARE(m.getStartFrame(), 20);
|
Chris@1611
|
76 QCOMPARE(m.getEndFrame(), 60);
|
Chris@1611
|
77 QCOMPARE(m.containsPoint(p1), true);
|
Chris@1611
|
78 m.deletePoint(p1);
|
Chris@1611
|
79 QCOMPARE(m.getPointCount(), 1);
|
Chris@1611
|
80 QCOMPARE(m.getPoints().size(), 1);
|
Chris@1611
|
81 QCOMPARE(*m.getPoints().begin(), p2);
|
Chris@1611
|
82 QCOMPARE(m.getStartFrame(), 50);
|
Chris@1611
|
83 QCOMPARE(m.getEndFrame(), 60);
|
Chris@1611
|
84 QCOMPARE(m.containsPoint(p1), false);
|
Chris@1611
|
85 }
|
Chris@1611
|
86
|
Chris@1611
|
87 void s1d_sample() {
|
Chris@1611
|
88 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1611
|
89 SparseOneDimensionalModel::Point p1(20), p2(20), p3(50);
|
Chris@1611
|
90 m.addPoint(p1);
|
Chris@1611
|
91 m.addPoint(p2);
|
Chris@1611
|
92 m.addPoint(p3);
|
Chris@1611
|
93 QCOMPARE(m.getPoints().size(), 3);
|
Chris@1611
|
94 QCOMPARE(*m.getPoints().begin(), p1);
|
Chris@1611
|
95 QCOMPARE(*m.getPoints().rbegin(), p3);
|
Chris@1643
|
96 /*!!!
|
Chris@1611
|
97 auto pp = m.getPoints(20, 30);
|
Chris@1611
|
98 QCOMPARE(pp.size(), 2);
|
Chris@1611
|
99 QCOMPARE(*pp.begin(), p1);
|
Chris@1611
|
100 QCOMPARE(*pp.rbegin(), p2);
|
Chris@1643
|
101
|
Chris@1611
|
102 pp = m.getPoints(40, 50);
|
Chris@1611
|
103 QCOMPARE(pp.size(), 0);
|
Chris@1611
|
104
|
Chris@1611
|
105 pp = m.getPoints(50, 50);
|
Chris@1611
|
106 QCOMPARE(pp.size(), 1);
|
Chris@1611
|
107 QCOMPARE(*pp.begin(), p3);
|
Chris@1643
|
108 */
|
Chris@1611
|
109 }
|
Chris@1611
|
110
|
Chris@1611
|
111 void s1d_xml() {
|
Chris@1611
|
112 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1611
|
113 m.setObjectName("This \"&\" that");
|
Chris@1611
|
114 SparseOneDimensionalModel::Point p1(20), p2(20), p3(50);
|
Chris@1611
|
115 p2.label = "Label &'\">";
|
Chris@1611
|
116 m.addPoint(p1);
|
Chris@1611
|
117 m.addPoint(p2);
|
Chris@1611
|
118 m.addPoint(p3);
|
Chris@1611
|
119 QString xml;
|
Chris@1611
|
120 QTextStream str(&xml, QIODevice::WriteOnly);
|
Chris@1611
|
121 m.toXml(str);
|
Chris@1611
|
122 str.flush();
|
Chris@1611
|
123 QString expected =
|
Chris@1611
|
124 "<model id='1' name='This "&" that' sampleRate='100' start='20' end='60' type='sparse' dimensions='1' resolution='10' notifyOnAdd='false' dataset='0' />\n"
|
Chris@1611
|
125 "<dataset id='0' dimensions='1'>\n"
|
Chris@1611
|
126 " <point frame='20' label='' />\n"
|
Chris@1611
|
127 " <point frame='20' label='Label &'">' />\n"
|
Chris@1611
|
128 " <point frame='50' label='' />\n"
|
Chris@1611
|
129 "</dataset>\n";
|
Chris@1611
|
130 expected.replace("\'", "\"");
|
Chris@1611
|
131 if (xml != expected) {
|
Chris@1611
|
132 cerr << "Obtained xml:\n" << xml
|
Chris@1611
|
133 << "\nExpected:\n" << expected << endl;
|
Chris@1611
|
134 }
|
Chris@1611
|
135 QCOMPARE(xml, expected);
|
Chris@1611
|
136 }
|
Chris@1611
|
137
|
Chris@1611
|
138 void note_extents() {
|
Chris@1611
|
139 NoteModel m(100, 10, false);
|
Chris@1643
|
140 Event p1(20, 123.4f, 40, 0.8f, "note 1");
|
Chris@1644
|
141 m.add(p1);
|
Chris@1611
|
142 QCOMPARE(m.isEmpty(), false);
|
Chris@1644
|
143 QCOMPARE(m.getEventCount(), 1);
|
Chris@1643
|
144 Event p2(50, 124.3f, 30, 0.9f, "note 2");
|
Chris@1644
|
145 m.add(p2);
|
Chris@1611
|
146 QCOMPARE(m.isEmpty(), false);
|
Chris@1644
|
147 QCOMPARE(m.getEventCount(), 2);
|
Chris@1644
|
148 QCOMPARE(m.getAllEvents().size(), 2);
|
Chris@1644
|
149 QCOMPARE(*m.getAllEvents().begin(), p1);
|
Chris@1644
|
150 QCOMPARE(*m.getAllEvents().rbegin(), p2);
|
Chris@1611
|
151 QCOMPARE(m.getStartFrame(), 20);
|
Chris@1611
|
152 QCOMPARE(m.getEndFrame(), 80);
|
Chris@1644
|
153 QCOMPARE(m.containsEvent(p1), true);
|
Chris@1643
|
154 QCOMPARE(m.getValueMinimum(), 123.4f);
|
Chris@1643
|
155 QCOMPARE(m.getValueMaximum(), 124.3f);
|
Chris@1644
|
156 m.remove(p1);
|
Chris@1644
|
157 QCOMPARE(m.getEventCount(), 1);
|
Chris@1644
|
158 QCOMPARE(m.getAllEvents().size(), 1);
|
Chris@1644
|
159 QCOMPARE(*m.getAllEvents().begin(), p2);
|
Chris@1611
|
160 QCOMPARE(m.getStartFrame(), 50);
|
Chris@1611
|
161 QCOMPARE(m.getEndFrame(), 80);
|
Chris@1644
|
162 QCOMPARE(m.containsEvent(p1), false);
|
Chris@1611
|
163 }
|
Chris@1611
|
164
|
Chris@1611
|
165 void note_sample() {
|
Chris@1611
|
166 NoteModel m(100, 10, false);
|
Chris@1643
|
167 Event p1(20, 123.4f, 10, 0.8f, "note 1");
|
Chris@1643
|
168 Event p2(20, 124.3f, 20, 0.9f, "note 2");
|
Chris@1643
|
169 Event p3(50, 126.3f, 30, 0.9f, "note 3");
|
Chris@1644
|
170 m.add(p1);
|
Chris@1644
|
171 m.add(p2);
|
Chris@1644
|
172 m.add(p3);
|
Chris@1611
|
173
|
Chris@1644
|
174 QCOMPARE(m.getAllEvents().size(), 3);
|
Chris@1644
|
175 QCOMPARE(*m.getAllEvents().begin(), p1);
|
Chris@1644
|
176 QCOMPARE(*m.getAllEvents().rbegin(), p3);
|
Chris@1611
|
177
|
Chris@1644
|
178 auto pp = m.getEventsSpanning(20, 10);
|
Chris@1611
|
179 QCOMPARE(pp.size(), 2);
|
Chris@1611
|
180 QCOMPARE(*pp.begin(), p1);
|
Chris@1611
|
181 QCOMPARE(*pp.rbegin(), p2);
|
Chris@1611
|
182
|
Chris@1644
|
183 pp = m.getEventsSpanning(30, 20);
|
Chris@1611
|
184 QCOMPARE(pp.size(), 1);
|
Chris@1643
|
185 QCOMPARE(*pp.begin(), p2);
|
Chris@1611
|
186
|
Chris@1644
|
187 pp = m.getEventsSpanning(40, 10);
|
Chris@1611
|
188 QCOMPARE(pp.size(), 0);
|
Chris@1611
|
189
|
Chris@1644
|
190 pp = m.getEventsCovering(50);
|
Chris@1644
|
191 QCOMPARE(pp.size(), 1);
|
Chris@1644
|
192 QCOMPARE(*pp.begin(), p3);
|
Chris@1611
|
193 }
|
Chris@1611
|
194
|
Chris@1611
|
195 void note_xml() {
|
Chris@1611
|
196 NoteModel m(100, 10, false);
|
Chris@1643
|
197 Event p1(20, 123.4f, 20, 0.8f, "note 1");
|
Chris@1643
|
198 Event p2(20, 124.3f, 10, 0.9f, "note 2");
|
Chris@1643
|
199 Event p3(50, 126.3f, 30, 0.9f, "note 3");
|
Chris@1611
|
200 m.setScaleUnits("Hz");
|
Chris@1644
|
201 m.add(p1);
|
Chris@1644
|
202 m.add(p2);
|
Chris@1644
|
203 m.add(p3);
|
Chris@1611
|
204 QString xml;
|
Chris@1611
|
205 QTextStream str(&xml, QIODevice::WriteOnly);
|
Chris@1611
|
206 m.toXml(str);
|
Chris@1611
|
207 str.flush();
|
Chris@1611
|
208 QString expected =
|
Chris@1643
|
209 "<model id='3' name='' sampleRate='100' start='20' end='80' type='sparse' dimensions='3' resolution='10' notifyOnAdd='false' dataset='2' subtype='note' valueQuantization='0' minimum='123.4' maximum='126.3' units='Hz' />\n"
|
Chris@1611
|
210 "<dataset id='2' dimensions='3'>\n"
|
Chris@1643
|
211 " <point frame='20' value='124.3' duration='10' level='0.9' label='note 2' />\n"
|
Chris@1611
|
212 " <point frame='20' value='123.4' duration='20' level='0.8' label='note 1' />\n"
|
Chris@1611
|
213 " <point frame='50' value='126.3' duration='30' level='0.9' label='note 3' />\n"
|
Chris@1611
|
214 "</dataset>\n";
|
Chris@1611
|
215 expected.replace("\'", "\"");
|
Chris@1611
|
216 if (xml != expected) {
|
Chris@1611
|
217 cerr << "Obtained xml:\n" << xml
|
Chris@1611
|
218 << "\nExpected:\n" << expected << endl;
|
Chris@1611
|
219 }
|
Chris@1611
|
220 QCOMPARE(xml, expected);
|
Chris@1611
|
221 }
|
Chris@1611
|
222
|
Chris@1611
|
223 };
|
Chris@1611
|
224
|
Chris@1611
|
225 #endif
|