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