PR target/27599
[official-gcc.git] / libjava / classpath / test / java.io / RandomAccessFileTest.java
blob8ee53b735f1f43eab8182edcfadf038effe6799d
1 /*************************************************************************
2 /* RandomAccessFileTest.java -- Tests RandomAccessFile's
3 /*
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
6 /*
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 /*************************************************************************/
22 import java.io.*;
24 // Write some data using DataOutput and read it using DataInput.
26 public class RandomAccessFileTest
29 public static void
30 runReadTest(String filename, int seq, String testname)
32 try
34 System.out.println("Test " + seq + ": " + testname);
36 RandomAccessFile ras = new RandomAccessFile(filename, "r");
38 boolean passed = true;
40 boolean b = ras.readBoolean();
41 if (b != true)
43 passed = false;
44 System.out.println("Failed to read boolean. Expected true and got false");
46 b = ras.readBoolean();
47 if (b != false)
49 passed = false;
50 System.out.println("Failed to read boolean. Expected false and got true");
52 byte bt = ras.readByte();
53 if (bt != 8)
55 passed = false;
56 System.out.println("Failed to read byte. Expected 8 and got "+ bt);
58 bt = ras.readByte();
59 if (bt != -122)
61 passed = false;
62 System.out.println("Failed to read byte. Expected -122 and got "+ bt);
64 char c = ras.readChar();
65 if (c != 'a')
67 passed = false;
68 System.out.println("Failed to read char. Expected a and got " + c);
70 c = ras.readChar();
71 if (c != '\uE2D2')
73 passed = false;
74 System.out.println("Failed to read char. Expected \\uE2D2 and got " + c);
76 short s = ras.readShort();
77 if (s != 32000)
79 passed = false;
80 System.out.println("Failed to read short. Expected 32000 and got " + s);
82 int i = ras.readInt();
83 if (i != 8675309)
85 passed = false;
86 System.out.println("Failed to read int. Expected 8675309 and got " + i);
88 long l = ras.readLong();
89 if (l != 696969696969L)
91 passed = false;
92 System.out.println("Failed to read long. Expected 696969696969 and got " + l);
94 float f = ras.readFloat();
95 if (!Float.toString(f).equals("3.1415"))
97 passed = false;
98 System.out.println("Failed to read float. Expected 3.1415 and got " + f);
100 double d = ras.readDouble();
101 if (d != 999999999.999)
103 passed = false;
104 System.out.println("Failed to read double. Expected 999999999.999 and got " + d);
106 String str = ras.readUTF();
107 if (!str.equals("Testing code is such a boring activity but it must be done"))
109 passed = false;
110 System.out.println("Read unexpected String: " + str);
112 str = ras.readUTF();
113 if (!str.equals("a-->\u01FF\uA000\u6666\u0200RRR"))
115 passed = false;
116 System.out.println("Read unexpected String: " + str);
119 if (passed)
120 System.out.println("PASSED: " + testname + " read test");
121 else
122 System.out.println("FAILED: " + testname + " read test");
124 catch (IOException e)
126 System.out.println("FAILED: " + testname + " read test: " + e);
131 public static void
132 main(String[] argv)
134 System.out.println("Started test of RandomAccessFile");
136 System.out.println("Test 1: RandomAccessFile write test");
139 RandomAccessFile raf = new RandomAccessFile("dataoutput.out", "rw");
141 raf.writeBoolean(true);
142 raf.writeBoolean(false);
143 raf.writeByte((byte)8);
144 raf.writeByte((byte)-122);
145 raf.writeChar((char)'a');
146 raf.writeChar((char)'\uE2D2');
147 raf.writeShort((short)32000);
148 raf.writeInt((int)8675309);
149 raf.writeLong((long) 696969696969L);
150 raf.writeFloat((float)3.1415);
151 raf.writeDouble((double)999999999.999);
152 raf.writeUTF("Testing code is such a boring activity but it must be done");
153 raf.writeUTF("a-->\u01FF\uA000\u6666\u0200RRR");
154 raf.close();
156 // We'll find out if this was really right later, but conditionally
157 // report success for now
158 System.out.println("PASSED: RandomAccessFile write test");
160 catch(IOException e)
162 System.out.println("FAILED: RandomAccessFile write test: " + e);
165 runReadTest("dataoutput.out", 2, "Read of JCL written data file");
166 runReadTest("dataoutput-jdk.out", 3, "Read of JDK written data file");
168 System.out.println("Test 2: Seek Test");
171 RandomAccessFile raf = new RandomAccessFile("/etc/services", "r");
173 System.out.println("Length: " + raf.length());
175 raf.skipBytes(24);
176 if (raf.getFilePointer() != 24)
177 throw new IOException("Unexpected file pointer value " +
178 raf.getFilePointer());
180 raf.seek(0);
181 if (raf.getFilePointer() != 0)
182 throw new IOException("Unexpected file pointer value " +
183 raf.getFilePointer());
185 raf.seek(100);
186 if (raf.getFilePointer() != 100)
187 throw new IOException("Unexpected file pointer value " +
188 raf.getFilePointer());
190 System.out.println("PASSED: Seek Test");
192 catch(IOException e)
194 System.out.println("FAILED: Seek Test: " + e);
197 System.out.println("Test 3: Validation Test");
198 boolean failed = false;
201 new RandomAccessFile("/vmlinuz", "rwx");
202 System.out.println("Did not detect invalid mode");
203 failed = true;
205 catch (IllegalArgumentException e) { ; }
206 catch (IOException e) { ; }
210 new RandomAccessFile("/vmlinuz", "rw");
211 System.out.println("Did not detect read only file opened for write");
212 failed = true;
214 catch (IOException e) { ; }
218 new RandomAccessFile("/sherlockholmes", "r");
219 System.out.println("Did not detect non-existent file");
220 failed = true;
222 catch (IOException e) { ; }
226 RandomAccessFile raf = new RandomAccessFile("/etc/services", "r");
227 raf.seek(raf.length());
228 raf.write('\n');
229 System.out.println("Did not detect invalid write operation on read only file");
230 failed = true;
232 catch (IOException e) { ; }
234 if (failed)
235 System.out.println("FAILED: Validation Test");
236 else
237 System.out.println("PASSED: Validation Test");
240 System.out.println("Test 4: Set File Length Rest");
243 File f = new File("tmptmptmp");
244 RandomAccessFile raf = new RandomAccessFile("tmptmptmp", "rw");
246 raf.setLength(50L);
247 if (raf.length() != 50)
248 throw new IOException("Bad length on extending file of " + raf.length());
250 raf.setLength(25L);
251 if (raf.length() != 25)
252 throw new IOException("Bad length on extending file of " + raf.length());
254 raf.close();
255 f.delete();
257 System.out.println("PASSED: Set File Length Test");
259 catch(IOException e)
261 System.out.println("FAILED: Set File Length Test: " + e);
262 (new File("tmptmptmp")).delete();
265 System.out.println("Finished test of RandomAccessFile");
266 } // main
268 } // class DataInputOutputTest