comparison base/test/TestColumnOp.h @ 1265:e2e66bfd4a88 3.0-integration

Start tests for ColumnOp (+ some resulting fixes)
author Chris Cannam
date Thu, 17 Nov 2016 11:56:54 +0000
parents
children dd190086db73
comparison
equal deleted inserted replaced
1264:a99641535e02 1265:e2e66bfd4a88
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_COLUMN_OP_H
16 #define TEST_COLUMN_OP_H
17
18 #include "../ColumnOp.h"
19
20 #include <QObject>
21 #include <QtTest>
22 #include <QDir>
23
24 #include <iostream>
25
26 using namespace std;
27
28 class TestColumnOp : public QObject
29 {
30 Q_OBJECT
31
32 typedef ColumnOp C;
33 typedef ColumnOp::Column Column;
34
35 private slots:
36 void applyGain() {
37 QCOMPARE(C::applyGain({}, 1.0), Column());
38 Column c { 1, 2, 3, -4, 5, 6 };
39 Column actual(C::applyGain(c, 1.5));
40 Column expected { 1.5, 3, 4.5, -6, 7.5, 9 };
41 QCOMPARE(actual, expected);
42 actual = C::applyGain(c, 1.0);
43 QCOMPARE(actual, c);
44 actual = C::applyGain(c, 0.0);
45 expected = { 0, 0, 0, 0, 0, 0 };
46 QCOMPARE(actual, expected);
47 }
48
49 void fftScale() {
50 QCOMPARE(C::fftScale({}, 2.0), Column());
51 Column c { 1, 2, 3, -4, 5 };
52 Column actual(C::fftScale(c, 8));
53 Column expected { 0.25, 0.5, 0.75, -1, 1.25 };
54 QCOMPARE(actual, expected);
55 }
56
57 void isPeak_null() {
58 QVERIFY(!C::isPeak({}, 0));
59 QVERIFY(!C::isPeak({}, 1));
60 QVERIFY(!C::isPeak({}, -1));
61 }
62
63 void isPeak_obvious() {
64 Column c { 0.4, 0.5, 0.3 };
65 QVERIFY(!C::isPeak(c, 0));
66 QVERIFY(C::isPeak(c, 1));
67 QVERIFY(!C::isPeak(c, 2));
68 }
69
70 void isPeak_edges() {
71 Column c { 0.5, 0.4, 0.3 };
72 QVERIFY(C::isPeak(c, 0));
73 QVERIFY(!C::isPeak(c, 1));
74 QVERIFY(!C::isPeak(c, 2));
75 QVERIFY(!C::isPeak(c, 3));
76 QVERIFY(!C::isPeak(c, -1));
77 c = { 1.4, 1.5 };
78 QVERIFY(!C::isPeak(c, 0));
79 QVERIFY(C::isPeak(c, 1));
80 }
81
82 void isPeak_flat() {
83 Column c { 0.0, 0.0, 0.0 };
84 QVERIFY(C::isPeak(c, 0));
85 QVERIFY(!C::isPeak(c, 1));
86 QVERIFY(!C::isPeak(c, 2));
87 }
88
89 void isPeak_mixedSign() {
90 Column c { 0.4, -0.5, -0.3, -0.6, 0.1, -0.3 };
91 QVERIFY(C::isPeak(c, 0));
92 QVERIFY(!C::isPeak(c, 1));
93 QVERIFY(C::isPeak(c, 2));
94 QVERIFY(!C::isPeak(c, 3));
95 QVERIFY(C::isPeak(c, 4));
96 QVERIFY(!C::isPeak(c, 5));
97 }
98
99 void isPeak_duplicate() {
100 Column c({ 0.5, 0.5, 0.4, 0.4 });
101 QVERIFY(C::isPeak(c, 0));
102 QVERIFY(!C::isPeak(c, 1));
103 QVERIFY(!C::isPeak(c, 2));
104 QVERIFY(!C::isPeak(c, 3));
105 c = { 0.4, 0.4, 0.5, 0.5 };
106 QVERIFY(C::isPeak(c, 0)); // counterintuitive but necessary
107 QVERIFY(!C::isPeak(c, 1));
108 QVERIFY(C::isPeak(c, 2));
109 QVERIFY(!C::isPeak(c, 3));
110 }
111
112 void peakPick() {
113 QCOMPARE(C::peakPick({}), Column());
114 Column c({ 0.5, 0.5, 0.4, 0.4 });
115 QCOMPARE(C::peakPick(c), Column({ 0.5, 0.0, 0.0, 0.0 }));
116 c = Column({ 0.4, -0.5, -0.3, -0.6, 0.1, -0.3 });
117 QCOMPARE(C::peakPick(c), Column({ 0.4, 0.0, -0.3, 0.0, 0.1, 0.0 }));
118 }
119
120 void normalize_null() {
121 QCOMPARE(C::normalize({}, ColumnNormalization::None), Column());
122 QCOMPARE(C::normalize({}, ColumnNormalization::Sum1), Column());
123 QCOMPARE(C::normalize({}, ColumnNormalization::Max1), Column());
124 QCOMPARE(C::normalize({}, ColumnNormalization::Hybrid), Column());
125 }
126
127 void normalize_none() {
128 Column c { 1, 2, 3, 4 };
129 QCOMPARE(C::normalize(c, ColumnNormalization::None), c);
130 }
131
132 void normalize_sum1() {
133 Column c { 1, 2, 4, 3 };
134 QCOMPARE(C::normalize(c, ColumnNormalization::Sum1),
135 Column({ 0.1, 0.2, 0.4, 0.3 }));
136 }
137
138 void normalize_max1() {
139 Column c { 4, 3, 2, 1 };
140 QCOMPARE(C::normalize(c, ColumnNormalization::Max1),
141 Column({ 1.0, 0.75, 0.5, 0.25 }));
142 }
143
144 void normalize_hybrid() {
145 // with max == 99, log10(max+1) == 2 so scale factor will be 2/99
146 Column c { 22, 44, 99, 66 };
147 QCOMPARE(C::normalize(c, ColumnNormalization::Hybrid),
148 Column({ 44.0/99.0, 88.0/99.0, 2.0, 132.0/99.0 }));
149 }
150
151
152
153 };
154
155 #endif
156