KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > PDFNumberTestCase


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 /* $Id: PDFNumberTestCase.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.util;
21
22 import org.apache.fop.pdf.PDFNumber;
23
24 import junit.framework.TestCase;
25
26 /**
27  * This test tests PDFNumber's doubleOut() methods.
28  */

29 public class PDFNumberTestCase extends TestCase {
30
31     /**
32      * Tests PDFNumber.doubleOut().
33      * @throws Exception if the test fails
34      */

35     public void testDoubleOut1() throws Exception JavaDoc {
36         //Default is 6 decimal digits
37
assertEquals("0", PDFNumber.doubleOut(0.0f));
38         assertEquals("0", PDFNumber.doubleOut(0.0000000000000000000123f));
39         assertEquals("0.1", PDFNumber.doubleOut(0.1f));
40         assertEquals("100", PDFNumber.doubleOut(100.0f));
41         assertEquals("100", PDFNumber.doubleOut(99.99999999999999999999999f));
42         
43         //You'd expect 100.123456 here but DecimalFormat uses the BigDecimal.ROUND_HALF_EVEN
44
//strategy. I don't know if that's a problem. The strange thing testDoubleOut2
45
//seems to return the normally expected value. Weird.
46
assertEquals("100.123459", PDFNumber.doubleOut(100.12345611111111f));
47         assertEquals("-100.123459", PDFNumber.doubleOut(-100.12345611111111f));
48     }
49     
50     /**
51      * Tests PDFNumber.doubleOut().
52      * @throws Exception if the test fails
53      */

54     public void testDoubleOut2() throws Exception JavaDoc {
55         //4 decimal digits in this case
56
assertEquals("0", PDFNumber.doubleOut(0.0f, 4));
57         assertEquals("0", PDFNumber.doubleOut(0.0000000000000000000123f, 4));
58         assertEquals("0.1", PDFNumber.doubleOut(0.1f, 4));
59         assertEquals("100", PDFNumber.doubleOut(100.0f, 4));
60         assertEquals("100", PDFNumber.doubleOut(99.99999999999999999999999f, 4));
61         assertEquals("100.1234", PDFNumber.doubleOut(100.12341111111111f, 4));
62         assertEquals("-100.1234", PDFNumber.doubleOut(-100.12341111111111f, 4));
63     }
64     
65     /**
66      * Tests PDFNumber.doubleOut().
67      * @throws Exception if the test fails
68      */

69     public void testDoubleOut3() throws Exception JavaDoc {
70         //0 decimal digits in this case
71
assertEquals("0", PDFNumber.doubleOut(0.0f, 0));
72         assertEquals("0", PDFNumber.doubleOut(0.1f, 0));
73         assertEquals("1", PDFNumber.doubleOut(0.6f, 0));
74         assertEquals("100", PDFNumber.doubleOut(100.1234f, 0));
75         assertEquals("-100", PDFNumber.doubleOut(-100.1234f, 0));
76     }
77     
78     /**
79      * Tests PDFNumber.doubleOut(). Special cases (former bugs).
80      * @throws Exception if the test fails
81      */

82     public void testDoubleOut4() throws Exception JavaDoc {
83         double d = Double.parseDouble("5.7220458984375E-6");
84         assertEquals("0.000006", PDFNumber.doubleOut(d));
85         assertEquals("0", PDFNumber.doubleOut(d, 4));
86         assertEquals("0.00000572", PDFNumber.doubleOut(d, 8));
87     }
88     
89     /**
90      * Tests PDFNumber.doubleOut(). Tests for wrong parameters.
91      * @throws Exception if the test fails
92      */

93     public void testDoubleOutWrongParameters() throws Exception JavaDoc {
94         try {
95             PDFNumber.doubleOut(0.1f, -1);
96             fail("IllegalArgument expected!");
97         } catch (IllegalArgumentException JavaDoc iae) {
98             //we want that
99
}
100         try {
101             PDFNumber.doubleOut(0.1f, 17); //We support max 16 decimal digits
102
fail("IllegalArgument expected!");
103         } catch (IllegalArgumentException JavaDoc iae) {
104             //we want that
105
}
106         try {
107             PDFNumber.doubleOut(0.1f, 98274659);
108             fail("IllegalArgument expected!");
109         } catch (IllegalArgumentException JavaDoc iae) {
110             //we want that
111
}
112     }
113     
114 }
115
Popular Tags