comparison data/model/test/TestZoomConstraints.h @ 1552:05c3fbaec8ea

Introduce RelativelyFineZoomConstraint, which encodes more-or-less the scheme that was already used for the horizontal thumbwheel in the pane (which overrode the layers' own zoom constraints unless they said they couldn't support any other)
author Chris Cannam
date Wed, 10 Oct 2018 14:32:34 +0100
parents 2f3a77472c8c
children 616d3e8a250b
comparison
equal deleted inserted replaced
1551:4de4284d0596 1552:05c3fbaec8ea
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
18 #include "../PowerOfTwoZoomConstraint.h" 18 #include "../PowerOfTwoZoomConstraint.h"
19 #include "../PowerOfSqrtTwoZoomConstraint.h" 19 #include "../PowerOfSqrtTwoZoomConstraint.h"
20 #include "../RelativelyFineZoomConstraint.h"
20 21
21 #include <QObject> 22 #include <QObject>
22 #include <QtTest> 23 #include <QtTest>
23 #include <QDir> 24 #include <QDir>
24 25
27 using namespace std; 28 using namespace std;
28 29
29 class TestZoomConstraints : public QObject 30 class TestZoomConstraints : public QObject
30 { 31 {
31 Q_OBJECT 32 Q_OBJECT
33
34 string roundingName(ZoomConstraint::RoundingDirection dir) {
35 switch (dir) {
36 case ZoomConstraint::RoundDown: return "RoundDown";
37 case ZoomConstraint::RoundUp: return "RoundUp";
38 case ZoomConstraint::RoundNearest: return "RoundNearest";
39 }
40 return "<?>";
41 }
42
43 void compare(ZoomLevel zin,
44 ZoomConstraint::RoundingDirection dir,
45 ZoomLevel zobt,
46 ZoomLevel zexp) {
47 if (zexp.level == 1) {
48 // A zoom level of "1 pixel per frame" is not considered
49 // canonical - it should be "1 frame per pixel"
50 zexp.zone = ZoomLevel::FramesPerPixel;
51 }
52 if (zobt == zexp) {
53 return;
54 } else {
55 cerr << "For input " << zin << " and rounding direction "
56 << roundingName(dir)
57 << ", expected output " << zexp << " but obtained " << zobt
58 << endl;
59 QCOMPARE(zobt, zexp);
60 }
61 }
32 62
33 void checkFpp(const ZoomConstraint &c, 63 void checkFpp(const ZoomConstraint &c,
34 ZoomConstraint::RoundingDirection dir, 64 ZoomConstraint::RoundingDirection dir,
35 int n, 65 int n,
36 int expected) { 66 int expected) {
37 QCOMPARE(c.getNearestZoomLevel(ZoomLevel(ZoomLevel::FramesPerPixel, n), 67 ZoomLevel zin(ZoomLevel::FramesPerPixel, n);
38 dir), 68 ZoomLevel zexp(ZoomLevel::FramesPerPixel, expected);
39 ZoomLevel(ZoomLevel::FramesPerPixel, expected)); 69 ZoomLevel zobt(c.getNearestZoomLevel(zin, dir));
40 } 70 compare(zin, dir, zobt, zexp);
41 71 }
72
73 void checkPpf(const ZoomConstraint &c,
74 ZoomConstraint::RoundingDirection dir,
75 int n,
76 int expected) {
77 ZoomLevel zin(ZoomLevel::PixelsPerFrame, n);
78 ZoomLevel zexp(ZoomLevel::PixelsPerFrame, expected);
79 ZoomLevel zobt(c.getNearestZoomLevel(zin, dir));
80 compare(zin, dir, zobt, zexp);
81 }
82
83 void checkBoth(const ZoomConstraint &c,
84 ZoomConstraint::RoundingDirection dir,
85 int n,
86 int expected) {
87 checkFpp(c, dir, n, expected);
88 checkPpf(c, dir, n, expected);
89 }
90
91 void checkMaxMin(const ZoomConstraint &c,
92 ZoomConstraint::RoundingDirection dir) {
93 auto max = c.getMaxZoomLevel();
94 compare(max, dir,
95 c.getNearestZoomLevel(max, dir), max);
96 compare(max.incremented(), dir,
97 c.getNearestZoomLevel(max.incremented(), dir), max);
98 auto min = c.getMinZoomLevel();
99 compare(min, dir,
100 c.getNearestZoomLevel(min, dir), min);
101 compare(min.decremented(), dir,
102 c.getNearestZoomLevel(min.decremented(), dir), min);
103 }
104
105 const static ZoomConstraint::RoundingDirection up = ZoomConstraint::RoundUp;
106 const static ZoomConstraint::RoundingDirection down = ZoomConstraint::RoundDown;
107 const static ZoomConstraint::RoundingDirection nearest = ZoomConstraint::RoundNearest;
108
42 private slots: 109 private slots:
43 void unconstrainedNearest() { 110 void unconstrainedNearest() {
44 ZoomConstraint c; 111 ZoomConstraint c;
45 checkFpp(c, ZoomConstraint::RoundNearest, 1, 1); 112 checkBoth(c, nearest, 1, 1);
46 checkFpp(c, ZoomConstraint::RoundNearest, 2, 2); 113 checkBoth(c, nearest, 2, 2);
47 checkFpp(c, ZoomConstraint::RoundNearest, 3, 3); 114 checkBoth(c, nearest, 3, 3);
48 checkFpp(c, ZoomConstraint::RoundNearest, 4, 4); 115 checkBoth(c, nearest, 4, 4);
49 checkFpp(c, ZoomConstraint::RoundNearest, 20, 20); 116 checkBoth(c, nearest, 20, 20);
50 checkFpp(c, ZoomConstraint::RoundNearest, 32, 32); 117 checkBoth(c, nearest, 32, 32);
51 auto max = c.getMaxZoomLevel(); 118 auto max = c.getMaxZoomLevel();
52 QCOMPARE(c.getNearestZoomLevel(max), max); 119 QCOMPARE(c.getNearestZoomLevel(max), max);
53 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max); 120 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
54 } 121 }
55 122
56 void unconstrainedUp() { 123 void unconstrainedUp() {
57 ZoomConstraint c; 124 ZoomConstraint c;
58 checkFpp(c, ZoomConstraint::RoundUp, 1, 1); 125 checkBoth(c, up, 1, 1);
59 checkFpp(c, ZoomConstraint::RoundUp, 2, 2); 126 checkBoth(c, up, 2, 2);
60 checkFpp(c, ZoomConstraint::RoundUp, 3, 3); 127 checkBoth(c, up, 3, 3);
61 checkFpp(c, ZoomConstraint::RoundUp, 4, 4); 128 checkBoth(c, up, 4, 4);
62 checkFpp(c, ZoomConstraint::RoundUp, 20, 20); 129 checkBoth(c, up, 20, 20);
63 checkFpp(c, ZoomConstraint::RoundUp, 32, 32); 130 checkBoth(c, up, 32, 32);
64 auto max = c.getMaxZoomLevel(); 131 auto max = c.getMaxZoomLevel();
65 QCOMPARE(c.getNearestZoomLevel(max, 132 QCOMPARE(c.getNearestZoomLevel(max, up), max);
66 ZoomConstraint::RoundUp), max); 133 QCOMPARE(c.getNearestZoomLevel(max.incremented(), up), max);
67 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
68 ZoomConstraint::RoundUp), max);
69 } 134 }
70 135
71 void unconstrainedDown() { 136 void unconstrainedDown() {
72 ZoomConstraint c; 137 ZoomConstraint c;
73 checkFpp(c, ZoomConstraint::RoundDown, 1, 1); 138 checkBoth(c, down, 1, 1);
74 checkFpp(c, ZoomConstraint::RoundDown, 2, 2); 139 checkBoth(c, down, 2, 2);
75 checkFpp(c, ZoomConstraint::RoundDown, 3, 3); 140 checkBoth(c, down, 3, 3);
76 checkFpp(c, ZoomConstraint::RoundDown, 4, 4); 141 checkBoth(c, down, 4, 4);
77 checkFpp(c, ZoomConstraint::RoundDown, 20, 20); 142 checkBoth(c, down, 20, 20);
78 checkFpp(c, ZoomConstraint::RoundDown, 32, 32); 143 checkBoth(c, down, 32, 32);
79 auto max = c.getMaxZoomLevel(); 144 auto max = c.getMaxZoomLevel();
80 QCOMPARE(c.getNearestZoomLevel(max, 145 QCOMPARE(c.getNearestZoomLevel(max, down), max);
81 ZoomConstraint::RoundDown), max); 146 QCOMPARE(c.getNearestZoomLevel(max.incremented(), down), max);
82 QCOMPARE(c.getNearestZoomLevel(max.incremented(),
83 ZoomConstraint::RoundDown), max);
84 } 147 }
85 148
86 void powerOfTwoNearest() { 149 void powerOfTwoNearest() {
87 PowerOfTwoZoomConstraint c; 150 PowerOfTwoZoomConstraint c;
88 checkFpp(c, ZoomConstraint::RoundNearest, 1, 1); 151 checkBoth(c, nearest, 1, 1);
89 checkFpp(c, ZoomConstraint::RoundNearest, 2, 2); 152 checkBoth(c, nearest, 2, 2);
90 checkFpp(c, ZoomConstraint::RoundNearest, 3, 2); 153 checkBoth(c, nearest, 3, 2);
91 checkFpp(c, ZoomConstraint::RoundNearest, 4, 4); 154 checkBoth(c, nearest, 4, 4);
92 checkFpp(c, ZoomConstraint::RoundNearest, 20, 16); 155 checkBoth(c, nearest, 20, 16);
93 checkFpp(c, ZoomConstraint::RoundNearest, 23, 16); 156 checkBoth(c, nearest, 23, 16);
94 checkFpp(c, ZoomConstraint::RoundNearest, 24, 16); 157 checkBoth(c, nearest, 24, 16);
95 checkFpp(c, ZoomConstraint::RoundNearest, 25, 32); 158 checkBoth(c, nearest, 25, 32);
96 auto max = c.getMaxZoomLevel(); 159 auto max = c.getMaxZoomLevel();
97 QCOMPARE(c.getNearestZoomLevel(max), max); 160 QCOMPARE(c.getNearestZoomLevel(max), max);
98 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max); 161 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
99 } 162 }
100 163
101 void powerOfTwoUp() { 164 void powerOfTwoUp() {
102 PowerOfTwoZoomConstraint c; 165 PowerOfTwoZoomConstraint c;
103 checkFpp(c, ZoomConstraint::RoundUp, 1, 1); 166 checkBoth(c, up, 1, 1);
104 checkFpp(c, ZoomConstraint::RoundUp, 2, 2); 167 checkBoth(c, up, 2, 2);
105 checkFpp(c, ZoomConstraint::RoundUp, 3, 4); 168 checkFpp(c, up, 3, 4);
106 checkFpp(c, ZoomConstraint::RoundUp, 4, 4); 169 checkPpf(c, up, 3, 2);
107 checkFpp(c, ZoomConstraint::RoundUp, 20, 32); 170 checkBoth(c, up, 4, 4);
108 checkFpp(c, ZoomConstraint::RoundUp, 32, 32); 171 checkFpp(c, up, 20, 32);
109 checkFpp(c, ZoomConstraint::RoundUp, 33, 64); 172 checkPpf(c, up, 20, 16);
110 auto max = c.getMaxZoomLevel(); 173 checkBoth(c, up, 32, 32);
111 QCOMPARE(c.getNearestZoomLevel(max, 174 checkFpp(c, up, 33, 64);
112 ZoomConstraint::RoundUp), max); 175 checkPpf(c, up, 33, 32);
113 QCOMPARE(c.getNearestZoomLevel(max.incremented(), 176 checkMaxMin(c, up);
114 ZoomConstraint::RoundUp), max);
115 } 177 }
116 178
117 void powerOfTwoDown() { 179 void powerOfTwoDown() {
118 PowerOfTwoZoomConstraint c; 180 PowerOfTwoZoomConstraint c;
119 checkFpp(c, ZoomConstraint::RoundDown, 1, 1); 181 checkBoth(c, down, 1, 1);
120 checkFpp(c, ZoomConstraint::RoundDown, 2, 2); 182 checkBoth(c, down, 2, 2);
121 checkFpp(c, ZoomConstraint::RoundDown, 3, 2); 183 checkFpp(c, down, 3, 2);
122 checkFpp(c, ZoomConstraint::RoundDown, 4, 4); 184 checkPpf(c, down, 3, 4);
123 checkFpp(c, ZoomConstraint::RoundDown, 20, 16); 185 checkBoth(c, down, 4, 4);
124 checkFpp(c, ZoomConstraint::RoundDown, 32, 32); 186 checkFpp(c, down, 20, 16);
125 checkFpp(c, ZoomConstraint::RoundDown, 33, 32); 187 checkPpf(c, down, 20, 32);
126 auto max = c.getMaxZoomLevel(); 188 checkBoth(c, down, 32, 32);
127 QCOMPARE(c.getNearestZoomLevel(max, 189 checkFpp(c, down, 33, 32);
128 ZoomConstraint::RoundDown), max); 190 checkPpf(c, down, 33, 64);
129 QCOMPARE(c.getNearestZoomLevel(max.incremented(), 191 checkMaxMin(c, down);
130 ZoomConstraint::RoundDown), max);
131 } 192 }
132 193
133 void powerOfSqrtTwoNearest() { 194 void powerOfSqrtTwoNearest() {
134 PowerOfSqrtTwoZoomConstraint c; 195 PowerOfSqrtTwoZoomConstraint c;
135 checkFpp(c, ZoomConstraint::RoundNearest, 1, 1); 196 checkBoth(c, nearest, 1, 1);
136 checkFpp(c, ZoomConstraint::RoundNearest, 2, 2); 197 checkBoth(c, nearest, 2, 2);
137 checkFpp(c, ZoomConstraint::RoundNearest, 3, 2); 198 checkBoth(c, nearest, 3, 2);
138 checkFpp(c, ZoomConstraint::RoundNearest, 4, 4); 199 checkBoth(c, nearest, 4, 4);
139 checkFpp(c, ZoomConstraint::RoundNearest, 18, 16); 200 checkBoth(c, nearest, 18, 16);
140 checkFpp(c, ZoomConstraint::RoundNearest, 19, 16); 201 checkBoth(c, nearest, 19, 16);
141 checkFpp(c, ZoomConstraint::RoundNearest, 20, 22); 202 checkBoth(c, nearest, 20, 22);
142 checkFpp(c, ZoomConstraint::RoundNearest, 23, 22); 203 checkBoth(c, nearest, 23, 22);
143 checkFpp(c, ZoomConstraint::RoundNearest, 28, 32); 204 checkBoth(c, nearest, 28, 32);
144 // PowerOfSqrtTwoZoomConstraint makes an effort to ensure 205 // PowerOfSqrtTwoZoomConstraint makes an effort to ensure
145 // bigger numbers get rounded to a multiple of something 206 // bigger numbers get rounded to a multiple of something
146 // simple (64 or 90 depending on whether they are power-of-two 207 // simple (64 or 90 depending on whether they are power-of-two
147 // or power-of-sqrt-two types) 208 // or power-of-sqrt-two types)
148 checkFpp(c, ZoomConstraint::RoundNearest, 800, 720); 209 checkBoth(c, nearest, 350, 360);
149 checkFpp(c, ZoomConstraint::RoundNearest, 1023, 1024); 210 // The most extreme level available in ppf mode
150 checkFpp(c, ZoomConstraint::RoundNearest, 1024, 1024); 211 // (getMinZoomLevel()) is currently 512, so these bigger
151 checkFpp(c, ZoomConstraint::RoundNearest, 1024, 1024); 212 // numbers will only happen in fpp mode
152 checkFpp(c, ZoomConstraint::RoundNearest, 1025, 1024); 213 checkFpp(c, nearest, 800, 720);
153 auto max = c.getMaxZoomLevel(); 214 checkFpp(c, nearest, 1023, 1024);
154 QCOMPARE(c.getNearestZoomLevel(max), max); 215 checkFpp(c, nearest, 1024, 1024);
155 QCOMPARE(c.getNearestZoomLevel(max.incremented()), max); 216 checkFpp(c, nearest, 1024, 1024);
217 checkFpp(c, nearest, 1025, 1024);
218 checkPpf(c, nearest, 800, 512);
219 checkPpf(c, nearest, 1025, 512);
220 checkMaxMin(c, nearest);
156 } 221 }
157 222
158 void powerOfSqrtTwoUp() { 223 void powerOfSqrtTwoUp() {
159 PowerOfSqrtTwoZoomConstraint c; 224 PowerOfSqrtTwoZoomConstraint c;
160 checkFpp(c, ZoomConstraint::RoundUp, 1, 1); 225 checkBoth(c, up, 1, 1);
161 checkFpp(c, ZoomConstraint::RoundUp, 2, 2); 226 checkBoth(c, up, 2, 2);
162 checkFpp(c, ZoomConstraint::RoundUp, 3, 4); 227 checkFpp(c, up, 3, 4);
163 checkFpp(c, ZoomConstraint::RoundUp, 4, 4); 228 checkPpf(c, up, 3, 2);
164 checkFpp(c, ZoomConstraint::RoundUp, 18, 22); 229 checkBoth(c, up, 4, 4);
165 checkFpp(c, ZoomConstraint::RoundUp, 22, 22); 230 checkFpp(c, up, 18, 22);
166 checkFpp(c, ZoomConstraint::RoundUp, 23, 32); 231 checkPpf(c, up, 18, 16);
167 checkFpp(c, ZoomConstraint::RoundUp, 800, 1024); 232 checkBoth(c, up, 22, 22);
168 checkFpp(c, ZoomConstraint::RoundUp, 1023, 1024); 233 checkFpp(c, up, 23, 32);
169 checkFpp(c, ZoomConstraint::RoundUp, 1024, 1024); 234 checkPpf(c, up, 23, 22);
170 // see comment above 235 // see comments above
171 checkFpp(c, ZoomConstraint::RoundUp, 1025, 1440); 236 checkFpp(c, up, 800, 1024);
172 auto max = c.getMaxZoomLevel(); 237 checkFpp(c, up, 1023, 1024);
173 QCOMPARE(c.getNearestZoomLevel(max, 238 checkFpp(c, up, 1024, 1024);
174 ZoomConstraint::RoundUp), max); 239 checkFpp(c, up, 1025, 1440);
175 QCOMPARE(c.getNearestZoomLevel(max.incremented(), 240 checkPpf(c, up, 300, 256);
176 ZoomConstraint::RoundUp), max); 241 checkPpf(c, up, 800, 512);
242 checkPpf(c, up, 1600, 512);
243 checkMaxMin(c, up);
177 } 244 }
178 245
179 void powerOfSqrtTwoDown() { 246 void powerOfSqrtTwoDown() {
180 PowerOfSqrtTwoZoomConstraint c; 247 PowerOfSqrtTwoZoomConstraint c;
181 checkFpp(c, ZoomConstraint::RoundDown, 1, 1); 248 checkBoth(c, down, 1, 1);
182 checkFpp(c, ZoomConstraint::RoundDown, 2, 2); 249 checkBoth(c, down, 2, 2);
183 checkFpp(c, ZoomConstraint::RoundDown, 3, 2); 250 checkFpp(c, down, 3, 2);
184 checkFpp(c, ZoomConstraint::RoundDown, 4, 4); 251 checkPpf(c, down, 3, 4);
185 checkFpp(c, ZoomConstraint::RoundDown, 18, 16); 252 checkBoth(c, down, 4, 4);
186 checkFpp(c, ZoomConstraint::RoundDown, 22, 22); 253 checkFpp(c, down, 18, 16);
187 checkFpp(c, ZoomConstraint::RoundDown, 23, 22); 254 checkPpf(c, down, 18, 22);
188 // see comment above 255 checkBoth(c, down, 22, 22);
189 checkFpp(c, ZoomConstraint::RoundDown, 800, 720); 256 checkFpp(c, down, 23, 22);
190 checkFpp(c, ZoomConstraint::RoundDown, 1023, 720); 257 checkPpf(c, down, 23, 32);
191 checkFpp(c, ZoomConstraint::RoundDown, 1024, 1024); 258 // see comments above
192 checkFpp(c, ZoomConstraint::RoundDown, 1025, 1024); 259 checkFpp(c, down, 800, 720);
193 auto max = c.getMaxZoomLevel(); 260 checkFpp(c, down, 1023, 720);
194 QCOMPARE(c.getNearestZoomLevel(max, 261 checkFpp(c, down, 1024, 1024);
195 ZoomConstraint::RoundDown), max); 262 checkFpp(c, down, 1025, 1024);
196 QCOMPARE(c.getNearestZoomLevel(max.incremented(), 263 checkPpf(c, down, 300, 360);
197 ZoomConstraint::RoundDown), max); 264 checkPpf(c, down, 800, 512);
265 checkPpf(c, down, 1600, 512);
266 checkMaxMin(c, down);
267 }
268
269 void relativelyFineNearest() {
270 RelativelyFineZoomConstraint c;
271 checkBoth(c, nearest, 1, 1);
272 checkBoth(c, nearest, 2, 2);
273 checkBoth(c, nearest, 3, 3);
274 checkBoth(c, nearest, 4, 4);
275 checkBoth(c, nearest, 20, 20);
276 checkBoth(c, nearest, 33, 32);
277 checkBoth(c, nearest, 59, 56);
278 checkBoth(c, nearest, 69, 72);
279 checkBoth(c, nearest, 121, 128);
280 checkMaxMin(c, nearest);
281 }
282
283 void relativelyFineUp() {
284 RelativelyFineZoomConstraint c;
285 checkBoth(c, up, 1, 1);
286 checkBoth(c, up, 2, 2);
287 checkBoth(c, up, 3, 3);
288 checkBoth(c, up, 4, 4);
289 checkBoth(c, up, 20, 20);
290 checkFpp(c, up, 33, 36);
291 checkPpf(c, up, 33, 32);
292 checkFpp(c, up, 59, 64);
293 checkPpf(c, up, 59, 56);
294 checkFpp(c, up, 69, 72);
295 checkPpf(c, up, 69, 64);
296 checkFpp(c, up, 121, 128);
297 checkPpf(c, up, 121, 112);
298 checkMaxMin(c, up);
299 }
300
301 void relativelyFineDown() {
302 RelativelyFineZoomConstraint c;
303 checkBoth(c, down, 1, 1);
304 checkBoth(c, down, 2, 2);
305 checkBoth(c, down, 3, 3);
306 checkBoth(c, down, 4, 4);
307 checkBoth(c, down, 20, 20);
308 checkFpp(c, down, 33, 32);
309 checkPpf(c, down, 33, 36);
310 checkFpp(c, down, 59, 56);
311 checkPpf(c, down, 59, 64);
312 checkFpp(c, down, 69, 64);
313 checkPpf(c, down, 69, 72);
314 checkFpp(c, down, 121, 112);
315 checkPpf(c, down, 121, 128);
316 checkMaxMin(c, down);
198 } 317 }
199 }; 318 };
200 319
201 #endif 320 #endif
202 321