aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/test/java/com/google/protobuf/NanoTest.java
diff options
context:
space:
mode:
authorCharles Munger <clm@google.com>2014-12-28 17:49:06 -0800
committerCharles Munger <clm@google.com>2015-01-07 22:23:54 +0000
commitbcf7a816ad9d435cd4562bdc722fd01100a918d7 (patch)
treee35f9e9302239c7c1add696a73130251a315381b /java/src/test/java/com/google/protobuf/NanoTest.java
parent41be9083a4cef078c9a2e209ac99d065b5e71ad0 (diff)
downloadexternal_protobuf-bcf7a816ad9d435cd4562bdc722fd01100a918d7.zip
external_protobuf-bcf7a816ad9d435cd4562bdc722fd01100a918d7.tar.gz
external_protobuf-bcf7a816ad9d435cd4562bdc722fd01100a918d7.tar.bz2
Optimize measurement and serialization of nano protos.
Measuring the serialized size of nano protos is now a zero-alloc operation, and serializing a proto now allocates no memory (other than the output buffer) instead of O(total length of strings). Change-Id: Id5e2ac3bdc4ac56c0bf13d725472da3a00c9baec Signed-off-by: Charles Munger <clm@google.com>
Diffstat (limited to 'java/src/test/java/com/google/protobuf/NanoTest.java')
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 6b69aa7..aa555c8 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -2318,6 +2318,42 @@ public class NanoTest extends TestCase {
}
}
+ public void testDifferentStringLengthsNano() throws Exception {
+ // Test string serialization roundtrip using strings of the following lengths,
+ // with ASCII and Unicode characters requiring different UTF-8 byte counts per
+ // char, hence causing the length delimiter varint to sometimes require more
+ // bytes for the Unicode strings than the ASCII string of the same length.
+ int[] lengths = new int[] {
+ 0,
+ 1,
+ (1 << 4) - 1, // 1 byte for ASCII and Unicode
+ (1 << 7) - 1, // 1 byte for ASCII, 2 bytes for Unicode
+ (1 << 11) - 1, // 2 bytes for ASCII and Unicode
+ (1 << 14) - 1, // 2 bytes for ASCII, 3 bytes for Unicode
+ (1 << 17) - 1, // 3 bytes for ASCII and Unicode
+ };
+ for (int i : lengths) {
+ testEncodingOfString('q', i); // 1 byte per char
+ testEncodingOfString('\u07FF', i); // 2 bytes per char
+ testEncodingOfString('\u0981', i); // 3 bytes per char
+ }
+ }
+
+ private void testEncodingOfString(char c, int length) throws InvalidProtocolBufferNanoException {
+ TestAllTypesNano testAllTypesNano = new TestAllTypesNano();
+ final String fullString = fullString(c, length);
+ testAllTypesNano.optionalString = fullString;
+ final TestAllTypesNano resultNano = new TestAllTypesNano();
+ MessageNano.mergeFrom(resultNano, MessageNano.toByteArray(testAllTypesNano));
+ assertEquals(fullString, resultNano.optionalString);
+ }
+
+ private String fullString(char c, int length) {
+ char[] result = new char[length];
+ Arrays.fill(result, c);
+ return new String(result);
+ }
+
public void testNanoWithHasParseFrom() throws Exception {
TestAllTypesNanoHas msg = null;
// Test false on creation, after clear and upon empty parse.