481265938ec213dacc102be5389c0fa2dd06ffb7
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / text / DecimalFormatTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18
19
20 package gov.nasa.jpf.test.java.text;
21
22 import gov.nasa.jpf.util.test.TestJPF;
23
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
30 import org.junit.Test;
31
32 /**
33  * simple regression test for java.text.DecimalFormat
34  */
35 public class DecimalFormatTest extends TestJPF {
36
37   @Test
38   public void testDoubleConversion() {
39
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();
45       try {
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;
50       }
51     }
52   }
53
54   @Test
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());
67     }
68   }
69
70   @Test
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());
79     }
80   }
81
82   @Test
83   public void testSetGroupingUsed() {
84
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);
95     }
96   }
97
98   @Test
99   public void testParseDouble() {
100
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);
110     }
111   }
112
113   @Test
114   public void testParseInt() {
115
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);
124     }
125   }
126
127   @Test
128   public void testParseError() {
129
130     if (verifyNoPropertyViolation()) {
131       DecimalFormat dFormat = new DecimalFormat();
132       int parsePos = 1;
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);
138     }
139   }
140 }
141