package org.armedbear.lisp.util; import java.io.*; import java.util.*; import junit.framework.*; public class RACFTest extends TestCase { public RACFTest(String name) { super(name); } // public void test1() throws Exception { // RandomAccessCharacterFile f = new RandomAccessCharacterFile( // new RandomAccessFile(new File("test.dat"), // "rw"), // null); // try { // InputStream in = f.getInputStream(); // OutputStream out = f.getOutputStream(); // out.write('a'); // f.position(0); // // assertEquals('a', (char) in.read()); //// in.read(); //passes if you do this // // //alt //// byte[] buf = new byte[1]; //passes if len is 2 //// assertEquals(1, in.read(buf)); //// assertEquals('a', (char)buf[0]); // // assertEquals(1, f.position()); // out.write('b'); // assertEquals(2, f.position()); // } finally { // f.close(); // } // } public void test2() throws Exception { RandomAccessCharacterFile f = openRacf("test.dat"); try { write(f, 'a'); f.position(0); byte[] buf = new byte[1]; //passes if len is 2 assertEquals(1, f.read(buf, 0, buf.length)); assertEquals('a', (char)buf[0]); assertEquals(1, f.position()); write(f, 'b'); assertEquals(2, f.position()); } finally { f.close(); } } private RandomAccessCharacterFile openRacf(String name) throws IOException, FileNotFoundException { return new RandomAccessCharacterFile( new RandomAccessFile(new File(name), "rw"), null); } void write(RandomAccessCharacterFile f, char c) throws IOException { byte[] buf = new byte[1]; buf[0] = (byte) c; f.write(buf, 0, buf.length); } public void test3() throws Exception { int iters = 1500; int arraySize = 1025; byte[] b = new byte[arraySize]; Random rand = new Random(); RandomAccessCharacterFile f = openRacf("test3.dat"); byte[] buf = new byte[1]; for(int i = 0; i < iters; i++) { int nbytes = rand.nextInt(arraySize); if (rand.nextInt(2) == 0 || f.length() == 0) { long before = f.length(); f.position(before); write2(f, b, 0, nbytes); // f.flushBbuf(false); //on ABCL, don't need this before file-length assertEquals("iter " + i, nbytes, f.length() - before); } else { f.position(rand.nextInt((int) f.length())); f.read(buf, 0, buf.length); } } } private void write2(RandomAccessCharacterFile f, byte[] b, int offset, int len) throws IOException { f.write(b, offset, len); } }