annotate data/model/test/TestSparseModels.h @ 1641:751a52865270 single-point

Fix XML
author Chris Cannam
date Wed, 13 Mar 2019 14:46:40 +0000
parents 2e14a7876945
children 7a23dfe65d66
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@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@1611 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@1611 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@1611 108 }
Chris@1611 109
Chris@1611 110 void s1d_xml() {
Chris@1611 111 SparseOneDimensionalModel m(100, 10, false);
Chris@1611 112 m.setObjectName("This \"&\" that");
Chris@1611 113 SparseOneDimensionalModel::Point p1(20), p2(20), p3(50);
Chris@1611 114 p2.label = "Label &'\">";
Chris@1611 115 m.addPoint(p1);
Chris@1611 116 m.addPoint(p2);
Chris@1611 117 m.addPoint(p3);
Chris@1611 118 QString xml;
Chris@1611 119 QTextStream str(&xml, QIODevice::WriteOnly);
Chris@1611 120 m.toXml(str);
Chris@1611 121 str.flush();
Chris@1611 122 QString expected =
Chris@1611 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"
Chris@1611 124 "<dataset id='0' dimensions='1'>\n"
Chris@1611 125 " <point frame='20' label='' />\n"
Chris@1611 126 " <point frame='20' label='Label &amp;&apos;&quot;&gt;' />\n"
Chris@1611 127 " <point frame='50' label='' />\n"
Chris@1611 128 "</dataset>\n";
Chris@1611 129 expected.replace("\'", "\"");
Chris@1611 130 if (xml != expected) {
Chris@1611 131 cerr << "Obtained xml:\n" << xml
Chris@1611 132 << "\nExpected:\n" << expected << endl;
Chris@1611 133 }
Chris@1611 134 QCOMPARE(xml, expected);
Chris@1611 135 }
Chris@1611 136
Chris@1611 137 void note_extents() {
Chris@1611 138 NoteModel m(100, 10, false);
Chris@1614 139 NoteModel::Point p1(20, 123.4f, 40, 0.8f, "note 1");
Chris@1611 140 m.addPoint(p1);
Chris@1611 141 QCOMPARE(m.isEmpty(), false);
Chris@1611 142 QCOMPARE(m.getPointCount(), 1);
Chris@1614 143 NoteModel::Point p2(50, 124.3f, 30, 0.9f, "note 2");
Chris@1611 144 m.addPoint(p2);
Chris@1611 145 QCOMPARE(m.isEmpty(), false);
Chris@1611 146 QCOMPARE(m.getPointCount(), 2);
Chris@1611 147 QCOMPARE(m.getPoints().size(), 2);
Chris@1611 148 QCOMPARE(*m.getPoints().begin(), p1);
Chris@1611 149 QCOMPARE(*m.getPoints().rbegin(), p2);
Chris@1611 150 QCOMPARE(m.getStartFrame(), 20);
Chris@1611 151 QCOMPARE(m.getEndFrame(), 80);
Chris@1611 152 QCOMPARE(m.containsPoint(p1), true);
Chris@1611 153 QCOMPARE(m.getValueMinimum(), 123.4);
Chris@1611 154 QCOMPARE(m.getValueMaximum(), 124.3);
Chris@1611 155 m.deletePoint(p1);
Chris@1611 156 QCOMPARE(m.getPointCount(), 1);
Chris@1611 157 QCOMPARE(m.getPoints().size(), 1);
Chris@1611 158 QCOMPARE(*m.getPoints().begin(), p2);
Chris@1611 159 QCOMPARE(m.getStartFrame(), 50);
Chris@1611 160 QCOMPARE(m.getEndFrame(), 80);
Chris@1611 161 QCOMPARE(m.containsPoint(p1), false);
Chris@1611 162 }
Chris@1611 163
Chris@1611 164 void note_sample() {
Chris@1611 165 NoteModel m(100, 10, false);
Chris@1614 166 NoteModel::Point p1(20, 123.4f, 20, 0.8f, "note 1");
Chris@1614 167 NoteModel::Point p2(20, 124.3f, 10, 0.9f, "note 2");
Chris@1614 168 NoteModel::Point p3(50, 126.3f, 30, 0.9f, "note 3");
Chris@1611 169 m.addPoint(p1);
Chris@1611 170 m.addPoint(p2);
Chris@1611 171 m.addPoint(p3);
Chris@1611 172
Chris@1611 173 QCOMPARE(m.getPoints().size(), 3);
Chris@1611 174 QCOMPARE(*m.getPoints().begin(), p1);
Chris@1611 175 QCOMPARE(*m.getPoints().rbegin(), p3);
Chris@1611 176
Chris@1611 177 auto pp = m.getPoints(20, 30);
Chris@1611 178 QCOMPARE(pp.size(), 2);
Chris@1611 179 QCOMPARE(*pp.begin(), p1);
Chris@1611 180 QCOMPARE(*pp.rbegin(), p2);
Chris@1611 181
Chris@1611 182 pp = m.getPoints(30, 50);
Chris@1611 183 QCOMPARE(pp.size(), 1);
Chris@1611 184 QCOMPARE(*pp.begin(), p1);
Chris@1611 185
Chris@1611 186 pp = m.getPoints(40, 50);
Chris@1611 187 QCOMPARE(pp.size(), 0);
Chris@1611 188
Chris@1611 189 pp = m.getPoints(50, 50);
Chris@1611 190 QCOMPARE(pp.size(), 1);
Chris@1611 191 QCOMPARE(*pp.begin(), p3);
Chris@1611 192 }
Chris@1611 193
Chris@1611 194 void note_xml() {
Chris@1611 195 NoteModel m(100, 10, false);
Chris@1614 196 NoteModel::Point p1(20, 123.4f, 20, 0.8f, "note 1");
Chris@1614 197 NoteModel::Point p2(20, 124.3f, 10, 0.9f, "note 2");
Chris@1614 198 NoteModel::Point p3(50, 126.3f, 30, 0.9f, "note 3");
Chris@1611 199 m.setScaleUnits("Hz");
Chris@1611 200 m.addPoint(p1);
Chris@1611 201 m.addPoint(p2);
Chris@1611 202 m.addPoint(p3);
Chris@1611 203 QString xml;
Chris@1611 204 QTextStream str(&xml, QIODevice::WriteOnly);
Chris@1611 205 m.toXml(str);
Chris@1611 206 str.flush();
Chris@1611 207 QString expected =
Chris@1611 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"
Chris@1611 209 "<dataset id='2' dimensions='3'>\n"
Chris@1611 210 " <point frame='20' value='123.4' duration='20' level='0.8' label='note 1' />\n"
Chris@1611 211 " <point frame='20' value='124.3' duration='10' level='0.9' label='note 2' />\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@1611 221
Chris@1611 222
Chris@1611 223 };
Chris@1611 224
Chris@1611 225 #endif