All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.Ostermiller.util.CircularBufferTests Maven / Gradle / Ivy

Go to download

Open source (GPL) Java utilities maintained by Stephen Ostermiller with help from many contributors.

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

import java.io.*;
import java.util.Random;

/**
 * Regression test for circular buffers.  When run, this program
 * should output the Gettysburg Address and quite a few digits of
 * pi.
 * More information about this class is available from ostermiller.org.
 *
 * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
 * @since ostermillerutils 1.00.00
 */
class CircularBufferTests {

	private byte[] pi = {
		3,
		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,
		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,
		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,
		6,4,4,6,2,2,9,4,8,9,5,4,9,3,0,3,8,1,9,6,4,4,2,8,8,1,0,9,7,5,6,6,5,9,3,3,4,4,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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,
	};

	private String theGettysburgAddress = "Four score and seven years ago our fathers brought forth, " +
		"upon this continent, a new nation, conceived in Liberty, and dedicated " +
		"to the proposition that all men are created equal.\n" +
		"Now we are engaged in a great civil war, testing whether that nation, " +
		"or any nation so conceived, and so dedicated, can long endure. We are " +
		"met here on a great battlefield of that war. We have come to dedicate " +
		"a portion of it as a final resting place for those who here gave their " +
		"lives that that nation might live. It is altogether fitting and proper " +
		"that we should do this.\n" +
		"But in a larger sense we can not dedicate -- we can not consecrate -- " +
		"we can not hallow this ground. The brave men, living and dead, who " +
		"struggled, here, have consecrated it far above our poor power to add " +
		"or detract. The world will little note, nor long remember, what we say here, " +
		"but can never forget what they did here. It is for us, the living, rather " +
		"to be dedicated here to the unfinished work which they have, thus far, so " +
		"nobly carried on. It is rather for us to be here dedicated to the great task " +
		"remaining before us -- that from these honored dead we take increased devotion " +
		"to that cause for which they here gave the last full measure of devotion -- " +
		"that we here highly resolve that these dead shall not have died in vain; that " +
		"this nation shall have a new birth of freedom; and that this government of the " +
		"people, by the people, for the people, shall not perish from the earth.";

	private StringWriter tgbaWriter = new StringWriter();
	private StringWriter tgbaWriter2 = new StringWriter();

	private ByteArrayOutputStream piOutputStream = new ByteArrayOutputStream();

	private Random rand = new Random();

	private CircularCharBuffer ccb = new CircularCharBuffer(20);
	private Reader ccbin;
	private Writer ccbout;

	private CircularByteBuffer cbb = new CircularByteBuffer(20);
	private InputStream cbbin;
	private OutputStream cbbout;

	private CircularObjectBuffer cob = new CircularObjectBuffer(20);

	/**
	 * Main test method
	 * @param args command line arguments (ignored)
	 */
	public static void main(String args[]){
		try {
			new CircularBufferTests();
		} catch (Exception x){
			x.printStackTrace();
			System.exit(1);
		}
		System.exit(0);
	}

	private CircularBufferTests() throws Exception{

		ccbin = ccb.getReader();
		ccbout = ccb.getWriter();
		CCBProducer ccbp = new CCBProducer();
		CCBConsumer ccbc = new CCBConsumer();
		ccbc.start();
		ccbp.start();
		while (ccbc.isAlive() || ccbp.isAlive()){
			if(System.in.available() > 0){
				ccbc.interrupt();
				ccbp.interrupt();
				return;
			}
		}
		String s1 = tgbaWriter.toString();
		if (!theGettysburgAddress.equals(s1)){
			throw new Exception(s1);
		}

		cbbin = cbb.getInputStream();
		cbbout = cbb.getOutputStream();
		CBBProducer cbbp = new CBBProducer();
		CBBConsumer cbbc = new CBBConsumer();
		cbbc.start();
		cbbp.start();
		while (cbbc.isAlive() || cbbp.isAlive()){
			if(System.in.available() > 0){
				cbbc.interrupt();
				cbbp.interrupt();
				return;
			}
		}
		assertEqual(pi, piOutputStream.toByteArray());

		COBProducer cobp = new COBProducer();
		COBConsumer cobc = new COBConsumer();
		cobc.start();
		cobp.start();
		while (cobc.isAlive() || cobp.isAlive()){
			if(System.in.available() > 0){
				cobc.interrupt();
				cobp.interrupt();
				return;
			}
		}
		String s2 = tgbaWriter2.toString();
		if (!theGettysburgAddress.equals(s2)){
			throw new Exception(s2);
		}
	}

	private static void assertEqual(byte[] b1, byte[] b2) throws Exception {
		if (b1.length != b2.length) throw new Exception ("Length mismatch: " + b1.length + ", " + b2.length);
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy