comparison data/model/test/TestZoomConstraints.h @ 1551:4de4284d0596

Merge from branch zoom
author Chris Cannam
date Wed, 10 Oct 2018 08:44:15 +0100
parents 2f3a77472c8c
children 05c3fbaec8ea
comparison
equal deleted inserted replaced
1548:51d6551d5244 1551:4de4284d0596
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 2
3 /* 3 /*
4 Sonic Visualiser 4 Sonic Visualiser
5 An audio file viewer and annotation editor. 5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London. 6 Centre for Digital Music, Queen Mary, University of London.
7 7
8 This program is free software; you can redistribute it and/or 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 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 10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file 11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information. 12 COPYING included with this distribution for more information.
13 */ 13 */
14 14
15 #ifndef TEST_ZOOM_CONSTRAINTS_H 15 #ifndef TEST_ZOOM_CONSTRAINTS_H
16 #define TEST_ZOOM_CONSTRAINTS_H 16 #define TEST_ZOOM_CONSTRAINTS_H
17 17
28 28
29 class TestZoomConstraints : public QObject 29 class TestZoomConstraints : public QObject
30 { 30 {
31 Q_OBJECT 31 Q_OBJECT
32 32
33 void checkFpp(const ZoomConstraint &c,
34 ZoomConstraint::RoundingDirection dir,
35 int n,
36 int expected) {
37 QCOMPARE(c.getNearestZoomLevel(ZoomLevel(ZoomLevel::FramesPerPixel, n),
38 dir),
39 ZoomLevel(ZoomLevel::FramesPerPixel, expected));
40 }
41
33 private slots: 42 private slots:
34 void unconstrainedNearest() { 43 void unconstrainedNearest() {
35 ZoomConstraint c; 44 ZoomConstraint c;
36 QCOMPARE(c.getNearestBlockSize(1), 1); 45 checkFpp(c, ZoomConstraint::RoundNearest, 1, 1);
37 QCOMPARE(c.getNearestBlockSize(2), 2); 46 checkFpp(c, ZoomConstraint::RoundNearest, 2, 2);
38 QCOMPARE(c.getNearestBlockSize(3), 3); 47 checkFpp(c, ZoomConstraint::RoundNearest, 3, 3);
39 QCOMPARE(c.getNearestBlockSize(4), 4); 48 checkFpp(c, ZoomConstraint::RoundNearest, 4, 4);
40 QCOMPARE(c.getNearestBlockSize(20), 20); 49 checkFpp(c, ZoomConstraint::RoundNearest, 20, 20);
41 QCOMPARE(c.getNearestBlockSize(23), 23); 50 checkFpp(c, ZoomConstraint::RoundNearest, 32, 32);
42 int max = c.getMaxZoomLevel(); 51 auto max = c.getMaxZoomLevel();
43 QCOMPARE(c.getNearestBlockSize(max), max); 52 QCOMPARE(c.getNearestZoomLevel(max), max);
44 QCOMPARE(c.getNearestBlockSize(max+1), max); 53 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
45 } 54 }
46 55
47 void unconstrainedUp() { 56 void unconstrainedUp() {
48 ZoomConstraint c; 57 ZoomConstraint c;
49 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); 58 checkFpp(c, ZoomConstraint::RoundUp, 1, 1);
50 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); 59 checkFpp(c, ZoomConstraint::RoundUp, 2, 2);
51 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 3); 60 checkFpp(c, ZoomConstraint::RoundUp, 3, 3);
52 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); 61 checkFpp(c, ZoomConstraint::RoundUp, 4, 4);
53 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 20); 62 checkFpp(c, ZoomConstraint::RoundUp, 20, 20);
54 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32); 63 checkFpp(c, ZoomConstraint::RoundUp, 32, 32);
55 int max = c.getMaxZoomLevel(); 64 auto max = c.getMaxZoomLevel();
56 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); 65 QCOMPARE(c.getNearestZoomLevel(max,
57 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); 66 ZoomConstraint::RoundUp), max);
67 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
68 ZoomConstraint::RoundUp), max);
58 } 69 }
59 70
60 void unconstrainedDown() { 71 void unconstrainedDown() {
61 ZoomConstraint c; 72 ZoomConstraint c;
62 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); 73 checkFpp(c, ZoomConstraint::RoundDown, 1, 1);
63 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); 74 checkFpp(c, ZoomConstraint::RoundDown, 2, 2);
64 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 3); 75 checkFpp(c, ZoomConstraint::RoundDown, 3, 3);
65 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); 76 checkFpp(c, ZoomConstraint::RoundDown, 4, 4);
66 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 20); 77 checkFpp(c, ZoomConstraint::RoundDown, 20, 20);
67 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32); 78 checkFpp(c, ZoomConstraint::RoundDown, 32, 32);
68 int max = c.getMaxZoomLevel(); 79 auto max = c.getMaxZoomLevel();
69 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); 80 QCOMPARE(c.getNearestZoomLevel(max,
70 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); 81 ZoomConstraint::RoundDown), max);
82 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
83 ZoomConstraint::RoundDown), max);
71 } 84 }
72 85
73 void powerOfTwoNearest() { 86 void powerOfTwoNearest() {
74 PowerOfTwoZoomConstraint c; 87 PowerOfTwoZoomConstraint c;
75 QCOMPARE(c.getNearestBlockSize(1), 1); 88 checkFpp(c, ZoomConstraint::RoundNearest, 1, 1);
76 QCOMPARE(c.getNearestBlockSize(2), 2); 89 checkFpp(c, ZoomConstraint::RoundNearest, 2, 2);
77 QCOMPARE(c.getNearestBlockSize(3), 2); 90 checkFpp(c, ZoomConstraint::RoundNearest, 3, 2);
78 QCOMPARE(c.getNearestBlockSize(4), 4); 91 checkFpp(c, ZoomConstraint::RoundNearest, 4, 4);
79 QCOMPARE(c.getNearestBlockSize(20), 16); 92 checkFpp(c, ZoomConstraint::RoundNearest, 20, 16);
80 QCOMPARE(c.getNearestBlockSize(23), 16); 93 checkFpp(c, ZoomConstraint::RoundNearest, 23, 16);
81 QCOMPARE(c.getNearestBlockSize(24), 16); 94 checkFpp(c, ZoomConstraint::RoundNearest, 24, 16);
82 QCOMPARE(c.getNearestBlockSize(25), 32); 95 checkFpp(c, ZoomConstraint::RoundNearest, 25, 32);
83 int max = c.getMaxZoomLevel(); 96 auto max = c.getMaxZoomLevel();
84 QCOMPARE(c.getNearestBlockSize(max), max); 97 QCOMPARE(c.getNearestZoomLevel(max), max);
85 QCOMPARE(c.getNearestBlockSize(max+1), max); 98 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
86 } 99 }
87 100
88 void powerOfTwoUp() { 101 void powerOfTwoUp() {
89 PowerOfTwoZoomConstraint c; 102 PowerOfTwoZoomConstraint c;
90 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); 103 checkFpp(c, ZoomConstraint::RoundUp, 1, 1);
91 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); 104 checkFpp(c, ZoomConstraint::RoundUp, 2, 2);
92 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4); 105 checkFpp(c, ZoomConstraint::RoundUp, 3, 4);
93 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); 106 checkFpp(c, ZoomConstraint::RoundUp, 4, 4);
94 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 32); 107 checkFpp(c, ZoomConstraint::RoundUp, 20, 32);
95 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32); 108 checkFpp(c, ZoomConstraint::RoundUp, 32, 32);
96 QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundUp), 64); 109 checkFpp(c, ZoomConstraint::RoundUp, 33, 64);
97 int max = c.getMaxZoomLevel(); 110 auto max = c.getMaxZoomLevel();
98 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); 111 QCOMPARE(c.getNearestZoomLevel(max,
99 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); 112 ZoomConstraint::RoundUp), max);
113 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
114 ZoomConstraint::RoundUp), max);
100 } 115 }
101 116
102 void powerOfTwoDown() { 117 void powerOfTwoDown() {
103 PowerOfTwoZoomConstraint c; 118 PowerOfTwoZoomConstraint c;
104 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); 119 checkFpp(c, ZoomConstraint::RoundDown, 1, 1);
105 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); 120 checkFpp(c, ZoomConstraint::RoundDown, 2, 2);
106 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2); 121 checkFpp(c, ZoomConstraint::RoundDown, 3, 2);
107 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); 122 checkFpp(c, ZoomConstraint::RoundDown, 4, 4);
108 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 16); 123 checkFpp(c, ZoomConstraint::RoundDown, 20, 16);
109 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32); 124 checkFpp(c, ZoomConstraint::RoundDown, 32, 32);
110 QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundDown), 32); 125 checkFpp(c, ZoomConstraint::RoundDown, 33, 32);
111 int max = c.getMaxZoomLevel(); 126 auto max = c.getMaxZoomLevel();
112 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); 127 QCOMPARE(c.getNearestZoomLevel(max,
113 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); 128 ZoomConstraint::RoundDown), max);
129 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
130 ZoomConstraint::RoundDown), max);
114 } 131 }
115 132
116 void powerOfSqrtTwoNearest() { 133 void powerOfSqrtTwoNearest() {
117 PowerOfSqrtTwoZoomConstraint c; 134 PowerOfSqrtTwoZoomConstraint c;
118 QCOMPARE(c.getNearestBlockSize(1), 1); 135 checkFpp(c, ZoomConstraint::RoundNearest, 1, 1);
119 QCOMPARE(c.getNearestBlockSize(2), 2); 136 checkFpp(c, ZoomConstraint::RoundNearest, 2, 2);
120 QCOMPARE(c.getNearestBlockSize(3), 2); 137 checkFpp(c, ZoomConstraint::RoundNearest, 3, 2);
121 QCOMPARE(c.getNearestBlockSize(4), 4); 138 checkFpp(c, ZoomConstraint::RoundNearest, 4, 4);
122 QCOMPARE(c.getNearestBlockSize(18), 16); 139 checkFpp(c, ZoomConstraint::RoundNearest, 18, 16);
123 QCOMPARE(c.getNearestBlockSize(19), 16); 140 checkFpp(c, ZoomConstraint::RoundNearest, 19, 16);
124 QCOMPARE(c.getNearestBlockSize(20), 22); 141 checkFpp(c, ZoomConstraint::RoundNearest, 20, 22);
125 QCOMPARE(c.getNearestBlockSize(23), 22); 142 checkFpp(c, ZoomConstraint::RoundNearest, 23, 22);
126 QCOMPARE(c.getNearestBlockSize(28), 32); 143 checkFpp(c, ZoomConstraint::RoundNearest, 28, 32);
127 // PowerOfSqrtTwoZoomConstraint makes an effort to ensure 144 // PowerOfSqrtTwoZoomConstraint makes an effort to ensure
128 // bigger numbers get rounded to a multiple of something 145 // bigger numbers get rounded to a multiple of something
129 // simple (64 or 90 depending on whether they are power-of-two 146 // simple (64 or 90 depending on whether they are power-of-two
130 // or power-of-sqrt-two types) 147 // or power-of-sqrt-two types)
131 QCOMPARE(c.getNearestBlockSize(800), 720); 148 checkFpp(c, ZoomConstraint::RoundNearest, 800, 720);
132 QCOMPARE(c.getNearestBlockSize(1023), 1024); 149 checkFpp(c, ZoomConstraint::RoundNearest, 1023, 1024);
133 QCOMPARE(c.getNearestBlockSize(1024), 1024); 150 checkFpp(c, ZoomConstraint::RoundNearest, 1024, 1024);
134 QCOMPARE(c.getNearestBlockSize(1025), 1024); 151 checkFpp(c, ZoomConstraint::RoundNearest, 1024, 1024);
135 int max = c.getMaxZoomLevel(); 152 checkFpp(c, ZoomConstraint::RoundNearest, 1025, 1024);
136 QCOMPARE(c.getNearestBlockSize(max), max); 153 auto max = c.getMaxZoomLevel();
137 QCOMPARE(c.getNearestBlockSize(max+1), max); 154 QCOMPARE(c.getNearestZoomLevel(max), max);
155 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
138 } 156 }
139 157
140 void powerOfSqrtTwoUp() { 158 void powerOfSqrtTwoUp() {
141 PowerOfSqrtTwoZoomConstraint c; 159 PowerOfSqrtTwoZoomConstraint c;
142 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); 160 checkFpp(c, ZoomConstraint::RoundUp, 1, 1);
143 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); 161 checkFpp(c, ZoomConstraint::RoundUp, 2, 2);
144 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4); 162 checkFpp(c, ZoomConstraint::RoundUp, 3, 4);
145 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); 163 checkFpp(c, ZoomConstraint::RoundUp, 4, 4);
146 QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundUp), 22); 164 checkFpp(c, ZoomConstraint::RoundUp, 18, 22);
147 QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundUp), 22); 165 checkFpp(c, ZoomConstraint::RoundUp, 22, 22);
148 QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundUp), 32); 166 checkFpp(c, ZoomConstraint::RoundUp, 23, 32);
149 QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundUp), 1024); 167 checkFpp(c, ZoomConstraint::RoundUp, 800, 1024);
150 QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundUp), 1024); 168 checkFpp(c, ZoomConstraint::RoundUp, 1023, 1024);
151 QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundUp), 1024); 169 checkFpp(c, ZoomConstraint::RoundUp, 1024, 1024);
152 // see comment above 170 // see comment above
153 QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundUp), 1440); 171 checkFpp(c, ZoomConstraint::RoundUp, 1025, 1440);
154 int max = c.getMaxZoomLevel(); 172 auto max = c.getMaxZoomLevel();
155 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); 173 QCOMPARE(c.getNearestZoomLevel(max,
156 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); 174 ZoomConstraint::RoundUp), max);
175 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
176 ZoomConstraint::RoundUp), max);
157 } 177 }
158 178
159 void powerOfSqrtTwoDown() { 179 void powerOfSqrtTwoDown() {
160 PowerOfSqrtTwoZoomConstraint c; 180 PowerOfSqrtTwoZoomConstraint c;
161 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); 181 checkFpp(c, ZoomConstraint::RoundDown, 1, 1);
162 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); 182 checkFpp(c, ZoomConstraint::RoundDown, 2, 2);
163 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2); 183 checkFpp(c, ZoomConstraint::RoundDown, 3, 2);
164 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); 184 checkFpp(c, ZoomConstraint::RoundDown, 4, 4);
165 QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundDown), 16); 185 checkFpp(c, ZoomConstraint::RoundDown, 18, 16);
166 QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundDown), 22); 186 checkFpp(c, ZoomConstraint::RoundDown, 22, 22);
167 QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundDown), 22); 187 checkFpp(c, ZoomConstraint::RoundDown, 23, 22);
168 // see comment above 188 // see comment above
169 QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundDown), 720); 189 checkFpp(c, ZoomConstraint::RoundDown, 800, 720);
170 QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundDown), 720); 190 checkFpp(c, ZoomConstraint::RoundDown, 1023, 720);
171 QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundDown), 1024); 191 checkFpp(c, ZoomConstraint::RoundDown, 1024, 1024);
172 QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundDown), 1024); 192 checkFpp(c, ZoomConstraint::RoundDown, 1025, 1024);
173 int max = c.getMaxZoomLevel(); 193 auto max = c.getMaxZoomLevel();
174 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); 194 QCOMPARE(c.getNearestZoomLevel(max,
175 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); 195 ZoomConstraint::RoundDown), max);
196 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
197 ZoomConstraint::RoundDown), max);
176 } 198 }
177 }; 199 };
178 200
179 #endif 201 #endif
180 202