comparison data/model/test/TestSparseModels.h @ 1611:b2f32c554199 single-point

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