2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 package gov.nasa.jpf.test.java.text;
22 import gov.nasa.jpf.util.test.TestJPF;
24 import java.text.DecimalFormat;
25 import java.text.DecimalFormatSymbols;
26 import java.text.FieldPosition;
27 import java.text.NumberFormat;
28 import java.text.ParsePosition;
29 import java.util.Locale;
31 import org.junit.Test;
34 * simple regression test for java.text.DecimalFormat
36 public class DecimalFormatTest extends TestJPF {
39 public void testDoubleConversion() {
41 if (verifyNoPropertyViolation()) {
42 StringBuffer sb = new StringBuffer();
43 DecimalFormat dFormat = new DecimalFormat();
44 sb = dFormat.format(new Double(42), sb, new FieldPosition(0));
45 String output = sb.toString();
47 double d = Double.parseDouble(output);
48 assert (d == 42.0) : "parsed value differs: " + output;
49 } catch (NumberFormatException e) {
50 assert false : "output did not parse " + e;
56 public void testIsParseIntegerOnly () {
57 if (verifyNoPropertyViolation()) {
58 DecimalFormat dFormat = new DecimalFormat();
59 assertFalse(dFormat.isParseIntegerOnly());
60 dFormat.setParseIntegerOnly(true);
61 assertTrue(dFormat.isParseIntegerOnly());
62 dFormat.setParseIntegerOnly(false);
63 assertFalse(dFormat.isParseIntegerOnly());
64 NumberFormat format = NumberFormat.getIntegerInstance();
65 assertTrue(format.isParseIntegerOnly());
66 format = NumberFormat.getNumberInstance();
67 assertFalse(format.isParseIntegerOnly());
68 format = NumberFormat.getNumberInstance(Locale.ENGLISH);
69 assertFalse(format.isParseIntegerOnly());
74 public void testIsGroupingUsed() {
75 if (verifyNoPropertyViolation()) {
76 DecimalFormat dFormat = new DecimalFormat();
77 assertTrue(dFormat.isGroupingUsed());
78 dFormat.setGroupingUsed(false);
79 assertFalse(dFormat.isGroupingUsed());
80 dFormat.setGroupingUsed(true);
81 assertTrue(dFormat.isGroupingUsed());
86 public void testSetGroupingUsed() {
88 if (verifyNoPropertyViolation()) {
89 DecimalFormat dFormat = new DecimalFormat();
90 String s = dFormat.format(4200000L);
91 assertTrue(s.length() == 9);
92 dFormat.setGroupingUsed(false);
93 s = dFormat.format(4200000L);
94 assertTrue(s.equals("4200000"));
95 dFormat.setGroupingUsed(true);
96 s = dFormat.format(4200000L);
97 assertTrue(s.length() == 9);
102 public void testParseDouble() {
104 if (verifyNoPropertyViolation()) {
105 DecimalFormatSymbols dfs = new DecimalFormatSymbols();
106 DecimalFormat dFormat = new DecimalFormat();
107 ParsePosition ps = new ParsePosition(0);
108 Number nb = dFormat.parse("10" + dfs.getDecimalSeparator() + "10",ps);
109 assertTrue(nb instanceof Double);
110 assertTrue(nb.doubleValue() == 10.10d);
111 assertTrue(ps.getErrorIndex() == -1);
112 assertTrue(ps.getIndex() == 5);
117 public void testParseInt() {
119 if (verifyNoPropertyViolation()) {
120 DecimalFormat dFormat = new DecimalFormat();
121 ParsePosition ps = new ParsePosition(0);
122 Number nb = dFormat.parse("10",ps);
123 assertTrue(nb instanceof Long);
124 assertTrue(nb.doubleValue() == 10l);
125 assertTrue(ps.getErrorIndex() == -1);
126 assertTrue(ps.getIndex() == 2);
131 public void testParseError() {
133 if (verifyNoPropertyViolation()) {
134 DecimalFormat dFormat = new DecimalFormat();
136 ParsePosition ps = new ParsePosition(parsePos);
137 Number nb = dFormat.parse("^^10",ps);
138 assertEquals(nb,null);
139 assertEquals(ps.getIndex(), parsePos);
140 assertEquals(ps.getErrorIndex(), parsePos);