This source file includes following definitions.
- testIsValidUtf8_1Byte
- testIsValidUtf8_2Bytes
- testIsValidUtf8_3Bytes
- testIsValidUtf8_4BytesSamples
- testSomeSequences
- toByteArray
- toByteString
- assertValidUtf8
- assertValidUtf8
- assertInvalidUtf8
- asBytes
- testShardsHaveExpectedRoundTrippables
package com.google.protobuf;
import com.google.protobuf.IsValidUtf8TestUtil.Shard;
import junit.framework.TestCase;
import java.io.UnsupportedEncodingException;
public class IsValidUtf8Test extends TestCase {
public void testIsValidUtf8_1Byte() throws UnsupportedEncodingException {
IsValidUtf8TestUtil.testBytes(1,
IsValidUtf8TestUtil.EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT);
}
public void testIsValidUtf8_2Bytes() throws UnsupportedEncodingException {
IsValidUtf8TestUtil.testBytes(2,
IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT);
}
public void testIsValidUtf8_3Bytes() throws UnsupportedEncodingException {
IsValidUtf8TestUtil.testBytes(3,
IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT);
}
public void testIsValidUtf8_4BytesSamples()
throws UnsupportedEncodingException {
assertValidUtf8(0xF0, 0xA4, 0xAD, 0xA2);
assertInvalidUtf8(0xF0, 0xA4, 0xAD, 0x7F);
assertInvalidUtf8(0xF0, 0xA4, 0xAD, 0xC0);
assertInvalidUtf8(0xF0, 0x8F, 0xAD, 0xA2);
assertInvalidUtf8(0xF4, 0x90, 0xAD, 0xA2);
}
public void testSomeSequences() {
assertTrue(asBytes("").isValidUtf8());
assertTrue(asBytes("\u0000abc\u007f").isValidUtf8());
assertTrue(asBytes("\u00a2\u00a2").isValidUtf8());
assertTrue(asBytes("\u020ac\u020ac").isValidUtf8());
assertTrue(asBytes("\u024B62\u024B62").isValidUtf8());
assertTrue(
asBytes("a\u020ac\u00a2b\\u024B62u020acc\u00a2de\u024B62")
.isValidUtf8());
assertInvalidUtf8(-1, 0, -1, 0);
}
private byte[] toByteArray(int... bytes) {
byte[] realBytes = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
realBytes[i] = (byte) bytes[i];
}
return realBytes;
}
private ByteString toByteString(int... bytes) {
return ByteString.copyFrom(toByteArray(bytes));
}
private void assertValidUtf8(int[] bytes, boolean not) {
byte[] realBytes = toByteArray(bytes);
assertTrue(not ^ Utf8.isValidUtf8(realBytes));
assertTrue(not ^ Utf8.isValidUtf8(realBytes, 0, bytes.length));
ByteString lit = ByteString.copyFrom(realBytes);
ByteString sub = lit.substring(0, bytes.length);
assertTrue(not ^ lit.isValidUtf8());
assertTrue(not ^ sub.isValidUtf8());
ByteString[] ropes = {
RopeByteString.newInstanceForTest(ByteString.EMPTY, lit),
RopeByteString.newInstanceForTest(ByteString.EMPTY, sub),
RopeByteString.newInstanceForTest(lit, ByteString.EMPTY),
RopeByteString.newInstanceForTest(sub, ByteString.EMPTY),
RopeByteString.newInstanceForTest(sub, lit)
};
for (ByteString rope : ropes) {
assertTrue(not ^ rope.isValidUtf8());
}
}
private void assertValidUtf8(int... bytes) {
assertValidUtf8(bytes, false);
}
private void assertInvalidUtf8(int... bytes) {
assertValidUtf8(bytes, true);
}
private static ByteString asBytes(String s) {
return ByteString.copyFromUtf8(s);
}
public void testShardsHaveExpectedRoundTrippables() {
int actual = 0;
for (Shard shard : IsValidUtf8TestUtil.FOUR_BYTE_SHARDS) {
actual += shard.expected;
}
assertEquals(IsValidUtf8TestUtil.EXPECTED_FOUR_BYTE_ROUNDTRIPPABLE_COUNT,
actual);
}
}