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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/*
* Copyright (c) 2012, 2014, 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
*
* @bug 7131459 8039915
* @summary test various situations of NumberFormat rounding when close to tie
* @author Olivier Lagneau
* @run main TieRoundingTest
*
*/
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.math.RoundingMode;
import java.util.Locale;
public class TieRoundingTest {
static int testCounter = 0;
static int errorCounter = 0;
static boolean allPassed = true;
static void formatOutputTestDouble(NumberFormat nf,
double doubleToTest,
String tiePosition,
String inputDigits,
String expectedOutput) {
int mfd = nf.getMaximumFractionDigits();
RoundingMode rm = nf.getRoundingMode();
String result = nf.format(doubleToTest);
if (!result.equals(expectedOutput)) {
System.out.println();
System.out.println("========================================");
System.out.println("***Failure : error formatting value from string : " +
inputDigits);
System.out.println("NumberFormat pattern is : " +
((DecimalFormat ) nf).toPattern());
System.out.println("Maximum number of fractional digits : " + mfd);
System.out.println("Fractional rounding digit : " + (mfd + 1));
System.out.println("Position of value relative to tie : " + tiePosition);
System.out.println("Rounding Mode : " + rm);
System.out.println("BigDecimal output : " +
new BigDecimal(doubleToTest).toString());
System.out.println("FloatingDecimal output : " + doubleToTest);
System.out.println(
"Error. Formatted result different from expected." +
"\nExpected output is : \"" + expectedOutput + "\"" +
"\nFormated output is : \"" + result + "\"");
System.out.println("========================================");
System.out.println();
errorCounter++;
allPassed = false;
} else {
testCounter++;
System.out.println("\nSuccess for double value : " + doubleToTest + " :");
System.out.println(" Input digits :" + inputDigits +
", BigDecimal value : " +
new BigDecimal(doubleToTest).toString());
System.out.print(" Rounding mode: " + rm);
System.out.print(", fract digits : " + mfd);
System.out.print(", position : " + tiePosition + " tie");
System.out.print(", result : " + result);
System.out.println(", expected : " + expectedOutput);
}
}
static void formatOutputTestLong(NumberFormat nf,
long longToTest,
String tiePosition,
String inputDigits,
String expectedOutput) {
int mfd = nf.getMaximumFractionDigits();
RoundingMode rm = nf.getRoundingMode();
String result = nf.format(longToTest);
if (!result.equals(expectedOutput)) {
System.out.println();
System.out.println("========================================");
System.out.println("***Failure : error formatting value from string : " +
inputDigits);
System.out.println("NumberFormat pattern is : " +
((DecimalFormat ) nf).toPattern());
System.out.println("Maximum number of fractional digits : " + mfd);
System.out.println("Fractional rounding digit : " + (mfd + 1));
System.out.println("Position of value relative to tie : " + tiePosition);
System.out.println("Rounding Mode : " + rm);
System.out.println(
"Error. Formatted result different from expected." +
"\nExpected output is : \"" + expectedOutput + "\"" +
"\nFormated output is : \"" + result + "\"");
System.out.println("========================================");
System.out.println();
errorCounter++;
allPassed = false;
} else {
testCounter++;
System.out.print("Success. Long input :" + inputDigits);
System.out.print(", rounding : " + rm);
System.out.print(", fract digits : " + mfd);
System.out.print(", tie position : " + tiePosition);
System.out.println(", expected : " + expectedOutput);
}
}
static void formatOutputTestObject(NumberFormat nf,
Object someNumber,
String tiePosition,
String inputDigits,
String expectedOutput) {
int mfd = nf.getMaximumFractionDigits();
RoundingMode rm = nf.getRoundingMode();
String result = nf.format(someNumber);
if (!result.equals(expectedOutput)) {
System.out.println();
System.out.println("========================================");
System.out.println("***Failure : error formatting value from string : " +
inputDigits);
System.out.println("NumberFormat pattern is : " +
((DecimalFormat ) nf).toPattern());
System.out.println("Maximum number of fractional digits : " + mfd);
System.out.println("Fractional rounding digit : " + (mfd + 1));
System.out.println("Position of value relative to tie : " + tiePosition);
System.out.println("Rounding Mode : " + rm);
System.out.println("Number self output representation: " + someNumber);
System.out.println(
"Error. Formatted result different from expected." +
"\nExpected output is : \"" + expectedOutput + "\"" +
"\nFormated output is : \"" + result + "\"");
System.out.println("========================================");
System.out.println();
errorCounter++;
allPassed = false;
} else {
testCounter++;
System.out.print("Success. Number input :" + inputDigits);
System.out.print(", rounding : " + rm);
System.out.print(", fract digits : " + mfd);
System.out.print(", tie position : " + tiePosition);
System.out.println(", expected : " + expectedOutput);
}
}
public static void main(String[] args) {
// The 3 HALF_* rounding modes are impacted by bugs 7131459, 8039915.
// So we do not test the other rounding modes.
RoundingMode[] roundingModes = {
RoundingMode.HALF_DOWN,
RoundingMode.HALF_EVEN,
RoundingMode.HALF_UP
};
// Precise the relative position of input value against its closest tie.
// The double values tested below for 3 and 5 fractional digits must follow
// this scheme (position toward tie).
String[] tieRelativePositions = {
"below", "exact", "above",
"below", "exact", "above",
"below", "exact", "above",
"below", "above", "above",
"below", "below", "above",
"below", "exact", "above"
};
// =============== Testing double (and thus float) value cases =========
System.out.println("\n===== testing 3 digits rounding position =====");
double[] values3FractDigits = {
// unimpacting values close to tie, with less than 3 input fract digits
1.115d, 1.125d, 1.135d,
// HALF_* impacting close to tie values covering all 6 tie cases
0.3115d, 0.3125d, 0.3135d,
0.6865d, 0.6875d, 0.6885d,
// specific HALF_UP close to tie values
0.3124d, 0.3126d, 0.3128d,
// specific HALF_DOWN close to tie values
0.6864d, 0.6865d, 0.6868d,
// unimpacting values close to tie, with more than 3 input fract digits
1.46885d, 2.46875d, 1.46865d
};
String[] inputs3FractDigits = {
"1.115d", "1.125d", "1.135d",
"0.3115d", "0.3125d", "0.3135d",
"0.6865d", "0.6875d", "0.6885d",
"0.3124d", "0.3126d", "0.3128d",
"0.6864d", "0.6865d", "0.6868d",
"1.46885d", "2.46875d", "1.46865d"
};
String[][] expected3FractDigits = {
{"1.115", "1.125", "1.135",
"0.311", "0.312", "0.314",
"0.686", "0.687", "0.689",
"0.312", "0.313", "0.313",
"0.686", "0.686", "0.687",
"1.469", "2.469", "1.469"
},
{"1.115", "1.125", "1.135",
"0.311", "0.312", "0.314",
"0.686", "0.688", "0.689",
"0.312", "0.313", "0.313",
"0.686", "0.686", "0.687",
"1.469", "2.469", "1.469"
},
{"1.115", "1.125", "1.135",
"0.311", "0.313", "0.314",
"0.686", "0.688", "0.689",
"0.312", "0.313", "0.313",
"0.686", "0.686", "0.687",
"1.469", "2.469", "1.469"
},
};
for (int r = 0; r < roundingModes.length; r++) {
NumberFormat dfDefault = NumberFormat.getInstance(Locale.US);
RoundingMode rmode = roundingModes[r];
dfDefault.setRoundingMode(rmode);
System.out.println("\n----- Now checking " + rmode +
" rounding mode -----");
for (int i = 0; i < values3FractDigits.length; i++) {
double d = values3FractDigits[i];
String tiePosition = tieRelativePositions[i];
String input = inputs3FractDigits[i];
String expected = expected3FractDigits[r][i];
formatOutputTestDouble(dfDefault, d, tiePosition, input, expected);
}
}
System.out.println("\n===== testing 5 digits rounding position =====");
double[] values5FractDigits = {
// unimpacting values close to tie, with less than 5 input fract digits
1.3135d, 1.3125d, 1.3115d,
// HALF_* impacting values close to tie, covering all 6 cases
1.328115d, 1.328125d, 1.328135d,
1.796865d, 1.796875d, 1.796885d,
// specific HALF_UP close to tie values
1.328124d, 1.798876d, 1.796889d,
// specific HALF_DOWN close to tie values
1.328114d, 1.796865d, 1.328138d,
// unimpacting values close to tie, with more than 5 input fract digits
1.3281149999999d, 1.75390625d, 1.7968750000001d
};
String[] inputs5FractDigits = {
"1.3135d", "1.3125d", "1.3115d",
"1.328115d", "1.328125d", "1.328135d",
"1.796865d", "1.796875d", "1.796885d",
"1.328124d", "1.798876d", "1.796889d",
"1.328114d", "1.796865d", "1.328138d",
"1.3281149999999d", "1.75390625d", "1.7968750000001d"
};
String[][] expected5FractDigits = {
{"1.3135", "1.3125", "1.3115",
"1.32811", "1.32812", "1.32814",
"1.79686", "1.79687", "1.79689",
"1.32812", "1.79888", "1.79689",
"1.32811", "1.79686", "1.32814",
"1.32811", "1.75391", "1.79688"
},
{"1.3135", "1.3125", "1.3115",
"1.32811", "1.32812", "1.32814",
"1.79686", "1.79688", "1.79689",
"1.32812", "1.79888", "1.79689",
"1.32811", "1.79686", "1.32814",
"1.32811", "1.75391", "1.79688"
},
{"1.3135", "1.3125", "1.3115",
"1.32811", "1.32813", "1.32814",
"1.79686", "1.79688", "1.79689",
"1.32812", "1.79888", "1.79689",
"1.32811", "1.79686", "1.32814",
"1.32811", "1.75391", "1.79688"
}
};
for (int r = 0; r < roundingModes.length; r++) {
DecimalFormat df5 = (DecimalFormat) NumberFormat.getInstance(Locale.US);
RoundingMode rmode = roundingModes[r];
df5.setRoundingMode(rmode);
System.out.println("\n----- Now checking " + rmode +
" rounding mode -----");
df5.applyPattern("#,###.#####");
for (int i = 0; i < values5FractDigits.length; i++) {
double d = values5FractDigits[i];
String tiePosition = tieRelativePositions[i];
String input = inputs5FractDigits[i];
String expected = expected5FractDigits[r][i];
formatOutputTestDouble(df5, d, tiePosition, input, expected);
}
}
// ==================== Testing long value cases ====================
System.out.println("\n===== testing long values =====");
long l = 123456789012345L;
DecimalFormat dfLong = (DecimalFormat) NumberFormat.getInstance(Locale.US);
String tiePosition = "exact";
String input = "123456789012345L";
String expected = "123,456,789,012,345";
String result = dfLong.format(l);
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
dfLong.applyPattern("0.###E0");
expected = "1.235E14";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
l = 123450000000000L;
input = "123450000000000L";
expected = "1.234E14";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
l = 987750000000000L;
input = "987750000000000L";
expected = "9.878E14";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
dfLong.applyPattern("#,###.0E0");
l = 987755000000000L;
input = "987755000000000L";
expected = "987.76E12";
formatOutputTestLong(dfLong, l, tiePosition, input, expected);
// ================= Testing BigInteger value cases =================
System.out.println("\n===== testing BigInteger values =====");
String stringValue = "12345678901234567890123456789012345";
BigInteger bi = new BigInteger(stringValue);
DecimalFormat dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);
tiePosition = "exact";
input = stringValue;
expected = "12,345,678,901,234,567,890,123,456,789,012,345";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
dfBig.applyPattern("0.###E0");
expected = "1.235E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
stringValue = "12345000000000000000000000000000000";
input = stringValue;
bi = new BigInteger(stringValue);
expected = "1.234E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
stringValue = "12345000000000000000000000000000001";
input = stringValue;
bi = new BigInteger(stringValue);
expected = "1.235E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
stringValue = "98755000000000000000000000000000000";
input = stringValue;
bi = new BigInteger(stringValue);
expected = "9.876E34";
formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
dfLong.applyPattern("#,###.0E0");
stringValue = "98775500000000000000000000000000000";
input = stringValue;
expected = "987.76E34";
// =============== Testing BigDecimal value cases ================
System.out.println("\n===== testing BigDecimal values =====");
dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);
stringValue = "0.68850000000000000088817841970012523233890533447265625";
BigDecimal bd = new BigDecimal(stringValue);
tiePosition = "exact";
input = stringValue;
expected = "0.689";
formatOutputTestObject(dfBig, bd, tiePosition, input, expected);
stringValue = "0.31149999999999999911182158029987476766109466552734375";
bd = new BigDecimal(stringValue);
dfBig.applyPattern("#,##0.####");
tiePosition = "exact";
input = stringValue;
expected = "0.3115";
formatOutputTestObject(dfBig, bd, tiePosition, input, expected);
// ==================== Printing results and exiting ===================
System.out.println();
System.out.println("==> " + testCounter + " tests passed successfully");
System.out.println("==> " + errorCounter + " tests failed");
System.out.println();
if (allPassed) {
System.out.println(
"Success in formating all the values with the given parameters");
} else {
String s = "Test failed with " + errorCounter + " formating error(s).";
System.out.println(s);
throw new RuntimeException(s);
}
}
}
|