001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.format;
032:
033: import java.math.BigDecimal;
034: import java.util.*;
035: import net.sf.retrotranslator.tests.TestCaseBase;
036:
037: /**
038: * @author Taras Puchko
039: */
040: public class FloatingPointConversionTestCase extends TestCaseBase {
041:
042: public void testFormat_ComputerizedScientific() throws Exception {
043: assertFormat("0.000000e+00", "%e", 0.0);
044: assertFormat("-0.000000e+00", "%e", -0.0);
045: assertFormat("0.000000e+00", "%e", BigDecimal.ZERO);
046: assertFormat("1.234500e+02", "%e", 123.45);
047: assertFormat("-1.234500e+02", "%e", -123.45);
048: assertFormat("1.234500e+02", "%e", BigDecimal.valueOf(123.45));
049: assertFormat("-1.234500e+02", "%e", BigDecimal.valueOf(-123.45));
050:
051: assertFormat("1.5e-01", "%1.1e", 0.145);
052: assertFormat("1.5E-01", "%1.1E", 0.145);
053: assertFormat("-1.5e-01", "%1.1e", -0.145);
054: assertFormat("1.5e-01", "%1.1e", BigDecimal.valueOf(0.145));
055: assertFormat("-1.5e-01", "%1.1e", BigDecimal.valueOf(-0.145));
056: assertFormat("1.234500e+02", "%e", 123.45f);
057: assertFormat("0.0e+00", "%.1e", 0f);
058: assertFormat("0e+00", "%.0e", 0f);
059: assertFormat("1.230000e+02", "%e", 123f);
060: assertFormat("1.230000e-01", "%e", 0.123);
061:
062: assertFormat("1.234568e+242", "%e", 123456789e234);
063: assertFormat(" 1.235e+242", "%12.3e", 123456789e234);
064: assertFormat(" 1e+242", "%12.0e", 123456789e234);
065: assertFormat("1e+242 ", "%-12.0e", 123456789e234);
066: assertFormat("001.235e+242", "%012.3e", 123456789e234);
067: assertFormat("-01.235e+242", "%012.3e", -123456789e234);
068: assertFormat("(0001.235e+242)", "%(015.3e", -123456789e234);
069: assertFormat("+00001.235e+242", "%+015.3e", 123456789e234);
070: assertFormat("+00001.235e+242", "%+(015.3e", 123456789e234);
071: assertFormat(" 00001.235e+242", "% 015.3e", 123456789e234);
072: assertFormat(HINDI, "1.234568e+06", "%e", 1234567.8f);
073:
074: assertFormat("2.76701161105643274210e+19", "%1.20e", BigDecimal
075: .valueOf(Long.MAX_VALUE)
076: .multiply(BigDecimal.valueOf(3)));
077: assertFormat("1.e+00", "%#.0e", 1f);
078:
079: assertFormat(" NaN", "% 015.2e", Double.NaN);
080: assertFormat(" Infinity", "% 015.2e",
081: Double.POSITIVE_INFINITY);
082: assertFormat(" -Infinity", "% 015.2e",
083: Double.NEGATIVE_INFINITY);
084: assertFormat(" nu", "% 015.2e", (Object) null);
085: assertFormatException(
086: FormatFlagsConversionMismatchException.class, "%,e");
087: assertFormatException(IllegalFormatConversionException.class,
088: "%e", "x");
089: assertFormatException(IllegalFormatFlagsException.class,
090: "%-012.3e");
091: assertFormatException(IllegalFormatFlagsException.class,
092: "%+ 12.3e");
093: assertFormatException(IllegalFormatFlagsException.class,
094: "%+ ,12.3e");
095: }
096:
097: public void testFormat_Decimal() throws Exception {
098: assertFormat("0,000000", "%f", 0.0);
099: assertFormat("-0,000000", "%f", -0.0);
100: assertFormat("0,000000", "%f", BigDecimal.ZERO);
101: assertFormat("12,345000", "%f", 12.345);
102: assertFormat("-12,345000", "%f", -12.345);
103: assertFormat("12,345000", "%f", BigDecimal.valueOf(12.345));
104: assertFormat("-12,345000", "%f", BigDecimal.valueOf(-12.345));
105: assertFormat("0,001230", "%f", BigDecimal.valueOf(0.00123));
106:
107: if (System.getProperty("java.vm.version").startsWith("1.4")) {
108: assertFormat("1230000000000", "%1.0f", new BigDecimal(
109: "123e10"));
110: }
111:
112: assertFormat("0,0", "%.1f", 0f);
113: assertFormat("0", "%.0f", 0f);
114: assertFormat("0,", "%#.0f", 0f);
115: assertFormat("12,35", "%1.2f", 12.345);
116: assertFormat(" +12,35", "%+10.2f", 12.345);
117: assertFormat(" -12,35", "%+10.2f", -12.345);
118: assertFormat(" 12,35", "% 1.2f", 12.345);
119: assertFormat("-12,35", "% 1.2f", -12.345);
120: assertFormat("0000012,35", "%010.2f", 12.345);
121: assertFormat("-000012,35", "%010.2f", -12.345);
122: assertFormat(" 12,35", "%(10.2f", 12.345);
123: assertFormat(" (12,35)", "%(10.2f", -12.345);
124: assertFormat("(12,35) ", "%(-10.2f", -12.345);
125: assertFormat("-12,35 ", "%- 10.2f", -12.345);
126: assertFormat("+000012,35", "%+010.2f", 12.345);
127: assertFormat("-000012,35", "%+010.2f", -12.345);
128:
129: assertFormat("1234567936,000000", "%f", 1234567890f);
130: assertFormat("1\u00a0234\u00a0567\u00a0936,000000", "%,f",
131: 1234567890f);
132: assertFormat("1\u00a0234\u00a0567\u00a0936,000000", "%,f",
133: BigDecimal.valueOf(1234567890f));
134: assertFormat("-1\u00a0234\u00a0567\u00a0890,123457", "%,f",
135: -1234567890.123456789d);
136: assertFormat("27670116110564327421,000000", "%f", BigDecimal
137: .valueOf(Long.MAX_VALUE)
138: .multiply(BigDecimal.valueOf(3)));
139:
140: if (isJava14AtLeast()) {
141: assertFormat(
142: HINDI,
143: "\u0967,\u0968\u0969\u096a,\u096b\u096c\u096d.\u096e",
144: "%,1.1f", 1234567.8f);
145: }
146:
147: assertFormat("NaN ", "%-15.2f", Double.NaN);
148: assertFormat("Infinity ", "%-15.2f",
149: Double.POSITIVE_INFINITY);
150: assertFormat("-Infinity ", "%-15.2f",
151: Double.NEGATIVE_INFINITY);
152: assertFormat("(Infinity)", "%(f", Double.NEGATIVE_INFINITY);
153: assertFormat("nu ", "%-15.2f", (Object) null);
154: assertFormatException(MissingFormatWidthException.class, "%0f");
155: assertFormatException(MissingFormatWidthException.class, "%-f");
156: assertFormatException(MissingFormatWidthException.class, "%-0f");
157: assertFormatException(UnknownFormatConversionException.class,
158: "%F");
159: assertFormatException(IllegalFormatConversionException.class,
160: "%f", "x");
161: assertFormatException(IllegalFormatFlagsException.class,
162: "%-012.3f");
163: assertFormatException(IllegalFormatFlagsException.class,
164: "%+ 12.3f");
165:
166: assertFormat("1234567936,000", "%.3f", 1234567890f);
167: assertFormatException(UnknownFormatConversionException.class,
168: "%.f");
169: }
170:
171: public void testFormat_GeneralScientific() throws Exception {
172: assertFormat("12.3450", "%g", 12.345f);
173: assertFormat("12.3450", "%g", 12.345d);
174: assertFormat("12,3450", "%g", BigDecimal.valueOf(12.345d));
175: assertFormat(HINDI, "12.3450", "%g", 12.345d);
176: if (isJava14AtLeast()) {
177: assertFormat(HINDI,
178: "\u0967\u0968.\u0969\u096a\u096b\u0966", "%g",
179: BigDecimal.valueOf(12.345d));
180: }
181:
182: assertFormat("1", "%1.0g", 1.2345f);
183: assertFormat("1e+01", "%1.0g", 12.345f);
184: assertFormat("1e+01", "%1.1g", 12.345f);
185: assertFormat("12", "%1.2g", 12.345f);
186: assertFormat("12.3", "%1.3g", 12.345f);
187: assertFormat("+12.3", "%+1.3g", 12.345f);
188: assertFormat("-12.3", "%+1.3g", -12.345f);
189:
190: assertFormat("1234567890", "%1.10g", 1234567890d);
191: assertFormat("1,234,567,890", "%,1.10g", 1234567890d);
192: assertFormat("+1,234,567,890", "%+,1.10g", 1234567890d);
193: assertFormat(" 1,234,567,890", "% ,1.10g", 1234567890d);
194: assertFormat("1.23456789e+09", "%,1.9g", 1234567890d);
195: assertFormat("(1234567890)", "%(1.10g", -1234567890d);
196: assertFormat("(1.23456789e+09)", "%(1.9g", -1234567890d);
197:
198: assertFormat("12345679", "%1.8g", 12345678.5d);
199: assertFormat("1.234568e+07", "%1.7g", 12345678.5d);
200: assertFormat("1.234568E+07", "%1.7G", 12345678.5d);
201: assertFormat("1.2300000e-05", "%1.8g", 0.0000123);
202: assertFormat("0.00012300000", "%1.8g", 0.000123);
203: assertFormat("0.00012", "%1.2g", 0.000123);
204: assertFormat("0.0001", "%1.0g", 0.000123);
205: assertFormat(" 0.0001", "%10.0g", 0.000123);
206: assertFormat("0.0001 ", "%-10.0g", 0.000123);
207: assertFormat("1.23e+04", "%1.3g", BigDecimal
208: .valueOf(12345.6789));
209: assertFormat(HINDI, "1.23e+04", "%1.3g", BigDecimal
210: .valueOf(12345.6789));
211:
212: assertFormat("12\u00a0345,68", "%,1.7g", BigDecimal
213: .valueOf(12345.6789));
214: assertFormat("12,345.68", "%,1.7g", 12345.6789);
215: assertFormat("2.767011611056432742e+19", "%1.19g", BigDecimal
216: .valueOf(Long.MAX_VALUE)
217: .multiply(BigDecimal.valueOf(3)));
218: assertFormat("27670116110564327421", "%1.20g", BigDecimal
219: .valueOf(Long.MAX_VALUE)
220: .multiply(BigDecimal.valueOf(3)));
221:
222: assertFormat("NAN", "%1.1G", Double.NaN);
223: assertFormat("INFINITY", "%1.1G", Double.POSITIVE_INFINITY);
224: assertFormat("+Infinity", "%+1.1g", Double.POSITIVE_INFINITY);
225: assertFormat("-Infinity", "%1.1g", Double.NEGATIVE_INFINITY);
226: assertFormat("(Infinity)", "%(1.1g", Double.NEGATIVE_INFINITY);
227: assertFormat(" NULL", "%7.5G", (Object) null);
228: assertFormatException(IllegalFormatConversionException.class,
229: "%g", "x");
230: assertFormatException(IllegalFormatFlagsException.class,
231: "%-012.3g");
232: assertFormatException(IllegalFormatFlagsException.class,
233: "%+ 12.3g");
234: assertFormatException(IllegalFormatFlagsException.class,
235: "%#+ 12.3g", 0d);
236: assertFormatException(
237: FormatFlagsConversionMismatchException.class,
238: "%#12.3g", 0d);
239: }
240:
241: }
|