KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > Test


1 package com.quadcap.util;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.*;
42 import java.util.*;
43
44 /**
45  * Test harness for the com.quadcap.util package. This class is also the
46  * base class for other package Test classes.
47  *
48  * @author Stan Bailes
49  */

50 public class Test {
51     String JavaDoc currentTest = "";
52     OutputStream out;
53     protected PrintWriter writer;
54     String JavaDoc outFile;
55     boolean generate = false;
56     boolean verbose = false;
57     protected boolean failed = false;
58
59     /**
60      * Test constructor. Open the output file as specified.
61      */

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     /**
106      * For tests which generate output to a stream, compare the actual stream
107      * to the expected (pregenerated) stream. If the <code>gen</code>
108      * system property is <b>true</b>, we simply close the stream, which
109      * is used to generate the file of expected data used to compare in
110      * subsequent test runs.
111      */

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     /**
149      * Indicate a test error with the specified detail message
150      *
151      * @param s the message
152      */

153     public void testError(String JavaDoc s) {
154     System.out.println("ERROR: " + currentTest + ": " + s);
155         failed = true;
156     }
157
158     /**
159      * Compare two strings, resulting in a test error if the strings are
160      * not equal
161      *
162      * @param exp the expected value
163      * @param act the actual value
164      */

165     public void testCompare(String JavaDoc exp, String JavaDoc act) {
166     if (!act.equals(exp)) {
167         testError("expected = " + exp + ", actual = " + act);
168     }
169     }
170
171     /**
172      * Assert
173      */

174     public void testAssert(boolean val, String JavaDoc msg) {
175         if (!val) testError(msg);
176     }
177
178     public void testAssert(boolean val) {
179         if (!val) {
180             String JavaDoc 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     /**
194      * Main test driver. Set up environment, run each test by using reflection
195      * to find the methods in the derived class as specified by the
196      * <code>tests</code> system property.
197      */

198     public void test(String JavaDoc args[]) {
199     String JavaDoc 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 JavaDoc myClass = this.getClass();
206         Class JavaDoc[] argTypes = {
207         Class.forName("[Ljava.lang.String;")
208         };
209         Object JavaDoc[] objArgs = {
210         args
211         };
212
213         Vector tv = Util.split(System.getProperty("tests"), ',');
214         for (int i = 0; i < tv.size(); i++) {
215         String JavaDoc test = (String JavaDoc)tv.elementAt(i);
216         currentTest = test;
217         java.lang.reflect.Method JavaDoc 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 JavaDoc t) {
226                         t.printStackTrace(writer);
227             Debug.print(t);
228             System.out.println("ERROR: " + t);
229             } finally {
230                     }
231         }
232         }
233     } catch (Exception JavaDoc e) {
234         Debug.print(e);
235         System.out.println("ERROR: " + e.toString());
236     }
237
238     checkOutput();
239     }
240
241     // ---- Package com.quadcap.util Tests
242
/**
243      * testUtilStrBytes -- test <code>Util.strBytes</code>
244      *
245      * @param args ignored
246      */

247     public void testUtilStrBytes(String JavaDoc 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     /**
256      * testDlist -- test the DList class.
257      */

258     public void testDList(String JavaDoc args[]) throws ListException {
259     DList dl = new DList();
260     for (int i = 0; i < 10; i++) dl.addFront(new Integer JavaDoc(i));
261     writer.print("[0-9]: " + dl);
262     for (int i = 0; i < 10; i++) {
263         dl.addBack(new Integer JavaDoc(i+10));
264         int j = ((Integer JavaDoc)dl.tail().obj).intValue();
265         if (j != i+10) {
266         throw new RuntimeException JavaDoc("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 JavaDoc obj) {
295     System.out.println("class(" + obj.getClass().getName() + ")");
296     }
297
298     public void testFoo(String JavaDoc args[]) {
299     tfoo(new byte[10]);
300     }
301
302     public void testFile1(String JavaDoc 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 JavaDoc 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 JavaDoc args[]) throws Exception JavaDoc {
325     java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc("yyyy-MM-dd");
326     System.out.println(args[0] + " = " + df.parse(args[0]).getTime());
327     }
328
329     public void testFile3(String JavaDoc args[]) throws Exception JavaDoc {
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 JavaDoc readString1(DataInput is) throws IOException {
338     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc readString4(DataInput is) throws IOException {
347     int cnt = is.readInt();
348     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(cnt);
349     while (cnt-- > 0) {
350         sb.append(is.readChar());
351     }
352     return sb.toString();
353     }
354
355     final static String JavaDoc 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 JavaDoc(c);
362     }
363
364     final static String JavaDoc 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 JavaDoc(c);
376     }
377
378     final static String JavaDoc 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 JavaDoc(c);
391     }
392
393     final static String JavaDoc 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 JavaDoc(c);
412     }
413
414     final static void writeString1(String JavaDoc 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 JavaDoc 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 JavaDoc args[]) throws Exception JavaDoc {
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 // public void testStr2(String args[]) throws Exception {
447
// final int bytes = 4000;
448
// final int loops = 1000;
449
// byte[] wrt = new byte[6];
450
// for (int i = 0; i < loops; i++) {
451
// com.quadcap.sql.file.ByteArrayRandomAccess ba = new com.quadcap.sql.file.ByteArrayRandomAccess();
452
// com.quadcap.sql.file.RandomAccessOutputStream out = new com.quadcap.sql.file.RandomAccessOutputStream(ba);
453
// for (int j = 0; j < bytes; j++) {
454
// out.write(wrt);
455
// }
456
// byte[] ret = ba.toByteArray();
457
// }
458
// }
459

460     public void testTiming(String JavaDoc args[]) throws Exception JavaDoc {
461     String JavaDoc 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 JavaDoc s = readString22(dis);
474     }
475     }
476
477     public void testHdr1(String JavaDoc 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     /**
486      * Main.
487      *
488      * @param args passed to test driver
489      */

490     public static void main(String JavaDoc args[]) {
491     Test test = new Test();
492     test.test(args);
493     }
494 }
495
Popular Tags