jdk/test/java/math/BigDecimal/MultiplyTests.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3053 a42e2cc2aaa5
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3053
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     4
 *
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     8
 *
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    13
 * accompanied this code).
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    14
 *
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3053
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3053
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3053
diff changeset
    21
 * questions.
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    22
 */
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    23
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    24
/*
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    25
 * @test
3053
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    26
 * @bug 6850606
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    27
 * @summary Test BigDecimal.multiply(BigDecimal)
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    28
 * @author xlu
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    29
 */
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    30
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    31
import java.math.*;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    32
import static java.math.BigDecimal.*;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    33
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    34
public class MultiplyTests {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    35
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    36
    private static int multiplyTests() {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    37
        int failures = 0;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    38
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    39
        BigDecimal[] bd1 = {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    40
            new BigDecimal("123456789"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    41
            new BigDecimal("1234567898"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    42
            new BigDecimal("12345678987")
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    43
        };
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    44
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    45
        BigDecimal[] bd2 = {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    46
            new BigDecimal("987654321"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    47
            new BigDecimal("8987654321"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    48
            new BigDecimal("78987654321")
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    49
        };
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    50
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    51
        // Two dimensonal array recording bd1[i] * bd2[j] &
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    52
        // 0 <= i <= 2 && 0 <= j <= 2;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    53
        BigDecimal[][] expectedResults = {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    54
            {new BigDecimal("121932631112635269"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    55
             new BigDecimal("1109586943112635269"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    56
             new BigDecimal("9751562173112635269")
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    57
            },
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    58
            { new BigDecimal("1219326319027587258"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    59
              new BigDecimal("11095869503027587258"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    60
              new BigDecimal("97515622363027587258")
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    61
            },
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    62
            { new BigDecimal("12193263197189452827"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    63
              new BigDecimal("110958695093189452827"),
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    64
              new BigDecimal("975156224183189452827")
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    65
            }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    66
        };
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    67
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    68
        for (int i = 0; i < bd1.length; i++) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    69
            for (int j = 0; j < bd2.length; j++) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    70
                if (!bd1[i].multiply(bd2[j]).equals(expectedResults[i][j])) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    71
                    failures++;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    72
                }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    73
            }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    74
        }
3053
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    75
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    76
        BigDecimal x = BigDecimal.valueOf(8L, 1);
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    77
        BigDecimal xPower = BigDecimal.valueOf(-1L);
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    78
        try {
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    79
            for (int i = 0; i < 100; i++) {
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    80
                xPower = xPower.multiply(x);
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    81
            }
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    82
        } catch (Exception ex) {
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    83
            failures++;
a42e2cc2aaa5 6850606: Regression from JDK 1.6.0_12
xlu
parents: 2923
diff changeset
    84
        }
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    85
        return failures;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    86
    }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    87
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    88
    public static void main(String[] args) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    89
        int failures = 0;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    90
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    91
        failures += multiplyTests();
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    92
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    93
        if (failures > 0) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    94
            throw new RuntimeException("Incurred " + failures +
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    95
                                       " failures while testing multiply.");
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    96
        }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    97
    }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    98
}