Quadcap Embeddable Server

com/quadcap/util/Test.java

Go to the documentation of this file.
00001 package com.quadcap.util; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.*; 00042 import java.util.*; 00043 00044 /** 00045 * Test harness for the com.quadcap.util package. This class is also the 00046 * base class for other package Test classes. 00047 * 00048 * @author Stan Bailes 00049 */ 00050 public class Test { 00051 String currentTest = ""; 00052 OutputStream out; 00053 protected PrintWriter writer; 00054 String outFile; 00055 boolean generate = false; 00056 boolean verbose = false; 00057 protected boolean failed = false; 00058 00059 /** 00060 * Test constructor. Open the output file as specified. 00061 */ 00062 public Test() { 00063 try { 00064 outFile = System.getProperty("out"); 00065 generate = Util.boolProperty("gen"); 00066 if (outFile != null) { 00067 if (generate) { 00068 out = new FileOutputStream(outFile); 00069 } else { 00070 out = new FileOutputStream(outFile + ".gen"); 00071 } 00072 out = new SedOutputStream(out); 00073 writer = new PrintWriter(out); 00074 } else { 00075 writer = new PrintWriter(System.out); 00076 } 00077 } catch (IOException e) { 00078 Debug.print(e); 00079 System.out.println("ERROR: " + e); 00080 } 00081 } 00082 00083 class SedOutputStream extends OutputStream { 00084 int state = 0; 00085 OutputStream out; 00086 int[] lit = { 'T', 'E', 'S', 'T' }; 00087 SedOutputStream(OutputStream out) { this.out = out; } 00088 public void write(int c) throws IOException { 00089 boolean skip = false; 00090 if (state == 4) { 00091 if (Character.isDigit((char)c) || c == '_') { 00092 skip = true; 00093 } else { 00094 state = 0; 00095 } 00096 } else { 00097 if (lit[state] == Character.toUpperCase((char)c)) state++; 00098 else if (lit[0] == c) state = 1; 00099 else state = 0; 00100 } 00101 if (!skip) out.write(c); 00102 } 00103 } 00104 00105 /** 00106 * For tests which generate output to a stream, compare the actual stream 00107 * to the expected (pregenerated) stream. If the <code>gen</code> 00108 * system property is <b>true</b>, we simply close the stream, which 00109 * is used to generate the file of expected data used to compare in 00110 * subsequent test runs. 00111 */ 00112 public void checkOutput() { 00113 writer.flush(); 00114 if (outFile != null) { 00115 try { 00116 out.close(); 00117 if (!generate) { 00118 failed = 0 != Util.execCommand("diff -w " + 00119 outFile + " " + 00120 outFile + ".gen", 00121 System.out); 00122 printFinal(); 00123 } 00124 } catch (IOException e) { 00125 Debug.print(e); 00126 System.out.println("ERROR: " + e); 00127 return; 00128 } 00129 } else { 00130 printFinal(); 00131 } 00132 } 00133 00134 final void printFinal() { 00135 if (outFile == null) { 00136 outFile = System.getProperty("tests"); 00137 } 00138 if (failed) { 00139 System.out.println("FAILED: " + outFile); 00140 if (Boolean.getBoolean("exitOnFail")) { 00141 System.exit(1); 00142 } 00143 } else { 00144 System.out.println("PASSED: " + outFile); 00145 } 00146 } 00147 00148 /** 00149 * Indicate a test error with the specified detail message 00150 * 00151 * @param s the message 00152 */ 00153 public void testError(String s) { 00154 System.out.println("ERROR: " + currentTest + ": " + s); 00155 failed = true; 00156 } 00157 00158 /** 00159 * Compare two strings, resulting in a test error if the strings are 00160 * not equal 00161 * 00162 * @param exp the expected value 00163 * @param act the actual value 00164 */ 00165 public void testCompare(String exp, String act) { 00166 if (!act.equals(exp)) { 00167 testError("expected = " + exp + ", actual = " + act); 00168 } 00169 } 00170 00171 /** 00172 * Assert 00173 */ 00174 public void testAssert(boolean val, String msg) { 00175 if (!val) testError(msg); 00176 } 00177 00178 public void testAssert(boolean val) { 00179 if (!val) { 00180 String t = Util.stackTrace(); 00181 int idx = t.indexOf("\n"); 00182 if (idx > 0) { 00183 t = t.substring(idx+1); 00184 idx = t.indexOf("\n"); 00185 if (idx > 0) { 00186 t = t.substring(0, idx); 00187 } 00188 } 00189 testError("Assert failed: " + t); 00190 } 00191 } 00192 00193 /** 00194 * Main test driver. Set up environment, run each test by using reflection 00195 * to find the methods in the derived class as specified by the 00196 * <code>tests</code> system property. 00197 */ 00198 public void test(String args[]) { 00199 String tests = System.getProperty("tests"); 00200 Debug.debugMode = Debug.debugAll; 00201 Debug.printLevel = Util.intProperty("debug", 1); 00202 verbose = Util.boolProperty("verbose"); 00203 00204 try { 00205 Class myClass = this.getClass(); 00206 Class[] argTypes = { 00207 Class.forName("[Ljava.lang.String;") 00208 }; 00209 Object[] objArgs = { 00210 args 00211 }; 00212 00213 Vector tv = Util.split(System.getProperty("tests"), ','); 00214 for (int i = 0; i < tv.size(); i++) { 00215 String test = (String)tv.elementAt(i); 00216 currentTest = test; 00217 java.lang.reflect.Method m = 00218 myClass.getDeclaredMethod(test, argTypes); 00219 if (m != null) { 00220 try { 00221 if (verbose) { 00222 System.out.println("test: " + currentTest); 00223 } 00224 m.invoke(this, objArgs); 00225 } catch (Throwable t) { 00226 t.printStackTrace(writer); 00227 Debug.print(t); 00228 System.out.println("ERROR: " + t); 00229 } finally { 00230 } 00231 } 00232 } 00233 } catch (Exception e) { 00234 Debug.print(e); 00235 System.out.println("ERROR: " + e.toString()); 00236 } 00237 00238 checkOutput(); 00239 } 00240 00241 // ---- Package com.quadcap.util Tests 00242 /** 00243 * testUtilStrBytes -- test <code>Util.strBytes</code> 00244 * 00245 * @param args ignored 00246 */ 00247 public void testUtilStrBytes(String args[]) { 00248 byte[] buf = new byte[256]; 00249 for (int i = 0; i < buf.length; i++) { 00250 buf[i] = (byte)i; 00251 } 00252 writer.println(Util.strBytes(buf, 0, 256)); 00253 } 00254 00255 /** 00256 * testDlist -- test the DList class. 00257 */ 00258 public void testDList(String args[]) throws ListException { 00259 DList dl = new DList(); 00260 for (int i = 0; i < 10; i++) dl.addFront(new Integer(i)); 00261 writer.print("[0-9]: " + dl); 00262 for (int i = 0; i < 10; i++) { 00263 dl.addBack(new Integer(i+10)); 00264 int j = ((Integer)dl.tail().obj).intValue(); 00265 if (j != i+10) { 00266 throw new RuntimeException("tail should be " + (i+10) + 00267 ", is " + dl.tail().obj); 00268 } 00269 } 00270 writer.print("[10-19]"); 00271 dl.show(writer); 00272 00273 DListItem d = dl.head(); 00274 for (int i = 0; i < 15; i++) d = d.next; 00275 dl.moveFront(d); 00276 writer.print("[15->0]"); 00277 dl.show(writer); 00278 00279 DList d2 = new DList(); 00280 for (int i = 0; i < 25; i++) { 00281 if (dl.size() > 0) { 00282 d = null; 00283 d = ((i&1) != 0) ? dl.popFront() : dl.popBack(); 00284 d2.addBack(d.obj); 00285 } 00286 } 00287 writer.print("[altdel]"); 00288 dl.show(writer); 00289 00290 writer.print("[deleted]"); 00291 d2.show(writer); 00292 } 00293 00294 public static void tfoo(Object obj) { 00295 System.out.println("class(" + obj.getClass().getName() + ")"); 00296 } 00297 00298 public void testFoo(String args[]) { 00299 tfoo(new byte[10]); 00300 } 00301 00302 public void testFile1(String args[]) throws IOException { 00303 new File("test.1").delete(); 00304 RandomAccessFile f = new RandomAccessFile("test.1", "rw"); 00305 byte[] buf = new byte[1024]; 00306 for (int i = 0; i < 512; i++) { 00307 f.write(buf); 00308 f.getFD().sync(); 00309 } 00310 f.close(); 00311 } 00312 00313 public void testFile2(String args[]) throws IOException { 00314 new File("test.1").delete(); 00315 FileOutputStream f = new FileOutputStream("test.1"); 00316 byte[] buf = new byte[1024]; 00317 for (int i = 0; i < 512; i++) { 00318 f.write(buf); 00319 f.flush(); 00320 } 00321 f.close(); 00322 } 00323 00324 public void testTime(String args[]) throws Exception { 00325 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); 00326 System.out.println(args[0] + " = " + df.parse(args[0]).getTime()); 00327 } 00328 00329 public void testFile3(String args[]) throws Exception { 00330 FileOutputStream fox = new FileOutputStream("asc.out"); 00331 for (int i = 0; i < 256; i++) { 00332 fox.write(i); 00333 } 00334 fox.close(); 00335 } 00336 00337 final static String readString1(DataInput is) throws IOException { 00338 StringBuffer sb = new StringBuffer(); 00339 int cnt = is.readInt(); 00340 while (cnt-- > 0) { 00341 sb.append(is.readChar()); 00342 } 00343 return sb.toString(); 00344 } 00345 00346 final static String readString4(DataInput is) throws IOException { 00347 int cnt = is.readInt(); 00348 StringBuffer sb = new StringBuffer(cnt); 00349 while (cnt-- > 0) { 00350 sb.append(is.readChar()); 00351 } 00352 return sb.toString(); 00353 } 00354 00355 final static String readString2(DataInput is) throws IOException { 00356 int cnt = is.readInt(); 00357 char[] c = new char[cnt]; 00358 for (int i = 0; i < cnt; i++) { 00359 c[i] = is.readChar(); 00360 } 00361 return new String(c); 00362 } 00363 00364 final static String readString22(DataInput is) throws IOException { 00365 int cnt = is.readInt(); 00366 char[] c = new char[cnt]; 00367 byte[] b = new byte[cnt << 1]; 00368 is.readFully(b); 00369 int k = 0; 00370 for (int i = 0; i < cnt; i++) { 00371 int s = (b[k++] & 0xff) << 8; 00372 s |= (b[k++] & 0xff); 00373 c[i] = (char)s; 00374 } 00375 return new String(c); 00376 } 00377 00378 final static String readString3(DataInput is) throws IOException { 00379 int cnt = is.readInt(); 00380 char[] c = new char[cnt]; 00381 for (int i = 0; i < cnt; i++) { 00382 int s = is.readByte() & 0xff; 00383 if (s > 0x7f) { 00384 s &= 0x7f; 00385 s <<= 8; 00386 s |= is.readByte() & 0xff; 00387 } 00388 c[i] = (char)s; 00389 } 00390 return new String(c); 00391 } 00392 00393 final static String readString5(DataInput is) throws IOException { 00394 int cnt = is.readInt(); 00395 char[] c = new char[cnt]; 00396 byte[] b = new byte[cnt]; 00397 int k = 0; 00398 while (k < cnt) { 00399 int len = cnt - k; 00400 is.readFully(b, 0, len); 00401 for (int i = 0; i < len; i++) { 00402 int s = b[i] & 0xff; 00403 if (s > 0x7f) { 00404 s &= 0x7f; 00405 s <<= 8; 00406 s |= b[++i] & 0xff; 00407 } 00408 c[k++] = (char)s; 00409 } 00410 } 00411 return new String(c); 00412 } 00413 00414 final static void writeString1(String s, DataOutput os) throws IOException { 00415 os.writeInt(s.length()); 00416 for (int i = 0; i < s.length(); i++) { 00417 os.writeChar(s.charAt(i)); 00418 } 00419 } 00420 00421 final static void writeString3(String s, DataOutput os) throws IOException { 00422 os.writeInt(s.length()); 00423 for (int i = 0; i < s.length(); i++) { 00424 char c = s.charAt(i); 00425 if (c <= 0x7f) os.write(c); 00426 else { 00427 os.write(0x80 | ((c >> 8) & 0xff)); 00428 os.write(c & 0xff); 00429 } 00430 } 00431 } 00432 00433 public void testStr1(String args[]) throws Exception { 00434 final int bytes = 4000; 00435 final int loops = 1000; 00436 byte[] wrt = new byte[6]; 00437 for (int i = 0; i < loops; i++) { 00438 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 00439 for (int j = 0; j < bytes; j++) { 00440 bos.write(wrt); 00441 } 00442 byte[] ret = bos.toByteArray(); 00443 } 00444 } 00445 00446 // public void testStr2(String args[]) throws Exception { 00447 // final int bytes = 4000; 00448 // final int loops = 1000; 00449 // byte[] wrt = new byte[6]; 00450 // for (int i = 0; i < loops; i++) { 00451 // com.quadcap.sql.file.ByteArrayRandomAccess ba = new com.quadcap.sql.file.ByteArrayRandomAccess(); 00452 // com.quadcap.sql.file.RandomAccessOutputStream out = new com.quadcap.sql.file.RandomAccessOutputStream(ba); 00453 // for (int j = 0; j < bytes; j++) { 00454 // out.write(wrt); 00455 // } 00456 // byte[] ret = ba.toByteArray(); 00457 // } 00458 // } 00459 00460 public void testTiming(String args[]) throws Exception { 00461 String orig = "asdlkjsdlf;jkasdf;ljkasdf;lkjasdf;lkjasdf"; 00462 final int loops = 100000; 00463 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 00464 DataOutputStream dos = new DataOutputStream(bos); 00465 writeString1(orig, dos); 00466 byte[] f = bos.toByteArray(); 00467 ByteArrayInputStream bis = new ByteArrayInputStream(f); 00468 DataInputStream dis = new DataInputStream(bis); 00469 bis.mark(1000); 00470 00471 for (int i = 0; i < loops; i++) { 00472 dis.reset(); 00473 String s = readString22(dis); 00474 } 00475 } 00476 00477 public void testHdr1(String args[]) { 00478 Hashtable t1 = new Hashtable(); 00479 Hashtable t2 = new Hashtable(); 00480 final int loops = 1000000; 00481 for (int i = 0; i < loops; i++) { 00482 } 00483 } 00484 00485 /** 00486 * Main. 00487 * 00488 * @param args passed to test driver 00489 */ 00490 public static void main(String args[]) { 00491 Test test = new Test(); 00492 test.test(args); 00493 } 00494 }