]> gitweb.fperrin.net Git - GpsPrune.git/blob - test/tim/prune/jpeg/drew/RationalTest.java
Version 20.4, May 2021
[GpsPrune.git] / test / tim / prune / jpeg / drew / RationalTest.java
1 package tim.prune.jpeg.drew;
2
3 import static org.junit.jupiter.api.Assertions.*;
4
5 import org.junit.jupiter.api.Test;
6
7 /**
8  * JUnit tests for the Rational values used by the Exif
9  */
10 class RationalTest
11 {
12         @Test
13         void testManyInts()
14         {
15                 testIntVal(0, 0, 0);
16                 testIntVal(1, 0, 0);
17                 testIntVal(0, 1, 0);
18                 for (int i=0; i<16000; i++)
19                 {
20                         testIntVal(0, i, 0);
21                         testIntVal(i, 0, 0);
22                         testIntVal(i, 1, i);
23                         testIntVal(-i, 1, -i);
24                         testIntVal(i*2, 2, i);
25                         testIntVal(i*2+1, 2, i);        // rounding down the 0.5
26                         testIntVal(-i*2, 2, -i);
27                         testIntVal(i*2, -2, -i);
28                         testIntVal(-i*2, -2, i);
29                 }
30         }
31
32         /**
33          * Check that a rational converts to an integer properly
34          * @param inTop number on top of the rational (numerator)
35          * @param inBottom number on bottom of the rational (denominator)
36          * @param inExpected expected int value
37          */
38         private void testIntVal(long inTop, long inBottom, int inExpected)
39         {
40                 Rational value = new Rational(inTop, inBottom);
41                 assertEquals(inExpected, value.intValue(), "" + inTop + "/" + inBottom);
42         }
43
44         @Test
45         void testManyDoubles()
46         {
47                 for (int i=0; i<16000; i++)
48                 {
49                         testDoubleVal(0, i, 0.0);
50                         testDoubleVal(i, 0, 0.0);
51                         testDoubleVal(i, 1, i);
52                         testDoubleVal(i*2, 2, i);
53                         testDoubleVal(i*2+1, 2, i+0.5);
54                         testDoubleVal(i*2, -2, -i);
55                 }
56
57                 testDoubleVal(123, 3, 123.0/3.0);
58         }
59
60         /**
61          * Check that a rational converts to a double properly
62          * @param inTop number on top of the rational (numerator)
63          * @param inBottom number on bottom of the rational (denominator)
64          * @param inExpected expected double value (exact)
65          */
66         private void testDoubleVal(long inTop, long inBottom, double inExpected)
67         {
68                 Rational value = new Rational(inTop, inBottom);
69                 assertEquals(inExpected, value.doubleValue(), 0.0, "" + inTop + "/" + inBottom);
70         }
71 }