View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.common;
19  
20  import java.text.NumberFormat;
21  import java.util.Arrays;
22  import java.util.Collection;
23  
24  import org.apache.commons.imaging.ImagingTest;
25  import org.apache.commons.imaging.util.Debug;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.Parameterized;
29  
30  @RunWith(Parameterized.class)
31  public class RationalNumberTest extends ImagingTest {
32  
33      private final double testValue;
34  
35      @Parameterized.Parameters
36      public static Collection<Double> data() {
37          return Arrays.<Double> asList(
38                  0d, //
39                  0.1d, //
40                  0.01d, //
41                  0.001d, //
42                  0.0001d, //
43                  0.00001d, //
44                  0.000001d, //
45                  0.0000001d, //
46                  0.123456789d, //
47                  1d, //
48                  0.9d, //
49                  1.1d, //
50                  2.0d, //
51                  123.123d, //
52                  123123123.123d, //
53  
54                  (double) 123 / 45678, //
55                  (double) 12 / 34, //
56                  (double) 1 / 2, //
57                  (double) 1 / 3, //
58                  (double) 1 / 4, //
59                  (double) 1 / 5, //
60                  (double) 1 / 6, //
61                  (double) 1 / 7, //
62                  (double) 1 / 8, //
63                  (double) 1 / 9, //
64                  (double) 3 / 2, //
65                  (double) 2 / 3, //
66                  (double) 3 / 4, //
67                  (double) 4 / 5, //
68                  (double) 5 / 6, //
69                  (double) 6 / 7, //
70                  (double) 7 / 8, //
71                  (double) 8 / 9, //
72                  (double) 3 / 5, //
73                  (double) 3 / 6, //
74                  (double) 3 / 7, //
75                  (double) 3 / 8, //
76                  (double) 3 / 9, //
77  
78                  -0.1d, //
79                  -0.01d, //
80                  -0.001d, //
81                  -0.0001d, //
82                  -0.00001d, //
83                  -0.000001d, //
84                  -0.0000001d, //
85                  -0.123456789d, //
86                  -1d, //
87                  -0.9d, //
88                  -1.1d, //
89                  -2.0d, //
90                  -123.123d, //
91                  -123123123.123d, //
92  
93                  34d, //
94  
95                  (double) Integer.MAX_VALUE, //
96                  (double) Integer.MAX_VALUE + 0.1, //
97                  (double) Integer.MAX_VALUE - 0.1, //
98                  (double) -(Integer.MAX_VALUE), //
99                  -(Integer.MAX_VALUE + 0.1), //
100                 -(Integer.MAX_VALUE - 0.1), //
101 
102                 (double) Long.MAX_VALUE, //
103                 (double) Long.MAX_VALUE + 0.1, //
104                 (double) Long.MAX_VALUE - 0.1, //
105                 (double) -(Long.MAX_VALUE), //
106                 -(Long.MAX_VALUE + 0.1), //
107                 -(Long.MAX_VALUE - 0.1) //
108         );
109     }
110 
111     public RationalNumberTest(final double testValue) {
112         this.testValue = testValue;
113     }
114 
115     @Test
116     public void testRationalNumber() {
117         final RationalNumber rational = RationalNumber.valueOf(testValue);
118         final double difference = Math.abs(testValue - rational.doubleValue());
119 
120         final NumberFormat nf = NumberFormat.getInstance();
121         nf.setMaximumFractionDigits(15);
122 
123         // TODO assert something here, the following will fail for some values. Do we have a bug?
124         // assertEquals(0.0, difference, 0.0d);
125         Debug.debug("value: " + nf.format(testValue));
126         Debug.debug("rational: " + rational);
127         Debug.debug("difference: " + difference);
128         Debug.debug();
129     }
130 
131 }