OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [test/] [java.io/] [RandomAccessFileTest.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/*************************************************************************
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.
11
/*
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.
16
/*
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
/*************************************************************************/
21
 
22
import java.io.*;
23
 
24
// Write some data using DataOutput and read it using DataInput.
25
 
26
public class RandomAccessFileTest
27
{
28
 
29
public static void
30
runReadTest(String filename, int seq, String testname)
31
{
32
  try
33
    {
34
      System.out.println("Test " + seq + ": " + testname);
35
 
36
      RandomAccessFile ras = new RandomAccessFile(filename, "r");
37
 
38
      boolean passed = true;
39
 
40
      boolean b = ras.readBoolean();
41
      if (b != true)
42
        {
43
          passed = false;
44
          System.out.println("Failed to read boolean. Expected true and got false");
45
        }
46
      b = ras.readBoolean();
47
      if (b != false)
48
        {
49
          passed = false;
50
          System.out.println("Failed to read boolean. Expected false and got true");
51
        }
52
      byte bt = ras.readByte();
53
      if (bt != 8)
54
        {
55
          passed = false;
56
          System.out.println("Failed to read byte. Expected 8 and got "+ bt);
57
        }
58
      bt = ras.readByte();
59
      if (bt != -122)
60
        {
61
          passed = false;
62
          System.out.println("Failed to read byte. Expected -122 and got "+ bt);
63
        }
64
      char c = ras.readChar();
65
      if (c != 'a')
66
        {
67
          passed = false;
68
          System.out.println("Failed to read char. Expected a and got " + c);
69
        }
70
      c = ras.readChar();
71
      if (c != '\uE2D2')
72
        {
73
          passed = false;
74
          System.out.println("Failed to read char. Expected \\uE2D2 and got " + c);
75
        }
76
      short s = ras.readShort();
77
      if (s != 32000)
78
        {
79
          passed = false;
80
          System.out.println("Failed to read short. Expected 32000 and got " + s);
81
        }
82
      int i = ras.readInt();
83
      if (i != 8675309)
84
        {
85
          passed = false;
86
          System.out.println("Failed to read int. Expected 8675309 and got " + i);
87
        }
88
      long l = ras.readLong();
89
      if (l != 696969696969L)
90
        {
91
          passed = false;
92
          System.out.println("Failed to read long. Expected 696969696969 and got " + l);
93
        }
94
      float f = ras.readFloat();
95
      if (!Float.toString(f).equals("3.1415"))
96
        {
97
          passed = false;
98
          System.out.println("Failed to read float. Expected 3.1415 and got " + f);
99
        }
100
      double d = ras.readDouble();
101
      if (d != 999999999.999)
102
        {
103
          passed = false;
104
          System.out.println("Failed to read double.  Expected 999999999.999 and got " + d);
105
        }
106
      String str = ras.readUTF();
107
      if (!str.equals("Testing code is such a boring activity but it must be done"))
108
        {
109
          passed = false;
110
          System.out.println("Read unexpected String: " + str);
111
        }
112
      str = ras.readUTF();
113
      if (!str.equals("a-->\u01FF\uA000\u6666\u0200RRR"))
114
        {
115
          passed = false;
116
          System.out.println("Read unexpected String: " + str);
117
        }
118
 
119
      if (passed)
120
        System.out.println("PASSED: " + testname + " read test");
121
      else
122
        System.out.println("FAILED: " + testname + " read test");
123
    }
124
  catch (IOException e)
125
    {
126
      System.out.println("FAILED: " + testname + " read test: " + e);
127
    }
128
 
129
}
130
 
131
public static void
132
main(String[] argv)
133
{
134
  System.out.println("Started test of RandomAccessFile");
135
 
136
  System.out.println("Test 1: RandomAccessFile write test");
137
  try
138
    {
139
      RandomAccessFile raf = new RandomAccessFile("dataoutput.out", "rw");
140
 
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();
155
 
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");
159
    }
160
  catch(IOException e)
161
    {
162
      System.out.println("FAILED: RandomAccessFile write test: " + e);
163
    }
164
 
165
  runReadTest("dataoutput.out", 2, "Read of JCL written data file");
166
  runReadTest("dataoutput-jdk.out", 3, "Read of JDK written data file");
167
 
168
  System.out.println("Test 2: Seek Test");
169
  try
170
    {
171
      RandomAccessFile raf = new RandomAccessFile("/etc/services", "r");
172
 
173
      System.out.println("Length: " + raf.length());
174
 
175
      raf.skipBytes(24);
176
      if (raf.getFilePointer() != 24)
177
        throw new IOException("Unexpected file pointer value " +
178
                              raf.getFilePointer());
179
 
180
      raf.seek(0);
181
      if (raf.getFilePointer() != 0)
182
        throw new IOException("Unexpected file pointer value " +
183
                              raf.getFilePointer());
184
 
185
      raf.seek(100);
186
      if (raf.getFilePointer() != 100)
187
        throw new IOException("Unexpected file pointer value " +
188
                              raf.getFilePointer());
189
 
190
      System.out.println("PASSED: Seek Test");
191
    }
192
  catch(IOException e)
193
    {
194
      System.out.println("FAILED: Seek Test: " + e);
195
    }
196
 
197
  System.out.println("Test 3: Validation Test");
198
  boolean failed = false;
199
  try
200
    {
201
      new RandomAccessFile("/vmlinuz", "rwx");
202
      System.out.println("Did not detect invalid mode");
203
      failed = true;
204
    }
205
  catch (IllegalArgumentException e) { ; }
206
  catch (IOException e) { ; }
207
 
208
  try
209
    {
210
      new RandomAccessFile("/vmlinuz", "rw");
211
      System.out.println("Did not detect read only file opened for write");
212
      failed = true;
213
    }
214
  catch (IOException e) { ; }
215
 
216
  try
217
    {
218
      new RandomAccessFile("/sherlockholmes", "r");
219
      System.out.println("Did not detect non-existent file");
220
      failed = true;
221
    }
222
  catch (IOException e) { ; }
223
 
224
  try
225
    {
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;
231
    }
232
  catch (IOException e) { ; }
233
 
234
  if (failed)
235
    System.out.println("FAILED: Validation Test");
236
  else
237
    System.out.println("PASSED: Validation Test");
238
 
239
/*
240
  System.out.println("Test 4: Set File Length Rest");
241
  try
242
    {
243
      File f = new File("tmptmptmp");
244
      RandomAccessFile raf = new RandomAccessFile("tmptmptmp", "rw");
245
 
246
      raf.setLength(50L);
247
      if (raf.length() != 50)
248
        throw new IOException("Bad length on extending file of " + raf.length());
249
 
250
      raf.setLength(25L);
251
      if (raf.length() != 25)
252
        throw new IOException("Bad length on extending file of " + raf.length());
253
 
254
      raf.close();
255
      f.delete();
256
 
257
      System.out.println("PASSED: Set File Length Test");
258
    }
259
  catch(IOException e)
260
    {
261
      System.out.println("FAILED: Set File Length Test: " + e);
262
      (new File("tmptmptmp")).delete();
263
    }
264
*/
265
  System.out.println("Finished test of RandomAccessFile");
266
} // main
267
 
268
} // class DataInputOutputTest
269
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.