comparison base/test/TestRangeMapper.h @ 885:12a6140b3ae0

Add unclamped range mapper methods
author Chris Cannam
date Thu, 06 Feb 2014 15:31:16 +0000
parents 633a8fa622c9
children ccc8658914ef
comparison
equal deleted inserted replaced
884:633a8fa622c9 885:12a6140b3ae0
40 QCOMPARE(rm.getPositionForValue(3.1), 6); 40 QCOMPARE(rm.getPositionForValue(3.1), 6);
41 QCOMPARE(rm.getPositionForValue(3.4), 7); 41 QCOMPARE(rm.getPositionForValue(3.4), 7);
42 QCOMPARE(rm.getPositionForValue(0.2), 1); 42 QCOMPARE(rm.getPositionForValue(0.2), 1);
43 QCOMPARE(rm.getPositionForValue(-12), 1); 43 QCOMPARE(rm.getPositionForValue(-12), 1);
44 QCOMPARE(rm.getPositionForValue(6.1), 8); 44 QCOMPARE(rm.getPositionForValue(6.1), 8);
45 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 6);
46 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 0);
47 QCOMPARE(rm.getPositionForValueUnclamped(-12), -24);
48 QCOMPARE(rm.getPositionForValueUnclamped(6.1), 12);
45 } 49 }
46 50
47 void linearDownForward() 51 void linearDownForward()
48 { 52 {
49 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true); 53 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
54 QCOMPARE(rm.getPositionForValue(3.1), 3); 58 QCOMPARE(rm.getPositionForValue(3.1), 3);
55 QCOMPARE(rm.getPositionForValue(3.4), 2); 59 QCOMPARE(rm.getPositionForValue(3.4), 2);
56 QCOMPARE(rm.getPositionForValue(0.2), 8); 60 QCOMPARE(rm.getPositionForValue(0.2), 8);
57 QCOMPARE(rm.getPositionForValue(-12), 8); 61 QCOMPARE(rm.getPositionForValue(-12), 8);
58 QCOMPARE(rm.getPositionForValue(6.1), 1); 62 QCOMPARE(rm.getPositionForValue(6.1), 1);
63 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 3);
64 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 9);
65 QCOMPARE(rm.getPositionForValueUnclamped(-12), 33);
66 QCOMPARE(rm.getPositionForValueUnclamped(6.1), -3);
59 } 67 }
60 68
61 void linearUpBackward() 69 void linearUpBackward()
62 { 70 {
63 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false); 71 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
66 QCOMPARE(rm.getValueForPosition(8), 4.0); 74 QCOMPARE(rm.getValueForPosition(8), 4.0);
67 QCOMPARE(rm.getValueForPosition(6), 3.0); 75 QCOMPARE(rm.getValueForPosition(6), 3.0);
68 QCOMPARE(rm.getValueForPosition(7), 3.5); 76 QCOMPARE(rm.getValueForPosition(7), 3.5);
69 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1)); 77 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
70 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8)); 78 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
79 QCOMPARE(rm.getValueForPositionUnclamped(6), 3.0);
80 QCOMPARE(rm.getValueForPositionUnclamped(0), 0.0);
81 QCOMPARE(rm.getValueForPositionUnclamped(-24), -12.0);
82 QCOMPARE(rm.getValueForPositionUnclamped(12), 6.0);
71 } 83 }
72 84
73 void linearDownBackward() 85 void linearDownBackward()
74 { 86 {
75 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true); 87 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
78 QCOMPARE(rm.getValueForPosition(1), 4.0); 90 QCOMPARE(rm.getValueForPosition(1), 4.0);
79 QCOMPARE(rm.getValueForPosition(3), 3.0); 91 QCOMPARE(rm.getValueForPosition(3), 3.0);
80 QCOMPARE(rm.getValueForPosition(2), 3.5); 92 QCOMPARE(rm.getValueForPosition(2), 3.5);
81 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1)); 93 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
82 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8)); 94 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
95 QCOMPARE(rm.getValueForPositionUnclamped(3), 3.0);
96 QCOMPARE(rm.getValueForPositionUnclamped(9), 0.0);
97 QCOMPARE(rm.getValueForPositionUnclamped(33), -12.0);
98 QCOMPARE(rm.getValueForPositionUnclamped(-3), 6.0);
83 } 99 }
84 100
85 void logUpForward() 101 void logUpForward()
86 { 102 {
87 LogRangeMapper rm(3, 7, 10, 100000, "x", false); 103 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
91 QCOMPARE(rm.getPositionForValue(1.0), 3); 107 QCOMPARE(rm.getPositionForValue(1.0), 3);
92 QCOMPARE(rm.getPositionForValue(1000000.0), 7); 108 QCOMPARE(rm.getPositionForValue(1000000.0), 7);
93 QCOMPARE(rm.getPositionForValue(1000.0), 5); 109 QCOMPARE(rm.getPositionForValue(1000.0), 5);
94 QCOMPARE(rm.getPositionForValue(900.0), 5); 110 QCOMPARE(rm.getPositionForValue(900.0), 5);
95 QCOMPARE(rm.getPositionForValue(20000), 6); 111 QCOMPARE(rm.getPositionForValue(20000), 6);
112 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 2);
113 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 8);
114 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5);
96 } 115 }
97 116
98 void logDownForward() 117 void logDownForward()
99 { 118 {
100 LogRangeMapper rm(3, 7, 10, 100000, "x", true); 119 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
104 QCOMPARE(rm.getPositionForValue(1.0), 7); 123 QCOMPARE(rm.getPositionForValue(1.0), 7);
105 QCOMPARE(rm.getPositionForValue(1000000.0), 3); 124 QCOMPARE(rm.getPositionForValue(1000000.0), 3);
106 QCOMPARE(rm.getPositionForValue(1000.0), 5); 125 QCOMPARE(rm.getPositionForValue(1000.0), 5);
107 QCOMPARE(rm.getPositionForValue(900.0), 5); 126 QCOMPARE(rm.getPositionForValue(900.0), 5);
108 QCOMPARE(rm.getPositionForValue(20000), 4); 127 QCOMPARE(rm.getPositionForValue(20000), 4);
128 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 8);
129 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 2);
130 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5);
109 } 131 }
110 132
111 void logUpBackward() 133 void logUpBackward()
112 { 134 {
113 LogRangeMapper rm(3, 7, 10, 100000, "x", false); 135 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
116 QCOMPARE(rm.getValueForPosition(7), 100000.0); 138 QCOMPARE(rm.getValueForPosition(7), 100000.0);
117 QCOMPARE(rm.getValueForPosition(5), 1000.0); 139 QCOMPARE(rm.getValueForPosition(5), 1000.0);
118 QCOMPARE(rm.getValueForPosition(6), 10000.0); 140 QCOMPARE(rm.getValueForPosition(6), 10000.0);
119 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3)); 141 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
120 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7)); 142 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
143 QCOMPARE(rm.getValueForPositionUnclamped(2), 1.0);
144 QCOMPARE(rm.getValueForPositionUnclamped(8), 1000000.0);
145 QCOMPARE(rm.getValueForPositionUnclamped(5), 1000.0);
121 } 146 }
122 147
123 void logDownBackward() 148 void logDownBackward()
124 { 149 {
125 LogRangeMapper rm(3, 7, 10, 100000, "x", true); 150 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
128 QCOMPARE(rm.getValueForPosition(3), 100000.0); 153 QCOMPARE(rm.getValueForPosition(3), 100000.0);
129 QCOMPARE(rm.getValueForPosition(5), 1000.0); 154 QCOMPARE(rm.getValueForPosition(5), 1000.0);
130 QCOMPARE(rm.getValueForPosition(4), 10000.0); 155 QCOMPARE(rm.getValueForPosition(4), 10000.0);
131 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3)); 156 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
132 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7)); 157 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
158 QCOMPARE(rm.getValueForPositionUnclamped(8), 1.0);
159 QCOMPARE(rm.getValueForPositionUnclamped(2), 1000000.0);
160 QCOMPARE(rm.getValueForPositionUnclamped(5), 1000.0);
133 } 161 }
134 162
135 void interpolatingForward() 163 void interpolatingForward()
136 { 164 {
137 InterpolatingRangeMapper::CoordMap mappings; 165 InterpolatingRangeMapper::CoordMap mappings;
145 QCOMPARE(rm.getPositionForValue(5.0), 70); 173 QCOMPARE(rm.getPositionForValue(5.0), 70);
146 QCOMPARE(rm.getPositionForValue(6.0), 70); 174 QCOMPARE(rm.getPositionForValue(6.0), 70);
147 QCOMPARE(rm.getPositionForValue(3.0), 30); 175 QCOMPARE(rm.getPositionForValue(3.0), 30);
148 QCOMPARE(rm.getPositionForValue(2.5), 25); 176 QCOMPARE(rm.getPositionForValue(2.5), 25);
149 QCOMPARE(rm.getPositionForValue(4.5), 60); 177 QCOMPARE(rm.getPositionForValue(4.5), 60);
178 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0);
179 QCOMPARE(rm.getPositionForValueUnclamped(2.5), 25);
180 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90);
150 } 181 }
151 182
152 void interpolatingBackward() 183 void interpolatingBackward()
153 { 184 {
154 InterpolatingRangeMapper::CoordMap mappings; 185 InterpolatingRangeMapper::CoordMap mappings;
172 mappings[0.5] = 1; 203 mappings[0.5] = 1;
173 mappings[4.0] = 8; 204 mappings[4.0] = 8;
174 AutoRangeMapper rm1(mappings, "x"); 205 AutoRangeMapper rm1(mappings, "x");
175 QCOMPARE(rm1.getUnit(), QString("x")); 206 QCOMPARE(rm1.getUnit(), QString("x"));
176 QCOMPARE(rm1.getType(), AutoRangeMapper::StraightLine); 207 QCOMPARE(rm1.getType(), AutoRangeMapper::StraightLine);
208 QCOMPARE(rm1.getPositionForValue(0.1), 1);
177 QCOMPARE(rm1.getPositionForValue(0.5), 1); 209 QCOMPARE(rm1.getPositionForValue(0.5), 1);
178 QCOMPARE(rm1.getPositionForValue(4.0), 8); 210 QCOMPARE(rm1.getPositionForValue(4.0), 8);
211 QCOMPARE(rm1.getPositionForValue(4.5), 8);
179 QCOMPARE(rm1.getPositionForValue(3.0), 6); 212 QCOMPARE(rm1.getPositionForValue(3.0), 6);
180 QCOMPARE(rm1.getPositionForValue(3.1), 6); 213 QCOMPARE(rm1.getPositionForValue(3.1), 6);
214 QCOMPARE(rm1.getPositionForValueUnclamped(0.1), 0);
215 QCOMPARE(rm1.getPositionForValueUnclamped(3.1), 6);
216 QCOMPARE(rm1.getPositionForValueUnclamped(4.5), 9);
181 mappings[3.0] = 6; 217 mappings[3.0] = 6;
182 mappings[3.5] = 7; 218 mappings[3.5] = 7;
183 AutoRangeMapper rm2(mappings, "x"); 219 AutoRangeMapper rm2(mappings, "x");
184 QCOMPARE(rm2.getUnit(), QString("x")); 220 QCOMPARE(rm2.getUnit(), QString("x"));
185 QCOMPARE(rm2.getType(), AutoRangeMapper::StraightLine); 221 QCOMPARE(rm2.getType(), AutoRangeMapper::StraightLine);
203 QCOMPARE(rm1.getPositionForValue(1.0), 3); 239 QCOMPARE(rm1.getPositionForValue(1.0), 3);
204 QCOMPARE(rm1.getPositionForValue(1000000.0), 7); 240 QCOMPARE(rm1.getPositionForValue(1000000.0), 7);
205 QCOMPARE(rm1.getPositionForValue(1000.0), 5); 241 QCOMPARE(rm1.getPositionForValue(1000.0), 5);
206 QCOMPARE(rm1.getPositionForValue(900.0), 5); 242 QCOMPARE(rm1.getPositionForValue(900.0), 5);
207 QCOMPARE(rm1.getPositionForValue(20000), 6); 243 QCOMPARE(rm1.getPositionForValue(20000), 6);
244 QCOMPARE(rm1.getPositionForValueUnclamped(1.0), 2);
245 QCOMPARE(rm1.getPositionForValueUnclamped(900.0), 5);
246 QCOMPARE(rm1.getPositionForValueUnclamped(1000000.0), 8);
208 mappings[100] = 4; 247 mappings[100] = 4;
209 AutoRangeMapper rm2(mappings, "x"); 248 AutoRangeMapper rm2(mappings, "x");
210 QCOMPARE(rm2.getUnit(), QString("x")); 249 QCOMPARE(rm2.getUnit(), QString("x"));
211 QCOMPARE(rm2.getType(), AutoRangeMapper::Logarithmic); 250 QCOMPARE(rm2.getType(), AutoRangeMapper::Logarithmic);
212 QCOMPARE(rm2.getPositionForValue(10.0), 3); 251 QCOMPARE(rm2.getPositionForValue(10.0), 3);
232 QCOMPARE(rm.getPositionForValue(5.0), 70); 271 QCOMPARE(rm.getPositionForValue(5.0), 70);
233 QCOMPARE(rm.getPositionForValue(6.0), 70); 272 QCOMPARE(rm.getPositionForValue(6.0), 70);
234 QCOMPARE(rm.getPositionForValue(3.0), 30); 273 QCOMPARE(rm.getPositionForValue(3.0), 30);
235 QCOMPARE(rm.getPositionForValue(2.5), 25); 274 QCOMPARE(rm.getPositionForValue(2.5), 25);
236 QCOMPARE(rm.getPositionForValue(4.5), 60); 275 QCOMPARE(rm.getPositionForValue(4.5), 60);
276 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0);
277 QCOMPARE(rm.getPositionForValueUnclamped(5.0), 70);
278 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90);
237 } 279 }
238 }; 280 };
239 281
240 #endif 282 #endif
241 283