summaryrefslogtreecommitdiffstats
path: root/harmony-tests/src/test/java
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2013-11-27 12:04:59 +0000
committerNarayan Kamath <narayan@google.com>2013-12-12 10:33:00 +0000
commit91bb19d2ef9822cfb50fd1191cdb3ee06e2939b9 (patch)
tree98e783d52ca5ef0005b4a3726a366a947538ac54 /harmony-tests/src/test/java
parentf6899787fcd0a1ca67fb207f39a5c18472e8c892 (diff)
downloadlibcore-91bb19d2ef9822cfb50fd1191cdb3ee06e2939b9.zip
libcore-91bb19d2ef9822cfb50fd1191cdb3ee06e2939b9.tar.gz
libcore-91bb19d2ef9822cfb50fd1191cdb3ee06e2939b9.tar.bz2
Fix a CharsetEncoder bug.
When malformed or unmappable characters span input buffers, we'd end up setting a negative position on the buffer. Also fix up a few test cases which were wrong in several ways. - One test was simply checking for the wrong sort of exception (unmappable vs malformed) - Another test was expecting encode() to throw an error (and ignoring flush) but the API allows flush to throw an error instead of encode. bug: 10729779 Change-Id: I6560b749ca2445651d61ca651f8a5e388cf1c1b0
Diffstat (limited to 'harmony-tests/src/test/java')
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/charset/ASCIICharsetEncoderTest.java27
1 files changed, 16 insertions, 11 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/charset/ASCIICharsetEncoderTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/charset/ASCIICharsetEncoderTest.java
index 3be239c..11b030a 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/charset/ASCIICharsetEncoderTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/charset/ASCIICharsetEncoderTest.java
@@ -16,6 +16,7 @@
package org.apache.harmony.tests.java.nio.charset;
+import junit.framework.TestCase;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
@@ -26,13 +27,11 @@ import java.nio.charset.CodingErrorAction;
import java.nio.charset.MalformedInputException;
import java.nio.charset.UnmappableCharacterException;
-import junit.framework.TestCase;
-
public class ASCIICharsetEncoderTest extends TestCase {
// charset for ascii
- private static final Charset cs = Charset.forName("ascii");
- private static final CharsetEncoder encoder = cs.newEncoder();
+ private final Charset cs = Charset.forName("ascii");
+ private final CharsetEncoder encoder = cs.newEncoder();
private static final int MAXCODEPOINT = 0x7F;
/*
* @see CharsetEncoderTest#setUp()
@@ -83,15 +82,21 @@ public class ASCIICharsetEncoderTest extends TestCase {
}
encoder.reset();
ByteBuffer out = ByteBuffer.allocate(10);
- assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true)
- .isMalformed());
- encoder.flush(out);
+ assertEquals(CoderResult.UNDERFLOW,
+ encoder.encode(CharBuffer.wrap("\ud800"), out, true));
+ assertTrue(encoder.flush(out).isMalformed());
encoder.reset();
+
out = ByteBuffer.allocate(10);
- assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer
- .wrap("\ud800"), out, false));
- assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true)
- .isMalformed());
+ CharBuffer buffer1 = CharBuffer.wrap("\ud800");
+ CharBuffer buffer2 = CharBuffer.wrap("\udc00");
+ assertSame(CoderResult.UNDERFLOW, encoder.encode(buffer1, out, false));
+ // We consume the entire input buffer because we're in an underflow
+ // state. We can't make a decision on whether the char in this buffer
+ // is unmappable or malformed without looking at the next input buffer.
+ assertEquals(1, buffer1.position());
+ assertTrue(encoder.encode(buffer2, out, true).isUnmappable());
+ assertEquals(0, buffer2.position());
}
public void testEncodeMapping() throws CharacterCodingException {