1 /* 2 * Copyright (c) 2006, 2013, 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 package test.java.math.BigDecimal; 24 25 /* 26 * @test 27 * @bug 6473768 28 * @summary Tests of BigDecimal.compareTo 29 * @author Joseph D. Darcy 30 */ 31 32 import java.math.*; 33 import static java.math.BigDecimal.*; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Test; 37 38 // Android-changed: Replace error counting with asserts. 39 public class CompareToTests { 40 41 @Test compareToTests()42 public void compareToTests() { 43 final BigDecimal MINUS_ONE = BigDecimal.ONE.negate(); 44 45 // First operand, second operand, expected compareTo result 46 BigDecimal [][] testCases = { 47 // Basics 48 {valueOf(0), valueOf(0), ZERO}, 49 {valueOf(0), valueOf(1), MINUS_ONE}, 50 {valueOf(1), valueOf(2), MINUS_ONE}, 51 {valueOf(2), valueOf(1), ONE}, 52 {valueOf(10), valueOf(10), ZERO}, 53 54 // Significands would compare differently than scaled value 55 {valueOf(2,1), valueOf(2), MINUS_ONE}, 56 {valueOf(2,-1), valueOf(2), ONE}, 57 {valueOf(1,1), valueOf(2), MINUS_ONE}, 58 {valueOf(1,-1), valueOf(2), ONE}, 59 {valueOf(5,-1), valueOf(2), ONE}, 60 61 // Boundary and near boundary values 62 {valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO}, 63 {valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE}, 64 65 {valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE}, 66 {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE}, 67 68 {valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE}, 69 {valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE}, 70 71 {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE}, 72 {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO}, 73 74 {valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE}, 75 {valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE}, 76 77 {valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE}, 78 {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE}, 79 80 {valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO}, 81 {valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE}, 82 83 {valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE}, 84 {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE}, 85 }; 86 87 for (BigDecimal[] testCase : testCases) { 88 BigDecimal a = testCase[0]; 89 BigDecimal a_negate = a.negate(); 90 BigDecimal b = testCase[1]; 91 BigDecimal b_negate = b.negate(); 92 int expected = testCase[2].intValue(); 93 94 compareToTest(a, b, expected); 95 compareToTest(a_negate, b_negate, -expected); 96 } 97 } 98 compareToTest(BigDecimal a, BigDecimal b, int expected)99 private static void compareToTest(BigDecimal a, BigDecimal b, int expected) { 100 int result = a.compareTo(b); 101 Assert.assertEquals(result, expected, "(" + a + ").compareTo(" + b + ") => " + result + 102 "\n\tExpected " + expected); 103 } 104 } 105