00001
package com.quadcap.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.*;
00042
import java.util.*;
00043
00044
00045
00046
00047
00048
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
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
00107
00108
00109
00110
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
00150
00151
00152
00153 public void testError(String s) {
00154 System.out.println(
"ERROR: " +
currentTest +
": " + s);
00155
failed =
true;
00156 }
00157
00158
00159
00160
00161
00162
00163
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
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
00195
00196
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
00242
00243
00244
00245
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
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
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
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
00487
00488
00489
00490 public static void main(String args[]) {
00491
Test test =
new Test();
00492 test.
test(args);
00493 }
00494 }