1 // Tags: JDK1.2 |
|
2 |
|
3 // Copyright (C) 2004 David Gilbert <david.gilbert@object-refinery.com> |
|
4 |
|
5 // Mauve is free software; you can redistribute it and/or modify |
|
6 // it under the terms of the GNU General Public License as published by |
|
7 // the Free Software Foundation; either version 2, or (at your option) |
|
8 // any later version. |
|
9 |
|
10 // Mauve is distributed in the hope that it will be useful, |
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 // GNU General Public License for more details. |
|
14 |
|
15 // You should have received a copy of the GNU General Public License |
|
16 // along with Mauve; see the file COPYING. If not, write to |
|
17 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
18 // Boston, MA 02111-1307, USA. */ |
|
19 |
|
20 package gnu.testlet.java.lang.Float; |
|
21 |
|
22 import gnu.testlet.TestHarness; |
|
23 import gnu.testlet.Testlet; |
|
24 |
|
25 /** |
|
26 * Some checks for the parseFloat() method in the {@link Float} class. |
|
27 */ |
|
28 public class parseFloat implements Testlet |
|
29 { |
|
30 |
|
31 /** |
|
32 * Runs the test using the specified harness. |
|
33 * |
|
34 * @param harness the test harness (<code>null</code> not permitted). |
|
35 */ |
|
36 public void test(TestHarness harness) |
|
37 { |
|
38 testRegular(harness); |
|
39 testInfinities(harness); |
|
40 testNaN(harness); |
|
41 } |
|
42 |
|
43 /** |
|
44 * Tests some regular values. |
|
45 * |
|
46 * @param harness the test harness. |
|
47 */ |
|
48 public void testRegular(TestHarness harness) |
|
49 { |
|
50 harness.check(Float.parseFloat("1.0"), 1.0f); |
|
51 harness.check(Float.parseFloat("+1.0"), 1.0f); |
|
52 harness.check(Float.parseFloat("-1.0"), -1.0f); |
|
53 harness.check(Float.parseFloat(" 1.0 "), 1.0f); |
|
54 harness.check(Float.parseFloat(" -1.0 "), -1.0f); |
|
55 |
|
56 harness.check(Float.parseFloat("2."), 2.0f); |
|
57 harness.check(Float.parseFloat(".3"), 0.3f); |
|
58 harness.check(Float.parseFloat("1e-9"), 1e-9f); |
|
59 harness.check(Float.parseFloat("1e37"), 1e37f); |
|
60 |
|
61 // test some bad formats |
|
62 try |
|
63 { |
|
64 /* float f = */ Float.parseFloat(""); |
|
65 harness.check(false); |
|
66 } |
|
67 catch (NumberFormatException e) |
|
68 { |
|
69 harness.check(true); |
|
70 } |
|
71 |
|
72 try |
|
73 { |
|
74 /* float f = */ Float.parseFloat("X"); |
|
75 harness.check(false); |
|
76 } |
|
77 catch (NumberFormatException e) |
|
78 { |
|
79 harness.check(true); |
|
80 } |
|
81 |
|
82 try |
|
83 { |
|
84 /* float f = */ Float.parseFloat("e"); |
|
85 harness.check(false); |
|
86 } |
|
87 catch (NumberFormatException e) |
|
88 { |
|
89 harness.check(true); |
|
90 } |
|
91 |
|
92 try |
|
93 { |
|
94 /* float f = */ Float.parseFloat("+ 1.0"); |
|
95 harness.check(false); |
|
96 } |
|
97 catch (NumberFormatException e) |
|
98 { |
|
99 harness.check(true); |
|
100 } |
|
101 |
|
102 try |
|
103 { |
|
104 /* float f = */ Float.parseFloat("- 1.0"); |
|
105 harness.check(false); |
|
106 } |
|
107 catch (NumberFormatException e) |
|
108 { |
|
109 harness.check(true); |
|
110 } |
|
111 |
|
112 // null argument should throw NullPointerException |
|
113 try |
|
114 { |
|
115 /* float f = */ Float.parseFloat(null); |
|
116 harness.check(false); |
|
117 } |
|
118 catch (NullPointerException e) |
|
119 { |
|
120 harness.check(true); |
|
121 } |
|
122 } |
|
123 |
|
124 /** |
|
125 * Some checks for values that should parse to Double.POSITIVE_INFINITY |
|
126 * or Double.NEGATIVE_INFINITY. |
|
127 * |
|
128 * @param harness the test harness. |
|
129 */ |
|
130 public void testInfinities(TestHarness harness) |
|
131 { |
|
132 try |
|
133 { |
|
134 harness.check(Float.parseFloat("Infinity"), Float.POSITIVE_INFINITY); |
|
135 harness.check(Float.parseFloat("+Infinity"), Float.POSITIVE_INFINITY); |
|
136 harness.check(Float.parseFloat("-Infinity"), Float.NEGATIVE_INFINITY); |
|
137 harness.check(Float.parseFloat(" +Infinity "), Float.POSITIVE_INFINITY); |
|
138 harness.check(Float.parseFloat(" -Infinity "), Float.NEGATIVE_INFINITY); |
|
139 } |
|
140 catch (Exception e) |
|
141 { |
|
142 harness.check(false); |
|
143 harness.debug(e); |
|
144 } |
|
145 harness.check(Float.parseFloat("1e1000"), Float.POSITIVE_INFINITY); |
|
146 harness.check(Float.parseFloat("-1e1000"), Float.NEGATIVE_INFINITY); |
|
147 } |
|
148 |
|
149 /** |
|
150 * Some checks for 'NaN' values. |
|
151 * |
|
152 * @param harness the test harness. |
|
153 */ |
|
154 public void testNaN(TestHarness harness) |
|
155 { |
|
156 try |
|
157 { |
|
158 harness.check(Float.isNaN(Float.parseFloat("NaN"))); |
|
159 harness.check(Float.isNaN(Float.parseFloat("+NaN"))); |
|
160 harness.check(Float.isNaN(Float.parseFloat("-NaN"))); |
|
161 harness.check(Float.isNaN(Float.parseFloat(" +NaN "))); |
|
162 harness.check(Float.isNaN(Float.parseFloat(" -NaN "))); |
|
163 } |
|
164 catch (Exception e) |
|
165 { |
|
166 harness.check(false); |
|
167 harness.debug(e); |
|
168 } |
|
169 } |
|
170 |
|
171 } |
|
172 |
|