Updated Buffer and Matcher Classes (#171)
[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 import java.util.Locale;
30
31 import org.junit.Test;
32
33 /**
34  * simple regression test for java.text.DecimalFormat
35  */
36 public class DecimalFormatTest extends TestJPF {
37
38   @Test
39   public void testDoubleConversion() {
40
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();
46       try {
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;
51       }
52     }
53   }
54
55   @Test
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());
70     }
71   }
72
73   @Test
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());
82     }
83   }
84
85   @Test
86   public void testSetGroupingUsed() {
87
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);
98     }
99   }
100
101   @Test
102   public void testParseDouble() {
103
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);
113     }
114   }
115
116   @Test
117   public void testParseInt() {
118
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);
127     }
128   }
129
130   @Test
131   public void testParseError() {
132
133     if (verifyNoPropertyViolation()) {
134       DecimalFormat dFormat = new DecimalFormat();
135       int parsePos = 1;
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);
141     }
142   }
143 }
144