Chris@1265
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1265
|
2
|
Chris@1265
|
3 /*
|
Chris@1265
|
4 Sonic Visualiser
|
Chris@1265
|
5 An audio file viewer and annotation editor.
|
Chris@1265
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1265
|
7
|
Chris@1265
|
8 This program is free software; you can redistribute it and/or
|
Chris@1265
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1265
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1265
|
11 License, or (at your option) any later version. See the file
|
Chris@1265
|
12 COPYING included with this distribution for more information.
|
Chris@1265
|
13 */
|
Chris@1265
|
14
|
Chris@1265
|
15 #ifndef TEST_COLUMN_OP_H
|
Chris@1265
|
16 #define TEST_COLUMN_OP_H
|
Chris@1265
|
17
|
Chris@1265
|
18 #include "../ColumnOp.h"
|
Chris@1265
|
19
|
Chris@1265
|
20 #include <QObject>
|
Chris@1265
|
21 #include <QtTest>
|
Chris@1265
|
22 #include <QDir>
|
Chris@1265
|
23
|
Chris@1265
|
24 #include <iostream>
|
Chris@1265
|
25
|
Chris@1265
|
26 using namespace std;
|
Chris@1265
|
27
|
Chris@1265
|
28 class TestColumnOp : public QObject
|
Chris@1265
|
29 {
|
Chris@1265
|
30 Q_OBJECT
|
Chris@1265
|
31
|
Chris@1265
|
32 typedef ColumnOp C;
|
Chris@1265
|
33 typedef ColumnOp::Column Column;
|
Chris@1265
|
34
|
Chris@1265
|
35 private slots:
|
Chris@1265
|
36 void applyGain() {
|
Chris@1265
|
37 QCOMPARE(C::applyGain({}, 1.0), Column());
|
Chris@1265
|
38 Column c { 1, 2, 3, -4, 5, 6 };
|
Chris@1265
|
39 Column actual(C::applyGain(c, 1.5));
|
Chris@1265
|
40 Column expected { 1.5, 3, 4.5, -6, 7.5, 9 };
|
Chris@1265
|
41 QCOMPARE(actual, expected);
|
Chris@1265
|
42 actual = C::applyGain(c, 1.0);
|
Chris@1265
|
43 QCOMPARE(actual, c);
|
Chris@1265
|
44 actual = C::applyGain(c, 0.0);
|
Chris@1265
|
45 expected = { 0, 0, 0, 0, 0, 0 };
|
Chris@1265
|
46 QCOMPARE(actual, expected);
|
Chris@1265
|
47 }
|
Chris@1265
|
48
|
Chris@1265
|
49 void fftScale() {
|
Chris@1265
|
50 QCOMPARE(C::fftScale({}, 2.0), Column());
|
Chris@1265
|
51 Column c { 1, 2, 3, -4, 5 };
|
Chris@1265
|
52 Column actual(C::fftScale(c, 8));
|
Chris@1265
|
53 Column expected { 0.25, 0.5, 0.75, -1, 1.25 };
|
Chris@1265
|
54 QCOMPARE(actual, expected);
|
Chris@1265
|
55 }
|
Chris@1265
|
56
|
Chris@1265
|
57 void isPeak_null() {
|
Chris@1265
|
58 QVERIFY(!C::isPeak({}, 0));
|
Chris@1265
|
59 QVERIFY(!C::isPeak({}, 1));
|
Chris@1265
|
60 QVERIFY(!C::isPeak({}, -1));
|
Chris@1265
|
61 }
|
Chris@1265
|
62
|
Chris@1265
|
63 void isPeak_obvious() {
|
Chris@1265
|
64 Column c { 0.4, 0.5, 0.3 };
|
Chris@1265
|
65 QVERIFY(!C::isPeak(c, 0));
|
Chris@1265
|
66 QVERIFY(C::isPeak(c, 1));
|
Chris@1265
|
67 QVERIFY(!C::isPeak(c, 2));
|
Chris@1265
|
68 }
|
Chris@1265
|
69
|
Chris@1265
|
70 void isPeak_edges() {
|
Chris@1265
|
71 Column c { 0.5, 0.4, 0.3 };
|
Chris@1265
|
72 QVERIFY(C::isPeak(c, 0));
|
Chris@1265
|
73 QVERIFY(!C::isPeak(c, 1));
|
Chris@1265
|
74 QVERIFY(!C::isPeak(c, 2));
|
Chris@1265
|
75 QVERIFY(!C::isPeak(c, 3));
|
Chris@1265
|
76 QVERIFY(!C::isPeak(c, -1));
|
Chris@1265
|
77 c = { 1.4, 1.5 };
|
Chris@1265
|
78 QVERIFY(!C::isPeak(c, 0));
|
Chris@1265
|
79 QVERIFY(C::isPeak(c, 1));
|
Chris@1265
|
80 }
|
Chris@1265
|
81
|
Chris@1265
|
82 void isPeak_flat() {
|
Chris@1265
|
83 Column c { 0.0, 0.0, 0.0 };
|
Chris@1265
|
84 QVERIFY(C::isPeak(c, 0));
|
Chris@1265
|
85 QVERIFY(!C::isPeak(c, 1));
|
Chris@1265
|
86 QVERIFY(!C::isPeak(c, 2));
|
Chris@1265
|
87 }
|
Chris@1265
|
88
|
Chris@1265
|
89 void isPeak_mixedSign() {
|
Chris@1265
|
90 Column c { 0.4, -0.5, -0.3, -0.6, 0.1, -0.3 };
|
Chris@1265
|
91 QVERIFY(C::isPeak(c, 0));
|
Chris@1265
|
92 QVERIFY(!C::isPeak(c, 1));
|
Chris@1265
|
93 QVERIFY(C::isPeak(c, 2));
|
Chris@1265
|
94 QVERIFY(!C::isPeak(c, 3));
|
Chris@1265
|
95 QVERIFY(C::isPeak(c, 4));
|
Chris@1265
|
96 QVERIFY(!C::isPeak(c, 5));
|
Chris@1265
|
97 }
|
Chris@1265
|
98
|
Chris@1265
|
99 void isPeak_duplicate() {
|
Chris@1265
|
100 Column c({ 0.5, 0.5, 0.4, 0.4 });
|
Chris@1265
|
101 QVERIFY(C::isPeak(c, 0));
|
Chris@1265
|
102 QVERIFY(!C::isPeak(c, 1));
|
Chris@1265
|
103 QVERIFY(!C::isPeak(c, 2));
|
Chris@1265
|
104 QVERIFY(!C::isPeak(c, 3));
|
Chris@1265
|
105 c = { 0.4, 0.4, 0.5, 0.5 };
|
Chris@1265
|
106 QVERIFY(C::isPeak(c, 0)); // counterintuitive but necessary
|
Chris@1265
|
107 QVERIFY(!C::isPeak(c, 1));
|
Chris@1265
|
108 QVERIFY(C::isPeak(c, 2));
|
Chris@1265
|
109 QVERIFY(!C::isPeak(c, 3));
|
Chris@1265
|
110 }
|
Chris@1265
|
111
|
Chris@1265
|
112 void peakPick() {
|
Chris@1265
|
113 QCOMPARE(C::peakPick({}), Column());
|
Chris@1265
|
114 Column c({ 0.5, 0.5, 0.4, 0.4 });
|
Chris@1265
|
115 QCOMPARE(C::peakPick(c), Column({ 0.5, 0.0, 0.0, 0.0 }));
|
Chris@1265
|
116 c = Column({ 0.4, -0.5, -0.3, -0.6, 0.1, -0.3 });
|
Chris@1265
|
117 QCOMPARE(C::peakPick(c), Column({ 0.4, 0.0, -0.3, 0.0, 0.1, 0.0 }));
|
Chris@1265
|
118 }
|
Chris@1265
|
119
|
Chris@1265
|
120 void normalize_null() {
|
Chris@1265
|
121 QCOMPARE(C::normalize({}, ColumnNormalization::None), Column());
|
Chris@1265
|
122 QCOMPARE(C::normalize({}, ColumnNormalization::Sum1), Column());
|
Chris@1265
|
123 QCOMPARE(C::normalize({}, ColumnNormalization::Max1), Column());
|
Chris@1265
|
124 QCOMPARE(C::normalize({}, ColumnNormalization::Hybrid), Column());
|
Chris@1265
|
125 }
|
Chris@1265
|
126
|
Chris@1265
|
127 void normalize_none() {
|
Chris@1265
|
128 Column c { 1, 2, 3, 4 };
|
Chris@1265
|
129 QCOMPARE(C::normalize(c, ColumnNormalization::None), c);
|
Chris@1265
|
130 }
|
Chris@1265
|
131
|
Chris@1265
|
132 void normalize_sum1() {
|
Chris@1265
|
133 Column c { 1, 2, 4, 3 };
|
Chris@1265
|
134 QCOMPARE(C::normalize(c, ColumnNormalization::Sum1),
|
Chris@1265
|
135 Column({ 0.1, 0.2, 0.4, 0.3 }));
|
Chris@1265
|
136 }
|
Chris@1265
|
137
|
Chris@1265
|
138 void normalize_max1() {
|
Chris@1265
|
139 Column c { 4, 3, 2, 1 };
|
Chris@1265
|
140 QCOMPARE(C::normalize(c, ColumnNormalization::Max1),
|
Chris@1265
|
141 Column({ 1.0, 0.75, 0.5, 0.25 }));
|
Chris@1265
|
142 }
|
Chris@1265
|
143
|
Chris@1265
|
144 void normalize_hybrid() {
|
Chris@1265
|
145 // with max == 99, log10(max+1) == 2 so scale factor will be 2/99
|
Chris@1265
|
146 Column c { 22, 44, 99, 66 };
|
Chris@1265
|
147 QCOMPARE(C::normalize(c, ColumnNormalization::Hybrid),
|
Chris@1265
|
148 Column({ 44.0/99.0, 88.0/99.0, 2.0, 132.0/99.0 }));
|
Chris@1265
|
149 }
|
Chris@1265
|
150
|
Chris@1265
|
151
|
Chris@1265
|
152
|
Chris@1265
|
153 };
|
Chris@1265
|
154
|
Chris@1265
|
155 #endif
|
Chris@1265
|
156
|