Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/oldTests/BinaryTree.java
38828 views
/*1* Copyright (c) 2005, 2010, 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/* @test24* @summary it is new version of old test which was25* /src/share/test/serialization/piotest.java26* Test of serialization/deserialization of27* objects with BinaryTree types28*/2930import java.io.*;3132public class BinaryTree {33public static void main (String argv[]) {34System.err.println("\nRegression test for testing of " +35"serialization/deserialization of objects " +36"with BinaryTree types \n");3738try {39BinaryTreeTest base = new BinaryTreeTest(2);40FileOutputStream ostream = new FileOutputStream("piotest3.tmp");41try {42ObjectOutputStream p = new ObjectOutputStream(ostream);43p.writeObject(null);44p.writeObject(base);45p.flush();46} finally {47ostream.close();48}4950FileInputStream istream = new FileInputStream("piotest3.tmp");51try {52ObjectInputStream q = new ObjectInputStream(istream);53Object n = q.readObject();54if (n != null) {55System.err.println("\nnull read as " + n);56}57BinaryTreeTest nbase = (BinaryTreeTest)q.readObject();58if (!base.equals(nbase)) {59System.err.println("\nTEST FAILED: BinaryTree read " +60"incorrectly.");61throw new Error();62}63} finally {64istream.close();65}6667System.err.println("\nTEST PASSED");68} catch (Exception e) {69System.err.print("TEST FAILED: ");70e.printStackTrace();71throw new Error();72}73}74}7576class BinaryTreeTest implements java.io.Serializable {77public BinaryTreeTest left;78public BinaryTreeTest right;79public int id;80public int level;8182private static int count = 0;8384public BinaryTreeTest(int l) {85id = count++;86level = l;87if (l > 0) {88left = new BinaryTreeTest(l-1);89right = new BinaryTreeTest(l-1);90}91}9293public void print(int levels) {94for (int i = 0; i < level; i++) {95System.out.print(" ");96}97System.err.println("node " + id);9899if (level <= levels && left != null) {100left.print(levels);101}102103if (level <= levels && right != null) {104right.print(levels);105}106}107108public boolean equals(BinaryTreeTest other) {109if (other == null) {110return false;111}112113if (id != other.id) {114return false;115}116117if (left != null && !left.equals(other.left)) {118return false;119}120121if (right != null && !right.equals(other.right)) {122return false;123}124125return true;126}127}128129130