1 /*
2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 4109023 4153060 4153061
27  * @library /java/text/testlib
28  * @summary test ParsePosition and FieldPosition
29  */
30 /*
31 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
32 (C) Copyright IBM Corp. 1996 - All Rights Reserved
33 
34   The original version of this source code and documentation is copyrighted and
35 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
36 provided under terms of a License Agreement between Taligent and Sun. This
37 technology is protected by multiple US and International patents. This notice and
38 attribution to Taligent may not be removed.
39   Taligent is a registered trademark of Taligent, Inc.
40 */
41 
42 package test.java.text.Format.NumberFormat;
43 
44 import java.text.*;
45 import java.io.*;
46 
47 import test.java.text.testlib.IntlTest;
48 
49 public class PositionTest extends IntlTest {
50 
main(String[] args)51     public static void main(String[] args) throws Exception {
52         new PositionTest().run(args);
53     }
54 
TestParsePosition()55     public void TestParsePosition() {
56         ParsePosition pp1 = new ParsePosition(0);
57         if (pp1.getIndex() == 0) {
58             logln("PP constructor() tested.");
59         }else{
60             errln("*** PP getIndex or constructor() result");
61         }
62 
63         {
64             int to = 5;
65             ParsePosition pp2 = new ParsePosition ( to );
66             if (pp2.getIndex() == 5) {
67                 logln("PP getIndex and constructor(TextOffset) tested.");
68             }else{
69                 errln("*** PP getIndex or constructor(TextOffset) result");
70             }
71             pp2.setIndex( 3 );
72             if (pp2.getIndex() == 3) {
73                 logln("PP setIndex tested.");
74             }else{
75                 errln("*** PP getIndex or setIndex result");
76             }
77         }
78 
79         ParsePosition pp2, pp3;
80         pp2 = new ParsePosition( 3 );
81         pp3 = new ParsePosition( 5 );
82         ParsePosition pp4 = new ParsePosition(5);
83         if (! pp2.equals(pp3)) {
84             logln("PP not equals tested.");
85         }else{
86             errln("*** PP not equals fails");
87         }
88         if (pp3.equals(pp4)) {
89             logln("PP equals tested.");
90         }else{
91             errln("*** PP equals fails (" + pp3.getIndex() + " != " + pp4.getIndex() + ")");
92         }
93 
94         ParsePosition pp5;
95         pp5 = pp4;
96         if (pp4.equals(pp5)) {
97             logln("PP operator= tested.");
98         }else{
99             errln("*** PP operator= operator== or operator != result");
100         }
101 
102     }
103 
TestFieldPosition()104     public void TestFieldPosition() {
105         FieldPosition fp = new FieldPosition( 7 );
106 
107         if (fp.getField() == 7) {
108             logln("FP constructor(int) and getField tested.");
109         }else{
110             errln("*** FP constructor(int) or getField");
111         }
112 
113         FieldPosition fph = new FieldPosition( 3 );
114         if ( fph.getField() != 3) errln("*** FP getField or heap constr.");
115 
116         boolean err1 = false;
117         boolean err2 = false;
118         boolean err3 = false;
119 //        for (long i = -50; i < 50; i++ ) {
120 //            fp.setField( i+8 );
121 //            fp.setBeginIndex( i+6 );
122 //            fp.setEndIndex( i+7 );
123 //            if (fp.getField() != i+8)  err1 = true;
124 //            if (fp.getBeginIndex() != i+6) err2 = true;
125 //            if (fp.getEndIndex() != i+7) err3 = true;
126 //        }
127         if (!err1) {
128             logln("FP setField and getField tested.");
129         }else{
130             errln("*** FP setField or getField");
131         }
132         if (!err2) {
133             logln("FP setBeginIndex and getBeginIndex tested.");
134         }else{
135             errln("*** FP setBeginIndex or getBeginIndex");
136         }
137         if (!err3) {
138             logln("FP setEndIndex and getEndIndex tested.");
139         }else{
140             errln("*** FP setEndIndex or getEndIndex");
141         }
142 
143         logln("");
144     }
145 
TestFieldPosition_example()146     public void TestFieldPosition_example() {
147         //***** no error detection yet !!!!!!!
148         //***** this test is for compiler checks and visual verification only.
149         double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,
150             12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
151         int dNumSize = doubleNum.length;
152 
153         DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();
154         fmt.setDecimalSeparatorAlwaysShown(true);
155 
156         final int tempLen = 20;
157         StringBuffer temp;
158 
159         for (int i=0; i<dNumSize; i++) {
160             temp = new StringBuffer(); // Get new buffer
161 
162             FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
163             StringBuffer buf = new StringBuffer();
164             //char fmtText[tempLen];
165             //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
166             StringBuffer res = fmt.format(doubleNum[i], buf, pos);
167             int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?
168                 tempLen : (tempLen - pos.getEndIndex());
169             for (int j=0; j<tempOffset; j++) temp.append('='); // initialize
170             //cout << temp << fmtText   << endl;
171             logln("FP " + temp + res);
172         }
173 
174         logln("");
175     }
176     /* @bug 4109023
177      * Need to override ParsePosition.equals and FieldPosition.equals.
178      */
Test4109023()179     public void Test4109023()
180     {
181 
182         ParsePosition p = new ParsePosition(3);
183         ParsePosition p2 = new ParsePosition(3);
184         if (!p.equals(p2))
185             errln("Error : ParsePosition.equals() failed");
186         FieldPosition fp = new FieldPosition(2);
187         FieldPosition fp2 = new FieldPosition(2);
188         if (!fp.equals(fp2))
189             errln("Error : FieldPosition.equals() failed");
190     }
191 
192     /**
193      * @bug 4153060
194      * ParsePosition.hashCode() returns different values on equal objects.
195      */
Test4153060()196     public void Test4153060() {
197         ParsePosition p = new ParsePosition(53);
198         ParsePosition q = new ParsePosition(53);
199         if (!p.equals(q)) {
200             errln("" + p + " and " + q + " are not equal and should be");
201         }
202         if (p.hashCode() != q.hashCode()) {
203             errln("ParsePosition.hashCode() different for equal objects");
204         } else {
205             logln("hashCode(" + p + ") = " + p.hashCode());
206         }
207     }
208 
209     /**
210      * @bug 4153061
211      * FieldPosition.hashCode() returns different values on equal objects.
212      */
Test4153061()213     public void Test4153061() {
214         FieldPosition p = new FieldPosition(53);
215         FieldPosition q = new FieldPosition(53);
216         if (!p.equals(q)) {
217             errln("" + p + " and " + q + " are not equal and should be");
218         }
219         if (p.hashCode() != q.hashCode()) {
220             errln("FieldPosition.hashCode() different for equal objects");
221         } else {
222             logln("hashCode(" + p + ") = " + p.hashCode());
223         }
224     }
225 }
226