Debian Codegraph @

test/footleg/cavesurvey/tools/UtilityFunctionsTest.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2009-2017 Paul Fretwell - aka 'Footleg' ([email protected])
3 *
4 * This file is part of Cave Converter.
5 *
6 * Cave Converter is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Cave Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Cave Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package footleg.cavesurvey.tools;
20
21 import static org.junit.Assert.*;
22
23 import java.text.ParseException;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
27
28 import org.junit.Test;
29
30 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
31 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
32 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
33 import footleg.cavesurvey.converter.CmdLineLogger;
34 import footleg.cavesurvey.data.model.SeriesLink;
35 import footleg.cavesurvey.data.model.SurveySeries;
36 import footleg.cavesurvey.tools.UtilityFunctions;
37
38 /**
39 * Regression test class for UtilityFunctions static class methods.
40 *
41 * @author Footleg
42 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
43 * @since 1.6 (The Java version used)
44 */
45 public class UtilityFunctionsTest {
46 private static final double meanBearingTolerance = 1e-3;
47
48 /**
49 * Test helper method to call the UtilityFunctions.averageCompassBearings method for
50 * two given bearings
51 */
52 private double calcMeanBearing( double bearing1, double bearing2 ) {
53
54 double[] bearings = new double[2];
55
56 bearings[0] = bearing1;
57 bearings[1] = bearing2;
58
59 double meanBearing = UtilityFunctions.averageCompassBearings(bearings);
60 if ( meanBearing > 359.999) {
61 meanBearing = 0;
62 }
63
64 return meanBearing;
65 }
66
67 /**
68 * Test helper method to call the UtilityFunctions.averageCompassBearings method for
69 * three given bearings
70 */
71 private double calcMeanBearing( double bearing1, double bearing2, double bearing3 ) {
72
73 double[] bearings = new double[3];
74
75 bearings[0] = bearing1;
76 bearings[1] = bearing2;
77 bearings[2] = bearing3;
78
79 double meanBearing = UtilityFunctions.averageCompassBearings(bearings);
80
81 return meanBearing;
82 }
83
84 /**
85 * Test helper method to call the UtilityFunctions.averageCompassBearings method for
86 * two given bearings and an expected result, which formats a useful assertion message
87 * on test failure.
88 */
89 private void testMean2(double a, double b, double expectedMean) {
90 double mean = calcMeanBearing(a, b);
91 assertEquals("Mean bearing for values of " + a + " and " + b + "." , expectedMean, mean, meanBearingTolerance);
92 }
93
94 /**
95 * Test helper method to call the UtilityFunctions.averageCompassBearings method for
96 * three given bearings and an expected result, which formats a useful assertion message
97 * on test failure.
98 */
99 private void testMean3(double a, double b, double c, double expected) {
100 double expectedMean = expected;
101 double mean = calcMeanBearing(a, b, c);
102 if ( ( expectedMean > 359.5 ) && ( mean < 0.5 ) ) {
103 mean += 360;
104 } else if ( ( mean > 359.5 ) && ( expectedMean < 0.5 ) ) {
105 expectedMean += 360;
106 }
107 assertEquals("Mean bearing for values of " + a + ", " + b + ", and " + c + "." , expectedMean, mean, meanBearingTolerance);
108 }
109
110 /**
111 * Test helper method to compare the result from UtilityFunctions.averageCompassBearings method
112 * for three given bearings with the result from the numerical average method
113 */
114 private void testMean3VerusNumericalAverage(double a, double b, double c) {
115 double expectedMean = numericalMeanBearing3(a, b, c);
116 testMean3(a, b, c, expectedMean);
117 }
118
119 /**
120 * Test method which calculates the numerical average of two bearings using logical cases
121 * for relative positions of the two legs. This method essentially adds the two bearings
122 * together and divides by two to get an average but also handles the case where the bearings
123 * are either side of North (i.e. 359 & 1 should average to 0 and not 180).
124 * This method works for any pair of bearings in the range 0 - 360 degrees with any
125 * angle between them.
126 */
127 private double numericalMeanBearing2( double bearing1, double bearing2 ) {
128
129 double smallerBearing;
130 double largerBearing;
131 double meanBearing;
132
133 //If bearings are the same then average is the value of either bearing
134 if ( bearing1 == bearing2 ) {
135 meanBearing = bearing1;
136 }
137 else {
138 //Determine which is the larger value of the two bearings
139 if ( bearing1 > bearing2 ) {
140 largerBearing = bearing1;
141 smallerBearing = bearing2;
142 }
143 else {
144 largerBearing = bearing2;
145 smallerBearing = bearing1;
146 }
147
148 //Adjust bearings so smaller is zero, and larger is relative to smaller
149 double adjustedBearing = largerBearing - smallerBearing;
150 if (adjustedBearing > 180) {
151 //Larger bearing is on West side of compass dial, so average will be half way
152 //between this and 360 deg.
153 meanBearing = UtilityFunctions.adjustBearingWithinDegreesRange( 360 - ((360 - adjustedBearing) / 2) + smallerBearing, 0, 360);
154 }
155 else {
156 //Larger bearing is on East side of compass dial, so average will be half way
157 //between this and 0 deg.
158 meanBearing = (adjustedBearing / 2) + smallerBearing;
159 }
160 }
161
162 return meanBearing;
163 }
164
165 /**
166 * Test method which calculates the numerical average of three bearings using logical cases
167 * for relative positions of the three legs when all within 180 segment of each other. This
168 * method essentially adds the three bearings together and divides by three to get an average
169 * but also handles the case where the bearings are either side of North (i.e. 359, 357, 1
170 * should average to 359 and not 239)
171 */
172 private double numericalMeanBearing3( double bearing1, double bearing2, double bearing3 ) {
173
174 double bearingA = bearing1;
175 double bearingB = bearing2;
176 double bearingC = bearing3;
177 double meanBearing;
178
179 //If bearings are the same then average is the value of any bearing
180 if ( ( bearingA == bearingB ) && ( bearingA == bearingC ) ) {
181 meanBearing = bearingA;
182 }
183 else {
184 //Check for case where all bearings are in the northern half of the compass dial
185 //and split either side of North
186 if ( ( ( bearingA > 270 ) || ( bearingA < 90 ) )
187 && ( ( bearingB > 270 ) || ( bearingB < 90 ) )
188 && ( ( bearingC > 270 ) || ( bearingC < 90 ) ) ) {
189 if ( ( bearingA < 180 ) || ( bearingB < 180 ) || ( bearingC < 180 ) ) {
190 //At least one bearing on East side of dial, make any West side bearings negative
191 if ( bearingA > 180 ) {
192 bearingA -= 360;
193 }
194 if ( bearingB > 180 ) {
195 bearingB -= 360;
196 }
197 if ( bearingC > 180 ) {
198 bearingC -= 360;
199 }
200 }
201 }
202 //Determine which is the smallest and largest value of the three bearings
203 if ( bearingA > bearingB ) {
204 double park = bearingA;
205 bearingA = bearingB;
206 bearingB = park;
207 }
208 if ( bearingA > bearingC ) {
209 double park = bearingA;
210 bearingA = bearingC;
211 bearingC = park;
212 }
213 if ( bearingB > bearingC ) {
214 double park = bearingB;
215 bearingB = bearingC;
216 bearingC = park;
217 }
218
219 //Adjust bearings so smallest is zero, and larger are relative to smallest
220 double adjustedBearing2 = bearingB - bearingA;
221 double adjustedBearing3 = bearingC - bearingA;
222 if (adjustedBearing3 > 180) {
223 //Bearings span more than 180 degree range. Not supported by this function.
224 meanBearing = -999;
225 }
226 else {
227 //All bearings between 0-180 degrees, so safe to average
228 meanBearing = ( (adjustedBearing2 + adjustedBearing3) / 3) + bearingA;
229 }
230 }
231
232 return UtilityFunctions.adjustBearingWithinDegreesRange(meanBearing, 0, 360);
233 }
234
235 /**
236 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#averageCompassBearings(double[] bearings)}.
237 */
238 @Test
239 public void testMeanBearings() {
240 //Test average trend of a pair of bearings using vector averaging (atan2 method) is the same as
241 //the numerical average of the two angles, for values in all possible ranges
242 for (int diff = 0; diff < 180; diff += 4) {
243 for (int start = 0; start < 360 - diff; start += 1) {
244 double a = start;
245 double b = a + diff + 0.07;
246
247 double expectedMean = numericalMeanBearing2(a, b);
248 testMean2( a, b, expectedMean);
249 testMean2( b, a, expectedMean);
250 }
251 }
252
253 for (int diff = 0; diff < 180; diff += 4) {
254 for (int start = 1; start < 90; start += 1) {
255 double a = start;
256 double b = 360 - a - diff + 0.02;
257
258 double expectedMean = numericalMeanBearing2(a, b);
259 testMean2( a, b, expectedMean);
260 testMean2( b, a, expectedMean);
261 }
262 }
263 }
264
265 /**
266 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#averageCompassBearings(double[] bearings)}.
267 */
268 @Test
269 public void testMeanCompassBearing() {
270
271 double expectedMean = 0.2;
272 double mean;
273
274 mean = calcMeanBearing(0.1, 0.3);
275 assertEquals("Mean bearing for 2 similar values just over zero.", expectedMean, mean, meanBearingTolerance);
276
277 expectedMean = 180.0;
278 mean = calcMeanBearing(179, 181);
279 assertEquals("Mean bearing for 2 similar values around 180 deg.", expectedMean, mean, meanBearingTolerance);
280
281 expectedMean = 180.0;
282 mean = calcMeanBearing(90, 270);
283 assertEquals("Mean bearing for 2 values exactly East and West", expectedMean, mean, meanBearingTolerance);
284
285 expectedMean = 180.0;
286 mean = calcMeanBearing(270, 90);
287 assertEquals("Mean bearing for 2 values exactly West and East", expectedMean, mean, meanBearingTolerance);
288
289 expectedMean = 0.0;
290 mean = calcMeanBearing(271, 89);
291 assertEquals("Mean bearing for 2 values just North of West and East", expectedMean, mean, meanBearingTolerance);
292
293 expectedMean = 320.0;
294 mean = calcMeanBearing(20, 260);
295 assertEquals("Mean bearing for 2 values over 180 degrees different", expectedMean, mean, meanBearingTolerance);
296
297 //Some pairs to survey legs and expected average trend of passage
298 testMean2( 0.0, 0.0, 0.0);
299 testMean2( 0.0, 24.0, 12.0);
300 testMean2( 90.0, 0.0, 45.0);
301 testMean2( 120.0, 0.0, 60.0);
302 testMean2( 180.0, 180.0, 180.0);
303 testMean2( 180.0, 359.0, 269.5);
304
305 }
306
307 @Test
308 public void testMeanBearings3() {
309 boolean rigourousTests = false;
310
311 //Default increments for fast testing (test takes approx. 1 sec. with these values)
312 double mainBearingIncrement = 7.3; //Bearing 1 tested from 0-360 deg in steps of this size
313 double bearingSpreadIncrement = 0.3; //Maximum difference between all 3 bearings varies from 0-5 deg in steps of this size
314 double bearingSpreadDiffIncrement = 0.07; //Difference between bearing 1 and 2 varies in steps of this size between min and max.
315
316 if ( rigourousTests ) {
317 //Use smaller increments for better test coverage of numeric space at cost of running time
318 // (test takes approx. 1 minute with these values)
319 mainBearingIncrement = 0.37;
320 bearingSpreadIncrement = 0.13;
321 bearingSpreadDiffIncrement = 0.07;
322 }
323
324 //Full revolution for bearing 1
325 for (double start = 0; start < 360; start += mainBearingIncrement) {
326 double bearing1 = start;
327 //Vary maximum bearing spread from 0 - 5 degrees
328 for (double diff = 0; diff < 5; diff += bearingSpreadIncrement) {
329 double bearing3 = start + diff;
330 //Vary difference between 0 and max spread
331 for (double mid = 0; mid < diff; mid += bearingSpreadDiffIncrement) {
332 double bearing2 = start + mid;
333 //Test all ways
334 double a = bearing1;
335 double b = UtilityFunctions.adjustBearingWithinDegreesRange(bearing2, 0, 360);
336 double c = UtilityFunctions.adjustBearingWithinDegreesRange(bearing3, 0, 360);
337 testMean3VerusNumericalAverage( a, b, c);
338 testMean3VerusNumericalAverage( a, c, b);
339 testMean3VerusNumericalAverage( b, a, c);
340 testMean3VerusNumericalAverage( b, c, a);
341 testMean3VerusNumericalAverage( c, a, b);
342 testMean3VerusNumericalAverage( c, b, a);
343 }
344 }
345 }
346 }
347
348 /**
349 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#averageCompassBearings(double[] bearings)}.
350 */
351 @Test
352 public void testAverageCompassBearings() {
353
354 double expectedMean;
355 double mean;
356
357 expectedMean = 0.2;
358 mean = calcMeanBearing(0.1, 0.2, 0.3);
359 assertEquals("Mean bearing for 3 similar values just over zero.", expectedMean, mean, meanBearingTolerance);
360
361 expectedMean = 180.0;
362 mean = calcMeanBearing(179, 180, 181);
363 assertEquals("Mean bearing for 3 similar values around 180 deg.", expectedMean, mean, meanBearingTolerance);
364
365 expectedMean = 359.5;
366 mean = calcMeanBearing(359.9, 359.2, 359.4);
367 assertEquals("Mean bearing for 3 similar values around 359.5 deg.", expectedMean, mean, meanBearingTolerance);
368
369 expectedMean = 0.09;
370 mean = calcMeanBearing(359.94, 0.21, 0.12);
371 assertEquals("Mean bearing for 3 values either side of North", expectedMean, mean, meanBearingTolerance);
372
373 expectedMean = 359.9966667;
374 mean = calcMeanBearing(359.94, 0.03, 0.02);
375 assertEquals("Mean bearing for 3 values either side of North", expectedMean, mean, meanBearingTolerance);
376
377 //These bearings spread over 10 degree range show difference between mean vector angle and
378 //straight numerical averaging methods of calculating. So the correct answer is not 2.000
379 expectedMean = 2.00244;
380 mean = calcMeanBearing(356, 4, 6);
381 assertEquals("Mean bearing for 3 values around 0", expectedMean, mean, meanBearingTolerance);
382
383 //These bearings spread over 10 degree range show difference between mean vector angle and
384 //straight numerical averaging methods of calculating. So the correct answer is not 358.000
385 expectedMean = 358.00244;
386 mean = calcMeanBearing(2, 352, 0);
387 assertEquals("Mean bearing for 3 values around 0", expectedMean, mean, meanBearingTolerance);
388
389 //These bearings spread over 10 degree range show difference between mean vector angle and
390 //straight numerical averaging methods of calculating. So the correct answer is not 43.000
391 expectedMean = 43.002;
392 mean = calcMeanBearing(45, 37, 47);
393 assertEquals("Mean bearing for 3 values around 45", expectedMean, mean, meanBearingTolerance);
394
395 //These bearings spread over 10 degree range show difference between mean vector angle and
396 //straight numerical averaging methods of calculating. So the correct answer is not 88.000
397 expectedMean = 88.002;
398 mean = calcMeanBearing(90,92,82);
399 assertEquals("Mean bearing for 3 values around 90", expectedMean, mean, meanBearingTolerance);
400
401 //These bearings spread over 10 degree range show difference between mean vector angle and
402 //straight numerical averaging methods of calculating. So the correct answer is not 133.000
403 expectedMean = 133.002;
404 mean = calcMeanBearing(135,137,127);
405 assertEquals("Mean bearing for 3 values around 135", expectedMean, mean, meanBearingTolerance);
406
407 //These bearings spread over 10 degree range show difference between mean vector angle and
408 //straight numerical averaging methods of calculating. So the correct answer is not 178.000
409 expectedMean = 178.002;
410 mean = calcMeanBearing(180,182,172);
411 assertEquals("Mean bearing for 3 values around 180", expectedMean, mean, meanBearingTolerance);
412
413 expectedMean = 223.002;
414 mean = calcMeanBearing(225,227,217);
415 assertEquals("Mean bearing for 3 values around 225", expectedMean, mean, meanBearingTolerance);
416
417 expectedMean = 268.002;
418 mean = calcMeanBearing(270,272,262);
419 assertEquals("Mean bearing for 3 values around 270", expectedMean, mean, meanBearingTolerance);
420
421 expectedMean = 313.002;
422 mean = calcMeanBearing(315,317,307);
423 assertEquals("Mean bearing for 3 values around 315", expectedMean, mean, meanBearingTolerance);
424
425 expectedMean = 357.002;
426 mean = calcMeanBearing(359,1,351);
427 assertEquals("Mean bearing for 3 values around 360", expectedMean, mean, meanBearingTolerance);
428
429 expectedMean = 4.610;
430 mean = calcMeanBearing(0.0, 0.0, 13.86);
431 assertEquals("Mean bearing for 3 values around 360", expectedMean, mean, meanBearingTolerance);
432
433 //These bearings spread over 22.5 degree range show difference between mean vector angle and
434 //straight numerical averaging methods of calculating. So the correct answer is not 116.533
435 expectedMean = 116.548;
436 testMean3(104.06, 118.87, 126.67, expectedMean);
437
438 //These bearings spread over 160 degree range show difference between mean vector angle and
439 //straight numerical averaging methods of calculating. So the correct answer is not 126.667
440 //as clearly with bearings pulling almost North or South the average trend would be in the
441 //North-South direction, and pulling Eastwards nearly as much as a numerical average suggests.
442 expectedMean = 162.122;
443 testMean3(180, 180, 20, expectedMean);
444
445 }
446
447 /**
448 * Test method for helper method in this test file.
449 */
450 @Test
451 public void testNumericalMeanBearing3() {
452
453 double expectedMean = 0.2;
454 double mean;
455
456 mean = numericalMeanBearing3(0.1, 0.2, 0.3);
457 assertEquals("Mean bearing for 3 similar values just over zero.", expectedMean, mean, meanBearingTolerance);
458
459 expectedMean = 180.0;
460 mean = numericalMeanBearing3(179, 180, 181);
461 assertEquals("Mean bearing for 3 similar values around 180 deg.", expectedMean, mean, meanBearingTolerance);
462
463 expectedMean = 359.5;
464 mean = numericalMeanBearing3(359.9, 359.2, 359.4);
465 assertEquals("Mean bearing for 3 similar values around 359.5 deg.", expectedMean, mean, meanBearingTolerance);
466
467 expectedMean = 0.09;
468 mean = numericalMeanBearing3(359.94, 0.21, 0.12);
469 assertEquals("Mean bearing for 3 values either side of North", expectedMean, mean, meanBearingTolerance);
470
471 expectedMean = 359.9966667;
472 mean = numericalMeanBearing3(359.94, 0.03, 0.02);
473 assertEquals("Mean bearing for 3 values either side of North", expectedMean, mean, meanBearingTolerance);
474
475 expectedMean = 2;
476 mean = numericalMeanBearing3(356, 4, 6);
477 assertEquals("Mean bearing for 3 values around 0", expectedMean, mean, meanBearingTolerance);
478
479 expectedMean = 358;
480 mean = numericalMeanBearing3(2, 352, 0);
481 assertEquals("Mean bearing for 3 values around 0", expectedMean, mean, meanBearingTolerance);
482
483 expectedMean = 43;
484 mean = numericalMeanBearing3(45, 37, 47);
485 assertEquals("Mean bearing for 3 values around 45", expectedMean, mean, meanBearingTolerance);
486
487 expectedMean = 88;
488 mean = numericalMeanBearing3(90,92,82);
489 assertEquals("Mean bearing for 3 values around 90", expectedMean, mean, meanBearingTolerance);
490
491 expectedMean = 133;
492 mean = numericalMeanBearing3(135,137,127);
493 assertEquals("Mean bearing for 3 values around 135", expectedMean, mean, meanBearingTolerance);
494
495 expectedMean = 178;
496 mean = numericalMeanBearing3(180,182,172);
497 assertEquals("Mean bearing for 3 values around 180", expectedMean, mean, meanBearingTolerance);
498
499 expectedMean = 223;
500 mean = numericalMeanBearing3(225,227,217);
501 assertEquals("Mean bearing for 3 values around 225", expectedMean, mean, meanBearingTolerance);
502
503 expectedMean = 268;
504 mean = numericalMeanBearing3(270,272,262);
505 assertEquals("Mean bearing for 3 values around 270", expectedMean, mean, meanBearingTolerance);
506
507 expectedMean = 313;
508 mean = numericalMeanBearing3(315,317,307);
509 assertEquals("Mean bearing for 3 values around 315", expectedMean, mean, meanBearingTolerance);
510
511 expectedMean = 357;
512 mean = numericalMeanBearing3(359,1,351);
513 assertEquals("Mean bearing for 3 values around 360", expectedMean, mean, meanBearingTolerance);
514
515 expectedMean = 116.533333;
516 mean = numericalMeanBearing3(104.06, 118.87, 126.67);
517 assertEquals("Mean bearing for 3 values around 360", expectedMean, mean, meanBearingTolerance);
518
519 }
520
521 /**
522 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#bearingDifferenceDegrees(double angle1, double angle2)}.
523 */
524 @Test
525 public void testBearingDifference() {
526
527 double expectedDiff = 10.0;
528 double calcDiff;
529
530 calcDiff = UtilityFunctions.bearingDifferenceDegrees( 5.0, 355.0 );
531 assertEquals("Difference between two bearings either side of North", expectedDiff, calcDiff, meanBearingTolerance);
532
533 expectedDiff = 180.0;
534 calcDiff = UtilityFunctions.bearingDifferenceDegrees( 90.0, 270.0 );
535 assertEquals("Difference between two bearings of exactly East and West", expectedDiff, calcDiff, meanBearingTolerance);
536
537 expectedDiff = 4.8;
538 calcDiff = UtilityFunctions.bearingDifferenceDegrees( 120.4, 125.2 );
539 assertEquals("Difference between two bearings of similar direction", expectedDiff, calcDiff, meanBearingTolerance);
540 }
541
542 /**
543 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#stringToDate( String dateString, String dateFormat )},
544 * {@link footleg.cavesurvey.tools.UtilityFunctions#dateToString( Date date, String dateFormat )}.
545 */
546 @Test
547 public void testDateStringConversionPocketTopo() {
548 String dateOutFormat = UtilityFunctions.POCKETTOPO_DATE_FORMAT;
549 String dateInFormat = "dd-MM-yyyy";
550 String dateIn = "27-12-1984";
551 String dateExpectedOut = "1984/12/27";
552
553 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
554
555 dateIn = "01-02-1947";
556 dateExpectedOut = "1947/02/01";
557 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
558
559 dateIn = "31-12-2012";
560 dateExpectedOut = "2012/12/31";
561 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
562
563 dateInFormat = "MM-dd-yy";
564 dateIn = "03-05-07";
565 dateExpectedOut = "2007/03/05";
566 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
567 }
568
569 /**
570 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#stringToDate( String dateString, String dateFormat )},
571 * {@link footleg.cavesurvey.tools.UtilityFunctions#dateToString( Date date, String dateFormat )}.
572 */
573 @Test
574 public void testDateStringConversionSurvex() {
575 String dateOutFormat = UtilityFunctions.SURVEXDATE_FORMAT;
576 String dateInFormat = "dd-MM-yyyy";
577 String dateIn = "27-12-1984";
578 String dateExpectedOut = "1984.12.27";
579
580 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
581
582 dateIn = "01-02-1947";
583 dateExpectedOut = "1947.02.01";
584 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
585
586 dateIn = "31-12-2012";
587 dateExpectedOut = "2012.12.31";
588 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
589
590 dateInFormat = "MM-dd-yy";
591 dateIn = "03-05-07";
592 dateExpectedOut = "2007.03.05";
593 doDateStringConversion(dateIn, dateInFormat, dateExpectedOut, dateOutFormat);
594 }
595
596 /**
597 * Converts a string from one data format to another, with parse exception handling.
598 * Tests that the string generated matches the specified expected string.
599 */
600 private void doDateStringConversion( String dateIn, String dateInFormat,
601 String dateExpectedOut, String dateOutFormat ) {
602 try {
603 Date testDate = UtilityFunctions.stringToDate( dateIn, dateInFormat);
604 String dateOut = UtilityFunctions.dateToString( testDate, dateOutFormat );
605 assertEquals("Date converted from one string format to another", dateExpectedOut, dateOut);
606 } catch (ParseException e) {
607 fail("Date string '" + dateIn + "' raised parse exception using format '" + dateInFormat + "'.");
608 }
609 }
610
611 /**
612 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#parseDataStringIntoDataItems( String dataIn )}.
613 */
614 @Test
615 public void testParseDataLineIntoDataItems() {
616 String dataLine = "";
617 String[] dataItems = new String[6];
618
619 dataItems[0] = "Item1";
620 dataItems[1] = "2";
621 dataItems[2] = "Item3";
622 dataItems[3] = "";
623 dataItems[4] = "Item 5";
624 dataItems[5] = "Item6";
625
626 dataLine = dataItems[0] + " " + dataItems[1] + " " + dataItems[2] + "\t\"" + dataItems[3] + "\" \t\"" + dataItems[4] + "\"\t " + dataItems[5];
627 splitStringIntoItemsTest( dataLine, dataItems );
628
629 dataLine = dataItems[0] + " " + dataItems[1] + " " + dataItems[2] + " \t \"" + dataItems[3] + "\" \t \"" + dataItems[4] + "\" \t \t " + dataItems[5];
630 splitStringIntoItemsTest( dataLine, dataItems );
631
632 dataItems = new String[1];
633 dataItems[0] = "Item1";
634 dataLine = dataItems[0];
635 splitStringIntoItemsTest( dataLine, dataItems );
636
637 dataLine = "\"" + dataItems[0] + "\"";
638 splitStringIntoItemsTest( dataLine, dataItems );
639
640 dataItems[0] = "A long sentance of misc text";
641 dataLine = "\"" + dataItems[0] + "\"";
642 splitStringIntoItemsTest( dataLine, dataItems );
643
644 dataItems[0] = "Some\ttext\ncontaining\rtabs and new line chars ";
645 dataLine = "\"" + dataItems[0] + "\"";
646 splitStringIntoItemsTest( dataLine, dataItems );
647
648 dataItems = new String[2];
649
650 dataItems[0] = "Item\t1";
651 dataItems[1] = "Item 2";
652 dataLine = "\"" + dataItems[0] + "\"" + "\"" + dataItems[1] + "\"";
653 splitStringIntoItemsTest( dataLine, dataItems );
654 }
655
656 private void splitStringIntoItemsTest( String dataLine, String[] items ) {
657 //Parse data string into items
658 List<String> parsedData = UtilityFunctions.parseDataStringIntoDataItems( dataLine );
659
660 //Check expected number of items were returned
661 assertEquals( "Data string '" + dataLine + "' returns expected number of items.", items.length, parsedData.size() );
662
663 //Check each item returned matches expected data item
664 for (int i = 0; i < items.length; i++) {
665 assertEquals( "Item " + (i + 1) + " in '" + dataLine + "'.", items[i], parsedData.get(i) );
666 }
667 }
668
669 /**
670 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#convertToLinearSeries( SurveySeries seriesIn )}.
671 */
672 @Test
673 public void testConvertToLinearSeriesOrderedT() {
674 CmdLineLogger logger = new CmdLineLogger();
675 //Create series of test data
676 SurveySeries masterSeries = new SurveySeries("test");
677 //Add legs to series
678 masterSeries.addLeg( TestHelper.createTestLeg(1,2,1.0,110.0,0.0) );
679 masterSeries.addLeg( TestHelper.createTestLeg(2,3,1.0,100.0,0.0) );
680 masterSeries.addLeg( TestHelper.createTestLeg(3,4,1.0,190.0,0.0) );
681 masterSeries.addLeg( TestHelper.createTestLeg(4,5,1.0,180.0,0.0) );
682 masterSeries.addLeg( TestHelper.createTestLeg(3,6,1.0,270.0,0.0) );
683 masterSeries.addLeg( TestHelper.createTestLeg(6,7,1.0,290.0,0.0) );
684
685 //Convert to single series
686 SurveySeries convertedSeries = UtilityFunctions.convertToLinearSeries( masterSeries, logger );
687
688 assertEquals("Expect 2 series from t-shaped single series.", 2, convertedSeries.innerSeriesCount() );
689 assertEquals("Expect links between all series.", 1, convertedSeries.getLinks().size() );
690 SeriesLink firstLink = convertedSeries.getLinks().get(0);
691 assertEquals("Expect link from series 1.", convertedSeries.getInnerSeries(0).getSeriesName(), firstLink.getSeries2() );
692 assertEquals("Expect link to series 2.", convertedSeries.getInnerSeries(1).getSeriesName(), firstLink.getSeries1() );
693
694 }
695
696 /**
697 * Test method for {@link footleg.cavesurvey.tools.UtilityFunctions#compareStringLists( List<String> list1, List<String> list2 )}.
698 */
699 @Test
700 public void testCompareStringLists() {
701 List<String> list1 = new ArrayList<String>();
702 List<String> list2 = new ArrayList<String>();
703
704 //Test empty lists are the same
705 assertTrue( "Expect pair of empty string lists to be considered identical.", UtilityFunctions.compareStringLists( list1, list2 ) );
706
707 list1.add("A String");
708
709 //Test empty list against list with data are not the same
710 assertFalse( "Expect an empty string list not to be considered identical to one containing data.", UtilityFunctions.compareStringLists( list1, list2 ) );
711
712 list1.add("Another");
713 list1.add("More Data");
714
715 //Test empty list against list with data are not the same
716 assertFalse( "Expect an empty string list to not to be considered identical to one containing data.", UtilityFunctions.compareStringLists( list1, list2 ) );
717
718 list2.add("A String");
719
720 //Test lists for different sizes are not the same
721 assertFalse( "Expect string lists not to be considered identical when containing different numbers of items.", UtilityFunctions.compareStringLists( list1, list2 ) );
722
723 list2.add("Another");
724
725 //Test lists for different sizes are not the same
726 assertFalse( "Expect string lists not to be considered identical when containing different numbers of items.", UtilityFunctions.compareStringLists( list1, list2 ) );
727
728 list2.add("More Data");
729
730 //Test lists containing the same data are the same
731 assertTrue( "Expect string lists to be considered identical when containing the same items.", UtilityFunctions.compareStringLists( list1, list2 ) );
732
733 list2.clear();
734 list2.add("Another");
735 list2.add("A String");
736 list2.add("More Data");
737
738 //Test lists containing the same data are the same
739 assertFalse( "Expect string lists not to be considered identical when containing the same items in a different order.", UtilityFunctions.compareStringLists( list1, list2 ) );
740
741 }
742
743 /**
744 * Test method for
745 * {@link footleg.cavesurvey.tools.UtilityFunctions#lengthToMetres(double length, LengthUnit units)},
746 * {@link footleg.cavesurvey.tools.UtilityFunctions#lengthFromMetres(double metrelength, LengthUnit units)}.
747 * Tests conversion from feet and yards to metres and back.
748 */
749 @Test
750 public void testLengthUnitsConversions() {
751 double feet = 100;
752 double metres = 30.48;
753
754 assertEquals("Converting feet to metres", metres,
755 UtilityFunctions.lengthToMetres(feet, LengthUnit.Feet), 1e-6);
756 assertEquals("Converting inclincation in degrees to percentage slope", feet ,
757 UtilityFunctions.lengthFromMetres(metres, LengthUnit.Feet), 1e-6);
758
759 double yards = 40;
760 metres = 36.576;
761 assertEquals("Converting metres to yards", metres,
762 UtilityFunctions.lengthToMetres(yards, LengthUnit.Yards), 1e-6);
763 assertEquals("Converting yards to metres", yards ,
764 UtilityFunctions.lengthFromMetres(metres, LengthUnit.Yards), 1e-6);
765 }
766
767 /**
768 * Test method for
769 * {@link footleg.cavesurvey.tools.UtilityFunctions#bearingFromDegrees(double bearingDegrees, BearingUnit units)},
770 * {@link footleg.cavesurvey.tools.UtilityFunctions#bearingToDegrees(double bearing, BearingUnit units)}.
771 * Tests conversion from percent to degrees and back. Tests conversion from grads to degrees and back.
772 */
773 @Test
774 public void testBearingToDegreesConversions() {
775 double minutes = 30;
776 double degrees = 0.5;
777
778 assertEquals("Converting bearing in minutes to degrees", degrees,
779 UtilityFunctions.bearingToDegrees(minutes, BearingUnit.Minutes), 1e-6);
780 assertEquals("Converting bearing in degrees to minutes", minutes ,
781 UtilityFunctions.bearingFromDegrees(degrees, BearingUnit.Minutes), 1e-6);
782
783 double grads = 400;
784 degrees = 360;
785 assertEquals("Converting bearing in grads to degrees", degrees,
786 UtilityFunctions.bearingToDegrees(grads, BearingUnit.Grads), 1e-6);
787 assertEquals("Converting bearing in degrees to grads", grads ,
788 UtilityFunctions.bearingFromDegrees(degrees, BearingUnit.Grads), 1e-6);
789 }
790
791 /**
792 * Test method for
793 * {@link footleg.cavesurvey.tools.UtilityFunctions#gradientFromDegrees(double gradientDegrees, GradientUnit units)},
794 * {@link footleg.cavesurvey.tools.UtilityFunctions#gradientToDegrees(double gradient, GradientUnit units)}.
795 * Tests conversion from percent to degrees and back. Tests conversion from grads to degrees and back.
796 */
797 @Test
798 public void testGradientToDegreesConversions() {
799 double percent = 100;
800 double degrees = 45;
801
802 assertEquals("Converting percentage slope to inclincation degrees", degrees,
803 UtilityFunctions.gradientToDegrees(percent, GradientUnit.Percent), 1e-6);
804 assertEquals("Converting inclincation in degrees to percentage slope", percent ,
805 UtilityFunctions.gradientFromDegrees(degrees, GradientUnit.Percent), 1e-6);
806
807 percent = 12.5;
808 degrees = 7.125016349;
809 assertEquals("Converting percentage slope to inclincation degrees", degrees,
810 UtilityFunctions.gradientToDegrees(percent, GradientUnit.Percent), 1e-6);
811 assertEquals("Converting inclincation in degrees to percentage slope", percent ,
812 UtilityFunctions.gradientFromDegrees(degrees, GradientUnit.Percent), 1e-6);
813
814 double grads = 400;
815 degrees = 360;
816 assertEquals("Converting inclincation grads to degrees", degrees,
817 UtilityFunctions.gradientToDegrees(grads, GradientUnit.Grads), 1e-6);
818 assertEquals("Converting inclincation in degrees to grads", grads ,
819 UtilityFunctions.gradientFromDegrees(degrees, GradientUnit.Grads), 1e-6);
820
821 grads = 100;
822 degrees = 90;
823 assertEquals("Converting inclincation grads to degrees", degrees,
824 UtilityFunctions.gradientToDegrees(grads, GradientUnit.Grads), 1e-6);
825 assertEquals("Converting inclincation in degrees to grads", grads ,
826 UtilityFunctions.gradientFromDegrees(degrees, GradientUnit.Grads), 1e-6);
827 }
828
829 }