import ti.io.*; import java.io.*; import java.lang.*; public class bulkraftest { public static long starttime; public static void MSG(String s) { System.out.println((System.currentTimeMillis() - starttime) / 1000.0 + ": " + s); } public static single void main (String single [] single args) { if (args.length != 2) { if (Ti.thisProc() == 0) System.out.println("Usage: bulktest "); Ti.barrier(); System.exit(1); } String infilename = args[0]; File infile = new File(infilename); String type = args[1]; String outfilename = "bulktest.tmp"; File outfile = new File(outfilename); starttime = System.currentTimeMillis(); try { BulkRandomAccessFile brafin = new BulkRandomAccessFile(infile, "r"); MSG("File size is: " + brafin.length()); int len = (new Long(brafin.length())).intValue(); // Java sucks //------------------------------------------------------------------------------------ if (type.indexOf("byte") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning byte test.. -------------- "); int numelem = len; byte[] a = new byte[numelem]; byte[] b = new byte[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Re-reading.."); brafout.seek(0); brafout.readArray(b, 0, numelem); MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("short") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning short test.. -------------- "); int numelem = len/2; short[] a = new short[numelem]; short[] b = new short[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Re-reading.."); brafout.seek(0); brafout.readArray(b, 0, numelem); MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("int") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning int test.. -------------- "); int numelem = len/4; int[] a = new int[numelem]; int[] b = new int[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Re-reading.."); brafout.seek(0); brafout.readArray(b, 0, numelem); MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("float") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning float test.. -------------- "); int numelem = len/4; float[] a = new float[numelem]; float[] b = new float[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Re-reading.."); brafout.seek(0); brafout.readArray(b, 0, numelem); MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i] && !Float.isNaN(a[i]) && !Float.isNaN(b[i])) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("long") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning long test.. -------------- "); int numelem = len/8; long[] a = new long[numelem]; long[] b = new long[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Re-reading.."); brafout.seek(0); brafout.readArray(b, 0, numelem); MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("consistb") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); RandomAccessFile vrafin = new RandomAccessFile(infile, "r"); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning byte consistency test.. -------------- "); int numelem = len; byte[] a = new byte[numelem]; byte[] b = new byte[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Reference Read.."); for (int i=0; i < numelem; i++) { b[i] = vrafin.readByte(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Integer.toHexString(a[x]) + " b[" + x + "]= 0x" + Integer.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Reference Read.."); RandomAccessFile vrafout = new RandomAccessFile(outfile, "r"); for (int i=0; i < numelem; i++) { b[i] = vrafout.readByte(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Integer.toHexString(a[x]) + " b[" + x + "]= 0x" + Integer.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("consists") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); RandomAccessFile vrafin = new RandomAccessFile(infile, "r"); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning short consistency test.. -------------- "); int numelem = len/2; short[] a = new short[numelem]; short[] b = new short[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Reference Read.."); for (int i=0; i < numelem; i++) { b[i] = vrafin.readShort(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Integer.toHexString(a[x]) + " b[" + x + "]= 0x" + Integer.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Reference Read.."); RandomAccessFile vrafout = new RandomAccessFile(outfile, "r"); for (int i=0; i < numelem; i++) { b[i] = vrafout.readShort(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Integer.toHexString(a[x]) + " b[" + x + "]= 0x" + Integer.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("consisti") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); RandomAccessFile vrafin = new RandomAccessFile(infile, "r"); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning int consistency test.. -------------- "); int numelem = len/4; int[] a = new int[numelem]; int[] b = new int[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Reference Read.."); for (int i=0; i < numelem; i++) { b[i] = vrafin.readInt(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Integer.toHexString(a[x]) + " b[" + x + "]= 0x" + Integer.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Reference Read.."); RandomAccessFile vrafout = new RandomAccessFile(outfile, "r"); for (int i=0; i < numelem; i++) { b[i] = vrafout.readInt(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Integer.toHexString(a[x]) + " b[" + x + "]= 0x" + Integer.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("consistl") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); RandomAccessFile vrafin = new RandomAccessFile(infile, "r"); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning long consistency test.. -------------- "); int numelem = len/8; long[] a = new long[numelem]; long[] b = new long[numelem]; MSG("Seeking.."); brafin.seek(0); MSG("Reading.."); brafin.readArray(a, 0, numelem); MSG("Reference Read.."); for (int i=0; i < numelem; i++) { b[i] = vrafin.readLong(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Long.toHexString(a[x]) + " b[" + x + "]= 0x" + Long.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); MSG("Writing.."); brafout.writeArray(a, 0, numelem); MSG("Reference Read.."); RandomAccessFile vrafout = new RandomAccessFile(outfile, "r"); for (int i=0; i < numelem; i++) { b[i] = vrafout.readLong(); } MSG("Verifying.."); for (int i=0; i < numelem; i++) { if (a[i] != b[i]) { int first = i-5; int last = i+5; if (first < 0) first = 0; if (last >= numelem) last = numelem; for (int x = first; x < last; x++) { MSG(" a[" + x + "]= " + a[x] + " b[" + x + "]= " + b[x]); MSG(" a[" + x + "]= 0x" + Long.toHexString(a[x]) + " b[" + x + "]= 0x" + Long.toHexString(b[x])); } throw new InternalError("Verification mismatch at element " + i); } } MSG("Verified."); } //------------------------------------------------------------------------------------ if (type.indexOf("misc") >= 0 || type.indexOf("all") >= 0) { if (outfile.exists()) outfile.delete(); BulkRandomAccessFile brafout = new BulkRandomAccessFile(outfile, "rw"); MSG(" -------------- Beginning miscellaneous tests.. -------------- "); int sz = 512; int[] a = new int[sz]; int[] b = new int[sz]; for (int i = 0; i < sz; i++) { a[i] = i; b[i] = 0; } MSG("Seeking.."); brafout.seek(0); MSG("Writing.."); brafout.writeArray(a); MSG("Seeking.."); brafout.seek(0); MSG("Reading.."); brafout.readArray(b); MSG("Verifying.."); for (int k=0; k < sz; k++) { if (b[k] != a[k]) throw new InternalError("Verification mismatch at offset " + k + ", b[" + k + "] = " + b[k] + " a[" + k + "] = " + a[k]); } MSG("Verified."); MSG("Seeking.."); brafout.seek(0); MSG("Writing.."); brafout.writeArray(a, 100, 200); MSG("Seeking.."); brafout.seek(100*4); MSG("Reading.."); brafout.readArray(b, 400, 100); MSG("Verifying.."); for (int j=0; j < 100; j++) { if (b[j+400] != a[j+200]) throw new InternalError("Verification mismatch at offset " + j + ", b[" + (j+400) + "] = " + b[j+400] + " a[" + (j+200) + "] = " + a[j+200]); } MSG("Verified."); MSG("Seeking.."); brafin.seek(0); MSG("Testing EOF.."); try { byte[] a = new byte[len+1]; brafin.readArray(a); MSG("EOF Test Failed."); throw new InternalError("EOF Test Failed."); } catch(EOFException exn) { MSG("EOF Test Passed."); } MSG("Testing Legacy EOF.."); BulkRandomAccessFile vraf = new BulkRandomAccessFile(infile,"r"); try { byte x; for (int i=0; i < len; i++) x = vraf.readByte(); } catch(EOFException exn) { MSG("EOF Test Failed."); throw new InternalError("Legacy EOF Test Failed."); } try { byte x; x = vraf.readByte(); MSG("EOF Test Failed."); throw new InternalError("Legacy EOF Test Failed."); } catch(EOFException exn) { MSG("Legacy EOF Test Passed."); } } if (outfile.exists()) outfile.delete(); } catch (Throwable exn) { MSG("caught " + ExnType.gettype(exn) + " : " + exn.getMessage()); } } }