/* Reattore HTTP Server
Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: TestByteSourceSink.java,v 1.5 2003/01/21 22:43:12 michaelh Exp $
*/
package test.juju.reattore.io.impl;
import junit.framework.*;
import juju.reattore.io.impl.ByteSourceSink;
import juju.reattore.io.Source;
public class TestByteSourceSink
extends TestCase {
private ByteSourceSink ss = new ByteSourceSink();
/** Tests that put stores the correct set of bytes */
public void testPut() {
ss.put(1);
ss.put(2);
ss.put(-15);
assertTrue(ss.compareTo(new byte[] { 1, 2, -15 }) == 0);
ss.put(123);
ss.put(45);
ss.put(-126);
assertTrue(ss.compareTo(new byte[] { 1, 2, -15, 123, 45, -126 }) == 0);
}
/** Tests the compareTo method */
public void testCompareTo() {
byte[] seed = new byte[] { 1, 2, 3, 4 };
ss.put(seed, 0, seed.length);
assertEquals(ss.compareTo(seed), 0);
assertTrue(ss.compareTo(new byte[] { }) > 0);
assertTrue(ss.compareTo(new byte[] { 1, 2, 3, 4, 5, 6, 7 }) < 0);
assertTrue(ss.compareTo(new byte[] { 8, 8, 8, 8, 8, 8, 8 }) < 0);
assertTrue(ss.compareTo(new byte[] { 1, 2, 3, 4 }) == 0);
assertTrue(ss.compareTo(new byte[] { 1, 2, 4, 4 }) < 0);
assertTrue(ss.compareTo(new byte[] { 1, -17, 3, 4 }) > 0);
}
/** Tests bulk put and sequential get */
public void testGet() {
byte[] seed = new byte[] { 1, 2, 4, 5, -1 };
ss.put(seed, 0, seed.length);
assertEquals(1, ss.get());
assertEquals(2, ss.get());
assertEquals(4, ss.get());
assertEquals(5, ss.get());
assertEquals(-1, ss.get());
assertEquals(Source.EOF, ss.get());
assertEquals(Source.EOF, ss.get());
}
/** Tests mark() and reset() */
public void testMarkReset() {
byte[] seed = new byte[] { 1, 2, 4, 5, -1 };
ss.put(seed, 0, seed.length);
assertEquals(1, ss.get());
assertEquals(2, ss.get());
assertEquals(4, ss.get());
assertEquals(5, ss.get());
assertEquals(-1, ss.get());
assertEquals(Source.EOF, ss.get());
ss.reset();
assertEquals(1, ss.get());
assertEquals(2, ss.get());
assertEquals(4, ss.get());
ss.mark();
assertEquals(5, ss.get());
assertEquals(-1, ss.get());
assertEquals(Source.EOF, ss.get());
ss.reset();
assertEquals(5, ss.get());
assertEquals(-1, ss.get());
assertEquals(Source.EOF, ss.get());
ss.reset();
assertEquals(5, ss.get());
assertEquals(-1, ss.get());
assertEquals(Source.EOF, ss.get());
}
/** Tests rewind() */
public void testRewind() {
byte[] seed = new byte[] { 20, -25, 30, -40, 60 };
ss.put(seed, 0, seed.length);
assertEquals(20, ss.get());
assertEquals(-25, ss.get());
assertEquals(30, ss.get());
ss.mark();
assertEquals(-40, ss.get());
assertEquals(60, ss.get());
ss.rewind();
assertEquals(20, ss.get());
assertEquals(-25, ss.get());
assertEquals(30, ss.get());
assertEquals(-40, ss.get());
assertEquals(60, ss.get());
ss.reset();
assertEquals(-40, ss.get());
ss.rewind();
assertEquals(20, ss.get());
}
/** Tests compact() */
public void testCompact() {
byte[] seed = new byte[] { 1, 2, 3 };
ss.put(seed, 0, seed.length);
ss.compact();
assertTrue(ss.array().length == 3);
assertEquals(1, ss.array()[0]);
assertEquals(2, ss.array()[1]);
assertEquals(3, ss.array()[2]);
}
public void testGrow() {
ss.put(new byte[128], 0, 128);
ss.put('a');
}
public void testCreate() {
ss = new ByteSourceSink(new byte[] { 1, 2, 3 });
assertEquals(3, ss.remaining());
assertEquals(1, ss.get());
assertEquals(2, ss.get());
assertEquals(3, ss.get());
assertEquals(Source.EOF, ss.get());
}
/** @see TestSuite */
public static Test suite() {
return new TestSuite(TestByteSourceSink.class);
}
}