import junit.framework.*;
import java.util.Random;
import java.io.IOException;
import java.io.InputStream;
import Utils.*;
public class testStorage extends TestCase
{
public testStorage(String name)
{
super(name);
}
private Storage m_disk;
public void setUp() {
// Creates a dummy disk (10kb) for use in the tests
// All but a couple of tests will make use of this
m_disk = new Storage(10240);
// Fill with sequential incrementing numbers as shorts
for ( short i = 0; i < m_disk.getSize() / 2; ++i ) {
m_disk.writeShort(i);
}
m_disk.seekFromStart(0);
}
private int getAfterShort(int pos) {
return pos + 2 < m_disk.getSize() ? m_disk.getShort(pos + 2) : 0;
}
private int getBeforeShort(int pos) {
return pos - 2 > 0 ? m_disk.getShort(pos - 2) : 0;
}
public void testSeqReadShort()
{
for ( short i = 0; i < m_disk.getSize() / 2; ++i ) {
assertEquals(m_disk.readShort(), i);
assertEquals(m_disk.getPosition(), i * 2 + 2);
}
}
public void testRandomReadShort()
{
Random r = new Random();
int seekPos = 512; // Seek to a non-zero position
m_disk.seekFromStart(seekPos);
for ( short i = 0; i < m_disk.getSize(); ++i ) {
int x = r.nextInt(m_disk.getSize() / 2);
assertEquals(m_disk.getShort(x * 2), x);
assertEquals(m_disk.getPosition(), seekPos);
}
}
public void testSeqWriteShort()
{
int count = m_disk.getSize() / 2;
int seekPos = 512; // Seek to a non-zero position
m_disk.seekFromStart(seekPos);
for ( short x = 0; x < count; ++x ) {
int left = getBeforeShort(x * 2);
int right = getAfterShort(x * 2);
m_disk.putShort(x * 2, (short)(-x));
assertEquals(m_disk.getShort(x * 2), (short)(-x));
assertEquals(m_disk.getPosition(), seekPos);
assertEquals(left, getBeforeShort(x * 2));
assertEquals(right, getAfterShort(x * 2));
}
}
public void testRandomWriteShort()
{
Random r = new Random();
int seekPos = 512; // Seek to a non-zero position
m_disk.seekFromStart(seekPos);
int count = m_disk.getSize() / 2;
for ( short i = 0; i < count; ++i ) {
int x = r.nextInt(count);
int left = getBeforeShort(x * 2);
int right = getAfterShort(x * 2);
m_disk.putShort(x * 2, i);
assertEquals(m_disk.getShort(x * 2), i);
assertEquals(m_disk.getPosition(), seekPos);
assertEquals(left, getBeforeShort(x * 2));
assertEquals(right, getAfterShort(x * 2));
}
}
public void testSeqReadByte()
{
for ( short i = 0; i < m_disk.getSize(); ++i ) {
assertEquals(m_disk.getPosition(), i);
assertEquals(m_disk.readByte(), i % 2 == 0 ? (byte)(i >> 9) : (byte)(i / 2));
assertEquals(m_disk.getPosition(), i + 1);
}
}
public void testRandomReadByte()
{
Random r = new Random();
int seekPos = 512; // Seek to a non-zero position
m_disk.seekFromStart(seekPos);
for ( short i = 0; i < m_disk.getSize(); ++i ) {
int x = r.nextInt(m_disk.getSize() / 2);
assertEquals(m_disk.getByte(x), x % 2 == 0 ? (byte)(x >> 9) : (byte)(x / 2));
assertEquals(m_disk.getPosition(), seekPos);
}
}
private int getAfterByte(int pos) {
return pos + 1 < m_disk.getSize() ? m_disk.getByte(pos + 1) : 0;
}
private int getBeforeByte(int pos) {
return pos - 1 > 0 ? m_disk.getByte(pos - 1) : 0;
}
public void testSeqWriteByte()
{
int count = m_disk.getSize();
int seekPos = 512; // Seek to a non-zero position
m_disk.seekFromStart(seekPos);
for ( int x = 0; x < count; ++x ) {
int left = getBeforeByte(x);
int right = getAfterByte(x);
m_disk.putByte(x, (byte)(x));
assertEquals(m_disk.getByte(x), (byte)(x));
assertEquals(m_disk.getPosition(), seekPos);
assertEquals(left, getBeforeByte(x));
assertEquals(right, getAfterByte(x));
}
}
public void testRandomWriteByte()
{
Random r = new Random();
int seekPos = 512; // Seek to a non-zero position
m_disk.seekFromStart(seekPos);
int count = m_disk.getSize() / 2;
for ( short i = 0; i < count; ++i ) {
int x = r.nextInt(count);
int left = getBeforeByte(x);
int right = getAfterByte(x);
m_disk.putByte(x, (byte)(i));
assertEquals(m_disk.getByte(x), (byte)(i));
assertEquals(m_disk.getPosition(), seekPos);
assertEquals(left, getBeforeByte(x));
assertEquals(right, getAfterByte(x));
}
}
public void testLoadFromFile() throws IOException
{
InputStream stream = testStorage.class.getResourceAsStream("storageData.dat");
assertEquals(42 * 42, m_disk.loadFromFile(stream));
for (int i = 0; i < 42 * 42; ++i) {
assertEquals((byte)(i % 256), m_disk.readByte());
}
}
}