1 package com.quadcap.util; 2 3 40 41 import java.io.*; 42 import java.util.*; 43 44 50 public class Test { 51 String currentTest = ""; 52 OutputStream out; 53 protected PrintWriter writer; 54 String outFile; 55 boolean generate = false; 56 boolean verbose = false; 57 protected boolean failed = false; 58 59 62 public Test() { 63 try { 64 outFile = System.getProperty("out"); 65 generate = Util.boolProperty("gen"); 66 if (outFile != null) { 67 if (generate) { 68 out = new FileOutputStream(outFile); 69 } else { 70 out = new FileOutputStream(outFile + ".gen"); 71 } 72 out = new SedOutputStream(out); 73 writer = new PrintWriter(out); 74 } else { 75 writer = new PrintWriter(System.out); 76 } 77 } catch (IOException e) { 78 Debug.print(e); 79 System.out.println("ERROR: " + e); 80 } 81 } 82 83 class SedOutputStream extends OutputStream { 84 int state = 0; 85 OutputStream out; 86 int[] lit = { 'T', 'E', 'S', 'T' }; 87 SedOutputStream(OutputStream out) { this.out = out; } 88 public void write(int c) throws IOException { 89 boolean skip = false; 90 if (state == 4) { 91 if (Character.isDigit((char)c) || c == '_') { 92 skip = true; 93 } else { 94 state = 0; 95 } 96 } else { 97 if (lit[state] == Character.toUpperCase((char)c)) state++; 98 else if (lit[0] == c) state = 1; 99 else state = 0; 100 } 101 if (!skip) out.write(c); 102 } 103 } 104 105 112 public void checkOutput() { 113 writer.flush(); 114 if (outFile != null) { 115 try { 116 out.close(); 117 if (!generate) { 118 failed = 0 != Util.execCommand("diff -w " + 119 outFile + " " + 120 outFile + ".gen", 121 System.out); 122 printFinal(); 123 } 124 } catch (IOException e) { 125 Debug.print(e); 126 System.out.println("ERROR: " + e); 127 return; 128 } 129 } else { 130 printFinal(); 131 } 132 } 133 134 final void printFinal() { 135 if (outFile == null) { 136 outFile = System.getProperty("tests"); 137 } 138 if (failed) { 139 System.out.println("FAILED: " + outFile); 140 if (Boolean.getBoolean("exitOnFail")) { 141 System.exit(1); 142 } 143 } else { 144 System.out.println("PASSED: " + outFile); 145 } 146 } 147 148 153 public void testError(String s) { 154 System.out.println("ERROR: " + currentTest + ": " + s); 155 failed = true; 156 } 157 158 165 public void testCompare(String exp, String act) { 166 if (!act.equals(exp)) { 167 testError("expected = " + exp + ", actual = " + act); 168 } 169 } 170 171 174 public void testAssert(boolean val, String msg) { 175 if (!val) testError(msg); 176 } 177 178 public void testAssert(boolean val) { 179 if (!val) { 180 String t = Util.stackTrace(); 181 int idx = t.indexOf("\n"); 182 if (idx > 0) { 183 t = t.substring(idx+1); 184 idx = t.indexOf("\n"); 185 if (idx > 0) { 186 t = t.substring(0, idx); 187 } 188 } 189 testError("Assert failed: " + t); 190 } 191 } 192 193 198 public void test(String args[]) { 199 String tests = System.getProperty("tests"); 200 Debug.debugMode = Debug.debugAll; 201 Debug.printLevel = Util.intProperty("debug", 1); 202 verbose = Util.boolProperty("verbose"); 203 204 try { 205 Class myClass = this.getClass(); 206 Class [] argTypes = { 207 Class.forName("[Ljava.lang.String;") 208 }; 209 Object [] objArgs = { 210 args 211 }; 212 213 Vector tv = Util.split(System.getProperty("tests"), ','); 214 for (int i = 0; i < tv.size(); i++) { 215 String test = (String )tv.elementAt(i); 216 currentTest = test; 217 java.lang.reflect.Method m = 218 myClass.getDeclaredMethod(test, argTypes); 219 if (m != null) { 220 try { 221 if (verbose) { 222 System.out.println("test: " + currentTest); 223 } 224 m.invoke(this, objArgs); 225 } catch (Throwable t) { 226 t.printStackTrace(writer); 227 Debug.print(t); 228 System.out.println("ERROR: " + t); 229 } finally { 230 } 231 } 232 } 233 } catch (Exception e) { 234 Debug.print(e); 235 System.out.println("ERROR: " + e.toString()); 236 } 237 238 checkOutput(); 239 } 240 241 247 public void testUtilStrBytes(String args[]) { 248 byte[] buf = new byte[256]; 249 for (int i = 0; i < buf.length; i++) { 250 buf[i] = (byte)i; 251 } 252 writer.println(Util.strBytes(buf, 0, 256)); 253 } 254 255 258 public void testDList(String args[]) throws ListException { 259 DList dl = new DList(); 260 for (int i = 0; i < 10; i++) dl.addFront(new Integer (i)); 261 writer.print("[0-9]: " + dl); 262 for (int i = 0; i < 10; i++) { 263 dl.addBack(new Integer (i+10)); 264 int j = ((Integer )dl.tail().obj).intValue(); 265 if (j != i+10) { 266 throw new RuntimeException ("tail should be " + (i+10) + 267 ", is " + dl.tail().obj); 268 } 269 } 270 writer.print("[10-19]"); 271 dl.show(writer); 272 273 DListItem d = dl.head(); 274 for (int i = 0; i < 15; i++) d = d.next; 275 dl.moveFront(d); 276 writer.print("[15->0]"); 277 dl.show(writer); 278 279 DList d2 = new DList(); 280 for (int i = 0; i < 25; i++) { 281 if (dl.size() > 0) { 282 d = null; 283 d = ((i&1) != 0) ? dl.popFront() : dl.popBack(); 284 d2.addBack(d.obj); 285 } 286 } 287 writer.print("[altdel]"); 288 dl.show(writer); 289 290 writer.print("[deleted]"); 291 d2.show(writer); 292 } 293 294 public static void tfoo(Object obj) { 295 System.out.println("class(" + obj.getClass().getName() + ")"); 296 } 297 298 public void testFoo(String args[]) { 299 tfoo(new byte[10]); 300 } 301 302 public void testFile1(String args[]) throws IOException { 303 new File("test.1").delete(); 304 RandomAccessFile f = new RandomAccessFile("test.1", "rw"); 305 byte[] buf = new byte[1024]; 306 for (int i = 0; i < 512; i++) { 307 f.write(buf); 308 f.getFD().sync(); 309 } 310 f.close(); 311 } 312 313 public void testFile2(String args[]) throws IOException { 314 new File("test.1").delete(); 315 FileOutputStream f = new FileOutputStream("test.1"); 316 byte[] buf = new byte[1024]; 317 for (int i = 0; i < 512; i++) { 318 f.write(buf); 319 f.flush(); 320 } 321 f.close(); 322 } 323 324 public void testTime(String args[]) throws Exception { 325 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ("yyyy-MM-dd"); 326 System.out.println(args[0] + " = " + df.parse(args[0]).getTime()); 327 } 328 329 public void testFile3(String args[]) throws Exception { 330 FileOutputStream fox = new FileOutputStream("asc.out"); 331 for (int i = 0; i < 256; i++) { 332 fox.write(i); 333 } 334 fox.close(); 335 } 336 337 final static String readString1(DataInput is) throws IOException { 338 StringBuffer sb = new StringBuffer (); 339 int cnt = is.readInt(); 340 while (cnt-- > 0) { 341 sb.append(is.readChar()); 342 } 343 return sb.toString(); 344 } 345 346 final static String readString4(DataInput is) throws IOException { 347 int cnt = is.readInt(); 348 StringBuffer sb = new StringBuffer (cnt); 349 while (cnt-- > 0) { 350 sb.append(is.readChar()); 351 } 352 return sb.toString(); 353 } 354 355 final static String readString2(DataInput is) throws IOException { 356 int cnt = is.readInt(); 357 char[] c = new char[cnt]; 358 for (int i = 0; i < cnt; i++) { 359 c[i] = is.readChar(); 360 } 361 return new String (c); 362 } 363 364 final static String readString22(DataInput is) throws IOException { 365 int cnt = is.readInt(); 366 char[] c = new char[cnt]; 367 byte[] b = new byte[cnt << 1]; 368 is.readFully(b); 369 int k = 0; 370 for (int i = 0; i < cnt; i++) { 371 int s = (b[k++] & 0xff) << 8; 372 s |= (b[k++] & 0xff); 373 c[i] = (char)s; 374 } 375 return new String (c); 376 } 377 378 final static String readString3(DataInput is) throws IOException { 379 int cnt = is.readInt(); 380 char[] c = new char[cnt]; 381 for (int i = 0; i < cnt; i++) { 382 int s = is.readByte() & 0xff; 383 if (s > 0x7f) { 384 s &= 0x7f; 385 s <<= 8; 386 s |= is.readByte() & 0xff; 387 } 388 c[i] = (char)s; 389 } 390 return new String (c); 391 } 392 393 final static String readString5(DataInput is) throws IOException { 394 int cnt = is.readInt(); 395 char[] c = new char[cnt]; 396 byte[] b = new byte[cnt]; 397 int k = 0; 398 while (k < cnt) { 399 int len = cnt - k; 400 is.readFully(b, 0, len); 401 for (int i = 0; i < len; i++) { 402 int s = b[i] & 0xff; 403 if (s > 0x7f) { 404 s &= 0x7f; 405 s <<= 8; 406 s |= b[++i] & 0xff; 407 } 408 c[k++] = (char)s; 409 } 410 } 411 return new String (c); 412 } 413 414 final static void writeString1(String s, DataOutput os) throws IOException { 415 os.writeInt(s.length()); 416 for (int i = 0; i < s.length(); i++) { 417 os.writeChar(s.charAt(i)); 418 } 419 } 420 421 final static void writeString3(String s, DataOutput os) throws IOException { 422 os.writeInt(s.length()); 423 for (int i = 0; i < s.length(); i++) { 424 char c = s.charAt(i); 425 if (c <= 0x7f) os.write(c); 426 else { 427 os.write(0x80 | ((c >> 8) & 0xff)); 428 os.write(c & 0xff); 429 } 430 } 431 } 432 433 public void testStr1(String args[]) throws Exception { 434 final int bytes = 4000; 435 final int loops = 1000; 436 byte[] wrt = new byte[6]; 437 for (int i = 0; i < loops; i++) { 438 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 439 for (int j = 0; j < bytes; j++) { 440 bos.write(wrt); 441 } 442 byte[] ret = bos.toByteArray(); 443 } 444 } 445 446 460 public void testTiming(String args[]) throws Exception { 461 String orig = "asdlkjsdlf;jkasdf;ljkasdf;lkjasdf;lkjasdf"; 462 final int loops = 100000; 463 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 464 DataOutputStream dos = new DataOutputStream(bos); 465 writeString1(orig, dos); 466 byte[] f = bos.toByteArray(); 467 ByteArrayInputStream bis = new ByteArrayInputStream(f); 468 DataInputStream dis = new DataInputStream(bis); 469 bis.mark(1000); 470 471 for (int i = 0; i < loops; i++) { 472 dis.reset(); 473 String s = readString22(dis); 474 } 475 } 476 477 public void testHdr1(String args[]) { 478 Hashtable t1 = new Hashtable(); 479 Hashtable t2 = new Hashtable(); 480 final int loops = 1000000; 481 for (int i = 0; i < loops; i++) { 482 } 483 } 484 485 490 public static void main(String args[]) { 491 Test test = new Test(); 492 test.test(args); 493 } 494 } 495 | Popular Tags |