Path: blob/master/test/jdk/java/text/Format/NumberFormat/PositionTest.java
41152 views
/*1* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4109023 4153060 415306126* @library /java/text/testlib27* @summary test ParsePosition and FieldPosition28*/29/*30(C) Copyright Taligent, Inc. 1996 - All Rights Reserved31(C) Copyright IBM Corp. 1996 - All Rights Reserved3233The original version of this source code and documentation is copyrighted and34owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are35provided under terms of a License Agreement between Taligent and Sun. This36technology is protected by multiple US and International patents. This notice and37attribution to Taligent may not be removed.38Taligent is a registered trademark of Taligent, Inc.39*/4041import java.text.*;42import java.io.*;4344public class PositionTest extends IntlTest {4546public static void main(String[] args) throws Exception {47new PositionTest().run(args);48}4950public void TestParsePosition() {51ParsePosition pp1 = new ParsePosition(0);52if (pp1.getIndex() == 0) {53logln("PP constructor() tested.");54}else{55errln("*** PP getIndex or constructor() result");56}5758{59int to = 5;60ParsePosition pp2 = new ParsePosition ( to );61if (pp2.getIndex() == 5) {62logln("PP getIndex and constructor(TextOffset) tested.");63}else{64errln("*** PP getIndex or constructor(TextOffset) result");65}66pp2.setIndex( 3 );67if (pp2.getIndex() == 3) {68logln("PP setIndex tested.");69}else{70errln("*** PP getIndex or setIndex result");71}72}7374ParsePosition pp2, pp3;75pp2 = new ParsePosition( 3 );76pp3 = new ParsePosition( 5 );77ParsePosition pp4 = new ParsePosition(5);78if (! pp2.equals(pp3)) {79logln("PP not equals tested.");80}else{81errln("*** PP not equals fails");82}83if (pp3.equals(pp4)) {84logln("PP equals tested.");85}else{86errln("*** PP equals fails (" + pp3.getIndex() + " != " + pp4.getIndex() + ")");87}8889ParsePosition pp5;90pp5 = pp4;91if (pp4.equals(pp5)) {92logln("PP operator= tested.");93}else{94errln("*** PP operator= operator== or operator != result");95}9697}9899public void TestFieldPosition() {100FieldPosition fp = new FieldPosition( 7 );101102if (fp.getField() == 7) {103logln("FP constructor(int) and getField tested.");104}else{105errln("*** FP constructor(int) or getField");106}107108FieldPosition fph = new FieldPosition( 3 );109if ( fph.getField() != 3) errln("*** FP getField or heap constr.");110111boolean err1 = false;112boolean err2 = false;113boolean err3 = false;114// for (long i = -50; i < 50; i++ ) {115// fp.setField( i+8 );116// fp.setBeginIndex( i+6 );117// fp.setEndIndex( i+7 );118// if (fp.getField() != i+8) err1 = true;119// if (fp.getBeginIndex() != i+6) err2 = true;120// if (fp.getEndIndex() != i+7) err3 = true;121// }122if (!err1) {123logln("FP setField and getField tested.");124}else{125errln("*** FP setField or getField");126}127if (!err2) {128logln("FP setBeginIndex and getBeginIndex tested.");129}else{130errln("*** FP setBeginIndex or getBeginIndex");131}132if (!err3) {133logln("FP setEndIndex and getEndIndex tested.");134}else{135errln("*** FP setEndIndex or getEndIndex");136}137138logln("");139}140141public void TestFieldPosition_example() {142//***** no error detection yet !!!!!!!143//***** this test is for compiler checks and visual verification only.144double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,14512345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};146int dNumSize = doubleNum.length;147148DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();149fmt.setDecimalSeparatorAlwaysShown(true);150151final int tempLen = 20;152StringBuffer temp;153154for (int i=0; i<dNumSize; i++) {155temp = new StringBuffer(); // Get new buffer156157FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);158StringBuffer buf = new StringBuffer();159//char fmtText[tempLen];160//ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);161StringBuffer res = fmt.format(doubleNum[i], buf, pos);162int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?163tempLen : (tempLen - pos.getEndIndex());164for (int j=0; j<tempOffset; j++) temp.append('='); // initialize165//cout << temp << fmtText << endl;166logln("FP " + temp + res);167}168169logln("");170}171/* @bug 4109023172* Need to override ParsePosition.equals and FieldPosition.equals.173*/174public void Test4109023()175{176177ParsePosition p = new ParsePosition(3);178ParsePosition p2 = new ParsePosition(3);179if (!p.equals(p2))180errln("Error : ParsePosition.equals() failed");181FieldPosition fp = new FieldPosition(2);182FieldPosition fp2 = new FieldPosition(2);183if (!fp.equals(fp2))184errln("Error : FieldPosition.equals() failed");185}186187/**188* @bug 4153060189* ParsePosition.hashCode() returns different values on equal objects.190*/191public void Test4153060() {192ParsePosition p = new ParsePosition(53);193ParsePosition q = new ParsePosition(53);194if (!p.equals(q)) {195errln("" + p + " and " + q + " are not equal and should be");196}197if (p.hashCode() != q.hashCode()) {198errln("ParsePosition.hashCode() different for equal objects");199} else {200logln("hashCode(" + p + ") = " + p.hashCode());201}202}203204/**205* @bug 4153061206* FieldPosition.hashCode() returns different values on equal objects.207*/208public void Test4153061() {209FieldPosition p = new FieldPosition(53);210FieldPosition q = new FieldPosition(53);211if (!p.equals(q)) {212errln("" + p + " and " + q + " are not equal and should be");213}214if (p.hashCode() != q.hashCode()) {215errln("FieldPosition.hashCode() different for equal objects");216} else {217logln("hashCode(" + p + ") = " + p.hashCode());218}219}220}221222223