< prev index next >

test/jdk/java/lang/Float/ParseFloat.java

Print this page

  1 /*
  2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */

259             paddedBadStrings[i] = pad + badStrings[i] + pad;
260 
261         paddedGoodStrings = new String[goodStrings.length];
262         for(int i = 0 ; i <  goodStrings.length; i++)
263             paddedGoodStrings[i] = pad + goodStrings[i] + pad;
264 
265     }
266 
267     /*
268      * Throws an exception if <code>Input</code> is
269      * <code>exceptionalInput</code> and {@link Float.parseFloat
270      * parseFloat} does <em>not</em> throw an exception or if
271      * <code>Input</code> is not <code>exceptionalInput</code> and
272      * <code>parseFloat</code> throws an exception.  This method does
273      * not attempt to test whether the string is converted to the
274      * proper value; just whether the input is accepted appropriately
275      * or not.
276      */
277     private static void testParsing(String [] input,
278                                     boolean exceptionalInput) {
279         for(int i = 0; i < input.length; i++) {
280             double d;
281 
282             try {
283                 d = Float.parseFloat(input[i]);
284                 check(input[i]);
285             }
286             catch (NumberFormatException e) {
287                 if (! exceptionalInput) {
288                     throw new RuntimeException("Float.parseFloat rejected " +
289                                                "good string `" + input[i] +
290                                                "'.");
291                 }
292                 break;
293             }
294             if (exceptionalInput) {
295                 throw new RuntimeException("Float.parseFloat accepted " +
296                                            "bad string `" + input[i] +
297                                            "'.");
298             }
299         }
300     }
301 
302     /**
303      * For each power of two, test at boundaries of
304      * region that should convert to that value.
305      */
306     private static void testPowers() {
307         for(int i = -149; i <= +127; i++) {
308             float f = Math.scalb(1.0f, i);
309             BigDecimal f_BD = new BigDecimal(f);
310 
311             BigDecimal lowerBound = f_BD.subtract(new BigDecimal(Math.ulp(-Math.nextUp(-f))).multiply(HALF));
312             BigDecimal upperBound = f_BD.add(new BigDecimal(Math.ulp(f)).multiply(HALF));
313 
314             check(lowerBound.toString());
315             check(upperBound.toString());
316         }

  1 /*
  2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */

259             paddedBadStrings[i] = pad + badStrings[i] + pad;
260 
261         paddedGoodStrings = new String[goodStrings.length];
262         for(int i = 0 ; i <  goodStrings.length; i++)
263             paddedGoodStrings[i] = pad + goodStrings[i] + pad;
264 
265     }
266 
267     /*
268      * Throws an exception if <code>Input</code> is
269      * <code>exceptionalInput</code> and {@link Float.parseFloat
270      * parseFloat} does <em>not</em> throw an exception or if
271      * <code>Input</code> is not <code>exceptionalInput</code> and
272      * <code>parseFloat</code> throws an exception.  This method does
273      * not attempt to test whether the string is converted to the
274      * proper value; just whether the input is accepted appropriately
275      * or not.
276      */
277     private static void testParsing(String [] input,
278                                     boolean exceptionalInput) {
279         for (String s : input) {


280             try {
281                 Float.parseFloat(s);
282                 check(s);
283             } catch (NumberFormatException e) {
284                 if (!exceptionalInput) {

285                     throw new RuntimeException("Float.parseFloat rejected " +
286                                                "good string `" + s +
287                                                "'.");
288                 }
289                 continue;
290             }
291             if (exceptionalInput) {
292                 throw new RuntimeException("Float.parseFloat accepted " +
293                                            "bad string `" + s +
294                                            "'.");
295             }
296         }
297     }
298 
299     /**
300      * For each power of two, test at boundaries of
301      * region that should convert to that value.
302      */
303     private static void testPowers() {
304         for(int i = -149; i <= +127; i++) {
305             float f = Math.scalb(1.0f, i);
306             BigDecimal f_BD = new BigDecimal(f);
307 
308             BigDecimal lowerBound = f_BD.subtract(new BigDecimal(Math.ulp(-Math.nextUp(-f))).multiply(HALF));
309             BigDecimal upperBound = f_BD.add(new BigDecimal(Math.ulp(f)).multiply(HALF));
310 
311             check(lowerBound.toString());
312             check(upperBound.toString());
313         }
< prev index next >