1 /*************************************************************************
2 /* DataInputOutputTest.java -- Tests Data{Input,Output}Stream's
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
7 /* This program is free software; you can redistribute it and/or modify
8 /* it under the terms of the GNU General Public License as published
9 /* by the Free Software Foundation, either version 2 of the License, or
10 /* (at your option) any later version.
12 /* This program is distributed in the hope that it will be useful, but
13 /* WITHOUT ANY WARRANTY; without even the implied warranty of
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 /* GNU General Public License for more details.
17 /* You should have received a copy of the GNU General Public License
18 /* along with this program; if not, write to the Free Software Foundation
19 /* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*************************************************************************/
24 // Write some data using DataOutput and read it using DataInput.
26 public class DataInputOutputTest
30 runReadTest(String filename
, int seq
, String testname
)
34 System
.out
.println("Test " + seq
+ ": " + testname
);
36 FileInputStream fis
= new FileInputStream(filename
);
37 DataInputStream dis
= new DataInputStream(fis
);
39 boolean passed
= true;
41 boolean b
= dis
.readBoolean();
45 System
.out
.println("Failed to read boolean. Expected true and got false");
47 b
= dis
.readBoolean();
51 System
.out
.println("Failed to read boolean. Expected false and got true");
53 byte bt
= dis
.readByte();
57 System
.out
.println("Failed to read byte. Expected 8 and got "+ bt
);
63 System
.out
.println("Failed to read byte. Expected -122 and got "+ bt
);
65 char c
= dis
.readChar();
69 System
.out
.println("Failed to read char. Expected a and got " + c
);
75 System
.out
.println("Failed to read char. Expected \\uE2D2 and got " + c
);
77 short s
= dis
.readShort();
81 System
.out
.println("Failed to read short. Expected 32000 and got " + s
);
83 int i
= dis
.readInt();
87 System
.out
.println("Failed to read int. Expected 8675309 and got " + i
);
89 long l
= dis
.readLong();
90 if (l
!= 696969696969L)
93 System
.out
.println("Failed to read long. Expected 696969696969 and got " + l
);
95 float f
= dis
.readFloat();
96 if (!Float
.toString(f
).equals("3.1415"))
99 System
.out
.println("Failed to read float. Expected 3.1415 and got " + f
);
101 double d
= dis
.readDouble();
102 if (d
!= 999999999.999)
105 System
.out
.println("Failed to read double. Expected 999999999.999 and got " + d
);
107 String str
= dis
.readUTF();
108 if (!str
.equals("Testing code is such a boring activity but it must be done"))
111 System
.out
.println("Read unexpected String: " + str
);
114 if (!str
.equals("a-->\u01FF\uA000\u6666\u0200RRR"))
117 System
.out
.println("Read unexpected String: " + str
);
121 System
.out
.println("PASSED: " + testname
+ " read test");
123 System
.out
.println("FAILED: " + testname
+ " read test");
125 catch (IOException e
)
127 System
.out
.println("FAILED: " + testname
+ " read test: " + e
);
135 System
.out
.println("Started test of DataInputStream and DataOutputStream");
139 System
.out
.println("Test 1: DataOutputStream write test");
141 FileOutputStream fos
= new FileOutputStream("dataoutput.out");
142 DataOutputStream dos
= new DataOutputStream(fos
);
144 dos
.writeBoolean(true);
145 dos
.writeBoolean(false);
146 dos
.writeByte((byte)8);
147 dos
.writeByte((byte)-122);
148 dos
.writeChar((char)'a');
149 dos
.writeChar((char)'\uE2D2');
150 dos
.writeShort((short)32000);
151 dos
.writeInt((int)8675309);
152 dos
.writeLong(696969696969L);
153 dos
.writeFloat((float)3.1415);
154 dos
.writeDouble((double)999999999.999);
155 dos
.writeUTF("Testing code is such a boring activity but it must be done");
156 dos
.writeUTF("a-->\u01FF\uA000\u6666\u0200RRR");
159 // We'll find out if this was really right later, but conditionally
160 // report success for now
161 System
.out
.println("PASSED: DataOutputStream write test");
165 System
.out
.println("FAILED: DataOutputStream write test: " + e
);
168 runReadTest("dataoutput.out", 2, "Read of JCL written data file");
169 runReadTest("dataoutput-jdk.out", 3, "Read of JDK written data file");
173 } // class DataInputOutputTest