Chris@1611: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1611: Chris@1611: /* Chris@1611: Sonic Visualiser Chris@1611: An audio file viewer and annotation editor. Chris@1611: Centre for Digital Music, Queen Mary, University of London. Chris@1611: Chris@1611: This program is free software; you can redistribute it and/or Chris@1611: modify it under the terms of the GNU General Public License as Chris@1611: published by the Free Software Foundation; either version 2 of the Chris@1611: License, or (at your option) any later version. See the file Chris@1611: COPYING included with this distribution for more information. Chris@1611: */ Chris@1611: Chris@1611: #ifndef TEST_SPARSE_MODELS_H Chris@1611: #define TEST_SPARSE_MODELS_H Chris@1611: Chris@1611: #include "../SparseOneDimensionalModel.h" Chris@1611: #include "../NoteModel.h" Chris@1611: Chris@1611: #include Chris@1611: #include Chris@1611: Chris@1611: #include Chris@1611: Chris@1611: using namespace std; Chris@1611: Chris@1611: class TestSparseModels : public QObject Chris@1611: { Chris@1611: Q_OBJECT Chris@1611: Chris@1611: private slots: Chris@1611: void s1d_empty() { Chris@1611: SparseOneDimensionalModel m(100, 10, false); Chris@1611: QCOMPARE(m.isEmpty(), true); Chris@1611: QCOMPARE(m.getPointCount(), 0); Chris@1611: QCOMPARE(m.getPoints().begin(), m.getPoints().end()); Chris@1611: QCOMPARE(m.getStartFrame(), 0); Chris@1611: QCOMPARE(m.getEndFrame(), 0); Chris@1611: QCOMPARE(m.getSampleRate(), 100); Chris@1611: QCOMPARE(m.getResolution(), 10); Chris@1611: QCOMPARE(m.isSparse(), true); Chris@1611: Chris@1611: SparseOneDimensionalModel::Point p(10); Chris@1611: m.addPoint(p); Chris@1611: m.clear(); Chris@1611: QCOMPARE(m.isEmpty(), true); Chris@1611: QCOMPARE(m.getPointCount(), 0); Chris@1611: QCOMPARE(m.getPoints().begin(), m.getPoints().end()); Chris@1611: QCOMPARE(m.getStartFrame(), 0); Chris@1611: QCOMPARE(m.getEndFrame(), 0); Chris@1611: Chris@1611: m.addPoint(p); Chris@1611: m.deletePoint(p); Chris@1611: QCOMPARE(m.isEmpty(), true); Chris@1611: QCOMPARE(m.getPointCount(), 0); Chris@1611: QCOMPARE(m.getPoints().begin(), m.getPoints().end()); Chris@1611: QCOMPARE(m.getStartFrame(), 0); Chris@1611: QCOMPARE(m.getEndFrame(), 0); Chris@1611: } Chris@1611: Chris@1611: void s1d_extents() { Chris@1611: SparseOneDimensionalModel m(100, 10, false); Chris@1611: SparseOneDimensionalModel::Point p1(20); Chris@1611: m.addPoint(p1); Chris@1611: QCOMPARE(m.isEmpty(), false); Chris@1611: QCOMPARE(m.getPointCount(), 1); Chris@1611: SparseOneDimensionalModel::Point p2(50); Chris@1611: m.addPoint(p2); Chris@1611: QCOMPARE(m.isEmpty(), false); Chris@1611: QCOMPARE(m.getPointCount(), 2); Chris@1611: QCOMPARE(m.getPoints().size(), 2); Chris@1611: QCOMPARE(*m.getPoints().begin(), p1); Chris@1611: QCOMPARE(*m.getPoints().rbegin(), p2); Chris@1611: QCOMPARE(m.getStartFrame(), 20); Chris@1611: QCOMPARE(m.getEndFrame(), 60); Chris@1611: QCOMPARE(m.containsPoint(p1), true); Chris@1611: m.deletePoint(p1); Chris@1611: QCOMPARE(m.getPointCount(), 1); Chris@1611: QCOMPARE(m.getPoints().size(), 1); Chris@1611: QCOMPARE(*m.getPoints().begin(), p2); Chris@1611: QCOMPARE(m.getStartFrame(), 50); Chris@1611: QCOMPARE(m.getEndFrame(), 60); Chris@1611: QCOMPARE(m.containsPoint(p1), false); Chris@1611: } Chris@1611: Chris@1611: void s1d_sample() { Chris@1611: SparseOneDimensionalModel m(100, 10, false); Chris@1611: SparseOneDimensionalModel::Point p1(20), p2(20), p3(50); Chris@1611: m.addPoint(p1); Chris@1611: m.addPoint(p2); Chris@1611: m.addPoint(p3); Chris@1611: QCOMPARE(m.getPoints().size(), 3); Chris@1611: QCOMPARE(*m.getPoints().begin(), p1); Chris@1611: QCOMPARE(*m.getPoints().rbegin(), p3); Chris@1611: Chris@1611: auto pp = m.getPoints(20, 30); Chris@1611: QCOMPARE(pp.size(), 2); Chris@1611: QCOMPARE(*pp.begin(), p1); Chris@1611: QCOMPARE(*pp.rbegin(), p2); Chris@1611: Chris@1611: pp = m.getPoints(40, 50); Chris@1611: QCOMPARE(pp.size(), 0); Chris@1611: Chris@1611: pp = m.getPoints(50, 50); Chris@1611: QCOMPARE(pp.size(), 1); Chris@1611: QCOMPARE(*pp.begin(), p3); Chris@1611: } Chris@1611: Chris@1611: void s1d_xml() { Chris@1611: SparseOneDimensionalModel m(100, 10, false); Chris@1611: m.setObjectName("This \"&\" that"); Chris@1611: SparseOneDimensionalModel::Point p1(20), p2(20), p3(50); Chris@1611: p2.label = "Label &'\">"; Chris@1611: m.addPoint(p1); Chris@1611: m.addPoint(p2); Chris@1611: m.addPoint(p3); Chris@1611: QString xml; Chris@1611: QTextStream str(&xml, QIODevice::WriteOnly); Chris@1611: m.toXml(str); Chris@1611: str.flush(); Chris@1611: QString expected = Chris@1611: "\n" Chris@1611: "\n" Chris@1611: " \n" Chris@1611: " \n" Chris@1611: " \n" Chris@1611: "\n"; Chris@1611: expected.replace("\'", "\""); Chris@1611: if (xml != expected) { Chris@1611: cerr << "Obtained xml:\n" << xml Chris@1611: << "\nExpected:\n" << expected << endl; Chris@1611: } Chris@1611: QCOMPARE(xml, expected); Chris@1611: } Chris@1611: Chris@1611: void note_extents() { Chris@1611: NoteModel m(100, 10, false); Chris@1614: NoteModel::Point p1(20, 123.4f, 40, 0.8f, "note 1"); Chris@1611: m.addPoint(p1); Chris@1611: QCOMPARE(m.isEmpty(), false); Chris@1611: QCOMPARE(m.getPointCount(), 1); Chris@1614: NoteModel::Point p2(50, 124.3f, 30, 0.9f, "note 2"); Chris@1611: m.addPoint(p2); Chris@1611: QCOMPARE(m.isEmpty(), false); Chris@1611: QCOMPARE(m.getPointCount(), 2); Chris@1611: QCOMPARE(m.getPoints().size(), 2); Chris@1611: QCOMPARE(*m.getPoints().begin(), p1); Chris@1611: QCOMPARE(*m.getPoints().rbegin(), p2); Chris@1611: QCOMPARE(m.getStartFrame(), 20); Chris@1611: QCOMPARE(m.getEndFrame(), 80); Chris@1611: QCOMPARE(m.containsPoint(p1), true); Chris@1611: QCOMPARE(m.getValueMinimum(), 123.4); Chris@1611: QCOMPARE(m.getValueMaximum(), 124.3); Chris@1611: m.deletePoint(p1); Chris@1611: QCOMPARE(m.getPointCount(), 1); Chris@1611: QCOMPARE(m.getPoints().size(), 1); Chris@1611: QCOMPARE(*m.getPoints().begin(), p2); Chris@1611: QCOMPARE(m.getStartFrame(), 50); Chris@1611: QCOMPARE(m.getEndFrame(), 80); Chris@1611: QCOMPARE(m.containsPoint(p1), false); Chris@1611: } Chris@1611: Chris@1611: void note_sample() { Chris@1611: NoteModel m(100, 10, false); Chris@1614: NoteModel::Point p1(20, 123.4f, 20, 0.8f, "note 1"); Chris@1614: NoteModel::Point p2(20, 124.3f, 10, 0.9f, "note 2"); Chris@1614: NoteModel::Point p3(50, 126.3f, 30, 0.9f, "note 3"); Chris@1611: m.addPoint(p1); Chris@1611: m.addPoint(p2); Chris@1611: m.addPoint(p3); Chris@1611: Chris@1611: QCOMPARE(m.getPoints().size(), 3); Chris@1611: QCOMPARE(*m.getPoints().begin(), p1); Chris@1611: QCOMPARE(*m.getPoints().rbegin(), p3); Chris@1611: Chris@1611: auto pp = m.getPoints(20, 30); Chris@1611: QCOMPARE(pp.size(), 2); Chris@1611: QCOMPARE(*pp.begin(), p1); Chris@1611: QCOMPARE(*pp.rbegin(), p2); Chris@1611: Chris@1611: pp = m.getPoints(30, 50); Chris@1611: QCOMPARE(pp.size(), 1); Chris@1611: QCOMPARE(*pp.begin(), p1); Chris@1611: Chris@1611: pp = m.getPoints(40, 50); Chris@1611: QCOMPARE(pp.size(), 0); Chris@1611: Chris@1611: pp = m.getPoints(50, 50); Chris@1611: QCOMPARE(pp.size(), 1); Chris@1611: QCOMPARE(*pp.begin(), p3); Chris@1611: } Chris@1611: Chris@1611: void note_xml() { Chris@1611: NoteModel m(100, 10, false); Chris@1614: NoteModel::Point p1(20, 123.4f, 20, 0.8f, "note 1"); Chris@1614: NoteModel::Point p2(20, 124.3f, 10, 0.9f, "note 2"); Chris@1614: NoteModel::Point p3(50, 126.3f, 30, 0.9f, "note 3"); Chris@1611: m.setScaleUnits("Hz"); Chris@1611: m.addPoint(p1); Chris@1611: m.addPoint(p2); Chris@1611: m.addPoint(p3); Chris@1611: QString xml; Chris@1611: QTextStream str(&xml, QIODevice::WriteOnly); Chris@1611: m.toXml(str); Chris@1611: str.flush(); Chris@1611: QString expected = Chris@1611: "\n" Chris@1611: "\n" Chris@1611: " \n" Chris@1611: " \n" Chris@1611: " \n" Chris@1611: "\n"; Chris@1611: expected.replace("\'", "\""); Chris@1611: if (xml != expected) { Chris@1611: cerr << "Obtained xml:\n" << xml Chris@1611: << "\nExpected:\n" << expected << endl; Chris@1611: } Chris@1611: QCOMPARE(xml, expected); Chris@1611: } Chris@1611: Chris@1611: Chris@1611: }; Chris@1611: Chris@1611: #endif