TestByteFieldpublic class TestByteField extends TestCase Title: Unit test for ByteField class
Description: Unit test for ByteField class |
Fields Summary |
---|
private static final byte[] | _test_array |
Constructors Summary |
---|
public TestByteField(String name)Constructor
super(name);
|
Methods Summary |
---|
public static void | main(java.lang.String[] ignored_args)Main
System.out.println("Testing util.ByteField functionality");
junit.textui.TestRunner.run(TestByteField.class);
| public void | testConstructors()Test constructors.
try
{
new ByteField(-1);
fail("Should have caught ArrayIndexOutOfBoundsException");
}
catch (ArrayIndexOutOfBoundsException ignored_e)
{
// as expected
}
ByteField field = new ByteField(2);
assertEquals(( byte ) 0, field.get());
try
{
new ByteField(-1, ( byte ) 1);
fail("Should have caught ArrayIndexOutOfBoundsException");
}
catch (ArrayIndexOutOfBoundsException ignored_e)
{
// as expected
}
field = new ByteField(2, ( byte ) 3);
assertEquals(( byte ) 3, field.get());
byte[] array = new byte[ 3 ];
try
{
new ByteField(-1, ( byte ) 1, array);
fail("Should have caught ArrayIndexOutOfBoundsException");
}
catch (ArrayIndexOutOfBoundsException ignored_e)
{
// as expected
}
field = new ByteField(2, ( byte ) 4, array);
assertEquals(( byte ) 4, field.get());
assertEquals(( byte ) 4, array[ 2 ]);
array = new byte[ 2 ];
try
{
new ByteField(2, ( byte ) 5, array);
fail("should have gotten ArrayIndexOutOfBoundsException");
}
catch (ArrayIndexOutOfBoundsException ignored_e)
{
// as expected
}
for (int j = 0; j < _test_array.length; j++)
{
array = new byte[ 1 ];
new ByteField(0, _test_array[ j ], array);
assertEquals(_test_array[ j ], new ByteField(0, array).get());
}
| public void | testReadFromBytes()Test readFromBytes
ByteField field = new ByteField(1);
byte[] array = new byte[ 1 ];
try
{
field.readFromBytes(array);
fail("should have caught ArrayIndexOutOfBoundsException");
}
catch (ArrayIndexOutOfBoundsException ignored_e)
{
// as expected
}
field = new ByteField(0);
for (int j = 0; j < _test_array.length; j++)
{
array[ 0 ] = _test_array[ j ];
field.readFromBytes(array);
assertEquals("testing " + j, _test_array[ j ], field.get());
}
| public void | testReadFromStream()Test readFromStream
ByteField field = new ByteField(0);
byte[] buffer = new byte[ _test_array.length ];
System.arraycopy(_test_array, 0, buffer, 0, buffer.length);
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
for (int j = 0; j < buffer.length; j++)
{
field.readFromStream(stream);
assertEquals("Testing " + j, _test_array[ j ], field.get());
}
| public void | testSet()Test set() methods
ByteField field = new ByteField(0);
byte[] array = new byte[ 1 ];
for (int j = 0; j < _test_array.length; j++)
{
field.set(_test_array[ j ]);
assertEquals("testing _1 " + j, _test_array[ j ], field.get());
field = new ByteField(0);
field.set(_test_array[ j ], array);
assertEquals("testing _2 ", _test_array[ j ], field.get());
assertEquals("testing _3 ", _test_array[ j ], array[ 0 ]);
}
| public void | testWriteToBytes()test writeToBytes
ByteField field = new ByteField(0);
byte[] array = new byte[ 1 ];
for (int j = 0; j < _test_array.length; j++)
{
field.set(_test_array[ j ]);
field.writeToBytes(array);
assertEquals("testing ", _test_array[ j ], array[ 0 ]);
}
|
|