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;
30 import org.junit.Test;
33 * simple regression test for java.text.DecimalFormat
35 public class DecimalFormatTest extends TestJPF {
38 public void testDoubleConversion() {
40 if (verifyNoPropertyViolation()) {
41 StringBuffer sb = new StringBuffer();
42 DecimalFormat dFormat = new DecimalFormat();
43 sb = dFormat.format(new Double(42), sb, new FieldPosition(0));
44 String output = sb.toString();
46 double d = Double.parseDouble(output);
47 assert (d == 42.0) : "parsed value differs: " + output;
48 } catch (NumberFormatException e) {
49 assert false : "output did not parse " + e;
55 public void testIsParseIntegerOnly () {
56 if (verifyNoPropertyViolation()) {
57 DecimalFormat dFormat = new DecimalFormat();
58 assertFalse(dFormat.isParseIntegerOnly());
59 dFormat.setParseIntegerOnly(true);
60 assertTrue(dFormat.isParseIntegerOnly());
61 dFormat.setParseIntegerOnly(false);
62 assertFalse(dFormat.isParseIntegerOnly());
63 NumberFormat format = NumberFormat.getIntegerInstance();
64 assertTrue(format.isParseIntegerOnly());
65 format = NumberFormat.getNumberInstance();
66 assertFalse(format.isParseIntegerOnly());
71 public void testIsGroupingUsed() {
72 if (verifyNoPropertyViolation()) {
73 DecimalFormat dFormat = new DecimalFormat();
74 assertTrue(dFormat.isGroupingUsed());
75 dFormat.setGroupingUsed(false);
76 assertFalse(dFormat.isGroupingUsed());
77 dFormat.setGroupingUsed(true);
78 assertTrue(dFormat.isGroupingUsed());
83 public void testSetGroupingUsed() {
85 if (verifyNoPropertyViolation()) {
86 DecimalFormat dFormat = new DecimalFormat();
87 String s = dFormat.format(4200000L);
88 assertTrue(s.length() == 9);
89 dFormat.setGroupingUsed(false);
90 s = dFormat.format(4200000L);
91 assertTrue(s.equals("4200000"));
92 dFormat.setGroupingUsed(true);
93 s = dFormat.format(4200000L);
94 assertTrue(s.length() == 9);
99 public void testParseDouble() {
101 if (verifyNoPropertyViolation()) {
102 DecimalFormatSymbols dfs = new DecimalFormatSymbols();
103 DecimalFormat dFormat = new DecimalFormat();
104 ParsePosition ps = new ParsePosition(0);
105 Number nb = dFormat.parse("10" + dfs.getDecimalSeparator() + "10",ps);
106 assertTrue(nb instanceof Double);
107 assertTrue(nb.doubleValue() == 10.10d);
108 assertTrue(ps.getErrorIndex() == -1);
109 assertTrue(ps.getIndex() == 5);
114 public void testParseInt() {
116 if (verifyNoPropertyViolation()) {
117 DecimalFormat dFormat = new DecimalFormat();
118 ParsePosition ps = new ParsePosition(0);
119 Number nb = dFormat.parse("10",ps);
120 assertTrue(nb instanceof Long);
121 assertTrue(nb.doubleValue() == 10l);
122 assertTrue(ps.getErrorIndex() == -1);
123 assertTrue(ps.getIndex() == 2);
128 public void testParseError() {
130 if (verifyNoPropertyViolation()) {
131 DecimalFormat dFormat = new DecimalFormat();
133 ParsePosition ps = new ParsePosition(parsePos);
134 Number nb = dFormat.parse("^^10",ps);
135 assertEquals(nb,null);
136 assertEquals(ps.getIndex(), parsePos);
137 assertEquals(ps.getErrorIndex(), parsePos);