for (int i = 0; i < n; i++) { int tRow = Math2.random(n); Test.ensureEqual(pt.readBinaryInt(3, tRow), tRow, ""); }
/** * This tests this class. * * @throws Throwable if trouble. */ public static void test() throws Throwable { String2.log("\nPersistentTable.test()"); verbose = true; reallyVerbose = true; int n; long time; // find longest FLOAT_LENGTH String s = ("" + Float.MIN_VALUE * -4f / 3f); int longest = s.length(); String longestS = s; for (int i = 0; i < 1000; i++) { s = "" + ((float) Math.random() / -1e10f); if (s.length() > longest) { longest = s.length(); longestS = s; } } String2.log("float longestS=" + longestS + " length=" + longest); Test.ensureTrue(longest <= 15, ""); // find longest DOUBLE_LENGTH s = ("" + Double.MIN_VALUE * -4.0 / 3.0); longest = s.length(); longestS = s; for (int i = 0; i < 1000; i++) { s = "" + (Math.random() / -1e150); if (s.length() > longest) { longest = s.length(); longestS = s; } } String2.log("double longestS=" + longestS + " length=" + longest); Test.ensureTrue(longest <= 24, ""); // make a new table String name = EDStatic.fullTestCacheDirectory + "testPersistentTable.txt"; File2.delete(name); int widths[] = { BOOLEAN_LENGTH, BYTE_LENGTH, BINARY_BYTE_LENGTH, BINARY_CHAR_LENGTH, SHORT_LENGTH, BINARY_SHORT_LENGTH, INT_LENGTH, BINARY_INT_LENGTH, LONG_LENGTH, BINARY_LONG_LENGTH, FLOAT_LENGTH, BINARY_FLOAT_LENGTH, DOUBLE_LENGTH, BINARY_DOUBLE_LENGTH, 20 }; PersistentTable pt = new PersistentTable(name, "rw", widths); Test.ensureEqual(pt.nRows(), 0, ""); Test.ensureEqual(pt.addRows(2), 2, ""); String testS = "Now is the time f\u0F22r all good countrymen to come ..."; pt.writeBoolean(0, 0, true); pt.writeBoolean(0, 1, false); pt.writeByte(1, 0, Byte.MIN_VALUE); pt.writeByte(1, 1, Byte.MAX_VALUE); pt.writeBinaryByte(2, 0, Byte.MIN_VALUE); pt.writeBinaryByte(2, 1, Byte.MAX_VALUE); pt.writeBinaryChar(3, 0, ' '); // hard because read will trim it to "" pt.writeBinaryChar(3, 1, '\u0F22'); pt.writeShort(4, 0, Short.MIN_VALUE); pt.writeShort(4, 1, Short.MAX_VALUE); pt.writeBinaryShort(5, 0, Short.MIN_VALUE); pt.writeBinaryShort(5, 1, Short.MAX_VALUE); pt.writeInt(6, 0, Integer.MIN_VALUE); pt.writeInt(6, 1, Integer.MAX_VALUE); pt.writeBinaryInt(7, 0, Integer.MIN_VALUE); pt.writeBinaryInt(7, 1, Integer.MAX_VALUE); pt.writeLong(8, 0, Long.MIN_VALUE); pt.writeLong(8, 1, Long.MAX_VALUE); pt.writeBinaryLong(9, 0, Long.MIN_VALUE); pt.writeBinaryLong(9, 1, Long.MAX_VALUE); pt.writeFloat(10, 0, -Float.MAX_VALUE); pt.writeFloat(10, 1, Float.NaN); pt.writeBinaryFloat(11, 0, -Float.MAX_VALUE); pt.writeBinaryFloat(11, 1, Float.NaN); pt.writeDouble(12, 0, -Double.MAX_VALUE); pt.writeDouble(12, 1, Double.NaN); pt.writeBinaryDouble(13, 0, -Double.MAX_VALUE); pt.writeBinaryDouble(13, 1, Double.NaN); pt.writeString(14, 0, ""); pt.writeString(14, 1, testS); Test.ensureEqual(pt.readBoolean(0, 0), true, ""); Test.ensureEqual(pt.readBoolean(0, 1), false, ""); Test.ensureEqual(pt.readByte(1, 0), Byte.MIN_VALUE, ""); Test.ensureEqual(pt.readByte(1, 1), Byte.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryByte(2, 0), Byte.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryByte(2, 1), Byte.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryChar(3, 0), ' ', ""); Test.ensureEqual(pt.readBinaryChar(3, 1), '\u0F22', ""); Test.ensureEqual(pt.readShort(4, 0), Short.MIN_VALUE, ""); Test.ensureEqual(pt.readShort(4, 1), Short.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryShort(5, 0), Short.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryShort(5, 1), Short.MAX_VALUE, ""); Test.ensureEqual(pt.readInt(6, 0), Integer.MIN_VALUE, ""); Test.ensureEqual(pt.readInt(6, 1), Integer.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryInt(7, 0), Integer.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryInt(7, 1), Integer.MAX_VALUE, ""); Test.ensureEqual(pt.readLong(8, 0), Long.MIN_VALUE, ""); Test.ensureEqual(pt.readLong(8, 1), Long.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryLong(9, 0), Long.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryLong(9, 1), Long.MAX_VALUE, ""); Test.ensureEqual(pt.readFloat(10, 0), -Float.MAX_VALUE, ""); Test.ensureEqual(pt.readFloat(10, 1), Float.NaN, ""); Test.ensureEqual(pt.readBinaryFloat(11, 0), -Float.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryFloat(11, 1), Float.NaN, ""); Test.ensureEqual(pt.readDouble(12, 0), -Double.MAX_VALUE, ""); Test.ensureEqual(pt.readDouble(12, 1), Double.NaN, ""); Test.ensureEqual(pt.readBinaryDouble(13, 0), -Double.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryDouble(13, 1), Double.NaN, ""); Test.ensureEqual(pt.readString(14, 0), "", ""); // only 18 char returned because one takes 3 bytes in UTF-8 Test.ensureEqual(pt.readString(14, 1), testS.substring(0, 18), ""); pt.close(); // reopen the file data still there? pt = new PersistentTable(name, "rw", widths); Test.ensureEqual(pt.nRows(), 2, ""); Test.ensureEqual(pt.readBoolean(0, 0), true, ""); Test.ensureEqual(pt.readBoolean(0, 1), false, ""); Test.ensureEqual(pt.readByte(1, 0), Byte.MIN_VALUE, ""); Test.ensureEqual(pt.readByte(1, 1), Byte.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryByte(2, 0), Byte.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryByte(2, 1), Byte.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryChar(3, 0), ' ', ""); Test.ensureEqual(pt.readBinaryChar(3, 1), '\u0F22', ""); Test.ensureEqual(pt.readShort(4, 0), Short.MIN_VALUE, ""); Test.ensureEqual(pt.readShort(4, 1), Short.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryShort(5, 0), Short.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryShort(5, 1), Short.MAX_VALUE, ""); Test.ensureEqual(pt.readInt(6, 0), Integer.MIN_VALUE, ""); Test.ensureEqual(pt.readInt(6, 1), Integer.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryInt(7, 0), Integer.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryInt(7, 1), Integer.MAX_VALUE, ""); Test.ensureEqual(pt.readLong(8, 0), Long.MIN_VALUE, ""); Test.ensureEqual(pt.readLong(8, 1), Long.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryLong(9, 0), Long.MIN_VALUE, ""); Test.ensureEqual(pt.readBinaryLong(9, 1), Long.MAX_VALUE, ""); Test.ensureEqual(pt.readFloat(10, 0), -Float.MAX_VALUE, ""); Test.ensureEqual(pt.readFloat(10, 1), Float.NaN, ""); Test.ensureEqual(pt.readBinaryFloat(11, 0), -Float.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryFloat(11, 1), Float.NaN, ""); Test.ensureEqual(pt.readDouble(12, 0), -Double.MAX_VALUE, ""); Test.ensureEqual(pt.readDouble(12, 1), Double.NaN, ""); Test.ensureEqual(pt.readBinaryDouble(13, 0), -Double.MAX_VALUE, ""); Test.ensureEqual(pt.readBinaryDouble(13, 1), Double.NaN, ""); Test.ensureEqual(pt.readString(14, 0), "", ""); // only 18 char returned because one takes 3 bytes in UTF-8 Test.ensureEqual(pt.readString(14, 1), testS.substring(0, 18), ""); pt.close(); String modes[] = {"rw", "rw", "rws", "rwd"}; n = 1000; for (int mode = 0; mode < modes.length; mode++) { File2.delete(name); pt = new PersistentTable( name, modes[mode], new int[] {80, BINARY_DOUBLE_LENGTH, DOUBLE_LENGTH, BINARY_INT_LENGTH, INT_LENGTH}); pt.addRows(n); if (mode == 1) String2.log("*** Note: 2nd rw test uses flush()"); // string speed test time = System.currentTimeMillis(); long modeTime = System.currentTimeMillis(); for (int i = 0; i < n; i++) pt.writeString(0, i, testS + i); if (mode == 1) pt.flush(); time = System.currentTimeMillis(); String2.log( "\n" + modes[mode] + " time to write " + n + " Strings=" + (System.currentTimeMillis() - time) + " (" + new int[] {0, 0, 0, 0}[mode] + "ms)"); // java 1.6 0,0,0,0 for (int i = 0; i < n; i++) { int tRow = Math2.random(n); Test.ensureEqual(pt.readString(0, tRow), testS + tRow, ""); } String2.log( modes[mode] + " time to read " + n + " Strings=" + (System.currentTimeMillis() - time) + " (" + new int[] {15, 16, 47, 15}[mode]
for (int i = 0; i < n; i++) { int tRow = Math2.random(n); Test.ensureEqual(pt.readDouble(2, tRow), tRow, ""); }
/** This tests SgtUtil. */ public static void test() throws Exception { // test splitLine String2.log("\n*** SgtUtil.test"); StringArray sa = new StringArray(); // wide sa.clear(); splitLine(38, sa, "This is a test of splitline."); Test.ensureEqual(sa.size(), 1, ""); Test.ensureEqual(sa.get(0), "This is a test of splitline.", ""); // narrow sa.clear(); splitLine(12, sa, "This is a test of splitline."); Test.ensureEqual(sa.size(), 3, ""); Test.ensureEqual(sa.get(0), "This is a ", ""); Test.ensureEqual(sa.get(1), "test of ", ""); Test.ensureEqual(sa.get(2), "splitline.", ""); // narrow and can't split, so chop at limit sa.clear(); splitLine(12, sa, "This1is2a3test4of5splitline."); Test.ensureEqual(sa.size(), 3, ""); Test.ensureEqual(sa.get(0), "This1is2a3t", ""); Test.ensureEqual(sa.get(1), "est4of5split", ""); Test.ensureEqual(sa.get(2), "line.", ""); // caps sa.clear(); splitLine(12, sa, "THESE ARE a a REALLY WIDE."); Test.ensureEqual(sa.size(), 3, ""); Test.ensureEqual(sa.get(0), "THESE ARE ", ""); Test.ensureEqual(sa.get(1), "a a REALLY ", ""); Test.ensureEqual(sa.get(2), "WIDE.", ""); // test suggestPaletteRange Test.ensureEqual( suggestPaletteRange(.3, 8.9), new double[] {0, 10}, ""); // typical Rainbow Linear Test.ensureEqual( suggestPaletteRange(.11, 890), new double[] {.1, 1000}, ""); // typical Rainbow Log Test.ensureEqual( suggestPaletteRange(-7, 8), new double[] {-10, 10}, ""); // typical BlueWhiteRed Linear symmetric // test suggestPalette Test.ensureEqual( suggestPalette(.3, 8.9), "WhiteRedBlack", ""); // small positive, large positive Test.ensureEqual(suggestPalette(300, 890), "Rainbow", ""); // typical Rainbow Log Test.ensureEqual( suggestPalette(-7, 8), "BlueWhiteRed", ""); // typical BlueWhiteRed Linear symmetric // test suggestPaletteScale Test.ensureEqual(suggestPaletteScale(.3, 8.9), "Linear", ""); // typical Rainbow Linear Test.ensureEqual(suggestPaletteScale(.11, 890), "Log", ""); // typical Rainbow Log Test.ensureEqual( suggestPaletteScale(-7, 8), "Linear", ""); // typical BlueWhiteRed Linear symmetric BufferedImage bi = ImageIO.read(new File(String2.unitTestDataDir + "graphs/erdBAssta5day.png")); long time = System.currentTimeMillis(); Test.ensureEqual(findGraph(bi), new int[] {24, 334, 150, 21}, ""); String2.log("findGraph time=" + (System.currentTimeMillis() - time)); }