Index: third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java |
diff --git a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteStringTest.java b/third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java |
similarity index 72% |
copy from third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteStringTest.java |
copy to third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java |
index ad9f266e4cdb4f96c99faafcffda742907ed4cb6..8bb9f731d2face671c4af880ca277db736db34e0 100644 |
--- a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteStringTest.java |
+++ b/third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java |
@@ -1,6 +1,6 @@ |
// Protocol Buffers - Google's data interchange format |
// Copyright 2008 Google Inc. All rights reserved. |
-// https://developers.google.com/protocol-buffers/ |
+// http://code.google.com/p/protobuf/ |
// |
// Redistribution and use in source and binary forms, with or without |
// modification, are permitted provided that the following conditions are |
@@ -28,20 +28,19 @@ |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
-package com.google.protobuf; |
+package com.google.protobuf.test; |
+import com.google.protobuf.*; |
import com.google.protobuf.ByteString.Output; |
import junit.framework.TestCase; |
import java.io.ByteArrayInputStream; |
-import java.io.ByteArrayOutputStream; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.OutputStream; |
-import java.lang.reflect.Field; |
+import java.io.UnsupportedEncodingException; |
import java.nio.ByteBuffer; |
-import java.nio.charset.Charset; |
import java.util.ArrayList; |
import java.util.Arrays; |
import java.util.Iterator; |
@@ -57,7 +56,7 @@ import java.util.Random; |
*/ |
public class ByteStringTest extends TestCase { |
- private static final Charset UTF_16 = Charset.forName("UTF-16"); |
+ private static final String UTF_16 = "UTF-16"; |
static byte[] getTestBytes(int size, long seed) { |
Random random = new Random(seed); |
@@ -129,7 +128,7 @@ public class ByteStringTest extends TestCase { |
isArrayRange(byteString.toByteArray(), bytes, 500, bytes.length - 500)); |
} |
- public void testCopyFrom_StringEncoding() { |
+ public void testCopyFrom_StringEncoding() throws UnsupportedEncodingException { |
String testString = "I love unicode \u1234\u5678 characters"; |
ByteString byteString = ByteString.copyFrom(testString, UTF_16); |
byte[] testBytes = testString.getBytes(UTF_16); |
@@ -137,10 +136,10 @@ public class ByteStringTest extends TestCase { |
isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); |
} |
- public void testCopyFrom_Utf8() { |
+ public void testCopyFrom_Utf8() throws UnsupportedEncodingException { |
String testString = "I love unicode \u1234\u5678 characters"; |
ByteString byteString = ByteString.copyFromUtf8(testString); |
- byte[] testBytes = testString.getBytes(Internal.UTF_8); |
+ byte[] testBytes = testString.getBytes("UTF-8"); |
assertTrue("copyFromUtf8 string must respect the charset", |
isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); |
} |
@@ -154,7 +153,6 @@ public class ByteStringTest extends TestCase { |
isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); |
// Call copyFrom on an iteration that's not a collection |
ByteString byteStringAlt = ByteString.copyFrom(new Iterable<ByteString>() { |
- @Override |
public Iterator<ByteString> iterator() { |
return pieces.iterator(); |
} |
@@ -199,14 +197,6 @@ public class ByteStringTest extends TestCase { |
Arrays.equals(originalValue, newValue)); |
} |
- // Tests sizes that are near the rope copy-out threshold. |
- public void testReadFrom_mediumStream() throws IOException { |
- assertReadFrom(getTestBytes(ByteString.CONCATENATE_BY_COPY_SIZE - 1)); |
- assertReadFrom(getTestBytes(ByteString.CONCATENATE_BY_COPY_SIZE)); |
- assertReadFrom(getTestBytes(ByteString.CONCATENATE_BY_COPY_SIZE + 1)); |
- assertReadFrom(getTestBytes(200)); |
- } |
- |
// Tests sizes that are over multi-segment rope threshold. |
public void testReadFrom_largeStream() throws IOException { |
assertReadFrom(getTestBytes(0x100)); |
@@ -220,36 +210,6 @@ public class ByteStringTest extends TestCase { |
assertReadFrom(getTestBytes(0x10010)); |
} |
- // Tests sizes that are near the read buffer size. |
- public void testReadFrom_byteBoundaries() throws IOException { |
- final int min = ByteString.MIN_READ_FROM_CHUNK_SIZE; |
- final int max = ByteString.MAX_READ_FROM_CHUNK_SIZE; |
- |
- assertReadFrom(getTestBytes(min - 1)); |
- assertReadFrom(getTestBytes(min)); |
- assertReadFrom(getTestBytes(min + 1)); |
- |
- assertReadFrom(getTestBytes(min * 2 - 1)); |
- assertReadFrom(getTestBytes(min * 2)); |
- assertReadFrom(getTestBytes(min * 2 + 1)); |
- |
- assertReadFrom(getTestBytes(min * 4 - 1)); |
- assertReadFrom(getTestBytes(min * 4)); |
- assertReadFrom(getTestBytes(min * 4 + 1)); |
- |
- assertReadFrom(getTestBytes(min * 8 - 1)); |
- assertReadFrom(getTestBytes(min * 8)); |
- assertReadFrom(getTestBytes(min * 8 + 1)); |
- |
- assertReadFrom(getTestBytes(max - 1)); |
- assertReadFrom(getTestBytes(max)); |
- assertReadFrom(getTestBytes(max + 1)); |
- |
- assertReadFrom(getTestBytes(max * 2 - 1)); |
- assertReadFrom(getTestBytes(max * 2)); |
- assertReadFrom(getTestBytes(max * 2 + 1)); |
- } |
- |
// Tests that IOExceptions propagate through ByteString.readFrom(). |
public void testReadFrom_IOExceptions() { |
try { |
@@ -382,7 +342,7 @@ public class ByteStringTest extends TestCase { |
return -1; |
} |
} |
- |
+ |
// A stream which exposes the byte array passed into write(byte[], int, int). |
private static class EvilOutputStream extends OutputStream { |
public byte[] capturedArray = null; |
@@ -400,9 +360,9 @@ public class ByteStringTest extends TestCase { |
} |
} |
- public void testToStringUtf8() { |
+ public void testToStringUtf8() throws UnsupportedEncodingException { |
String testString = "I love unicode \u1234\u5678 characters"; |
- byte[] testBytes = testString.getBytes(Internal.UTF_8); |
+ byte[] testBytes = testString.getBytes("UTF-8"); |
ByteString byteString = ByteString.copyFrom(testBytes); |
assertEquals("copyToStringUtf8 must respect the charset", |
testString, byteString.toStringUtf8()); |
@@ -420,7 +380,7 @@ public class ByteStringTest extends TestCase { |
// Test newOutput() using a variety of buffer sizes and a variety of (fixed) |
// write sizes |
- public void testNewOutput_ArrayWrite() { |
+ public void testNewOutput_ArrayWrite() throws IOException { |
byte[] bytes = getTestBytes(); |
int length = bytes.length; |
int[] bufferSizes = {128, 256, length / 2, length - 1, length, length + 1, |
@@ -443,7 +403,7 @@ public class ByteStringTest extends TestCase { |
// Test newOutput() using a variety of buffer sizes, but writing all the |
// characters using write(byte); |
- public void testNewOutput_WriteChar() { |
+ public void testNewOutput_WriteChar() throws IOException { |
byte[] bytes = getTestBytes(); |
int length = bytes.length; |
int[] bufferSizes = {0, 1, 128, 256, length / 2, |
@@ -462,7 +422,7 @@ public class ByteStringTest extends TestCase { |
// Test newOutput() in which we write the bytes using a variety of methods |
// and sizes, and in which we repeatedly call toByteString() in the middle. |
- public void testNewOutput_Mixed() { |
+ public void testNewOutput_Mixed() throws IOException { |
Random rng = new Random(1); |
byte[] bytes = getTestBytes(); |
int length = bytes.length; |
@@ -494,13 +454,13 @@ public class ByteStringTest extends TestCase { |
isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); |
} |
} |
- |
- public void testNewOutputEmpty() { |
+ |
+ public void testNewOutputEmpty() throws IOException { |
// Make sure newOutput() correctly builds empty byte strings |
ByteString byteString = ByteString.newOutput().toByteString(); |
assertEquals(ByteString.EMPTY, byteString); |
} |
- |
+ |
public void testNewOutput_Mutating() throws IOException { |
Output os = ByteString.newOutput(5); |
os.write(new byte[] {1, 2, 3, 4, 5}); |
@@ -515,15 +475,6 @@ public class ByteStringTest extends TestCase { |
Arrays.equals(oldValue, newValue)); |
} |
- public void testNewCodedBuilder() throws IOException { |
- byte[] bytes = getTestBytes(); |
- ByteString.CodedBuilder builder = ByteString.newCodedBuilder(bytes.length); |
- builder.getCodedOutput().writeRawBytes(bytes); |
- ByteString byteString = builder.build(); |
- assertTrue("String built from newCodedBuilder() must contain the expected bytes", |
- isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); |
- } |
- |
public void testSubstringParity() { |
byte[] bigBytes = getTestBytes(2048 * 1024, 113344L); |
int start = 512 * 1024 - 3333; |
@@ -608,62 +559,6 @@ public class ByteStringTest extends TestCase { |
literalString.hashCode(), concatenatedString.hashCode()); |
} |
- /** |
- * Test the Rope implementation can deal with Empty nodes, even though we |
- * guard against them. See also {@link LiteralByteStringTest#testConcat_empty()}. |
- */ |
- public void testConcat_empty() { |
- byte[] referenceBytes = getTestBytes(7748, 113344L); |
- ByteString literalString = ByteString.copyFrom(referenceBytes); |
- |
- ByteString duo = RopeByteString.newInstanceForTest(literalString, literalString); |
- ByteString temp = RopeByteString.newInstanceForTest( |
- RopeByteString.newInstanceForTest(literalString, ByteString.EMPTY), |
- RopeByteString.newInstanceForTest(ByteString.EMPTY, literalString)); |
- ByteString quintet = RopeByteString.newInstanceForTest(temp, ByteString.EMPTY); |
- |
- assertTrue("String with concatenated nulls must equal simple concatenate", |
- duo.equals(quintet)); |
- assertEquals("String with concatenated nulls have same hashcode as simple concatenate", |
- duo.hashCode(), quintet.hashCode()); |
- |
- ByteString.ByteIterator duoIter = duo.iterator(); |
- ByteString.ByteIterator quintetIter = quintet.iterator(); |
- boolean stillEqual = true; |
- while (stillEqual && quintetIter.hasNext()) { |
- stillEqual = (duoIter.nextByte() == quintetIter.nextByte()); |
- } |
- assertTrue("We must get the same characters by iterating", stillEqual); |
- assertFalse("Iterator must be exhausted", duoIter.hasNext()); |
- try { |
- duoIter.nextByte(); |
- fail("Should have thrown an exception."); |
- } catch (NoSuchElementException e) { |
- // This is success |
- } |
- try { |
- quintetIter.nextByte(); |
- fail("Should have thrown an exception."); |
- } catch (NoSuchElementException e) { |
- // This is success |
- } |
- |
- // Test that even if we force empty strings in as rope leaves in this |
- // configuration, we always get a (possibly Bounded) LiteralByteString |
- // for a length 1 substring. |
- // |
- // It is possible, using the testing factory method to create deeply nested |
- // trees of empty leaves, to make a string that will fail this test. |
- for (int i = 1; i < duo.size(); ++i) { |
- assertTrue("Substrings of size() < 2 must not be RopeByteStrings", |
- duo.substring(i - 1, i) instanceof ByteString.LeafByteString); |
- } |
- for (int i = 1; i < quintet.size(); ++i) { |
- assertTrue("Substrings of size() < 2 must not be RopeByteStrings", |
- quintet.substring(i - 1, i) instanceof ByteString.LeafByteString); |
- } |
- } |
- |
public void testStartsWith() { |
byte[] bytes = getTestBytes(1000, 1234L); |
ByteString string = ByteString.copyFrom(bytes); |
@@ -679,21 +574,6 @@ public class ByteStringTest extends TestCase { |
assertTrue(ByteString.EMPTY.startsWith(ByteString.EMPTY)); |
} |
- public void testEndsWith() { |
- byte[] bytes = getTestBytes(1000, 1234L); |
- ByteString string = ByteString.copyFrom(bytes); |
- ByteString prefix = ByteString.copyFrom(bytes, 0, 500); |
- ByteString suffix = ByteString.copyFrom(bytes, 400, 600); |
- assertTrue(string.endsWith(ByteString.EMPTY)); |
- assertTrue(string.endsWith(string)); |
- assertTrue(string.endsWith(suffix)); |
- assertFalse(string.endsWith(prefix)); |
- assertFalse(suffix.endsWith(prefix)); |
- assertFalse(prefix.endsWith(suffix)); |
- assertFalse(ByteString.EMPTY.endsWith(suffix)); |
- assertTrue(ByteString.EMPTY.endsWith(ByteString.EMPTY)); |
- } |
- |
static List<ByteString> makeConcretePieces(byte[] referenceBytes) { |
List<ByteString> pieces = new ArrayList<ByteString>(); |
// Starting length should be small enough that we'll do some concatenating by |
@@ -707,68 +587,4 @@ public class ByteStringTest extends TestCase { |
} |
return pieces; |
} |
- |
- private byte[] substringUsingWriteTo( |
- ByteString data, int offset, int length) throws IOException { |
- ByteArrayOutputStream output = new ByteArrayOutputStream(); |
- data.writeTo(output, offset, length); |
- return output.toByteArray(); |
- } |
- |
- public void testWriteToOutputStream() throws Exception { |
- // Choose a size large enough so when two ByteStrings are concatenated they |
- // won't be merged into one byte array due to some optimizations. |
- final int dataSize = ByteString.CONCATENATE_BY_COPY_SIZE + 1; |
- byte[] data1 = new byte[dataSize]; |
- for (int i = 0; i < data1.length; i++) { |
- data1[i] = (byte) 1; |
- } |
- data1[1] = (byte) 11; |
- // Test LiteralByteString.writeTo(OutputStream,int,int) |
- ByteString left = ByteString.wrap(data1); |
- byte[] result = substringUsingWriteTo(left, 1, 1); |
- assertEquals(1, result.length); |
- assertEquals((byte) 11, result[0]); |
- |
- byte[] data2 = new byte[dataSize]; |
- for (int i = 0; i < data1.length; i++) { |
- data2[i] = (byte) 2; |
- } |
- ByteString right = ByteString.wrap(data2); |
- // Concatenate two ByteStrings to create a RopeByteString. |
- ByteString root = left.concat(right); |
- // Make sure we are actually testing a RopeByteString with a simple tree |
- // structure. |
- assertEquals(1, root.getTreeDepth()); |
- // Write parts of the left node. |
- result = substringUsingWriteTo(root, 0, dataSize); |
- assertEquals(dataSize, result.length); |
- assertEquals((byte) 1, result[0]); |
- assertEquals((byte) 1, result[dataSize - 1]); |
- // Write parts of the right node. |
- result = substringUsingWriteTo(root, dataSize, dataSize); |
- assertEquals(dataSize, result.length); |
- assertEquals((byte) 2, result[0]); |
- assertEquals((byte) 2, result[dataSize - 1]); |
- // Write a segment of bytes that runs across both nodes. |
- result = substringUsingWriteTo(root, dataSize / 2, dataSize); |
- assertEquals(dataSize, result.length); |
- assertEquals((byte) 1, result[0]); |
- assertEquals((byte) 1, result[dataSize - dataSize / 2 - 1]); |
- assertEquals((byte) 2, result[dataSize - dataSize / 2]); |
- assertEquals((byte) 2, result[dataSize - 1]); |
- } |
- |
- /** |
- * Tests ByteString uses Arrays based byte copier when running under Hotstop VM. |
- */ |
- public void testByteArrayCopier() throws Exception { |
- Field field = ByteString.class.getDeclaredField("byteArrayCopier"); |
- field.setAccessible(true); |
- Object byteArrayCopier = field.get(null); |
- assertNotNull(byteArrayCopier); |
- assertTrue( |
- byteArrayCopier.toString(), |
- byteArrayCopier.getClass().getSimpleName().endsWith("ArraysByteArrayCopier")); |
- } |
} |