File: IntlTestDecimalFormatAPI.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (255 lines) | stat: -rw-r--r-- 9,351 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @library /java/text/testlib
 * @summary test International Decimal Format API
 */
/*
(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved

  The original version of this source code and documentation is copyrighted and
owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
provided under terms of a License Agreement between Taligent and Sun. This
technology is protected by multiple US and International patents. This notice and
attribution to Taligent may not be removed.
  Taligent is a registered trademark of Taligent, Inc.
*/

import java.text.*;
import java.util.*;

public class IntlTestDecimalFormatAPI extends IntlTest
{
    public static void main(String[] args)  throws Exception {
        new IntlTestDecimalFormatAPI().run(args);
    }

    // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
    public void TestAPI()
    {
        Locale reservedLocale = Locale.getDefault();
        try {
            logln("DecimalFormat API test---"); logln("");
            Locale.setDefault(Locale.ENGLISH);

            // ======= Test constructors

            logln("Testing DecimalFormat constructors");

            DecimalFormat def = new DecimalFormat();

            final String pattern = new String("#,##0.# FF");
            DecimalFormat pat = null;
            try {
                pat = new DecimalFormat(pattern);
            }
            catch (IllegalArgumentException e) {
                errln("ERROR: Could not create DecimalFormat (pattern)");
            }

            DecimalFormatSymbols symbols =
                    new DecimalFormatSymbols(Locale.FRENCH);

            DecimalFormat cust1 = new DecimalFormat(pattern, symbols);

            // ======= Test clone(), assignment, and equality

            logln("Testing clone() and equality operators");

            Format clone = (Format) def.clone();
            if( ! def.equals(clone)) {
                errln("ERROR: Clone() failed");
            }

            // ======= Test various format() methods

            logln("Testing various format() methods");

//          final double d = -10456.0037; // this appears as
                                          // -10456.003700000001 on NT
//          final double d = -1.04560037e-4; // this appears as
                                             // -1.0456003700000002E-4 on NT
            final double d = -10456.00370000000000; // this works!
            final long l = 100000000;
            logln("" + d + " is the double value");

            StringBuffer res1 = new StringBuffer();
            StringBuffer res2 = new StringBuffer();
            StringBuffer res3 = new StringBuffer();
            StringBuffer res4 = new StringBuffer();
            FieldPosition pos1 = new FieldPosition(0);
            FieldPosition pos2 = new FieldPosition(0);
            FieldPosition pos3 = new FieldPosition(0);
            FieldPosition pos4 = new FieldPosition(0);

            res1 = def.format(d, res1, pos1);
            logln("" + d + " formatted to " + res1);

            res2 = pat.format(l, res2, pos2);
            logln("" + l + " formatted to " + res2);

            res3 = cust1.format(d, res3, pos3);
            logln("" + d + " formatted to " + res3);

            res4 = cust1.format(l, res4, pos4);
            logln("" + l + " formatted to " + res4);

            // ======= Test parse()

            logln("Testing parse()");

            String text = new String("-10,456.0037");
            ParsePosition pos = new ParsePosition(0);
            String patt = new String("#,##0.#");
            pat.applyPattern(patt);
            double d2 = pat.parse(text, pos).doubleValue();
            if(d2 != d) {
                errln("ERROR: Roundtrip failed (via parse(" +
                    d2 + " != " + d + ")) for " + text);
            }
            logln(text + " parsed into " + (long) d2);

            // ======= Test getters and setters

            logln("Testing getters and setters");

            final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
            def.setDecimalFormatSymbols(syms);
            if(!pat.getDecimalFormatSymbols().equals(
                    def.getDecimalFormatSymbols())) {
                errln("ERROR: set DecimalFormatSymbols() failed");
            }

            String posPrefix;
            pat.setPositivePrefix("+");
            posPrefix = pat.getPositivePrefix();
            logln("Positive prefix (should be +): " + posPrefix);
            if(posPrefix != "+") {
                errln("ERROR: setPositivePrefix() failed");
            }

            String negPrefix;
            pat.setNegativePrefix("-");
            negPrefix = pat.getNegativePrefix();
            logln("Negative prefix (should be -): " + negPrefix);
            if(negPrefix != "-") {
                errln("ERROR: setNegativePrefix() failed");
            }

            String posSuffix;
            pat.setPositiveSuffix("_");
            posSuffix = pat.getPositiveSuffix();
            logln("Positive suffix (should be _): " + posSuffix);
            if(posSuffix != "_") {
                errln("ERROR: setPositiveSuffix() failed");
            }

            String negSuffix;
            pat.setNegativeSuffix("~");
            negSuffix = pat.getNegativeSuffix();
            logln("Negative suffix (should be ~): " + negSuffix);
            if(negSuffix != "~") {
                errln("ERROR: setNegativeSuffix() failed");
            }

            long multiplier = 0;
            pat.setMultiplier(8);
            multiplier = pat.getMultiplier();
            logln("Multiplier (should be 8): " + multiplier);
            if(multiplier != 8) {
                errln("ERROR: setMultiplier() failed");
            }

            int groupingSize = 0;
            pat.setGroupingSize(2);
            groupingSize = pat.getGroupingSize();
            logln("Grouping size (should be 2): " + (long) groupingSize);
            if(groupingSize != 2) {
                errln("ERROR: setGroupingSize() failed");
            }

            pat.setDecimalSeparatorAlwaysShown(true);
            boolean tf = pat.isDecimalSeparatorAlwaysShown();
            logln("DecimalSeparatorIsAlwaysShown (should be true) is " +
                                                (tf ? "true" : "false"));
            if(tf != true) {
                errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
            }

            String funkyPat;
            funkyPat = pat.toPattern();
            logln("Pattern is " + funkyPat);

            String locPat;
            locPat = pat.toLocalizedPattern();
            logln("Localized pattern is " + locPat);

            // ======= Test applyPattern()

            logln("Testing applyPattern()");

            String p1 = new String("#,##0.0#;(#,##0.0#)");
            logln("Applying pattern " + p1);
            pat.applyPattern(p1);
            String s2;
            s2 = pat.toPattern();
            logln("Extracted pattern is " + s2);
            if( ! s2.equals(p1) ) {
                errln("ERROR: toPattern() result did not match " +
                        "pattern applied");
            }

            String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
            logln("Applying pattern " + p2);
            pat.applyLocalizedPattern(p2);
            String s3;
            s3 = pat.toLocalizedPattern();
            logln("Extracted pattern is " + s3);
            if( ! s3.equals(p2) ) {
                errln("ERROR: toLocalizedPattern() result did not match " +
                        "pattern applied");
            }

            // ======= Test getStaticClassID()

//          logln("Testing instanceof()");

//          try {
//             NumberFormat test = new DecimalFormat();

//              if (! (test instanceof DecimalFormat)) {
//                  errln("ERROR: instanceof failed");
//              }
//          }
//          catch (Exception e) {
//              errln("ERROR: Couldn't create a DecimalFormat");
//          }
        } finally {
            // restore the reserved locale
            Locale.setDefault(reservedLocale);
        }
    }
}