{
final MyBasicPlugIn p = new MyBasicPlugIn();
// test empty buffer:
{
final Buffer b = new Buffer();
assertEquals(b.getData(), null);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
for (int i = 0; i < 10; ++i)
{
final short[] ba = p.doValidateShortArraySize(b, i);
assertEquals(ba.length, i);
for (int j = 0; j < i; ++j)
{ assertEquals(ba[j], 0);
}
assertEquals(b.getData(), ba);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
}
}
// buf of len 5 with length set to 5:
{
final Buffer b = new Buffer();
final short[] bBuf = new short[5];
b.setData(bBuf);
b.setLength(bBuf.length);
assertTrue(b.getData() == bBuf);
assertEquals(b.getLength(), bBuf.length);
assertEquals(b.getOffset(), 0);
for (int i = 0; i < 10; ++i)
{
final short[] ba = p.doValidateShortArraySize(b, i);
if (i > bBuf.length)
assertTrue(ba != bBuf);
else
assertTrue(ba == bBuf);
final int max = i > bBuf.length ? i : bBuf.length;
assertEquals(ba.length, max);
for (int j = 0; j < i; ++j)
{ assertEquals(ba[j], 0);
}
assertEquals(b.getData(), ba);
assertEquals(b.getLength(), bBuf.length);
assertEquals(b.getOffset(), 0);
}
}
// buf of len 5 with length set to 0:
{
final Buffer b = new Buffer();
final short[] bBuf = new short[5];
b.setData(bBuf);
b.setLength(0);
assertTrue(b.getData() == bBuf);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
for (int i = 0; i < 10; ++i)
{
final short[] ba = p.doValidateShortArraySize(b, i);
if (i > bBuf.length)
assertTrue(ba != bBuf);
else
assertTrue(ba == bBuf);
final int max = i > bBuf.length ? i : bBuf.length;
assertEquals(ba.length, max);
for (int j = 0; j < i; ++j)
{ assertEquals(ba[j], 0);
}
assertEquals(b.getData(), ba);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
}
}
// it appears that getLength/setLength has nothing to do with doValidateShortArraySize.
// doValidateShortArraySize looks like it checks the size of the buf, and reallocates it
// if too small.
// try with a non-shortarray
{
final Buffer b = new Buffer();
final int[] bBuf = new int[5];
b.setData(bBuf);
b.setLength(0);
assertTrue(b.getData() == bBuf);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
for (int i = 0; i < 10; ++i)
{
final short[] ba = p.doValidateShortArraySize(b, i);
// if (i > bBuf.length)
// assertTrue(ba != bBuf);
// else
// assertTrue(ba == bBuf);
assertEquals(ba.length, i);
for (int j = 0; j < i; ++j)
{ assertEquals(ba[j], 0);
}
assertEquals(b.getData(), ba);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
}
}
// appears to simply reallocate if not a short array.
// See if it copies existing data:
{
final Buffer b = new Buffer();
final short[] bBuf = new short[] {0, 1, 2, 3, 4};
b.setData(bBuf);
b.setLength(0);
assertTrue(b.getData() == bBuf);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
for (int i = 0; i < 10; ++i)
{
final short[] ba = p.doValidateShortArraySize(b, i);
if (i > bBuf.length)
assertTrue(ba != bBuf);
else
assertTrue(ba == bBuf);
final int max = i > bBuf.length ? i : bBuf.length;
assertEquals(ba.length, max);
for (int j = 0; j < i; ++j)
{
if (j < bBuf.length)
assertEquals(ba[j], bBuf[j]);
else
assertEquals(ba[j], 0);
}
assertEquals(b.getData(), ba);
assertEquals(b.getLength(), 0);
assertEquals(b.getOffset(), 0);
}
}
// it does copy existing data.