1 /* |
1 /* |
2 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
356 } |
356 } |
357 } |
357 } |
358 return failures; |
358 return failures; |
359 } |
359 } |
360 |
360 |
|
361 private static int divideByOneTests() { |
|
362 int failures = 0; |
|
363 |
|
364 //problematic divisor: one with scale 17 |
|
365 BigDecimal one = BigDecimal.ONE.setScale(17); |
|
366 RoundingMode rounding = RoundingMode.UNNECESSARY; |
|
367 |
|
368 long[][] unscaledAndScale = new long[][] { |
|
369 { Long.MAX_VALUE, 17}, |
|
370 {-Long.MAX_VALUE, 17}, |
|
371 { Long.MAX_VALUE, 0}, |
|
372 {-Long.MAX_VALUE, 0}, |
|
373 { Long.MAX_VALUE, 100}, |
|
374 {-Long.MAX_VALUE, 100} |
|
375 }; |
|
376 |
|
377 for (long[] uas : unscaledAndScale) { |
|
378 long unscaled = uas[0]; |
|
379 int scale = (int)uas[1]; |
|
380 |
|
381 BigDecimal noRound = null; |
|
382 try { |
|
383 noRound = BigDecimal.valueOf(unscaled, scale). |
|
384 divide(one, RoundingMode.UNNECESSARY); |
|
385 } catch (ArithmeticException e) { |
|
386 failures++; |
|
387 System.err.println("ArithmeticException for value " + unscaled |
|
388 + " and scale " + scale + " without rounding"); |
|
389 } |
|
390 |
|
391 BigDecimal roundDown = null; |
|
392 try { |
|
393 roundDown = BigDecimal.valueOf(unscaled, scale). |
|
394 divide(one, RoundingMode.DOWN); |
|
395 } catch (ArithmeticException e) { |
|
396 failures++; |
|
397 System.err.println("ArithmeticException for value " + unscaled |
|
398 + " and scale " + scale + " with rounding down"); |
|
399 } |
|
400 |
|
401 if (noRound != null && roundDown != null |
|
402 && noRound.compareTo(roundDown) != 0) { |
|
403 failures++; |
|
404 System.err.println("Equality failure for value " + unscaled |
|
405 + " and scale " + scale); |
|
406 } |
|
407 } |
|
408 |
|
409 return failures; |
|
410 } |
|
411 |
361 public static void main(String argv[]) { |
412 public static void main(String argv[]) { |
362 int failures = 0; |
413 int failures = 0; |
363 |
414 |
364 failures += powersOf2and5(); |
415 failures += powersOf2and5(); |
365 failures += nonTerminating(); |
416 failures += nonTerminating(); |
366 failures += properScaleTests(); |
417 failures += properScaleTests(); |
367 failures += trailingZeroTests(); |
418 failures += trailingZeroTests(); |
368 failures += scaledRoundedDivideTests(); |
419 failures += scaledRoundedDivideTests(); |
|
420 failures += divideByOneTests(); |
369 |
421 |
370 if (failures > 0) { |
422 if (failures > 0) { |
371 throw new RuntimeException("Incurred " + failures + |
423 throw new RuntimeException("Incurred " + failures + |
372 " failures while testing exact divide."); |
424 " failures while testing division."); |
373 } |
425 } |
374 } |
426 } |
375 } |
427 } |