LANG-1163 There are no tests for CharSequenceUtils.regionMatches

This commit is contained in:
Sebb 2015-08-11 14:29:57 +01:00
parent 68acbc803e
commit e8a2c29721
3 changed files with 119 additions and 1 deletions

View File

@ -62,6 +62,7 @@
<action issue="LANG-1074" type="add" dev="djones" due-to="Haiyang Li">Add a method to ArrayUtils for removing all occurrences of a given element</action>
<action issue="LANG-1107" type="update" dev="chas">Fix parsing edge cases in FastDateParser</action>
<action issue="LANG-1162" type="fix" dev="sebb">StringUtils#equals fails with Index OOBE on non-Strings with identical leading prefix</action>
<action issue="LANG-1163" type="fix" dev="sebb">There are no tests for CharSequenceUtils.regionMatches</action>
</release>
<release version="3.4" date="2014-04-06" description="Feature and bugfix release">

View File

@ -191,6 +191,20 @@ public class CharSequenceUtils {
int index2 = start;
int tmpLen = length;
// Extract these first so we detect NPEs the same as the java.lang.String version
final int srcLen = cs.length() - thisStart;
final int otherLen = substring.length() - start;
// Check for invalid parameters
if (thisStart < 0 || start < 0 || length < 0) {
return false;
}
// Check that the regions are long enough
if (srcLen < length || otherLen < length) {
return false;
}
while (tmpLen-- > 0) {
final char c1 = cs.charAt(index1++);
final char c2 = substring.charAt(index2++);

View File

@ -73,4 +73,107 @@ public class CharSequenceUtilsTest {
Assert.assertEquals(null, CharSequenceUtils.subSequence(StringUtils.EMPTY, 1));
}
static class TestData{
final String source;
final boolean ignoreCase;
final int toffset;
final String other;
final int ooffset;
final int len;
final boolean expected;
final Class<?> throwable;
TestData(String source, boolean ignoreCase, int toffset,
String other, int ooffset, int len, boolean expected){
this.source = source;
this.ignoreCase = ignoreCase;
this.toffset = toffset;
this.other = other;
this.ooffset = ooffset;
this.len = len;
this.expected = expected;
this.throwable = null;
}
TestData(String source, boolean ignoreCase, int toffset,
String other, int ooffset, int len, Class<?> throwable){
this.source = source;
this.ignoreCase = ignoreCase;
this.toffset = toffset;
this.other = other;
this.ooffset = ooffset;
this.len = len;
this.expected = false;
this.throwable = throwable;
}
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(source).append("[").append(toffset).append("]");
sb.append(ignoreCase? " caseblind ":" samecase ");
sb.append(other).append("[").append(ooffset).append("]");
sb.append(" ").append(len).append(" => ");
if (throwable != null) {
sb.append(throwable);
} else {
sb.append(expected);
}
return sb.toString();
}
}
private static final TestData[] TEST_DATA = {
// Source IgnoreCase Offset Other Offset Length Result
new TestData("", true, -1, "", -1, -1, false),
new TestData("", true, 0, "", 0, 1, false),
new TestData("a", true, 0, "abc", 0, 0, true),
new TestData("a", true, 0, "abc", 0, 1, true),
new TestData("a", true, 0, null, 0, 0, NullPointerException.class),
new TestData(null, true, 0, null, 0, 0, NullPointerException.class),
new TestData(null, true, 0, "", 0, 0, NullPointerException.class),
};
private static abstract class RunTest {
abstract boolean invoke();
void run(TestData data, String id) {
if (data.throwable != null) {
try {
invoke();
Assert.fail(id + " Expected " + data.throwable);
} catch (Exception e) {
if (!e.getClass().equals(data.throwable)) {
Assert.fail(id + " Expected " + data.throwable + " got " + e.getClass());
}
}
} else {
boolean stringCheck = invoke();
Assert.assertEquals(id + " Failed test " + data, data.expected, stringCheck);
}
}
}
@Test
public void testRegionMatches() {
for (final TestData data : TEST_DATA) {
new RunTest() {
@Override
boolean invoke() {
return data.source.regionMatches(data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
}
}.run(data, "String");
new RunTest() {
@Override
boolean invoke() {
return CharSequenceUtils.regionMatches(data.source, data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
}
}.run(data, "CSString");
new RunTest() {
@Override
boolean invoke() {
return CharSequenceUtils.regionMatches(new StringBuilder(data.source), data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
}
}.run(data, "CSNonString");
}
}
}