Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/ctc/wstx/util/StringVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public final class StringVector
*/

public StringVector(int initialCount) {
if (initialCount == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about checking for <= 0 as negative ones not ok either (will fail few lines down anyway but still)?

// StringVectors of internal size 0 can't grow, so they are not allowed
throw new IllegalArgumentException("Initial count cannot be zero");
}
mStrings = new String[initialCount];
}

Expand Down
74 changes: 74 additions & 0 deletions src/test/java/wstxtest/util/TestStringVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,78 @@ public void testBasic()
sv.clear(true);
assertEquals(0, sv.size());
}

public void testGetString()
{
StringVector sv = new StringVector(2);

try {
sv.getString(-1);
fail("Should have thrown IllegalArgumentException for negative index");
} catch (IllegalArgumentException e) {
// expected
}

try {
sv.getString(0);
fail("Should have thrown IllegalArgumentException for index 0 in empty vector");
} catch (IllegalArgumentException e) {
// expected
}

sv.addString("foo");
assertEquals("foo", sv.getString(0));

try {
sv.getString(1);
fail("Should have thrown IllegalArgumentException for index 1 in vector with size 1");
} catch (IllegalArgumentException e) {
// expected
}
}

public void testGetLastString()
{
StringVector sv = new StringVector(2);

try {
sv.getLastString();
fail("Should have thrown IllegalStateException for empty vector");
} catch (IllegalStateException e) {
// expected
}

sv.addString("foo");
assertEquals("foo", sv.getLastString());

sv.addString("bar");
assertEquals("bar", sv.getLastString());
}

public void testGrowArray()
{
try {
new StringVector(0);
fail("Should have thrown IllegalArgumentException for StringVector with internal length of zero");
} catch (IllegalArgumentException e) {
// expected
}

StringVector sv = new StringVector(2);

// Initial size is 2, so we can add two elements without growing
sv.addString("foo");
sv.addString("bar");
assertEquals(2, sv.getInternalArray().length);

// Adding a third element triples the array size
sv.addString("baz");
assertEquals(6, sv.getInternalArray().length);
assertEquals("baz", sv.getString(2));

// Adding more elements should continue to work without growing the array
sv.addString("qux");
assertEquals(6, sv.getInternalArray().length);
assertEquals("qux", sv.getString(3));
}
}
Loading