001: /*
002: * Copyright (C) 2005-2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: import java.io.*;
020:
021: /**
022: * Regression test for StraightStreamReader. When run, this program
023: * should nothing unless an error occurs.
024: *
025: * More information about this class is available from <a target="_top" href=
026: * "http://ostermiller.org/utils/StraightStreamReader.html">ostermiller.org</a>.
027: *
028: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
029: * @since ostermillerutils 1.06.02
030: */
031: class StraightStreamReaderTests {
032:
033: /**
034: * Test via command line
035: * @param args command line arguments (ignored)
036: */
037: public static void main(String args[]) {
038: try {
039: StraightStreamReader in;
040: char[] cbuf = new char[0x1000];
041: int read;
042: int totRead;
043:
044: // write a buffer with all possible values of bytes
045: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
046: for (int i = 0x00; i < 0x100; i++) {
047: buffer.write(i);
048: }
049: buffer.close();
050:
051: // read it back using the read single character method
052: in = new StraightStreamReader(new ByteArrayInputStream(
053: buffer.toByteArray()));
054: for (int i = 0x00; i < 0x100; i++) {
055: read = in.read();
056: if (read != i) {
057: throw new Exception("Error: " + i + " read as "
058: + read);
059: }
060: }
061: in.close();
062:
063: // read as much of it back as possible with one simple buffer read.
064: in = new StraightStreamReader(new ByteArrayInputStream(
065: buffer.toByteArray()));
066: totRead = in.read(cbuf);
067: if (totRead != 0x100) {
068: throw new Exception(
069: "Simple buffered read did not read the full amount: 0x"
070: + Integer.toHexString(totRead));
071: }
072: for (int i = 0x00; i < totRead; i++) {
073: if (cbuf[i] != i) {
074: throw new Exception("Error: 0x" + i + " read as 0x"
075: + cbuf[i]);
076: }
077: }
078: in.close();
079:
080: // read it back using buffer read method.
081: in = new StraightStreamReader(new ByteArrayInputStream(
082: buffer.toByteArray()));
083: totRead = 0;
084: while (totRead <= 0x100
085: && (read = in.read(cbuf, totRead, 0x100 - totRead)) > 0) {
086: totRead += read;
087: }
088: if (totRead != 0x100) {
089: throw new Exception("Not enough read. Bytes read: "
090: + Integer.toHexString(totRead));
091: }
092: for (int i = 0x00; i < totRead; i++) {
093: if (cbuf[i] != i) {
094: throw new Exception("Error: 0x" + i + " read as 0x"
095: + cbuf[i]);
096: }
097: }
098: in.close();
099:
100: // read it back using an offset buffer read method.
101: in = new StraightStreamReader(new ByteArrayInputStream(
102: buffer.toByteArray()));
103: totRead = 0;
104: while (totRead <= 0x100
105: && (read = in.read(cbuf, totRead + 0x123,
106: 0x100 - totRead)) > 0) {
107: totRead += read;
108: }
109: if (totRead != 0x100) {
110: throw new Exception("Not enough read. Bytes read: "
111: + Integer.toHexString(totRead));
112: }
113: for (int i = 0x00; i < totRead; i++) {
114: if (cbuf[i + 0x123] != i) {
115: throw new Exception("Error: 0x" + i + " read as 0x"
116: + cbuf[i + 0x123]);
117: }
118: }
119: in.close();
120:
121: // read it back using a partial offset buffer read method.
122: in = new StraightStreamReader(new ByteArrayInputStream(
123: buffer.toByteArray()));
124: totRead = 0;
125: while (totRead <= 0x100
126: && (read = in.read(cbuf, totRead + 0x123, 7)) > 0) {
127: totRead += read;
128: }
129: if (totRead != 0x100) {
130: throw new Exception("Not enough read. Bytes read: "
131: + Integer.toHexString(totRead));
132: }
133: for (int i = 0x00; i < totRead; i++) {
134: if (cbuf[i + 0x123] != i) {
135: throw new Exception("Error: 0x" + i + " read as 0x"
136: + cbuf[i + 0x123]);
137: }
138: }
139: in.close();
140:
141: } catch (Exception x) {
142: System.err.println(x.getMessage());
143: System.exit(1);
144: }
145: System.exit(0);
146: }
147: }
|