annotate data/model/test/TestSparseModels.h @ 1675:6804af71b7be osc-script

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