private Random rand = new Random();
public void testWeightIteratorFromBytes() {
byte[] bytes;
int offset, length;
WeightIterator iter;
bytes = new byte[0];
offset = 0;
length = 0;
// Empty WeightIterator
iter = new WeightIteratorFromBytes(bytes, offset, length);
assertEquals(false, iter.hasNext());
// Non-empty WeightIterator
int cnt = rand.nextInt(100) + 1;
offset = 0;
length = cnt * (Weight.ELEMENT_ID_NUM_BYTES + Weight.ELEMENT_WEIGHT_NUM_BYTES);
bytes = new byte[length];
ByteBuffer bb = ByteBuffer.wrap(bytes);
ArrayList<Weight> list1 = new ArrayList<Weight>(cnt);
for(int i = 0; i < cnt; i++) {
Weight weight = new Weight(i, rand.nextInt(10000));
list1.add(weight);
bb.putInt(weight.elementId);
bb.putInt(weight.elementWeight);
}
ArrayList<Weight> list2 = new ArrayList<Weight>(cnt);
iter = new WeightIteratorFromBytes(bytes, offset, length);
while(iter.hasNext()) {
Weight weight = new Weight(0, 0);
iter.next(weight);
list2.add(weight);
}
ArrayList<Weight> list3 = new ArrayList<Weight>(cnt);
iter = new WeightIteratorFromBytes(bytes, offset, length);
while(iter.hasNext()) {
list3.add(iter.next());
}
assertEquals(cnt, list1.size());
assertEquals(cnt, list2.size());
assertEquals(cnt, list3.size());