blob: 2fafcbaed4565e77390a4ae8fa098618760dabde [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 @bug 4092350
26 @summary Verify that reads and writes of primitives are correct
27 */
28
29// The bug mentioned is actually a performance bug that prompted
30// changes in the methods to write primitives
31import java.io.*;
32
33import java.io.*;
34
35public class ReadWritePrimitives {
36
37 public static void main(String args[]) throws IOException {
38 long start, finish;
39 start = System.currentTimeMillis();
40 testShort();
41 finish = System.currentTimeMillis();
42 // System.err.println("Time taken="+(finish-start));
43 start = System.currentTimeMillis();
44 testChar();
45 finish = System.currentTimeMillis();
46 // System.err.println("Time taken="+(finish-start));
47 start = System.currentTimeMillis();
48 testInt();
49 finish = System.currentTimeMillis();
50 // System.err.println("Time taken="+(finish-start));
51 start = System.currentTimeMillis();
52 testLong();
53 finish = System.currentTimeMillis();
54 // System.err.println("Time taken="+(finish-start));
55 }
56
57 private static void testShort() throws IOException {
58 File fh = new File(System.getProperty("test.dir", "."),
59 "x.ReadWriteGenerated");
60 RandomAccessFile f = new RandomAccessFile(fh,"rw");
61 for(int i = 0; i < 10000; i++){
62 f.writeShort((short)i);
63 }
64 f.writeShort((short)65535);
65 f.close();
66 f = new RandomAccessFile(fh,"r");
67 for(int i = 0; i < 10000; i++) {
68 short r = f.readShort();
69 if (r != ((short)i)) {
70 System.err.println("An error occurred. Read:" + r
71 + " i:" + ((short)i));
72 throw new IOException("Bad read from a writeShort");
73 }
74 }
75 short rmax = f.readShort();
76 if (rmax != ((short)65535)) {
77 System.err.println("An error occurred. Read:" + rmax);
78 throw new IOException("Bad read from a writeShort");
79 }
80 f.close();
81 }
82
83 private static void testChar() throws IOException {
84 File fh = new File(System.getProperty("test.dir", "."),
85 "x.ReadWriteGenerated");
86 RandomAccessFile f = new RandomAccessFile(fh,"rw");
87 for(int i = 0; i < 10000; i++){
88 f.writeChar((char)i);
89 }
90 f.close();
91 f = new RandomAccessFile(fh,"r");
92 for(int i = 0; i < 10000; i++) {
93 char r = f.readChar();
94 if (r != ((char)i)){
95 System.err.println("An error occurred. Read:" + r
96 + " i:" + ((char) i));
97 throw new IOException("Bad read from a writeChar");
98 }
99 }
100 f.close();
101 }
102
103
104 private static void testInt() throws IOException {
105 File fh = new File(System.getProperty("test.dir", "."),
106 "x.ReadWriteGenerated");
107 RandomAccessFile f = new RandomAccessFile(fh,"rw");
108 for(int i = 0; i < 10000; i++){
109 f.writeInt((short)i);
110 }
111 f.writeInt(Integer.MAX_VALUE);
112 f.close();
113 f = new RandomAccessFile(fh, "r");
114 for(int i = 0; i < 10000; i++) {
115 int r = f.readInt();
116 if (r != i){
117 System.err.println("An error occurred. Read:" + r
118 + " i:" + i);
119 throw new IOException("Bad read from a writeInt");
120 }
121 }
122 int rmax = f.readInt();
123 if (rmax != Integer.MAX_VALUE){
124 System.err.println("An error occurred. Read:" + rmax);
125 throw new IOException("Bad read from a writeInt");
126 }
127 f.close();
128 }
129
130 private static void testLong() throws IOException {
131 File fh = new File(System.getProperty("test.dir", "."),
132 "x.ReadWriteGenerated");
133 RandomAccessFile f = new RandomAccessFile(fh,"rw");
134 for(int i = 0; i < 10000; i++){
135 f.writeLong(123456789L * (long)i);
136 }
137 f.close();
138 f = new RandomAccessFile(fh,"r");
139 for(int i = 0; i < 10000; i++){
140 long r = f.readLong();
141 if (r != (((long) i) * 123456789L) ) {
142 System.err.println("An error occurred. Read:" + r
143 + " i" + ((long) i));
144
145 throw new IOException("Bad read from a writeInt");
146 }
147 }
148 f.close();
149
150 }
151
152}