KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > CircularBufferTests


1 /*
2  * Regression tests for Circular Buffers.
3  * Copyright (C) 2002 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21 import java.util.*;
22
23 /**
24  * Regression test for circular buffers. When run, this program
25  * should output the Gettysburg Address and quite a few digits of
26  * pi.
27  * More information about this class is available from <a target="_top" HREF=
28  * "http://ostermiller.org/utils/CircularCharBuffer.html">ostermiller.org</a>.
29  *
30  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
31  * @since ostermillerutils 1.00.00
32  */

33 class CircularBufferTests {
34
35     private byte[] pi = {
36         3,
37         1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0,5,8,2,0,9,7,4,9,4,4,
38         5,9,2,3,0,7,8,1,6,4,0,6,2,8,6,2,0,8,9,9,8,6,2,8,0,3,4,8,2,5,3,4,2,1,1,7,0,6,7,9,8,2,1,4,8,0,8,6,5,1,3,2,8,2,3,0,6,6,4,7,
39         0,9,3,8,4,4,6,0,9,5,5,0,5,8,2,2,3,1,7,2,5,3,5,9,4,0,8,1,2,8,4,8,1,1,1,7,4,5,0,2,8,4,1,0,2,7,0,1,9,3,8,5,2,1,1,0,5,5,5,9,
40         6,4,4,6,2,2,9,4,8,9,5,4,9,3,0,3,8,1,9,6,4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,2,0,1,9,9,5,6,1,1,2,1,2,9,0,2,1,9,6,0,
49         8,6,4,0,3,4,4,1,8,1,5,9,8,1,3,6,2,9,7,7,4,7,7,1,3,0,9,9,6,0,5,1,8,7,0,7,2,1,1,3,4,9,9,9,9,9,9,8,3,7,2,9,7,8,0,4,9,9,5,1,
50     };
51
52     private String JavaDoc theGettysburgAddress = "Four score and seven years ago our fathers brought forth, " +
53         "upon this continent, a new nation, conceived in Liberty, and dedicated " +
54         "to the proposition that all men are created equal.\n" +
55         "Now we are engaged in a great civil war, testing whether that nation, " +
56         "or any nation so conceived, and so dedicated, can long endure. We are " +
57         "met here on a great battlefield of that war. We have come to dedicate " +
58         "a portion of it as a final resting place for those who here gave their " +
59         "lives that that nation might live. It is altogether fitting and proper " +
60         "that we should do this.\n" +
61         "But in a larger sense we can not dedicate -- we can not consecrate -- " +
62         "we can not hallow this ground. The brave men, living and dead, who " +
63         "struggled, here, have consecrated it far above our poor power to add " +
64         "or detract. The world will little note, nor long remember, what we say here, " +
65         "but can never forget what they did here. It is for us, the living, rather " +
66         "to be dedicated here to the unfinished work which they have, thus far, so " +
67         "nobly carried on. It is rather for us to be here dedicated to the great task " +
68         "remaining before us -- that from these honored dead we take increased devotion " +
69         "to that cause for which they here gave the last full measure of devotion -- " +
70         "that we here highly resolve that these dead shall not have died in vain; that " +
71         "this nation shall have a new birth of freedom; and that this government of the " +
72         "people, by the people, for the people, shall not perish from the earth.";
73
74     private StringWriter tgbaWriter = new StringWriter();
75     private StringWriter tgbaWriter2 = new StringWriter();
76
77     private ByteArrayOutputStream piOutputStream = new ByteArrayOutputStream();
78
79     private Random rand = new Random();
80
81     private CircularCharBuffer ccb = new CircularCharBuffer(20);
82     private Reader ccbin;
83     private Writer ccbout;
84
85     private CircularByteBuffer cbb = new CircularByteBuffer(20);
86     private InputStream cbbin;
87     private OutputStream cbbout;
88
89     private CircularObjectBuffer<String JavaDoc> cob = new CircularObjectBuffer<String JavaDoc>(20);
90
91     public static void main(String JavaDoc args[]){
92         try {
93             new CircularBufferTests();
94         } catch (Exception JavaDoc x){
95             x.printStackTrace();
96             System.exit(1);
97         }
98         System.exit(0);
99     }
100
101     private CircularBufferTests() throws Exception JavaDoc{
102
103         ccbin = ccb.getReader();
104         ccbout = ccb.getWriter();
105         CCBProducer ccbp = new CCBProducer();
106         CCBConsumer ccbc = new CCBConsumer();
107         ccbc.start();
108         ccbp.start();
109         while (ccbc.isAlive() || ccbp.isAlive()){
110             if(System.in.available() > 0){
111                 ccbc.interrupt();
112                 ccbp.interrupt();
113                 return;
114             }
115         }
116         String JavaDoc s1 = tgbaWriter.toString();
117         if (!theGettysburgAddress.equals(s1)){
118             throw new Exception JavaDoc(s1);
119         }
120
121         cbbin = cbb.getInputStream();
122         cbbout = cbb.getOutputStream();
123         CBBProducer cbbp = new CBBProducer();
124         CBBConsumer cbbc = new CBBConsumer();
125         cbbc.start();
126         cbbp.start();
127         while (cbbc.isAlive() || cbbp.isAlive()){
128             if(System.in.available() > 0){
129                 cbbc.interrupt();
130                 cbbp.interrupt();
131                 return;
132             }
133         }
134         assertEqual(pi, piOutputStream.toByteArray());
135
136         COBProducer cobp = new COBProducer();
137         COBConsumer cobc = new COBConsumer();
138         cobc.start();
139         cobp.start();
140         while (cobc.isAlive() || cobp.isAlive()){
141             if(System.in.available() > 0){
142                 cobc.interrupt();
143                 cobp.interrupt();
144                 return;
145             }
146         }
147         String JavaDoc s2 = tgbaWriter2.toString();
148         if (!theGettysburgAddress.equals(s2)){
149             throw new Exception JavaDoc(s2);
150         }
151     }
152
153     private static void assertEqual(byte[] b1, byte[] b2) throws Exception JavaDoc {
154         if (b1.length != b2.length) throw new Exception JavaDoc ("Length mismatch: " + b1.length + ", " + b2.length);
155         for (int i=0; i<b1.length; i++){
156             if (b1[i] != b2[i]){
157                 throw new Exception JavaDoc("Mismatch at position " + i + ": " + b1[i] + ", " + b2[i]);
158             }
159         }
160     }
161
162     private class CCBProducer extends Thread JavaDoc {
163         public void run() {
164             try {
165                 for(int position = 0; !isInterrupted() && position < theGettysburgAddress.length(); position++){
166                     int len = rand.nextInt(30);
167                     len = Math.min(len, theGettysburgAddress.length() - position);
168                     int off = rand.nextInt(10);
169                     switch (rand.nextInt(4)){
170                         case 0: {
171                             char[] writeBuf = new char[len];
172                             for (int i=0; i<len; i++){
173                                 writeBuf[i] = theGettysburgAddress.charAt(position + i);
174                             }
175                             ccbout.write(writeBuf);
176                         } break;
177                         case 1: {
178                             char[] writeBuf = new char[off + len];
179                             for (int i=0; i<len; i++){
180                                 writeBuf[i+off] = theGettysburgAddress.charAt(position + i);
181                             }
182                             ccbout.write(writeBuf, off, len);
183                         } break;
184                         case 2: {
185                             for (int i=0; !isInterrupted() && i<len; i++){
186                                 ccbout.write(theGettysburgAddress.charAt(position + i));
187                             }
188                         } break;
189                         case 3: {
190                             ccbout.write(theGettysburgAddress.substring(position, position+len));
191                         } break;
192                         case 4: {
193                             ccbout.write(theGettysburgAddress, position, len);
194                         } break;
195                     }
196                     position += (len - 1);
197                     try {
198                         Thread.sleep(50 + rand.nextInt(100));
199                     } catch(Exception JavaDoc x){
200                         throw new IOException("Producer thread interrupted.");
201                     }
202                 }
203                 ccbout.close();
204             } catch (IOException x){
205                 System.err.println(x.getMessage());
206             }
207         }
208     }
209
210     private class CCBConsumer extends Thread JavaDoc {
211         public void run() {
212             try {
213                 boolean done = false;
214                 while(!isInterrupted() && !done){
215                     int len = rand.nextInt(30);
216                     int off = rand.nextInt(10);
217                     switch (rand.nextInt(2)){
218                         case 0: {
219                             char[] readBuf = new char[len];
220                             int read = ccbin.read(readBuf);
221                             if (read == -1){
222                                 done = true;
223                             } else {
224                                 for (int i=0; i<read; i++){
225                                     tgbaWriter.write(readBuf[i]);
226                                 }
227                             }
228                         } break;
229                         case 1: {
230                             char[] readBuf = new char[off + len];
231                             int read = ccbin.read(readBuf, off, len);
232                             if (read == -1){
233                                 done = true;
234                             } else {
235                                 for (int i=0; i<read; i++){
236                                     tgbaWriter.write(readBuf[i+off]);
237                                 }
238                             }
239                         } break;
240                         case 2: {
241                             for (int i=0; !isInterrupted() && !done && i<len; i++){
242                                 int read;
243                                 read = ccbin.read();
244                                 if (read == -1){
245                                     done = true;
246                                 } else {
247                                     tgbaWriter.write((char)read);
248                                 }
249                             }
250                         } break;
251                     }
252                     try {
253                         Thread.sleep(50 + rand.nextInt(100));
254                     } catch(Exception JavaDoc x){
255                         throw new IOException("Consumer thread interrupted.");
256                     }
257                 }
258             } catch (IOException x){
259                 System.err.println(x.getMessage());
260             }
261         }
262     }
263
264     private class CBBProducer extends Thread JavaDoc {
265         public void run() {
266             try {
267                 for(int position = 0; !isInterrupted() && position < pi.length; position++){
268                     int len = rand.nextInt(30);
269                     len = Math.min(len, pi.length - position);
270                     int off = rand.nextInt(10);
271                     switch (rand.nextInt(2)){
272                         case 0: {
273                             byte[] writeBuf = new byte[len];
274                             for (int i=0; i<len; i++){
275                                 writeBuf[i] = pi[position + i];
276                             }
277                             cbbout.write(writeBuf);
278                         } break;
279                         case 1: {
280                             byte[] writeBuf = new byte[off + len];
281                             for (int i=0; i<len; i++){
282                                 writeBuf[i+off] = pi[position + i];
283                             }
284                             cbbout.write(writeBuf, off, len);
285                         } break;
286                         case 2: {
287                             for (int i=0; !isInterrupted() && i<len; i++){
288                                 cbbout.write(pi[position + i]);
289                             }
290                         } break;
291                     }
292                     position += (len - 1);
293                     try {
294                         Thread.sleep(50 + rand.nextInt(100));
295                     } catch(Exception JavaDoc x){
296                         throw new IOException("Producer thread interrupted.");
297                     }
298                 }
299                 cbbout.close();
300             } catch (IOException x){
301                 System.err.println(x.getMessage());
302             }
303         }
304     }
305
306     private class CBBConsumer extends Thread JavaDoc {
307         public void run() {
308             try {
309                 boolean done = false;
310                 while(!isInterrupted() && !done){
311                     int len = rand.nextInt(30);
312                     int off = rand.nextInt(10);
313                     switch (rand.nextInt(2)){
314                         case 0: {
315                             byte[] readBuf = new byte[len];
316                             int read = cbbin.read(readBuf);
317                             if (read == -1){
318                                 done = true;
319                             } else {
320                                 for (int i=0; i<read; i++){
321                                     piOutputStream.write(readBuf[i]);
322                                 }
323                             }
324                         } break;
325                         case 1: {
326                             byte[] readBuf = new byte[off + len];
327                             int read = cbbin.read(readBuf, off, len);
328                             if (read == -1){
329                                 done = true;
330                             } else {
331                                 for (int i=0; i<read; i++){
332                                     piOutputStream.write(readBuf[i+off]);
333                                 }
334                             }
335                         } break;
336                         case 2: {
337                             for (int i=0; !isInterrupted() && !done && i<len; i++){
338                                 int read;
339                                 read = cbbin.read();
340                                 if (read == -1){
341                                     done = true;
342                                 } else {
343                                     piOutputStream.write((byte)read);
344                                 }
345                             }
346                         } break;
347                     }
348                     try {
349                         Thread.sleep(50 + rand.nextInt(100));
350                     } catch(Exception JavaDoc x){
351                         throw new IOException("Consumer thread interrupted.");
352                     }
353                 }
354             } catch (IOException x){
355                 System.err.println(x.getMessage());
356             }
357         }
358     }
359
360     private class COBProducer extends Thread JavaDoc {
361         public void run() {
362             try {
363                 for(int position = 0; !isInterrupted() && position < theGettysburgAddress.length(); position++){
364                     int len = rand.nextInt(30);
365                     len = Math.min(len, theGettysburgAddress.length() - position);
366                     int off = rand.nextInt(10);
367                     switch (rand.nextInt(2)){
368                         case 0: {
369                             String JavaDoc[] writeBuf = new String JavaDoc[len];
370                             for (int i=0; i<len; i++){
371                                 writeBuf[i] = Character.toString(theGettysburgAddress.charAt(position + i));
372                             }
373                             cob.write(writeBuf);
374                         } break;
375                         case 1: {
376                             String JavaDoc[] writeBuf = new String JavaDoc[off + len];
377                             for (int i=0; i<len; i++){
378                                 writeBuf[i+off] = Character.toString(theGettysburgAddress.charAt(position + i));
379                             }
380                             cob.write(writeBuf, off, len);
381                         } break;
382                         case 2: {
383                             for (int i=0; !isInterrupted() && i<len; i++){
384                                 cob.write(Character.toString(theGettysburgAddress.charAt(position + i)));
385                             }
386                         } break;
387                     }
388                     position += (len - 1);
389                     try {
390                         Thread.sleep(50 + rand.nextInt(100));
391                     } catch(Exception JavaDoc x){
392                         throw new IOException("Producer thread interrupted.");
393                     }
394                 }
395                 cob.done();
396             } catch (Exception JavaDoc x){
397                 System.err.println(x.getMessage());
398             }
399         }
400     }
401
402     private class COBConsumer extends Thread JavaDoc {
403         public void run() {
404             try {
405                 boolean done = false;
406                 while(!isInterrupted() && !done){
407                     int len = rand.nextInt(30);
408                     int off = rand.nextInt(10);
409                     switch (rand.nextInt(2)){
410                         case 0: {
411                             String JavaDoc[] readBuf = new String JavaDoc[len];
412                             int read = cob.read(readBuf);
413                             if (read == -1){
414                                 done = true;
415                             } else {
416                                 for (int i=0; i<read; i++){
417                                     tgbaWriter2.write((String JavaDoc)readBuf[i]);
418                                 }
419                             }
420                         } break;
421                         case 1: {
422                             String JavaDoc[] readBuf = new String JavaDoc[off + len];
423                             int read = cob.read(readBuf, off, len);
424                             if (read == -1){
425                                 done = true;
426                             } else {
427                                 for (int i=0; i<read; i++){
428                                     tgbaWriter2.write((String JavaDoc)readBuf[i+off]);
429                                 }
430                             }
431                         } break;
432                         case 2: {
433                             for (int i=0; !isInterrupted() && !done && i<len; i++){
434                                 String JavaDoc read;
435                                 read = cob.read();
436                                 if (read == null){
437                                     done = true;
438                                 } else {
439                                     tgbaWriter2.write((String JavaDoc)read);
440                                 }
441                             }
442                         } break;
443                     }
444                     try {
445                         Thread.sleep(50 + rand.nextInt(100));
446                     } catch(Exception JavaDoc x){
447                         throw new IOException("Consumer thread interrupted.");
448                     }
449                 }
450             } catch (Exception JavaDoc x){
451                 System.err.println(x.getMessage());
452             }
453         }
454     }
455 }
456
Popular Tags