Skip to content

Commit

Permalink
Improved test coverage of the asReader function and fixed a bug the t…
Browse files Browse the repository at this point in the history
…est uncovered
  • Loading branch information
DavyLandman committed Sep 19, 2024
1 parent e289490 commit a7cf69a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
endedWithHalfSurrogate = 0;
}
int endPos = off + len;
while (pos <= endPos) {
while (pos < endPos) {
if (!chars.hasNext()) {
break;
}
Expand All @@ -1469,7 +1469,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
} else {
cbuf[pos++] = Character.highSurrogate(nextChar);
char lowSide = Character.lowSurrogate(nextChar);
if (pos <= endPos) {
if (pos < endPos) {
cbuf[pos++] = lowSide;
} else {
endedWithHalfSurrogate = lowSide;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,38 @@ private String readerToString(IString a) {
}
}

private String readerSlowly(IString a) {
try (var r = a.asReader()) {
var result = new StringBuilder();
while (true) {
int oneChar = r.read();
if (oneChar == -1) {
break;
}
result.appendCodePoint(oneChar);
var buf = new char[3];
int read = r.read(buf);
if (read == -1) {
break;
}
result.append(buf, 0, read);
}
return result.toString();
} catch (IOException e) {
fail("IString::asReader failed", e);
return "";
}

}


private void assertEqualWriteAndRead(IString one, IString two) {
assertEquals(writerToString(one), writerToString(two), "IString::write had different results");
assertEquals(readerToString(one), readerToString(two), "IString::asReader had different results");
assertEquals(one.getValue(), writerToString(one), "IString::write should be the same as getValue");
assertEquals(one.getValue(), readerToString(one), "IString::asReader should be the same as getValue");
assertEquals(writerToString(one), writerToString(two), "IString::write had different results");
assertEquals(readerToString(one), readerToString(two), "IString::asReader had different results");
assertEquals(one.getValue(), readerSlowly(one), "IString::asReader had different results depending on buffer size");
assertEquals(two.getValue(), readerSlowly(two), "IString::asReader had different results depending on buffer size");
}

private void assertEqualCharAt(IString one, IString two) {
Expand Down

0 comments on commit a7cf69a

Please sign in to comment.