Skip to content

Commit

Permalink
Merge pull request real-logic#125 from menski/fix-IntArrayList-addInt
Browse files Browse the repository at this point in the history
[Java] Use current size for array copy in IntArrayList
  • Loading branch information
mjpt777 authored Jan 16, 2018
2 parents bc6bd50 + 81507d0 commit f6e40e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void addInt(

if (index < size)
{
System.arraycopy(elements, index, elements, index + 1, requiredSize - index);
System.arraycopy(elements, index, elements, index + 1, size - index);
}

elements[index] = element;
Expand Down
14 changes: 14 additions & 0 deletions agrona/src/test/java/org/agrona/collections/IntArrayListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public void shouldAddValueAtIndex()
assertThat(list.getInt(count), is(count - 1));
}

@Test
public void shouldAddValueAtIndexWithNearlyFullCapacity()
{
final int count = IntArrayList.INITIAL_CAPACITY - 1;
final int value = count + 1;
IntStream.range(0, count).forEachOrdered(list::addInt);

list.addInt(0, value);

assertThat(list.size(), is(count + 1));
assertThat(list.getInt(0), is(value));
assertThat(list.getInt(count), is(count - 1));
}

@Test
public void shouldSetIntValue()
{
Expand Down

0 comments on commit f6e40e3

Please sign in to comment.