comparison data/model/test/TestZoomConstraints.h @ 1528:a7485c1bdba5

Tests and a couple of minor fixes for zoom constraints
author Chris Cannam
date Tue, 18 Sep 2018 15:04:46 +0100
parents
children bf32b26d1dad
comparison
equal deleted inserted replaced
1526:8988b27ebf38 1528:a7485c1bdba5
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_ZOOM_CONSTRAINTS_H
16 #define TEST_ZOOM_CONSTRAINTS_H
17
18 #include "../PowerOfTwoZoomConstraint.h"
19 #include "../PowerOfSqrtTwoZoomConstraint.h"
20
21 #include <QObject>
22 #include <QtTest>
23 #include <QDir>
24
25 #include <iostream>
26
27 using namespace std;
28
29 class TestZoomConstraints : public QObject
30 {
31 Q_OBJECT
32
33 private slots:
34 void unconstrainedNearest() {
35 ZoomConstraint c;
36 QCOMPARE(c.getNearestBlockSize(1), 1);
37 QCOMPARE(c.getNearestBlockSize(2), 2);
38 QCOMPARE(c.getNearestBlockSize(3), 3);
39 QCOMPARE(c.getNearestBlockSize(4), 4);
40 QCOMPARE(c.getNearestBlockSize(20), 20);
41 QCOMPARE(c.getNearestBlockSize(23), 23);
42 int max = c.getMaxZoomLevel();
43 QCOMPARE(c.getNearestBlockSize(max), max);
44 QCOMPARE(c.getNearestBlockSize(max+1), max);
45 }
46
47 void unconstrainedUp() {
48 ZoomConstraint c;
49 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1);
50 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2);
51 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 3);
52 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4);
53 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 20);
54 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32);
55 int max = c.getMaxZoomLevel();
56 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max);
57 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max);
58 }
59
60 void unconstrainedDown() {
61 ZoomConstraint c;
62 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1);
63 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2);
64 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 3);
65 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4);
66 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 20);
67 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32);
68 int max = c.getMaxZoomLevel();
69 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max);
70 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max);
71 }
72
73 void powerOfTwoNearest() {
74 PowerOfTwoZoomConstraint c;
75 QCOMPARE(c.getNearestBlockSize(1), 1);
76 QCOMPARE(c.getNearestBlockSize(2), 2);
77 QCOMPARE(c.getNearestBlockSize(3), 2);
78 QCOMPARE(c.getNearestBlockSize(4), 4);
79 QCOMPARE(c.getNearestBlockSize(20), 16);
80 QCOMPARE(c.getNearestBlockSize(23), 16);
81 QCOMPARE(c.getNearestBlockSize(24), 16);
82 QCOMPARE(c.getNearestBlockSize(25), 32);
83 int max = c.getMaxZoomLevel();
84 QCOMPARE(c.getNearestBlockSize(max), max);
85 QCOMPARE(c.getNearestBlockSize(max+1), max);
86 }
87
88 void powerOfTwoUp() {
89 PowerOfTwoZoomConstraint c;
90 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1);
91 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2);
92 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4);
93 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4);
94 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 32);
95 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32);
96 QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundUp), 64);
97 int max = c.getMaxZoomLevel();
98 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max);
99 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max);
100 }
101
102 void powerOfTwoDown() {
103 PowerOfTwoZoomConstraint c;
104 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1);
105 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2);
106 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2);
107 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4);
108 QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 16);
109 QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32);
110 QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundDown), 32);
111 int max = c.getMaxZoomLevel();
112 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max);
113 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max);
114 }
115
116 void powerOfSqrtTwoNearest() {
117 PowerOfSqrtTwoZoomConstraint c;
118 QCOMPARE(c.getNearestBlockSize(1), 1);
119 QCOMPARE(c.getNearestBlockSize(2), 2);
120 QCOMPARE(c.getNearestBlockSize(3), 2);
121 QCOMPARE(c.getNearestBlockSize(4), 4);
122 QCOMPARE(c.getNearestBlockSize(18), 16);
123 QCOMPARE(c.getNearestBlockSize(19), 16);
124 QCOMPARE(c.getNearestBlockSize(20), 22);
125 QCOMPARE(c.getNearestBlockSize(23), 22);
126 QCOMPARE(c.getNearestBlockSize(28), 32);
127 // PowerOfSqrtTwoZoomConstraint makes an effort to ensure
128 // bigger numbers get rounded to a multiple of something
129 // simple (64 or 90 depending on whether they are power-of-two
130 // or power-of-sqrt-two types)
131 QCOMPARE(c.getNearestBlockSize(800), 720);
132 QCOMPARE(c.getNearestBlockSize(1023), 1024);
133 QCOMPARE(c.getNearestBlockSize(1024), 1024);
134 QCOMPARE(c.getNearestBlockSize(1025), 1024);
135 int max = c.getMaxZoomLevel();
136 QCOMPARE(c.getNearestBlockSize(max), max);
137 QCOMPARE(c.getNearestBlockSize(max+1), max);
138 }
139
140 void powerOfSqrtTwoUp() {
141 PowerOfSqrtTwoZoomConstraint c;
142 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1);
143 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2);
144 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4);
145 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4);
146 QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundUp), 22);
147 QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundUp), 22);
148 QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundUp), 32);
149 QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundUp), 1024);
150 QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundUp), 1024);
151 QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundUp), 1024);
152 // see comment above
153 QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundUp), 1440);
154 int max = c.getMaxZoomLevel();
155 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max);
156 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max);
157 }
158
159 void powerOfSqrtTwoDown() {
160 PowerOfSqrtTwoZoomConstraint c;
161 QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1);
162 QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2);
163 QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2);
164 QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4);
165 QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundDown), 16);
166 QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundDown), 22);
167 QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundDown), 22);
168 // see comment above
169 QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundDown), 720);
170 QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundDown), 720);
171 QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundDown), 1024);
172 QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundDown), 1024);
173 int max = c.getMaxZoomLevel();
174 QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max);
175 QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max);
176 }
177 };
178
179 #endif
180