KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > format > FloatingPointConversionTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.format;
33
34 import java.math.BigDecimal JavaDoc;
35 import java.util.*;
36 import net.sf.retrotranslator.tests.BaseTestCase;
37
38 /**
39  * @author Taras Puchko
40  */

41 public class FloatingPointConversionTestCase extends BaseTestCase {
42
43     public void testFormat_ComputerizedScientific() throws Exception JavaDoc {
44         assertFormat("0.000000e+00", "%e", 0.0);
45         assertFormat("-0.000000e+00", "%e", -0.0);
46         assertFormat("0.000000e+00", "%e", BigDecimal.ZERO);
47         assertFormat("1.234500e+02", "%e", 123.45);
48         assertFormat("-1.234500e+02", "%e", -123.45);
49         assertFormat("1.234500e+02", "%e", BigDecimal.valueOf(123.45));
50         assertFormat("-1.234500e+02", "%e", BigDecimal.valueOf(-123.45));
51
52         assertFormat("1.5e-01", "%1.1e", 0.145);
53         assertFormat("1.5E-01", "%1.1E", 0.145);
54         assertFormat("-1.5e-01", "%1.1e", -0.145);
55         assertFormat("1.5e-01", "%1.1e", BigDecimal.valueOf(0.145));
56         assertFormat("-1.5e-01", "%1.1e", BigDecimal.valueOf(-0.145));
57         assertFormat("1.234500e+02", "%e", 123.45f);
58         assertFormat("0.0e+00", "%.1e", 0f);
59         assertFormat("0e+00", "%.0e", 0f);
60         assertFormat("1.230000e+02", "%e", 123f);
61         assertFormat("1.230000e-01", "%e", 0.123);
62
63         assertFormat("1.234568e+242", "%e", 123456789e234);
64         assertFormat(" 1.235e+242", "%12.3e", 123456789e234);
65         assertFormat(" 1e+242", "%12.0e", 123456789e234);
66         assertFormat("1e+242 ", "%-12.0e", 123456789e234);
67         assertFormat("001.235e+242", "%012.3e", 123456789e234);
68         assertFormat("-01.235e+242", "%012.3e", -123456789e234);
69         assertFormat("(0001.235e+242)", "%(015.3e", -123456789e234);
70         assertFormat("+00001.235e+242", "%+015.3e", 123456789e234);
71         assertFormat("+00001.235e+242", "%+(015.3e", 123456789e234);
72         assertFormat(" 00001.235e+242", "% 015.3e", 123456789e234);
73         assertFormat(HINDI, "1.234568e+06", "%e", 1234567.8f);
74
75         assertFormat("2.76701161105643274210e+19", "%1.20e",
76                 BigDecimal.valueOf(Long.MAX_VALUE).multiply(BigDecimal.valueOf(3)));
77         assertFormat("1.e+00", "%#.0e", 1f);
78
79         assertFormat(" NaN", "% 015.2e", Double.NaN);
80         assertFormat(" Infinity", "% 015.2e", Double.POSITIVE_INFINITY);
81         assertFormat(" -Infinity", "% 015.2e", Double.NEGATIVE_INFINITY);
82         assertFormat(" nu", "% 015.2e", (Object JavaDoc) null);
83         assertFormatException(FormatFlagsConversionMismatchException.class, "%,e");
84         assertFormatException(IllegalFormatConversionException.class, "%e", "x");
85         assertFormatException(IllegalFormatFlagsException.class, "%-012.3e");
86         assertFormatException(IllegalFormatFlagsException.class, "%+ 12.3e");
87         assertFormatException(IllegalFormatFlagsException.class, "%+ ,12.3e");
88     }
89
90     public void testFormat_Decimal() throws Exception JavaDoc {
91         assertFormat("0,000000", "%f", 0.0);
92         assertFormat("-0,000000", "%f", -0.0);
93         assertFormat("0,000000", "%f", BigDecimal.ZERO);
94         assertFormat("12,345000", "%f", 12.345);
95         assertFormat("-12,345000", "%f", -12.345);
96         assertFormat("12,345000", "%f", BigDecimal.valueOf(12.345));
97         assertFormat("-12,345000", "%f", BigDecimal.valueOf(-12.345));
98         assertFormat("0,001230", "%f", BigDecimal.valueOf(0.00123));
99
100         if (System.getProperty("java.vm.version").startsWith("1.4")) {
101             assertFormat("1230000000000", "%1.0f", new BigDecimal JavaDoc("123e10"));
102         }
103         
104         assertFormat("0,0", "%.1f", 0f);
105         assertFormat("0", "%.0f", 0f);
106         assertFormat("0,", "%#.0f", 0f);
107         assertFormat("12,35", "%1.2f", 12.345);
108         assertFormat(" +12,35", "%+10.2f", 12.345);
109         assertFormat(" -12,35", "%+10.2f", -12.345);
110         assertFormat(" 12,35", "% 1.2f", 12.345);
111         assertFormat("-12,35", "% 1.2f", -12.345);
112         assertFormat("0000012,35", "%010.2f", 12.345);
113         assertFormat("-000012,35", "%010.2f", -12.345);
114         assertFormat(" 12,35", "%(10.2f", 12.345);
115         assertFormat(" (12,35)", "%(10.2f", -12.345);
116         assertFormat("(12,35) ", "%(-10.2f", -12.345);
117         assertFormat("-12,35 ", "%- 10.2f", -12.345);
118         assertFormat("+000012,35", "%+010.2f", 12.345);
119         assertFormat("-000012,35", "%+010.2f", -12.345);
120
121         assertFormat("1234567936,000000", "%f", 1234567890f);
122         assertFormat("1 234 567 936,000000", "%,f", 1234567890f);
123         assertFormat("1 234 567 936,000000", "%,f", BigDecimal.valueOf(1234567890f));
124         assertFormat("-1 234 567 890,123457", "%,f", -1234567890.123456789d);
125         assertFormat("27670116110564327421,000000", "%f",
126                 BigDecimal.valueOf(Long.MAX_VALUE).multiply(BigDecimal.valueOf(3)));
127
128         assertFormat(HINDI, "\u0967,\u0968\u0969\u096a,\u096b\u096c\u096d.\u096e", "%,1.1f", 1234567.8f);
129
130         assertFormat("NaN ", "%-15.2f", Double.NaN);
131         assertFormat("Infinity ", "%-15.2f", Double.POSITIVE_INFINITY);
132         assertFormat("-Infinity ", "%-15.2f", Double.NEGATIVE_INFINITY);
133         assertFormat("(Infinity)", "%(f", Double.NEGATIVE_INFINITY);
134         assertFormat("nu ", "%-15.2f", (Object JavaDoc) null);
135         assertFormatException(MissingFormatWidthException.class, "%0f");
136         assertFormatException(MissingFormatWidthException.class, "%-f");
137         assertFormatException(MissingFormatWidthException.class, "%-0f");
138         assertFormatException(UnknownFormatConversionException.class, "%F");
139         assertFormatException(IllegalFormatConversionException.class, "%f", "x");
140         assertFormatException(IllegalFormatFlagsException.class, "%-012.3f");
141         assertFormatException(IllegalFormatFlagsException.class, "%+ 12.3f");
142     }
143
144     public void testFormat_GeneralScientific() throws Exception JavaDoc {
145         assertFormat("12.3450", "%g", 12.345f);
146         assertFormat("12.3450", "%g", 12.345d);
147         assertFormat("12,3450", "%g", BigDecimal.valueOf(12.345d));
148         assertFormat(HINDI, "12.3450", "%g", 12.345d);
149         assertFormat(HINDI, "\u0967\u0968.\u0969\u096a\u096b\u0966", "%g", BigDecimal.valueOf(12.345d));
150
151         assertFormat("1", "%1.0g", 1.2345f);
152         assertFormat("1e+01", "%1.0g", 12.345f);
153         assertFormat("1e+01", "%1.1g", 12.345f);
154         assertFormat("12", "%1.2g", 12.345f);
155         assertFormat("12.3", "%1.3g", 12.345f);
156         assertFormat("+12.3", "%+1.3g", 12.345f);
157         assertFormat("-12.3", "%+1.3g", -12.345f);
158
159         assertFormat("1234567890", "%1.10g", 1234567890d);
160         assertFormat("1,234,567,890", "%,1.10g", 1234567890d);
161         assertFormat("+1,234,567,890", "%+,1.10g", 1234567890d);
162         assertFormat(" 1,234,567,890", "% ,1.10g", 1234567890d);
163         assertFormat("1.23456789e+09", "%,1.9g", 1234567890d);
164         assertFormat("(1234567890)", "%(1.10g", -1234567890d);
165         assertFormat("(1.23456789e+09)", "%(1.9g", -1234567890d);
166
167         assertFormat("12345679", "%1.8g", 12345678.5d);
168         assertFormat("1.234568e+07", "%1.7g", 12345678.5d);
169         assertFormat("1.234568E+07", "%1.7G", 12345678.5d);
170         assertFormat("1.2300000e-05", "%1.8g", 0.0000123);
171         assertFormat("0.00012300000", "%1.8g", 0.000123);
172         assertFormat("0.00012", "%1.2g", 0.000123);
173         assertFormat("0.0001", "%1.0g", 0.000123);
174         assertFormat(" 0.0001", "%10.0g", 0.000123);
175         assertFormat("0.0001 ", "%-10.0g", 0.000123);
176         assertFormat("1.23e+04", "%1.3g", BigDecimal.valueOf(12345.6789));
177         assertFormat(HINDI, "1.23e+04", "%1.3g", BigDecimal.valueOf(12345.6789));
178
179         assertFormat("12 345,68", "%,1.7g", BigDecimal.valueOf(12345.6789));
180         assertFormat("12,345.68", "%,1.7g", 12345.6789);
181         assertFormat("2.767011611056432742e+19", "%1.19g",
182                 BigDecimal.valueOf(Long.MAX_VALUE).multiply(BigDecimal.valueOf(3)));
183         assertFormat("27670116110564327421", "%1.20g",
184                 BigDecimal.valueOf(Long.MAX_VALUE).multiply(BigDecimal.valueOf(3)));
185
186         assertFormat("NAN", "%1.1G", Double.NaN);
187         assertFormat("INFINITY", "%1.1G", Double.POSITIVE_INFINITY);
188         assertFormat("+Infinity", "%+1.1g", Double.POSITIVE_INFINITY);
189         assertFormat("-Infinity", "%1.1g", Double.NEGATIVE_INFINITY);
190         assertFormat("(Infinity)", "%(1.1g", Double.NEGATIVE_INFINITY);
191         assertFormat(" NULL", "%7.5G", (Object JavaDoc) null);
192         assertFormatException(IllegalFormatConversionException.class, "%g", "x");
193         assertFormatException(IllegalFormatFlagsException.class, "%-012.3g");
194         assertFormatException(IllegalFormatFlagsException.class, "%+ 12.3g");
195         assertFormatException(IllegalFormatFlagsException.class, "%#+ 12.3g", 0d);
196         assertFormatException(FormatFlagsConversionMismatchException.class, "%#12.3g", 0d);
197     }
198
199 }
Popular Tags