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@1661
|
20 #include "../TextModel.h"
|
Chris@1611
|
21
|
Chris@1611
|
22 #include <QObject>
|
Chris@1611
|
23 #include <QtTest>
|
Chris@1611
|
24
|
Chris@1611
|
25 #include <iostream>
|
Chris@1611
|
26
|
Chris@1611
|
27 using namespace std;
|
Chris@1611
|
28
|
Chris@1611
|
29 class TestSparseModels : public QObject
|
Chris@1611
|
30 {
|
Chris@1611
|
31 Q_OBJECT
|
Chris@1611
|
32
|
Chris@1611
|
33 private slots:
|
Chris@1611
|
34 void s1d_empty() {
|
Chris@1611
|
35 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1611
|
36 QCOMPARE(m.isEmpty(), true);
|
Chris@1658
|
37 QCOMPARE(m.getEventCount(), 0);
|
Chris@1658
|
38 QCOMPARE(m.getAllEvents().size(), 0);
|
Chris@1611
|
39 QCOMPARE(m.getStartFrame(), 0);
|
Chris@1611
|
40 QCOMPARE(m.getEndFrame(), 0);
|
Chris@1611
|
41 QCOMPARE(m.getSampleRate(), 100);
|
Chris@1611
|
42 QCOMPARE(m.getResolution(), 10);
|
Chris@1611
|
43 QCOMPARE(m.isSparse(), true);
|
Chris@1611
|
44
|
Chris@1658
|
45 Event p(10);
|
Chris@1658
|
46 m.add(p);
|
Chris@1658
|
47 m.remove(p);
|
Chris@1611
|
48 QCOMPARE(m.isEmpty(), true);
|
Chris@1658
|
49 QCOMPARE(m.getEventCount(), 0);
|
Chris@1658
|
50 QCOMPARE(m.getAllEvents().size(), 0);
|
Chris@1611
|
51 QCOMPARE(m.getStartFrame(), 0);
|
Chris@1611
|
52 QCOMPARE(m.getEndFrame(), 0);
|
Chris@1611
|
53 }
|
Chris@1611
|
54
|
Chris@1611
|
55 void s1d_extents() {
|
Chris@1611
|
56 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1658
|
57 Event p1(20);
|
Chris@1658
|
58 m.add(p1);
|
Chris@1611
|
59 QCOMPARE(m.isEmpty(), false);
|
Chris@1658
|
60 QCOMPARE(m.getEventCount(), 1);
|
Chris@1658
|
61 Event p2(50);
|
Chris@1658
|
62 m.add(p2);
|
Chris@1611
|
63 QCOMPARE(m.isEmpty(), false);
|
Chris@1658
|
64 QCOMPARE(m.getEventCount(), 2);
|
Chris@1658
|
65 QCOMPARE(m.getAllEvents().size(), 2);
|
Chris@1658
|
66 QCOMPARE(*m.getAllEvents().begin(), p1);
|
Chris@1658
|
67 QCOMPARE(*m.getAllEvents().rbegin(), p2);
|
Chris@1611
|
68 QCOMPARE(m.getStartFrame(), 20);
|
Chris@1611
|
69 QCOMPARE(m.getEndFrame(), 60);
|
Chris@1658
|
70 QCOMPARE(m.containsEvent(p1), true);
|
Chris@1658
|
71 m.remove(p1);
|
Chris@1658
|
72 QCOMPARE(m.getEventCount(), 1);
|
Chris@1658
|
73 QCOMPARE(m.getAllEvents().size(), 1);
|
Chris@1658
|
74 QCOMPARE(*m.getAllEvents().begin(), p2);
|
Chris@1611
|
75 QCOMPARE(m.getStartFrame(), 50);
|
Chris@1611
|
76 QCOMPARE(m.getEndFrame(), 60);
|
Chris@1658
|
77 QCOMPARE(m.containsEvent(p1), false);
|
Chris@1611
|
78 }
|
Chris@1611
|
79
|
Chris@1611
|
80 void s1d_sample() {
|
Chris@1611
|
81 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1658
|
82 Event p1(20), p2(20), p3(50);
|
Chris@1658
|
83 m.add(p1);
|
Chris@1658
|
84 m.add(p2);
|
Chris@1658
|
85 m.add(p3);
|
Chris@1658
|
86 QCOMPARE(m.getAllEvents().size(), 3);
|
Chris@1658
|
87 QCOMPARE(*m.getAllEvents().begin(), p1);
|
Chris@1658
|
88 QCOMPARE(*m.getAllEvents().rbegin(), p3);
|
Chris@1659
|
89
|
Chris@1659
|
90 // The EventSeries that is used internally is tested more
|
Chris@1659
|
91 // thoroughly in its own test suite. This is just a check
|
Chris@1659
|
92 auto pp = m.getEventsWithin(20, 10);
|
Chris@1611
|
93 QCOMPARE(pp.size(), 2);
|
Chris@1611
|
94 QCOMPARE(*pp.begin(), p1);
|
Chris@1611
|
95 QCOMPARE(*pp.rbegin(), p2);
|
Chris@1643
|
96
|
Chris@1659
|
97 pp = m.getEventsWithin(40, 10);
|
Chris@1611
|
98 QCOMPARE(pp.size(), 0);
|
Chris@1611
|
99
|
Chris@1659
|
100 pp = m.getEventsStartingAt(50);
|
Chris@1611
|
101 QCOMPARE(pp.size(), 1);
|
Chris@1611
|
102 QCOMPARE(*pp.begin(), p3);
|
Chris@1611
|
103 }
|
Chris@1611
|
104
|
Chris@1611
|
105 void s1d_xml() {
|
Chris@1611
|
106 SparseOneDimensionalModel m(100, 10, false);
|
Chris@1611
|
107 m.setObjectName("This \"&\" that");
|
Chris@1658
|
108 Event p1(20), p2(20), p3(50);
|
Chris@1658
|
109 p2 = p2.withLabel("Label &'\">");
|
Chris@1658
|
110 m.add(p1);
|
Chris@1658
|
111 m.add(p2);
|
Chris@1658
|
112 m.add(p3);
|
Chris@1611
|
113 QString xml;
|
Chris@1611
|
114 QTextStream str(&xml, QIODevice::WriteOnly);
|
Chris@1611
|
115 m.toXml(str);
|
Chris@1611
|
116 str.flush();
|
Chris@1611
|
117 QString expected =
|
Chris@1658
|
118 "<model id='1' name='This "&" that' sampleRate='100' start='20' end='60' type='sparse' dimensions='1' resolution='10' notifyOnAdd='true' dataset='0' />\n"
|
Chris@1611
|
119 "<dataset id='0' dimensions='1'>\n"
|
Chris@1611
|
120 " <point frame='20' label='' />\n"
|
Chris@1611
|
121 " <point frame='20' label='Label &'">' />\n"
|
Chris@1611
|
122 " <point frame='50' label='' />\n"
|
Chris@1611
|
123 "</dataset>\n";
|
Chris@1611
|
124 expected.replace("\'", "\"");
|
Chris@1611
|
125 if (xml != expected) {
|
Chris@1611
|
126 cerr << "Obtained xml:\n" << xml
|
Chris@1611
|
127 << "\nExpected:\n" << expected << endl;
|
Chris@1611
|
128 }
|
Chris@1611
|
129 QCOMPARE(xml, expected);
|
Chris@1611
|
130 }
|
Chris@1611
|
131
|
Chris@1611
|
132 void note_extents() {
|
Chris@1611
|
133 NoteModel m(100, 10, false);
|
Chris@1643
|
134 Event p1(20, 123.4f, 40, 0.8f, "note 1");
|
Chris@1644
|
135 m.add(p1);
|
Chris@1611
|
136 QCOMPARE(m.isEmpty(), false);
|
Chris@1644
|
137 QCOMPARE(m.getEventCount(), 1);
|
Chris@1643
|
138 Event p2(50, 124.3f, 30, 0.9f, "note 2");
|
Chris@1644
|
139 m.add(p2);
|
Chris@1611
|
140 QCOMPARE(m.isEmpty(), false);
|
Chris@1644
|
141 QCOMPARE(m.getEventCount(), 2);
|
Chris@1644
|
142 QCOMPARE(m.getAllEvents().size(), 2);
|
Chris@1644
|
143 QCOMPARE(*m.getAllEvents().begin(), p1);
|
Chris@1644
|
144 QCOMPARE(*m.getAllEvents().rbegin(), p2);
|
Chris@1611
|
145 QCOMPARE(m.getStartFrame(), 20);
|
Chris@1611
|
146 QCOMPARE(m.getEndFrame(), 80);
|
Chris@1644
|
147 QCOMPARE(m.containsEvent(p1), true);
|
Chris@1643
|
148 QCOMPARE(m.getValueMinimum(), 123.4f);
|
Chris@1643
|
149 QCOMPARE(m.getValueMaximum(), 124.3f);
|
Chris@1644
|
150 m.remove(p1);
|
Chris@1644
|
151 QCOMPARE(m.getEventCount(), 1);
|
Chris@1644
|
152 QCOMPARE(m.getAllEvents().size(), 1);
|
Chris@1644
|
153 QCOMPARE(*m.getAllEvents().begin(), p2);
|
Chris@1611
|
154 QCOMPARE(m.getStartFrame(), 50);
|
Chris@1611
|
155 QCOMPARE(m.getEndFrame(), 80);
|
Chris@1644
|
156 QCOMPARE(m.containsEvent(p1), false);
|
Chris@1611
|
157 }
|
Chris@1611
|
158
|
Chris@1611
|
159 void note_sample() {
|
Chris@1611
|
160 NoteModel m(100, 10, false);
|
Chris@1643
|
161 Event p1(20, 123.4f, 10, 0.8f, "note 1");
|
Chris@1643
|
162 Event p2(20, 124.3f, 20, 0.9f, "note 2");
|
Chris@1643
|
163 Event p3(50, 126.3f, 30, 0.9f, "note 3");
|
Chris@1644
|
164 m.add(p1);
|
Chris@1644
|
165 m.add(p2);
|
Chris@1644
|
166 m.add(p3);
|
Chris@1611
|
167
|
Chris@1644
|
168 QCOMPARE(m.getAllEvents().size(), 3);
|
Chris@1644
|
169 QCOMPARE(*m.getAllEvents().begin(), p1);
|
Chris@1644
|
170 QCOMPARE(*m.getAllEvents().rbegin(), p3);
|
Chris@1611
|
171
|
Chris@1644
|
172 auto pp = m.getEventsSpanning(20, 10);
|
Chris@1611
|
173 QCOMPARE(pp.size(), 2);
|
Chris@1611
|
174 QCOMPARE(*pp.begin(), p1);
|
Chris@1611
|
175 QCOMPARE(*pp.rbegin(), p2);
|
Chris@1611
|
176
|
Chris@1644
|
177 pp = m.getEventsSpanning(30, 20);
|
Chris@1611
|
178 QCOMPARE(pp.size(), 1);
|
Chris@1643
|
179 QCOMPARE(*pp.begin(), p2);
|
Chris@1611
|
180
|
Chris@1644
|
181 pp = m.getEventsSpanning(40, 10);
|
Chris@1611
|
182 QCOMPARE(pp.size(), 0);
|
Chris@1611
|
183
|
Chris@1644
|
184 pp = m.getEventsCovering(50);
|
Chris@1644
|
185 QCOMPARE(pp.size(), 1);
|
Chris@1644
|
186 QCOMPARE(*pp.begin(), p3);
|
Chris@1611
|
187 }
|
Chris@1611
|
188
|
Chris@1611
|
189 void note_xml() {
|
Chris@1611
|
190 NoteModel m(100, 10, false);
|
Chris@1643
|
191 Event p1(20, 123.4f, 20, 0.8f, "note 1");
|
Chris@1643
|
192 Event p2(20, 124.3f, 10, 0.9f, "note 2");
|
Chris@1643
|
193 Event p3(50, 126.3f, 30, 0.9f, "note 3");
|
Chris@1611
|
194 m.setScaleUnits("Hz");
|
Chris@1644
|
195 m.add(p1);
|
Chris@1644
|
196 m.add(p2);
|
Chris@1644
|
197 m.add(p3);
|
Chris@1611
|
198 QString xml;
|
Chris@1611
|
199 QTextStream str(&xml, QIODevice::WriteOnly);
|
Chris@1611
|
200 m.toXml(str);
|
Chris@1611
|
201 str.flush();
|
Chris@1651
|
202
|
Chris@1651
|
203 //!!! This is not guaranteed - object export ids are in order
|
Chris@1651
|
204 //!!! of model pointer value, which is not trustworthy -
|
Chris@1651
|
205 //!!! replace them with something else
|
Chris@1651
|
206
|
Chris@1611
|
207 QString expected =
|
Chris@1651
|
208 "<model id='3' name='' sampleRate='100' start='20' end='80' type='sparse' dimensions='3' resolution='10' notifyOnAdd='true' dataset='2' subtype='note' valueQuantization='0' minimum='123.4' maximum='126.3' units='Hz' />\n"
|
Chris@1611
|
209 "<dataset id='2' dimensions='3'>\n"
|
Chris@1643
|
210 " <point frame='20' value='124.3' duration='10' level='0.9' label='note 2' />\n"
|
Chris@1611
|
211 " <point frame='20' value='123.4' duration='20' level='0.8' label='note 1' />\n"
|
Chris@1611
|
212 " <point frame='50' value='126.3' duration='30' level='0.9' label='note 3' />\n"
|
Chris@1611
|
213 "</dataset>\n";
|
Chris@1611
|
214 expected.replace("\'", "\"");
|
Chris@1611
|
215 if (xml != expected) {
|
Chris@1611
|
216 cerr << "Obtained xml:\n" << xml
|
Chris@1611
|
217 << "\nExpected:\n" << expected << endl;
|
Chris@1611
|
218 }
|
Chris@1611
|
219 QCOMPARE(xml, expected);
|
Chris@1611
|
220 }
|
Chris@1661
|
221
|
Chris@1661
|
222 void text_xml() {
|
Chris@1661
|
223 TextModel m(100, 10, false);
|
Chris@1661
|
224 Event p1(20, 1.0f, "text 1");
|
Chris@1661
|
225 Event p2(20, 0.0f, "text 2");
|
Chris@1661
|
226 Event p3(50, 0.3f, "text 3");
|
Chris@1661
|
227 m.add(p1);
|
Chris@1661
|
228 m.add(p2);
|
Chris@1661
|
229 m.add(p3);
|
Chris@1661
|
230 QString xml;
|
Chris@1661
|
231 QTextStream str(&xml, QIODevice::WriteOnly);
|
Chris@1661
|
232 m.toXml(str);
|
Chris@1661
|
233 str.flush();
|
Chris@1661
|
234
|
Chris@1661
|
235 QString expected =
|
Chris@1661
|
236 "<model id='5' name='' sampleRate='100' start='20' end='80' type='sparse' dimensions='2' resolution='10' notifyOnAdd='true' dataset='4' subtype='text' />\n"
|
Chris@1661
|
237 "<dataset id='4' dimensions='2'>\n"
|
Chris@1661
|
238 " <point frame='20' height='0' label='text 2' />\n"
|
Chris@1661
|
239 " <point frame='20' height='1' label='text 1' />\n"
|
Chris@1661
|
240 " <point frame='50' height='0.3' label='text 3' />\n"
|
Chris@1661
|
241 "</dataset>\n";
|
Chris@1661
|
242 expected.replace("\'", "\"");
|
Chris@1661
|
243 if (xml != expected) {
|
Chris@1661
|
244 cerr << "Obtained xml:\n" << xml
|
Chris@1661
|
245 << "\nExpected:\n" << expected << endl;
|
Chris@1661
|
246 }
|
Chris@1661
|
247 QCOMPARE(xml, expected);
|
Chris@1661
|
248 }
|
Chris@1611
|
249
|
Chris@1611
|
250 };
|
Chris@1611
|
251
|
Chris@1611
|
252 #endif
|