001: package com.quadcap.util;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.*;
042: import java.util.*;
043:
044: /**
045: * Test harness for the com.quadcap.util package. This class is also the
046: * base class for other package Test classes.
047: *
048: * @author Stan Bailes
049: */
050: public class Test {
051: String currentTest = "";
052: OutputStream out;
053: protected PrintWriter writer;
054: String outFile;
055: boolean generate = false;
056: boolean verbose = false;
057: protected boolean failed = false;
058:
059: /**
060: * Test constructor. Open the output file as specified.
061: */
062: public Test() {
063: try {
064: outFile = System.getProperty("out");
065: generate = Util.boolProperty("gen");
066: if (outFile != null) {
067: if (generate) {
068: out = new FileOutputStream(outFile);
069: } else {
070: out = new FileOutputStream(outFile + ".gen");
071: }
072: out = new SedOutputStream(out);
073: writer = new PrintWriter(out);
074: } else {
075: writer = new PrintWriter(System.out);
076: }
077: } catch (IOException e) {
078: Debug.print(e);
079: System.out.println("ERROR: " + e);
080: }
081: }
082:
083: class SedOutputStream extends OutputStream {
084: int state = 0;
085: OutputStream out;
086: int[] lit = { 'T', 'E', 'S', 'T' };
087:
088: SedOutputStream(OutputStream out) {
089: this .out = out;
090: }
091:
092: public void write(int c) throws IOException {
093: boolean skip = false;
094: if (state == 4) {
095: if (Character.isDigit((char) c) || c == '_') {
096: skip = true;
097: } else {
098: state = 0;
099: }
100: } else {
101: if (lit[state] == Character.toUpperCase((char) c))
102: state++;
103: else if (lit[0] == c)
104: state = 1;
105: else
106: state = 0;
107: }
108: if (!skip)
109: out.write(c);
110: }
111: }
112:
113: /**
114: * For tests which generate output to a stream, compare the actual stream
115: * to the expected (pregenerated) stream. If the <code>gen</code>
116: * system property is <b>true</b>, we simply close the stream, which
117: * is used to generate the file of expected data used to compare in
118: * subsequent test runs.
119: */
120: public void checkOutput() {
121: writer.flush();
122: if (outFile != null) {
123: try {
124: out.close();
125: if (!generate) {
126: failed = 0 != Util.execCommand("diff -w " + outFile
127: + " " + outFile + ".gen", System.out);
128: printFinal();
129: }
130: } catch (IOException e) {
131: Debug.print(e);
132: System.out.println("ERROR: " + e);
133: return;
134: }
135: } else {
136: printFinal();
137: }
138: }
139:
140: final void printFinal() {
141: if (outFile == null) {
142: outFile = System.getProperty("tests");
143: }
144: if (failed) {
145: System.out.println("FAILED: " + outFile);
146: if (Boolean.getBoolean("exitOnFail")) {
147: System.exit(1);
148: }
149: } else {
150: System.out.println("PASSED: " + outFile);
151: }
152: }
153:
154: /**
155: * Indicate a test error with the specified detail message
156: *
157: * @param s the message
158: */
159: public void testError(String s) {
160: System.out.println("ERROR: " + currentTest + ": " + s);
161: failed = true;
162: }
163:
164: /**
165: * Compare two strings, resulting in a test error if the strings are
166: * not equal
167: *
168: * @param exp the expected value
169: * @param act the actual value
170: */
171: public void testCompare(String exp, String act) {
172: if (!act.equals(exp)) {
173: testError("expected = " + exp + ", actual = " + act);
174: }
175: }
176:
177: /**
178: * Assert
179: */
180: public void testAssert(boolean val, String msg) {
181: if (!val)
182: testError(msg);
183: }
184:
185: public void testAssert(boolean val) {
186: if (!val) {
187: String t = Util.stackTrace();
188: int idx = t.indexOf("\n");
189: if (idx > 0) {
190: t = t.substring(idx + 1);
191: idx = t.indexOf("\n");
192: if (idx > 0) {
193: t = t.substring(0, idx);
194: }
195: }
196: testError("Assert failed: " + t);
197: }
198: }
199:
200: /**
201: * Main test driver. Set up environment, run each test by using reflection
202: * to find the methods in the derived class as specified by the
203: * <code>tests</code> system property.
204: */
205: public void test(String args[]) {
206: String tests = System.getProperty("tests");
207: Debug.debugMode = Debug.debugAll;
208: Debug.printLevel = Util.intProperty("debug", 1);
209: verbose = Util.boolProperty("verbose");
210:
211: try {
212: Class myClass = this .getClass();
213: Class[] argTypes = { Class.forName("[Ljava.lang.String;") };
214: Object[] objArgs = { args };
215:
216: Vector tv = Util.split(System.getProperty("tests"), ',');
217: for (int i = 0; i < tv.size(); i++) {
218: String test = (String) tv.elementAt(i);
219: currentTest = test;
220: java.lang.reflect.Method m = myClass.getDeclaredMethod(
221: test, argTypes);
222: if (m != null) {
223: try {
224: if (verbose) {
225: System.out.println("test: " + currentTest);
226: }
227: m.invoke(this , objArgs);
228: } catch (Throwable t) {
229: t.printStackTrace(writer);
230: Debug.print(t);
231: System.out.println("ERROR: " + t);
232: } finally {
233: }
234: }
235: }
236: } catch (Exception e) {
237: Debug.print(e);
238: System.out.println("ERROR: " + e.toString());
239: }
240:
241: checkOutput();
242: }
243:
244: // ---- Package com.quadcap.util Tests
245: /**
246: * testUtilStrBytes -- test <code>Util.strBytes</code>
247: *
248: * @param args ignored
249: */
250: public void testUtilStrBytes(String args[]) {
251: byte[] buf = new byte[256];
252: for (int i = 0; i < buf.length; i++) {
253: buf[i] = (byte) i;
254: }
255: writer.println(Util.strBytes(buf, 0, 256));
256: }
257:
258: /**
259: * testDlist -- test the DList class.
260: */
261: public void testDList(String args[]) throws ListException {
262: DList dl = new DList();
263: for (int i = 0; i < 10; i++)
264: dl.addFront(new Integer(i));
265: writer.print("[0-9]: " + dl);
266: for (int i = 0; i < 10; i++) {
267: dl.addBack(new Integer(i + 10));
268: int j = ((Integer) dl.tail().obj).intValue();
269: if (j != i + 10) {
270: throw new RuntimeException("tail should be " + (i + 10)
271: + ", is " + dl.tail().obj);
272: }
273: }
274: writer.print("[10-19]");
275: dl.show(writer);
276:
277: DListItem d = dl.head();
278: for (int i = 0; i < 15; i++)
279: d = d.next;
280: dl.moveFront(d);
281: writer.print("[15->0]");
282: dl.show(writer);
283:
284: DList d2 = new DList();
285: for (int i = 0; i < 25; i++) {
286: if (dl.size() > 0) {
287: d = null;
288: d = ((i & 1) != 0) ? dl.popFront() : dl.popBack();
289: d2.addBack(d.obj);
290: }
291: }
292: writer.print("[altdel]");
293: dl.show(writer);
294:
295: writer.print("[deleted]");
296: d2.show(writer);
297: }
298:
299: public static void tfoo(Object obj) {
300: System.out.println("class(" + obj.getClass().getName() + ")");
301: }
302:
303: public void testFoo(String args[]) {
304: tfoo(new byte[10]);
305: }
306:
307: public void testFile1(String args[]) throws IOException {
308: new File("test.1").delete();
309: RandomAccessFile f = new RandomAccessFile("test.1", "rw");
310: byte[] buf = new byte[1024];
311: for (int i = 0; i < 512; i++) {
312: f.write(buf);
313: f.getFD().sync();
314: }
315: f.close();
316: }
317:
318: public void testFile2(String args[]) throws IOException {
319: new File("test.1").delete();
320: FileOutputStream f = new FileOutputStream("test.1");
321: byte[] buf = new byte[1024];
322: for (int i = 0; i < 512; i++) {
323: f.write(buf);
324: f.flush();
325: }
326: f.close();
327: }
328:
329: public void testTime(String args[]) throws Exception {
330: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
331: "yyyy-MM-dd");
332: System.out.println(args[0] + " = "
333: + df.parse(args[0]).getTime());
334: }
335:
336: public void testFile3(String args[]) throws Exception {
337: FileOutputStream fox = new FileOutputStream("asc.out");
338: for (int i = 0; i < 256; i++) {
339: fox.write(i);
340: }
341: fox.close();
342: }
343:
344: final static String readString1(DataInput is) throws IOException {
345: StringBuffer sb = new StringBuffer();
346: int cnt = is.readInt();
347: while (cnt-- > 0) {
348: sb.append(is.readChar());
349: }
350: return sb.toString();
351: }
352:
353: final static String readString4(DataInput is) throws IOException {
354: int cnt = is.readInt();
355: StringBuffer sb = new StringBuffer(cnt);
356: while (cnt-- > 0) {
357: sb.append(is.readChar());
358: }
359: return sb.toString();
360: }
361:
362: final static String readString2(DataInput is) throws IOException {
363: int cnt = is.readInt();
364: char[] c = new char[cnt];
365: for (int i = 0; i < cnt; i++) {
366: c[i] = is.readChar();
367: }
368: return new String(c);
369: }
370:
371: final static String readString22(DataInput is) throws IOException {
372: int cnt = is.readInt();
373: char[] c = new char[cnt];
374: byte[] b = new byte[cnt << 1];
375: is.readFully(b);
376: int k = 0;
377: for (int i = 0; i < cnt; i++) {
378: int s = (b[k++] & 0xff) << 8;
379: s |= (b[k++] & 0xff);
380: c[i] = (char) s;
381: }
382: return new String(c);
383: }
384:
385: final static String readString3(DataInput is) throws IOException {
386: int cnt = is.readInt();
387: char[] c = new char[cnt];
388: for (int i = 0; i < cnt; i++) {
389: int s = is.readByte() & 0xff;
390: if (s > 0x7f) {
391: s &= 0x7f;
392: s <<= 8;
393: s |= is.readByte() & 0xff;
394: }
395: c[i] = (char) s;
396: }
397: return new String(c);
398: }
399:
400: final static String readString5(DataInput is) throws IOException {
401: int cnt = is.readInt();
402: char[] c = new char[cnt];
403: byte[] b = new byte[cnt];
404: int k = 0;
405: while (k < cnt) {
406: int len = cnt - k;
407: is.readFully(b, 0, len);
408: for (int i = 0; i < len; i++) {
409: int s = b[i] & 0xff;
410: if (s > 0x7f) {
411: s &= 0x7f;
412: s <<= 8;
413: s |= b[++i] & 0xff;
414: }
415: c[k++] = (char) s;
416: }
417: }
418: return new String(c);
419: }
420:
421: final static void writeString1(String s, DataOutput os)
422: throws IOException {
423: os.writeInt(s.length());
424: for (int i = 0; i < s.length(); i++) {
425: os.writeChar(s.charAt(i));
426: }
427: }
428:
429: final static void writeString3(String s, DataOutput os)
430: throws IOException {
431: os.writeInt(s.length());
432: for (int i = 0; i < s.length(); i++) {
433: char c = s.charAt(i);
434: if (c <= 0x7f)
435: os.write(c);
436: else {
437: os.write(0x80 | ((c >> 8) & 0xff));
438: os.write(c & 0xff);
439: }
440: }
441: }
442:
443: public void testStr1(String args[]) throws Exception {
444: final int bytes = 4000;
445: final int loops = 1000;
446: byte[] wrt = new byte[6];
447: for (int i = 0; i < loops; i++) {
448: ByteArrayOutputStream bos = new ByteArrayOutputStream();
449: for (int j = 0; j < bytes; j++) {
450: bos.write(wrt);
451: }
452: byte[] ret = bos.toByteArray();
453: }
454: }
455:
456: // public void testStr2(String args[]) throws Exception {
457: // final int bytes = 4000;
458: // final int loops = 1000;
459: // byte[] wrt = new byte[6];
460: // for (int i = 0; i < loops; i++) {
461: // com.quadcap.sql.file.ByteArrayRandomAccess ba = new com.quadcap.sql.file.ByteArrayRandomAccess();
462: // com.quadcap.sql.file.RandomAccessOutputStream out = new com.quadcap.sql.file.RandomAccessOutputStream(ba);
463: // for (int j = 0; j < bytes; j++) {
464: // out.write(wrt);
465: // }
466: // byte[] ret = ba.toByteArray();
467: // }
468: // }
469:
470: public void testTiming(String args[]) throws Exception {
471: String orig = "asdlkjsdlf;jkasdf;ljkasdf;lkjasdf;lkjasdf";
472: final int loops = 100000;
473: ByteArrayOutputStream bos = new ByteArrayOutputStream();
474: DataOutputStream dos = new DataOutputStream(bos);
475: writeString1(orig, dos);
476: byte[] f = bos.toByteArray();
477: ByteArrayInputStream bis = new ByteArrayInputStream(f);
478: DataInputStream dis = new DataInputStream(bis);
479: bis.mark(1000);
480:
481: for (int i = 0; i < loops; i++) {
482: dis.reset();
483: String s = readString22(dis);
484: }
485: }
486:
487: public void testHdr1(String args[]) {
488: Hashtable t1 = new Hashtable();
489: Hashtable t2 = new Hashtable();
490: final int loops = 1000000;
491: for (int i = 0; i < loops; i++) {
492: }
493: }
494:
495: /**
496: * Main.
497: *
498: * @param args passed to test driver
499: */
500: public static void main(String args[]) {
501: Test test = new Test();
502: test.test(args);
503: }
504: }
|