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 import java.text.*;
 43 import java.io.*;
 44 
 45 public class PositionTest extends IntlTest {
 46 
 47     public static void main(String[] args) throws Exception {
 48         new PositionTest().run(args);
 49     }
 50 
 51     public void TestParsePosition() {
 52         ParsePosition pp1 = new ParsePosition(0);
 53         if (pp1.getIndex() == 0) {
 54             logln("PP constructor() tested.");
 55         }else{
 56             errln("*** PP getIndex or constructor() result");
 57         }
 58 
 59         {
 60             int to = 5;
 61             ParsePosition pp2 = new ParsePosition ( to );
 62             if (pp2.getIndex() == 5) {
 63                 logln("PP getIndex and constructor(TextOffset) tested.");
 64             }else{
 65                 errln("*** PP getIndex or constructor(TextOffset) result");
 66             }
 67             pp2.setIndex( 3 );
 68             if (pp2.getIndex() == 3) {
 69                 logln("PP setIndex tested.");
 70             }else{
 71                 errln("*** PP getIndex or setIndex result");
 72             }
 73         }
 74 
 75         ParsePosition pp2, pp3;
 76         pp2 = new ParsePosition( 3 );
 77         pp3 = new ParsePosition( 5 );
 78         ParsePosition pp4 = new ParsePosition(5);
 79         if (! pp2.equals(pp3)) {
 80             logln("PP not equals tested.");
 81         }else{
 82             errln("*** PP not equals fails");
 83         }
 84         if (pp3.equals(pp4)) {
 85             logln("PP equals tested.");
 86         }else{
 87             errln("*** PP equals fails (" + pp3.getIndex() + " != " + pp4.getIndex() + ")");
 88         }
 89 
 90         ParsePosition pp5;
 91         pp5 = pp4;
 92         if (pp4.equals(pp5)) {
 93             logln("PP operator= tested.");
 94         }else{
 95             errln("*** PP operator= operator== or operator != result");
 96         }
 97 
 98     }
 99 
100     public void TestFieldPosition() {
101         FieldPosition fp = new FieldPosition( 7 );
102 
103         if (fp.getField() == 7) {
104             logln("FP constructor(int) and getField tested.");
105         }else{
106             errln("*** FP constructor(int) or getField");
107         }
108 
109         FieldPosition fph = new FieldPosition( 3 );
110         if ( fph.getField() != 3) errln("*** FP getField or heap constr.");
111 
112         boolean err1 = false;
113         boolean err2 = false;
114         boolean err3 = false;
115 //        for (long i = -50; i < 50; i++ ) {
116 //            fp.setField( i+8 );
117 //            fp.setBeginIndex( i+6 );
118 //            fp.setEndIndex( i+7 );
119 //            if (fp.getField() != i+8)  err1 = true;
120 //            if (fp.getBeginIndex() != i+6) err2 = true;
121 //            if (fp.getEndIndex() != i+7) err3 = true;
122 //        }
123         if (!err1) {
124             logln("FP setField and getField tested.");
125         }else{
126             errln("*** FP setField or getField");
127         }
128         if (!err2) {
129             logln("FP setBeginIndex and getBeginIndex tested.");
130         }else{
131             errln("*** FP setBeginIndex or getBeginIndex");
132         }
133         if (!err3) {
134             logln("FP setEndIndex and getEndIndex tested.");
135         }else{
136             errln("*** FP setEndIndex or getEndIndex");
137         }
138 
139         logln("");
140     }
141 
142     public void TestFieldPosition_example() {
143         //***** no error detection yet !!!!!!!
144         //***** this test is for compiler checks and visual verification only.
145         double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,
146             12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
147         int dNumSize = doubleNum.length;
148 
149         DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();
150         fmt.setDecimalSeparatorAlwaysShown(true);
151 
152         final int tempLen = 20;
153         StringBuffer temp;
154 
155         for (int i=0; i<dNumSize; i++) {
156             temp = new StringBuffer(); // Get new buffer
157 
158             FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
159             StringBuffer buf = new StringBuffer();
160             //char fmtText[tempLen];
161             //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
162             StringBuffer res = fmt.format(doubleNum[i], buf, pos);
163             int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?
164                 tempLen : (tempLen - pos.getEndIndex());
165             for (int j=0; j<tempOffset; j++) temp.append('='); // initialize
166             //cout << temp << fmtText   << endl;
167             logln("FP " + temp + res);
168         }
169 
170         logln("");
171     }
172     /* @bug 4109023
173      * Need to override ParsePosition.equals and FieldPosition.equals.
174      */
175     public void Test4109023()
176     {
177 
178         ParsePosition p = new ParsePosition(3);
179         ParsePosition p2 = new ParsePosition(3);
180         if (!p.equals(p2))
181             errln("Error : ParsePosition.equals() failed");
182         FieldPosition fp = new FieldPosition(2);
183         FieldPosition fp2 = new FieldPosition(2);
184         if (!fp.equals(fp2))
185             errln("Error : FieldPosition.equals() failed");
186     }
187 
188     /**
189      * @bug 4153060
190      * ParsePosition.hashCode() returns different values on equal objects.
191      */
192     public void Test4153060() {
193         ParsePosition p = new ParsePosition(53);
194         ParsePosition q = new ParsePosition(53);
195         if (!p.equals(q)) {
196             errln("" + p + " and " + q + " are not equal and should be");
197         }
198         if (p.hashCode() != q.hashCode()) {
199             errln("ParsePosition.hashCode() different for equal objects");
200         } else {
201             logln("hashCode(" + p + ") = " + p.hashCode());
202         }
203     }
204 
205     /**
206      * @bug 4153061
207      * FieldPosition.hashCode() returns different values on equal objects.
208      */
209     public void Test4153061() {
210         FieldPosition p = new FieldPosition(53);
211         FieldPosition q = new FieldPosition(53);
212         if (!p.equals(q)) {
213             errln("" + p + " and " + q + " are not equal and should be");
214         }
215         if (p.hashCode() != q.hashCode()) {
216             errln("FieldPosition.hashCode() different for equal objects");
217         } else {
218             logln("hashCode(" + p + ") = " + p.hashCode());
219         }
220     }
221 }