Add additional tests, with fixes for edge cases.

This commit is contained in:
Marcus Cavanaugh 2014-09-30 14:49:06 -07:00
Родитель 4f4d5d9a42
Коммит 7217c02efe
2 изменённых файлов: 127 добавлений и 10 удалений

Просмотреть файл

@ -405,6 +405,9 @@ Override.simple("java/lang/StringBuffer.setLength.(I)V", function(newLength) {
if (newLength > this.buf.length) {
expandCapacity.call(this, newLength);
}
for (; this.count < newLength; this.count++) {
this.buf[this.count] = '\0';
}
this.count = newLength;
});
@ -417,10 +420,12 @@ Override.simple("java/lang/StringBuffer.charAt.(I)C", function(index) {
});
Override.simple("java/lang/StringBuffer.getChars.(II[CI)V", function(srcBegin, srcEnd, dst, dstBegin) {
if (srcBegin < 0 || srcEnd < 0 || srcEnd > this.count || srcBegin > srcEnd ||
dstBegin + (srcEnd - srcBegin) > dst.length || dstBegin < 0) {
if (srcBegin < 0 || srcEnd < 0 || srcEnd > this.count || srcBegin > srcEnd) {
throw new JavaException("java/lang/StringIndexOutOfBoundsException");
}
if (dstBegin + (srcEnd - srcBegin) > dst.length || dstBegin < 0) {
throw new JavaException("java/lang/ArrayIndexOutOfBoundsException");
}
dst.set(this.buf.subarray(srcBegin, srcEnd), dstBegin);
});
@ -462,12 +467,18 @@ Override.simple("java/lang/StringBuffer.append.(Ljava/lang/String;)Ljava/lang/St
});
Override.simple("java/lang/StringBuffer.append.([C)Ljava/lang/StringBuffer;", function(chars) {
if (chars == null) {
throw new JavaException("java/lang/NullPointerException");
}
return stringBufferAppend.call(this, chars);
});
Override.simple("java/lang/StringBuffer.append.([CII)Ljava/lang/StringBuffer;", function(chars, offset, length) {
if (offset < 0) {
throw new JavaException("java/lang/IndexOutOfBoundsException");
if (chars == null) {
throw new JavaException("java/lang/NullPointerException");
}
if (offset < 0 || offset + length > chars.length) {
throw new JavaException("java/lang/ArrayIndexOutOfBoundsException");
}
return stringBufferAppend.call(this, chars.subarray(offset, offset + length));
});
@ -518,7 +529,7 @@ function stringBufferDelete(start, end) {
var len = end - start;
if (len > 0) {
// When Gecko 34 is released, we can use TypedArray.copyWithin() instead.
this.buf.set(this.buf.subarray(start + len, this.count), start);
this.buf.set(this.buf.subarray(end, this.count), start);
this.count -= len;
}
return this;
@ -547,7 +558,7 @@ function stringBufferInsert(offset, data) {
throw new JavaException("java/lang/NullPointerException");
}
if (offset < 0 || offset > this.count) {
throw new JavaException("java/lang/StringIndexOutOfBoundsException");
throw new JavaException("java/lang/ArrayIndexOutOfBoundsException");
}
if (!(data instanceof Uint16Array)) {
data = util.stringToCharArray(data);

Просмотреть файл

@ -24,11 +24,12 @@
package gnu.testlet.vm;
import gnu.testlet.*;
import java.io.UnsupportedEncodingException;
class StringBufferTest implements Testlet {
public void test(TestHarness th) {
th.check(test1(), 0);
th.check(test1(), 0);
}
public int test1() {
@ -140,8 +141,8 @@ class StringBufferTest implements Testlet {
/*
** charAt tests...
*/
str1 = new StringBuffer("abcd");
str1 = new StringBuffer("abcd");
if (str1.charAt(0) != 'a') {
return 320;
}
@ -533,8 +534,113 @@ class StringBufferTest implements Testlet {
return 1070;
}
return 0;
// Additional tests from http://goo.gl/UhDJwr
StringBuffer obj = new StringBuffer();
try {
obj.getChars(0, 0, new char[0], -1);
return 1080;
} catch (ArrayIndexOutOfBoundsException e) {
// expected
}
StringBuffer buffer = new StringBuffer("abcde");
try {
buffer.setLength(-1);
return 1090;
} catch (IndexOutOfBoundsException e) {
// expected
}
if (!"abcde".equals(buffer.toString())) { return 1091; }
buffer.setLength(1);
buffer.append('f');
System.out.println(buffer.toString());
if (!"af".equals(buffer.toString())) { return 1092; }
buffer = new StringBuffer("abcde");
buffer.setLength(3);
buffer.append('f');
if (!"abcf".equals(buffer.toString())) { return 1093; }
buffer = new StringBuffer("abcde");
buffer.setLength(2);
try {
buffer.charAt(3);
return 1100;
} catch (IndexOutOfBoundsException e) {
// Expected
}
buffer = new StringBuffer();
buffer.append("abcdefg");
buffer.setLength(2);
buffer.setLength(5);
for (int i = 2; i < 5; i++) {
if (buffer.charAt(i) != '\u0000') {
return 1101;
}
}
buffer = new StringBuffer();
buffer.append("abcdefg");
buffer.delete(2, 4);
buffer.setLength(7);
if (buffer.charAt(0) != 'a') { return 1102; }
if (buffer.charAt(1) != 'b') { return 1103; }
if (buffer.charAt(2) != 'e') { return 1104; }
if (buffer.charAt(3) != 'f') { return 1105; }
if (buffer.charAt(4) != 'g') { return 1106; }
for (int i = 5; i < 7; i++) {
if (buffer.charAt(i) != '\u0000') {
return 1107;
}
}
buffer = new StringBuffer();
buffer.append("abcdefg");
buffer.setLength(5);
buffer.setLength(7);
for (int i = 5; i < 7; i++) {
if (buffer.charAt(i) != '\u0000') {
return 1108;
}
}
buffer = new StringBuffer();
if (!"".equals(buffer.toString())) {
return 1109;
}
buffer.append("abcde");
if (!"abcde".equals(buffer.toString())) { return 1110; }
buffer.setLength(5);
buffer.append("fghij");
if (!"abcdefghij".equals(buffer.toString())) { return 1111; }
obj = new StringBuffer();
try {
obj.append(new char[0], -1, -1);
return 1112;
} catch (ArrayIndexOutOfBoundsException e) {
// expected
}
obj = new StringBuffer();
try {
obj.append((char[]) null, -1, -1);
return 1120;
} catch (NullPointerException e) {
// expected
}
obj = new StringBuffer();
try {
obj.insert(-1, ' ');
return 1130;
} catch (ArrayIndexOutOfBoundsException e) {
// expected
}
return 0;
}
}