Path: blob/master/test/jdk/java/math/BigDecimal/SerializationTests.java
41149 views
/*1* Copyright (c) 2005, 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 617783626* @summary Verify BigDecimal objects with collapsed values are serialized properly.27* @author Joseph D. Darcy28*/2930import java.math.*;31import java.io.*;3233public class SerializationTests {3435static void checkSerialForm(BigDecimal bd) throws Exception {36ByteArrayOutputStream bos = new ByteArrayOutputStream();37ObjectOutputStream oos = new ObjectOutputStream(bos);38oos.writeObject(bd);39oos.flush();40oos.close();41ObjectInputStream ois = new42ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));43BigDecimal tmp = (BigDecimal)ois.readObject();4445if (!bd.equals(tmp) ||46bd.hashCode() != tmp.hashCode()) {47System.err.print(" original : " + bd);48System.err.println(" (hash: 0x" + Integer.toHexString(bd.hashCode()) + ")");49System.err.print("serialized : " + tmp);50System.err.println(" (hash: 0x" + Integer.toHexString(tmp.hashCode()) + ")");51throw new RuntimeException("Bad serial roundtrip");52}53}5455public static void main(String[] args) throws Exception {56BigDecimal values[] = {57BigDecimal.ZERO,58BigDecimal.ONE,59BigDecimal.TEN,60new BigDecimal(0),61new BigDecimal(1),62new BigDecimal(10),63new BigDecimal(Integer.MAX_VALUE),64new BigDecimal(Long.MAX_VALUE-1),65new BigDecimal(BigInteger.valueOf(1), 1),66new BigDecimal(BigInteger.valueOf(100), 50),67};6869for(BigDecimal value : values) {70checkSerialForm(value);71checkSerialForm(value.negate());72}7374}75}767778