NanoTest.java revision 1ee2760aed05bf072a05fd7b6aeb158691a5dfbc
1// Protocol Buffers - Google's data interchange format
2// Copyright 2013 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31package com.google.protobuf;
32
33import com.google.protobuf.nano.CodedInputByteBufferNano;
34import com.google.protobuf.nano.EnumClassNanoMultiple;
35import com.google.protobuf.nano.EnumClassNanos;
36import com.google.protobuf.nano.EnumValidity;
37import com.google.protobuf.nano.EnumValidityAccessors;
38import com.google.protobuf.nano.Extensions;
39import com.google.protobuf.nano.Extensions.AnotherMessage;
40import com.google.protobuf.nano.Extensions.MessageWithGroup;
41import com.google.protobuf.nano.FileScopeEnumMultiple;
42import com.google.protobuf.nano.FileScopeEnumRefNano;
43import com.google.protobuf.nano.InternalNano;
44import com.google.protobuf.nano.MessageNano;
45import com.google.protobuf.nano.MessageScopeEnumRefNano;
46import com.google.protobuf.nano.MultipleImportingNonMultipleNano1;
47import com.google.protobuf.nano.MultipleImportingNonMultipleNano2;
48import com.google.protobuf.nano.MultipleNameClashNano;
49import com.google.protobuf.nano.NanoAccessorsOuterClass.TestNanoAccessors;
50import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas;
51import com.google.protobuf.nano.NanoOuterClass;
52import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano;
53import com.google.protobuf.nano.NanoReferenceTypes;
54import com.google.protobuf.nano.NanoRepeatedPackables;
55import com.google.protobuf.nano.PackedExtensions;
56import com.google.protobuf.nano.RepeatedExtensions;
57import com.google.protobuf.nano.SingularExtensions;
58import com.google.protobuf.nano.TestRepeatedMergeNano;
59import com.google.protobuf.nano.UnittestImportNano;
60import com.google.protobuf.nano.UnittestMultipleNano;
61import com.google.protobuf.nano.UnittestRecursiveNano.RecursiveMessageNano;
62import com.google.protobuf.nano.UnittestSimpleNano.SimpleMessageNano;
63import com.google.protobuf.nano.UnittestSingleNano.SingleMessageNano;
64
65import junit.framework.TestCase;
66
67import java.util.Arrays;
68import java.util.HashMap;
69
70/**
71 * Test nano runtime.
72 *
73 * @author ulas@google.com Ulas Kirazci
74 */
75public class NanoTest extends TestCase {
76  @Override
77  public void setUp() throws Exception {
78  }
79
80  public void testSimpleMessageNano() throws Exception {
81    SimpleMessageNano msg = new SimpleMessageNano();
82    assertEquals(123, msg.d);
83    assertEquals(null, msg.nestedMsg);
84    assertEquals(SimpleMessageNano.BAZ, msg.defaultNestedEnum);
85
86    msg.d = 456;
87    assertEquals(456, msg.d);
88
89    SimpleMessageNano.NestedMessage nestedMsg = new SimpleMessageNano.NestedMessage();
90    nestedMsg.bb = 2;
91    assertEquals(2, nestedMsg.bb);
92    msg.nestedMsg = nestedMsg;
93    assertEquals(2, msg.nestedMsg.bb);
94
95    msg.defaultNestedEnum = SimpleMessageNano.BAR;
96    assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
97
98    byte [] result = MessageNano.toByteArray(msg);
99    int msgSerializedSize = msg.getSerializedSize();
100    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
101    assertTrue(msgSerializedSize == 9);
102    assertEquals(result.length, msgSerializedSize);
103
104    SimpleMessageNano newMsg = SimpleMessageNano.parseFrom(result);
105    assertEquals(456, newMsg.d);
106    assertEquals(2, msg.nestedMsg.bb);
107    assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
108
109    msg.nestedMsg = null;
110    assertTrue(msgSerializedSize != msg.getSerializedSize());
111
112    msg.clear();
113    assertEquals(0, msg.getSerializedSize());
114  }
115
116  public void testRecursiveMessageNano() throws Exception {
117    RecursiveMessageNano msg = new RecursiveMessageNano();
118    assertTrue(msg.repeatedRecursiveMessageNano.length == 0);
119
120    RecursiveMessageNano msg1 = new RecursiveMessageNano();
121    msg1.id = 1;
122    assertEquals(1, msg1.id);
123    RecursiveMessageNano msg2 = new RecursiveMessageNano();
124    msg2.id = 2;
125    RecursiveMessageNano msg3 = new RecursiveMessageNano();
126    msg3.id = 3;
127
128    RecursiveMessageNano.NestedMessage nestedMsg = new RecursiveMessageNano.NestedMessage();
129    nestedMsg.a = msg1;
130    assertEquals(1, nestedMsg.a.id);
131
132    msg.id = 0;
133    msg.nestedMessage = nestedMsg;
134    msg.optionalRecursiveMessageNano = msg2;
135    msg.repeatedRecursiveMessageNano = new RecursiveMessageNano[] { msg3 };
136
137    byte [] result = MessageNano.toByteArray(msg);
138    int msgSerializedSize = msg.getSerializedSize();
139    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
140    assertTrue(msgSerializedSize == 16);
141    assertEquals(result.length, msgSerializedSize);
142
143    RecursiveMessageNano newMsg = RecursiveMessageNano.parseFrom(result);
144    assertEquals(1, newMsg.repeatedRecursiveMessageNano.length);
145
146    assertEquals(0, newMsg.id);
147    assertEquals(1, newMsg.nestedMessage.a.id);
148    assertEquals(2, newMsg.optionalRecursiveMessageNano.id);
149    assertEquals(3, newMsg.repeatedRecursiveMessageNano[0].id);
150  }
151
152  public void testMessageNoFields() {
153    SingleMessageNano msg = new SingleMessageNano();
154    assertEquals(0, msg.getSerializedSize());
155    assertEquals(0, MessageNano.toByteArray(msg).length);
156  }
157
158  public void testNanoRequiredInt32() throws Exception {
159    TestAllTypesNano msg = new TestAllTypesNano();
160    msg.id = 123;
161    assertEquals(123, msg.id);
162    msg.clear().id = 456;
163    assertEquals(456, msg.id);
164    msg.clear();
165
166    msg.id = 123;
167    byte [] result = MessageNano.toByteArray(msg);
168    int msgSerializedSize = msg.getSerializedSize();
169    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
170    assertTrue(msgSerializedSize == 3);
171    assertEquals(result.length, msgSerializedSize);
172
173    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
174    assertEquals(123, newMsg.id);
175  }
176
177  public void testNanoOptionalInt32() throws Exception {
178    TestAllTypesNano msg = new TestAllTypesNano();
179    msg.optionalInt32 = 123;
180    assertEquals(123, msg.optionalInt32);
181    msg.clear()
182       .optionalInt32 = 456;
183    assertEquals(456, msg.optionalInt32);
184    msg.clear();
185
186    msg.optionalInt32 = 123;
187    byte [] result = MessageNano.toByteArray(msg);
188    int msgSerializedSize = msg.getSerializedSize();
189    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
190    assertTrue(msgSerializedSize == 5);
191    assertEquals(result.length, msgSerializedSize);
192
193    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
194    assertEquals(123, newMsg.optionalInt32);
195  }
196
197  public void testNanoOptionalInt64() throws Exception {
198    TestAllTypesNano msg = new TestAllTypesNano();
199    msg.optionalInt64 = 123;
200    assertEquals(123, msg.optionalInt64);
201    msg.clear()
202       .optionalInt64 = 456;
203    assertEquals(456, msg.optionalInt64);
204    msg.clear();
205    assertEquals(0, msg.optionalInt64);
206
207    msg.optionalInt64 = 123;
208    byte [] result = MessageNano.toByteArray(msg);
209    int msgSerializedSize = msg.getSerializedSize();
210    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
211    assertTrue(msgSerializedSize == 5);
212    assertEquals(result.length, msgSerializedSize);
213
214    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
215    assertEquals(123, newMsg.optionalInt64);
216  }
217
218  public void testNanoOptionalUint32() throws Exception {
219    TestAllTypesNano msg = new TestAllTypesNano();
220    msg.optionalUint32 = 123;
221    assertEquals(123, msg.optionalUint32);
222    msg.clear()
223       .optionalUint32 = 456;
224    assertEquals(456, msg.optionalUint32);
225    msg.clear();
226    assertEquals(0, msg.optionalUint32);
227
228    msg.optionalUint32 = 123;
229    byte [] result = MessageNano.toByteArray(msg);
230    int msgSerializedSize = msg.getSerializedSize();
231    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
232    assertTrue(msgSerializedSize == 5);
233    assertEquals(result.length, msgSerializedSize);
234
235    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
236    assertEquals(123, newMsg.optionalUint32);
237  }
238
239  public void testNanoOptionalUint64() throws Exception {
240    TestAllTypesNano msg = new TestAllTypesNano();
241    msg.optionalUint64 = 123;
242    assertEquals(123, msg.optionalUint64);
243    msg.clear()
244       .optionalUint64 = 456;
245    assertEquals(456, msg.optionalUint64);
246    msg.clear();
247    assertEquals(0, msg.optionalUint64);
248
249    msg.optionalUint64 = 123;
250    byte [] result = MessageNano.toByteArray(msg);
251    int msgSerializedSize = msg.getSerializedSize();
252    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
253    assertTrue(msgSerializedSize == 5);
254    assertEquals(result.length, msgSerializedSize);
255
256    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
257    assertEquals(123, newMsg.optionalUint64);
258  }
259
260  public void testNanoOptionalSint32() throws Exception {
261    TestAllTypesNano msg = new TestAllTypesNano();
262    msg.optionalSint32 = 123;
263    assertEquals(123, msg.optionalSint32);
264    msg.clear()
265       .optionalSint32 = 456;
266    assertEquals(456, msg.optionalSint32);
267    msg.clear();
268    assertEquals(0, msg.optionalSint32);
269
270    msg.optionalSint32 = -123;
271    byte [] result = MessageNano.toByteArray(msg);
272    int msgSerializedSize = msg.getSerializedSize();
273    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
274    assertTrue(msgSerializedSize == 6);
275    assertEquals(result.length, msgSerializedSize);
276
277    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
278    assertEquals(-123, newMsg.optionalSint32);
279  }
280
281  public void testNanoOptionalSint64() throws Exception {
282    TestAllTypesNano msg = new TestAllTypesNano();
283    msg.optionalSint64 = 123;
284    assertEquals(123, msg.optionalSint64);
285    msg.clear()
286       .optionalSint64 = 456;
287    assertEquals(456, msg.optionalSint64);
288    msg.clear();
289    assertEquals(0, msg.optionalSint64);
290
291    msg.optionalSint64 = -123;
292    byte [] result = MessageNano.toByteArray(msg);
293    int msgSerializedSize = msg.getSerializedSize();
294    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
295    assertTrue(msgSerializedSize == 6);
296    assertEquals(result.length, msgSerializedSize);
297
298    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
299    assertEquals(-123, newMsg.optionalSint64);
300  }
301
302  public void testNanoOptionalFixed32() throws Exception {
303    TestAllTypesNano msg = new TestAllTypesNano();
304    msg.optionalFixed32 = 123;
305    assertEquals(123, msg.optionalFixed32);
306    msg.clear()
307       .optionalFixed32 = 456;
308    assertEquals(456, msg.optionalFixed32);
309    msg.clear();
310    assertEquals(0, msg.optionalFixed32);
311
312    msg.optionalFixed32 = 123;
313    byte [] result = MessageNano.toByteArray(msg);
314    int msgSerializedSize = msg.getSerializedSize();
315    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
316    assertTrue(msgSerializedSize == 8);
317    assertEquals(result.length, msgSerializedSize);
318
319    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
320    assertEquals(123, newMsg.optionalFixed32);
321  }
322
323  public void testNanoOptionalFixed64() throws Exception {
324    TestAllTypesNano msg = new TestAllTypesNano();
325    msg.optionalFixed64 = 123;
326    assertEquals(123, msg.optionalFixed64);
327    msg.clear()
328       .optionalFixed64 = 456;
329    assertEquals(456, msg.optionalFixed64);
330    msg.clear();
331    assertEquals(0, msg.optionalFixed64);
332
333    msg.optionalFixed64 = 123;
334    byte [] result = MessageNano.toByteArray(msg);
335    int msgSerializedSize = msg.getSerializedSize();
336    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
337    assertTrue(msgSerializedSize == 12);
338    assertEquals(result.length, msgSerializedSize);
339
340    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
341    assertEquals(123, newMsg.optionalFixed64);
342  }
343
344  public void testNanoOptionalSfixed32() throws Exception {
345    TestAllTypesNano msg = new TestAllTypesNano();
346    msg.optionalSfixed32 = 123;
347    assertEquals(123, msg.optionalSfixed32);
348    msg.clear()
349       .optionalSfixed32 = 456;
350    assertEquals(456, msg.optionalSfixed32);
351    msg.clear();
352    assertEquals(0, msg.optionalSfixed32);
353
354    msg.optionalSfixed32 = 123;
355    byte [] result = MessageNano.toByteArray(msg);
356    int msgSerializedSize = msg.getSerializedSize();
357    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
358    assertTrue(msgSerializedSize == 8);
359    assertEquals(result.length, msgSerializedSize);
360
361    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
362    assertEquals(123, newMsg.optionalSfixed32);
363  }
364
365  public void testNanoOptionalSfixed64() throws Exception {
366    TestAllTypesNano msg = new TestAllTypesNano();
367    msg.optionalSfixed64 = 123;
368    assertEquals(123, msg.optionalSfixed64);
369    msg.clear()
370       .optionalSfixed64 = 456;
371    assertEquals(456, msg.optionalSfixed64);
372    msg.clear();
373    assertEquals(0, msg.optionalSfixed64);
374
375    msg.optionalSfixed64 = -123;
376    byte [] result = MessageNano.toByteArray(msg);
377    int msgSerializedSize = msg.getSerializedSize();
378    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
379    assertTrue(msgSerializedSize == 12);
380    assertEquals(result.length, msgSerializedSize);
381
382    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
383    assertEquals(-123, newMsg.optionalSfixed64);
384  }
385
386  public void testNanoOptionalFloat() throws Exception {
387    TestAllTypesNano msg = new TestAllTypesNano();
388    msg.optionalFloat = 123f;
389    assertTrue(123.0f == msg.optionalFloat);
390    msg.clear()
391       .optionalFloat = 456.0f;
392    assertTrue(456.0f == msg.optionalFloat);
393    msg.clear();
394    assertTrue(0.0f == msg.optionalFloat);
395
396    msg.optionalFloat = -123.456f;
397    byte [] result = MessageNano.toByteArray(msg);
398    int msgSerializedSize = msg.getSerializedSize();
399    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
400    assertTrue(msgSerializedSize == 8);
401    assertEquals(result.length, msgSerializedSize);
402
403    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
404    assertTrue(-123.456f == newMsg.optionalFloat);
405  }
406
407  public void testNanoOptionalDouble() throws Exception {
408    TestAllTypesNano msg = new TestAllTypesNano();
409    msg.optionalDouble = 123;
410    assertTrue(123.0 == msg.optionalDouble);
411    msg.clear()
412       .optionalDouble = 456.0;
413    assertTrue(456.0 == msg.optionalDouble);
414    msg.clear();
415    assertTrue(0.0 == msg.optionalDouble);
416
417    msg.optionalDouble = -123.456;
418    byte [] result = MessageNano.toByteArray(msg);
419    int msgSerializedSize = msg.getSerializedSize();
420    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
421    assertTrue(msgSerializedSize == 12);
422    assertEquals(result.length, msgSerializedSize);
423
424    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
425    assertTrue(-123.456 == newMsg.optionalDouble);
426  }
427
428  public void testNanoOptionalBool() throws Exception {
429    TestAllTypesNano msg = new TestAllTypesNano();
430    msg.optionalBool = true;
431    assertTrue(msg.optionalBool);
432    msg.clear()
433       .optionalBool = true;
434    assertTrue(msg.optionalBool);
435    msg.clear();
436    assertFalse(msg.optionalBool);
437
438    msg.optionalBool = true;
439    byte [] result = MessageNano.toByteArray(msg);
440    int msgSerializedSize = msg.getSerializedSize();
441    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
442    assertTrue(msgSerializedSize == 5);
443    assertEquals(result.length, msgSerializedSize);
444
445    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
446    assertTrue(newMsg.optionalBool);
447  }
448
449  public void testNanoOptionalString() throws Exception {
450    TestAllTypesNano msg = new TestAllTypesNano();
451    msg.optionalString = "hello";
452    assertEquals("hello", msg.optionalString);
453    msg.clear();
454    assertTrue(msg.optionalString.isEmpty());
455    msg.clear()
456       .optionalString = "hello2";
457    assertEquals("hello2", msg.optionalString);
458    msg.clear();
459    assertTrue(msg.optionalString.isEmpty());
460
461    msg.optionalString = "bye";
462    byte [] result = MessageNano.toByteArray(msg);
463    int msgSerializedSize = msg.getSerializedSize();
464    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
465    assertTrue(msgSerializedSize == 8);
466    assertEquals(result.length, msgSerializedSize);
467
468    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
469    assertTrue(newMsg.optionalString != null);
470    assertEquals("bye", newMsg.optionalString);
471  }
472
473  public void testNanoOptionalBytes() throws Exception {
474    TestAllTypesNano msg = new TestAllTypesNano();
475    assertFalse(msg.optionalBytes.length > 0);
476    msg.optionalBytes = InternalNano.copyFromUtf8("hello");
477    assertTrue(msg.optionalBytes.length > 0);
478    assertEquals("hello", new String(msg.optionalBytes, "UTF-8"));
479    msg.clear();
480    assertFalse(msg.optionalBytes.length > 0);
481    msg.clear()
482       .optionalBytes = InternalNano.copyFromUtf8("hello");
483    assertTrue(msg.optionalBytes.length > 0);
484    msg.clear();
485    assertFalse(msg.optionalBytes.length > 0);
486
487    msg.optionalBytes = InternalNano.copyFromUtf8("bye");
488    byte [] result = MessageNano.toByteArray(msg);
489    int msgSerializedSize = msg.getSerializedSize();
490    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
491    assertTrue(msgSerializedSize == 8);
492    assertEquals(result.length, msgSerializedSize);
493
494    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
495    assertTrue(newMsg.optionalBytes.length > 0);
496    assertEquals("bye", new String(newMsg.optionalBytes, "UTF-8"));
497  }
498
499  public void testNanoOptionalGroup() throws Exception {
500    TestAllTypesNano msg = new TestAllTypesNano();
501    TestAllTypesNano.OptionalGroup grp = new TestAllTypesNano.OptionalGroup();
502    grp.a = 1;
503    assertFalse(msg.optionalGroup != null);
504    msg.optionalGroup = grp;
505    assertTrue(msg.optionalGroup != null);
506    assertEquals(1, msg.optionalGroup.a);
507    msg.clear();
508    assertFalse(msg.optionalGroup != null);
509    msg.clear()
510       .optionalGroup = new TestAllTypesNano.OptionalGroup();
511    msg.optionalGroup.a = 2;
512    assertTrue(msg.optionalGroup != null);
513    msg.clear();
514    assertFalse(msg.optionalGroup != null);
515
516    msg.optionalGroup = grp;
517    byte [] result = MessageNano.toByteArray(msg);
518    int msgSerializedSize = msg.getSerializedSize();
519    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
520    assertTrue(msgSerializedSize == 10);
521    assertEquals(result.length, msgSerializedSize);
522
523    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
524    assertTrue(newMsg.optionalGroup != null);
525    assertEquals(1, newMsg.optionalGroup.a);
526  }
527
528  public void testNanoOptionalGroupWithUnknownFieldsEnabled() throws Exception {
529    MessageWithGroup msg = new MessageWithGroup();
530    MessageWithGroup.Group grp = new MessageWithGroup.Group();
531    grp.a = 1;
532    msg.group = grp;
533    byte [] serialized = MessageNano.toByteArray(msg);
534
535    MessageWithGroup parsed = MessageWithGroup.parseFrom(serialized);
536    assertEquals(1, parsed.group.a);
537
538    byte [] serialized2 = MessageNano.toByteArray(parsed);
539    assertEquals(serialized.length, serialized2.length);
540    MessageWithGroup parsed2 = MessageWithGroup.parseFrom(serialized2);
541    assertEquals(1, parsed2.group.a);
542  }
543
544  public void testNanoOptionalNestedMessage() throws Exception {
545    TestAllTypesNano msg = new TestAllTypesNano();
546    TestAllTypesNano.NestedMessage nestedMsg = new TestAllTypesNano.NestedMessage();
547    nestedMsg.bb = 1;
548    assertFalse(msg.optionalNestedMessage != null);
549    msg.optionalNestedMessage = nestedMsg;
550    assertTrue(msg.optionalNestedMessage != null);
551    assertEquals(1, msg.optionalNestedMessage.bb);
552    msg.clear();
553    assertFalse(msg.optionalNestedMessage != null);
554    msg.clear()
555       .optionalNestedMessage = new TestAllTypesNano.NestedMessage();
556    msg.optionalNestedMessage.bb = 2;
557    assertTrue(msg.optionalNestedMessage != null);
558    msg.clear();
559    assertFalse(msg.optionalNestedMessage != null);
560
561    msg.optionalNestedMessage = nestedMsg;
562    byte [] result = MessageNano.toByteArray(msg);
563    int msgSerializedSize = msg.getSerializedSize();
564    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
565    assertTrue(msgSerializedSize == 8);
566    assertEquals(result.length, msgSerializedSize);
567
568    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
569    assertTrue(newMsg.optionalNestedMessage != null);
570    assertEquals(1, newMsg.optionalNestedMessage.bb);
571  }
572
573  public void testNanoOptionalForeignMessage() throws Exception {
574    TestAllTypesNano msg = new TestAllTypesNano();
575    NanoOuterClass.ForeignMessageNano nestedMsg = new NanoOuterClass.ForeignMessageNano();
576    nestedMsg.c = 1;
577    assertFalse(msg.optionalForeignMessage != null);
578    msg.optionalForeignMessage = nestedMsg;
579    assertTrue(msg.optionalForeignMessage != null);
580    assertEquals(1, msg.optionalForeignMessage.c);
581    msg.clear();
582    assertFalse(msg.optionalForeignMessage != null);
583    msg.clear()
584       .optionalForeignMessage = new NanoOuterClass.ForeignMessageNano();
585    msg.optionalForeignMessage.c = 2;
586    assertTrue(msg.optionalForeignMessage != null);
587    msg.clear();
588    assertFalse(msg.optionalForeignMessage != null);
589
590    msg.optionalForeignMessage = nestedMsg;
591    byte [] result = MessageNano.toByteArray(msg);
592    int msgSerializedSize = msg.getSerializedSize();
593    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
594    assertTrue(msgSerializedSize == 8);
595    assertEquals(result.length, msgSerializedSize);
596
597    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
598    assertTrue(newMsg.optionalForeignMessage != null);
599    assertEquals(1, newMsg.optionalForeignMessage.c);
600  }
601
602  public void testNanoOptionalImportMessage() throws Exception {
603    TestAllTypesNano msg = new TestAllTypesNano();
604    UnittestImportNano.ImportMessageNano nestedMsg = new UnittestImportNano.ImportMessageNano();
605    nestedMsg.d = 1;
606    assertFalse(msg.optionalImportMessage != null);
607    msg.optionalImportMessage = nestedMsg;
608    assertTrue(msg.optionalImportMessage != null);
609    assertEquals(1, msg.optionalImportMessage.d);
610    msg.clear();
611    assertFalse(msg.optionalImportMessage != null);
612    msg.clear()
613       .optionalImportMessage = new UnittestImportNano.ImportMessageNano();
614    msg.optionalImportMessage.d = 2;
615    assertTrue(msg.optionalImportMessage != null);
616    msg.clear();
617    assertFalse(msg.optionalImportMessage != null);
618
619    msg.optionalImportMessage = nestedMsg;
620    byte [] result = MessageNano.toByteArray(msg);
621    int msgSerializedSize = msg.getSerializedSize();
622    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
623    assertTrue(msgSerializedSize == 8);
624    assertEquals(result.length, msgSerializedSize);
625
626    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
627    assertTrue(newMsg.optionalImportMessage != null);
628    assertEquals(1, newMsg.optionalImportMessage.d);
629  }
630
631  public void testNanoOptionalNestedEnum() throws Exception {
632    TestAllTypesNano msg = new TestAllTypesNano();
633    msg.optionalNestedEnum = TestAllTypesNano.BAR;
634    assertEquals(TestAllTypesNano.BAR, msg.optionalNestedEnum);
635    msg.clear()
636       .optionalNestedEnum = TestAllTypesNano.BAZ;
637    assertEquals(TestAllTypesNano.BAZ, msg.optionalNestedEnum);
638    msg.clear();
639    assertEquals(TestAllTypesNano.FOO, msg.optionalNestedEnum);
640
641    msg.optionalNestedEnum = TestAllTypesNano.BAR;
642    byte [] result = MessageNano.toByteArray(msg);
643    int msgSerializedSize = msg.getSerializedSize();
644    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
645    assertTrue(msgSerializedSize == 6);
646    assertEquals(result.length, msgSerializedSize);
647
648    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
649    assertEquals(TestAllTypesNano.BAR, newMsg.optionalNestedEnum);
650  }
651
652  public void testNanoOptionalForeignEnum() throws Exception {
653    TestAllTypesNano msg = new TestAllTypesNano();
654    msg.optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAR;
655    assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.optionalForeignEnum);
656    msg.clear()
657       .optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAZ;
658    assertEquals(NanoOuterClass.FOREIGN_NANO_BAZ, msg.optionalForeignEnum);
659    msg.clear();
660    assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.optionalForeignEnum);
661
662    msg.optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAR;
663    byte [] result = MessageNano.toByteArray(msg);
664    int msgSerializedSize = msg.getSerializedSize();
665    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
666    assertTrue(msgSerializedSize == 6);
667    assertEquals(result.length, msgSerializedSize);
668
669    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
670    assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, newMsg.optionalForeignEnum);
671  }
672
673  public void testNanoOptionalImportEnum() throws Exception {
674    TestAllTypesNano msg = new TestAllTypesNano();
675    msg.optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAR;
676    assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.optionalImportEnum);
677    msg.clear()
678       .optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAZ;
679    assertEquals(UnittestImportNano.IMPORT_NANO_BAZ, msg.optionalImportEnum);
680    msg.clear();
681    assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.optionalImportEnum);
682
683    msg.optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAR;
684    byte [] result = MessageNano.toByteArray(msg);
685    int msgSerializedSize = msg.getSerializedSize();
686    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
687    assertTrue(msgSerializedSize == 6);
688    assertEquals(result.length, msgSerializedSize);
689
690    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
691    assertEquals(UnittestImportNano.IMPORT_NANO_BAR, newMsg.optionalImportEnum);
692  }
693
694  public void testNanoOptionalStringPiece() throws Exception {
695    TestAllTypesNano msg = new TestAllTypesNano();
696    msg.optionalStringPiece = "hello";
697    assertEquals("hello", msg.optionalStringPiece);
698    msg.clear();
699    assertTrue(msg.optionalStringPiece.isEmpty());
700    msg.clear()
701       .optionalStringPiece = "hello2";
702    assertEquals("hello2", msg.optionalStringPiece);
703    msg.clear();
704    assertTrue(msg.optionalStringPiece.isEmpty());
705
706    msg.optionalStringPiece = "bye";
707    byte [] result = MessageNano.toByteArray(msg);
708    int msgSerializedSize = msg.getSerializedSize();
709    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
710    assertTrue(msgSerializedSize == 9);
711    assertEquals(result.length, msgSerializedSize);
712
713    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
714    assertTrue(newMsg.optionalStringPiece != null);
715    assertEquals("bye", newMsg.optionalStringPiece);
716  }
717
718  public void testNanoOptionalCord() throws Exception {
719    TestAllTypesNano msg = new TestAllTypesNano();
720    msg.optionalCord = "hello";
721    assertEquals("hello", msg.optionalCord);
722    msg.clear();
723    assertTrue(msg.optionalCord.isEmpty());
724    msg.clear()
725       .optionalCord = "hello2";
726    assertEquals("hello2", msg.optionalCord);
727    msg.clear();
728    assertTrue(msg.optionalCord.isEmpty());
729
730    msg.optionalCord = "bye";
731    byte [] result = MessageNano.toByteArray(msg);
732    int msgSerializedSize = msg.getSerializedSize();
733    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
734    assertTrue(msgSerializedSize == 9);
735    assertEquals(result.length, msgSerializedSize);
736
737    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
738    assertTrue(newMsg.optionalCord != null);
739    assertEquals("bye", newMsg.optionalCord);
740  }
741
742  public void testNanoRepeatedInt32() throws Exception {
743    TestAllTypesNano msg = new TestAllTypesNano();
744    assertEquals(0, msg.repeatedInt32.length);
745    msg.repeatedInt32 = new int[] { 123, 789, 456 };
746    assertEquals(789, msg.repeatedInt32[1]);
747    assertEquals(456, msg.repeatedInt32[2]);
748    msg.clear();
749    assertEquals(0, msg.repeatedInt32.length);
750    msg.clear()
751       .repeatedInt32 = new int[] { 456 };
752    assertEquals(1, msg.repeatedInt32.length);
753    assertEquals(456, msg.repeatedInt32[0]);
754    msg.clear();
755    assertEquals(0, msg.repeatedInt32.length);
756
757    // Test 1 entry
758    msg.clear()
759       .repeatedInt32 = new int[] { 123 };
760    assertEquals(1, msg.repeatedInt32.length);
761    byte [] result = MessageNano.toByteArray(msg);
762    int msgSerializedSize = msg.getSerializedSize();
763    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
764    assertTrue(msgSerializedSize == 6);
765    assertEquals(result.length, msgSerializedSize);
766    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
767    assertEquals(1, newMsg.repeatedInt32.length);
768    assertEquals(123, newMsg.repeatedInt32[0]);
769
770    // Test 2 entries
771    msg.clear()
772       .repeatedInt32 = new int[] { 123, 456 };
773    assertEquals(2, msg.repeatedInt32.length);
774    result = MessageNano.toByteArray(msg);
775    msgSerializedSize = msg.getSerializedSize();
776    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
777    assertTrue(msgSerializedSize == 10);
778    assertEquals(result.length, msgSerializedSize);
779
780    newMsg = TestAllTypesNano.parseFrom(result);
781    assertEquals(2, newMsg.repeatedInt32.length);
782    assertEquals(123, newMsg.repeatedInt32[0]);
783    assertEquals(456, newMsg.repeatedInt32[1]);
784  }
785
786  public void testNanoRepeatedInt64() throws Exception {
787    TestAllTypesNano msg = new TestAllTypesNano();
788    assertEquals(0, msg.repeatedInt64.length);
789    msg.repeatedInt64 = new long[] { 123, 789, 456 };
790    assertEquals(789, msg.repeatedInt64[1]);
791    assertEquals(456, msg.repeatedInt64[2]);
792    msg.clear();
793    assertEquals(0, msg.repeatedInt64.length);
794    msg.clear()
795       .repeatedInt64 = new long[] { 456 };
796    assertEquals(1, msg.repeatedInt64.length);
797    assertEquals(456, msg.repeatedInt64[0]);
798    msg.clear();
799    assertEquals(0, msg.repeatedInt64.length);
800
801    // Test 1 entry
802    msg.clear()
803       .repeatedInt64 = new long[] { 123 };
804    assertEquals(1, msg.repeatedInt64.length);
805    byte [] result = MessageNano.toByteArray(msg);
806    int msgSerializedSize = msg.getSerializedSize();
807    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
808    assertTrue(msgSerializedSize == 6);
809    assertEquals(result.length, msgSerializedSize);
810    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
811    assertEquals(1, newMsg.repeatedInt64.length);
812    assertEquals(123, newMsg.repeatedInt64[0]);
813
814    // Test 2 entries
815    msg.clear()
816       .repeatedInt64 = new long[] { 123, 456 };
817    assertEquals(2, msg.repeatedInt64.length);
818    result = MessageNano.toByteArray(msg);
819    msgSerializedSize = msg.getSerializedSize();
820    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
821    assertTrue(msgSerializedSize == 10);
822    assertEquals(result.length, msgSerializedSize);
823
824    newMsg = TestAllTypesNano.parseFrom(result);
825    assertEquals(2, newMsg.repeatedInt64.length);
826    assertEquals(123, newMsg.repeatedInt64[0]);
827    assertEquals(456, newMsg.repeatedInt64[1]);
828  }
829
830  public void testNanoRepeatedUint32() throws Exception {
831    TestAllTypesNano msg = new TestAllTypesNano();
832    assertEquals(0, msg.repeatedUint32.length);
833    msg.repeatedUint32 = new int[] { 123, 789, 456 };
834    assertEquals(789, msg.repeatedUint32[1]);
835    assertEquals(456, msg.repeatedUint32[2]);
836    msg.clear();
837    assertEquals(0, msg.repeatedUint32.length);
838    msg.clear()
839       .repeatedUint32 = new int[] { 456 };
840    assertEquals(1, msg.repeatedUint32.length);
841    assertEquals(456, msg.repeatedUint32[0]);
842    msg.clear();
843    assertEquals(0, msg.repeatedUint32.length);
844
845    // Test 1 entry
846    msg.clear()
847       .repeatedUint32 = new int[] { 123 };
848    assertEquals(1, msg.repeatedUint32.length);
849    byte [] result = MessageNano.toByteArray(msg);
850    int msgSerializedSize = msg.getSerializedSize();
851    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
852    assertTrue(msgSerializedSize == 6);
853    assertEquals(result.length, msgSerializedSize);
854    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
855    assertEquals(1, newMsg.repeatedUint32.length);
856    assertEquals(123, newMsg.repeatedUint32[0]);
857
858    // Test 2 entries
859    msg.clear()
860       .repeatedUint32 = new int[] { 123, 456 };
861    assertEquals(2, msg.repeatedUint32.length);
862    result = MessageNano.toByteArray(msg);
863    msgSerializedSize = msg.getSerializedSize();
864    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
865    assertTrue(msgSerializedSize == 10);
866    assertEquals(result.length, msgSerializedSize);
867
868    newMsg = TestAllTypesNano.parseFrom(result);
869    assertEquals(2, newMsg.repeatedUint32.length);
870    assertEquals(123, newMsg.repeatedUint32[0]);
871    assertEquals(456, newMsg.repeatedUint32[1]);
872  }
873
874  public void testNanoRepeatedUint64() throws Exception {
875    TestAllTypesNano msg = new TestAllTypesNano();
876    assertEquals(0, msg.repeatedUint64.length);
877    msg.repeatedUint64 = new long[] { 123, 789, 456 };
878    assertEquals(789, msg.repeatedUint64[1]);
879    assertEquals(456, msg.repeatedUint64[2]);
880    msg.clear();
881    assertEquals(0, msg.repeatedUint64.length);
882    msg.clear()
883       .repeatedUint64 = new long[] { 456 };
884    assertEquals(1, msg.repeatedUint64.length);
885    assertEquals(456, msg.repeatedUint64[0]);
886    msg.clear();
887    assertEquals(0, msg.repeatedUint64.length);
888
889    // Test 1 entry
890    msg.clear()
891       .repeatedUint64 = new long[] { 123 };
892    assertEquals(1, msg.repeatedUint64.length);
893    byte [] result = MessageNano.toByteArray(msg);
894    int msgSerializedSize = msg.getSerializedSize();
895    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
896    assertTrue(msgSerializedSize == 6);
897    assertEquals(result.length, msgSerializedSize);
898    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
899    assertEquals(1, newMsg.repeatedUint64.length);
900    assertEquals(123, newMsg.repeatedUint64[0]);
901
902    // Test 2 entries
903    msg.clear()
904       .repeatedUint64 = new long[] { 123, 456 };
905    assertEquals(2, msg.repeatedUint64.length);
906    result = MessageNano.toByteArray(msg);
907    msgSerializedSize = msg.getSerializedSize();
908    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
909    assertTrue(msgSerializedSize == 10);
910    assertEquals(result.length, msgSerializedSize);
911
912    newMsg = TestAllTypesNano.parseFrom(result);
913    assertEquals(2, newMsg.repeatedUint64.length);
914    assertEquals(123, newMsg.repeatedUint64[0]);
915    assertEquals(456, newMsg.repeatedUint64[1]);
916  }
917
918  public void testNanoRepeatedSint32() throws Exception {
919    TestAllTypesNano msg = new TestAllTypesNano();
920    assertEquals(0, msg.repeatedSint32.length);
921    msg.repeatedSint32 = new int[] { 123, 789, 456 };
922    assertEquals(789, msg.repeatedSint32[1]);
923    assertEquals(456, msg.repeatedSint32[2]);
924    msg.clear();
925    assertEquals(0, msg.repeatedSint32.length);
926    msg.clear()
927       .repeatedSint32 = new int[] { 456 };
928    assertEquals(1, msg.repeatedSint32.length);
929    assertEquals(456, msg.repeatedSint32[0]);
930    msg.clear();
931    assertEquals(0, msg.repeatedSint32.length);
932
933    // Test 1 entry
934    msg.clear()
935       .repeatedSint32 = new int[] { 123 };
936    assertEquals(1, msg.repeatedSint32.length);
937    byte [] result = MessageNano.toByteArray(msg);
938    int msgSerializedSize = msg.getSerializedSize();
939    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
940    assertTrue(msgSerializedSize == 7);
941    assertEquals(result.length, msgSerializedSize);
942    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
943    assertEquals(1, newMsg.repeatedSint32.length);
944    assertEquals(123, newMsg.repeatedSint32[0]);
945
946    // Test 2 entries
947    msg.clear()
948       .repeatedSint32 = new int[] { 123, 456 };
949    assertEquals(2, msg.repeatedSint32.length);
950    result = MessageNano.toByteArray(msg);
951    msgSerializedSize = msg.getSerializedSize();
952    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
953    assertTrue(msgSerializedSize == 11);
954    assertEquals(result.length, msgSerializedSize);
955
956    newMsg = TestAllTypesNano.parseFrom(result);
957    assertEquals(2, newMsg.repeatedSint32.length);
958    assertEquals(123, newMsg.repeatedSint32[0]);
959    assertEquals(456, newMsg.repeatedSint32[1]);
960  }
961
962  public void testNanoRepeatedSint64() throws Exception {
963    TestAllTypesNano msg = new TestAllTypesNano();
964    assertEquals(0, msg.repeatedSint64.length);
965    msg.repeatedSint64 = new long[] { 123, 789, 456 };
966    assertEquals(789, msg.repeatedSint64[1]);
967    assertEquals(456, msg.repeatedSint64[2]);
968    msg.clear();
969    assertEquals(0, msg.repeatedSint64.length);
970    msg.clear()
971       .repeatedSint64 = new long[] { 456 };
972    assertEquals(1, msg.repeatedSint64.length);
973    assertEquals(456, msg.repeatedSint64[0]);
974    msg.clear();
975    assertEquals(0, msg.repeatedSint64.length);
976
977    // Test 1 entry
978    msg.clear()
979       .repeatedSint64 = new long[] { 123 };
980    assertEquals(1, msg.repeatedSint64.length);
981    byte [] result = MessageNano.toByteArray(msg);
982    int msgSerializedSize = msg.getSerializedSize();
983    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
984    assertTrue(msgSerializedSize == 7);
985    assertEquals(result.length, msgSerializedSize);
986    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
987    assertEquals(1, newMsg.repeatedSint64.length);
988    assertEquals(123, newMsg.repeatedSint64[0]);
989
990    // Test 2 entries
991    msg.clear()
992       .repeatedSint64 = new long[] { 123, 456 };
993    assertEquals(2, msg.repeatedSint64.length);
994    result = MessageNano.toByteArray(msg);
995    msgSerializedSize = msg.getSerializedSize();
996    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
997    assertTrue(msgSerializedSize == 11);
998    assertEquals(result.length, msgSerializedSize);
999
1000    newMsg = TestAllTypesNano.parseFrom(result);
1001    assertEquals(2, newMsg.repeatedSint64.length);
1002    assertEquals(123, newMsg.repeatedSint64[0]);
1003    assertEquals(456, newMsg.repeatedSint64[1]);
1004  }
1005
1006  public void testNanoRepeatedFixed32() throws Exception {
1007    TestAllTypesNano msg = new TestAllTypesNano();
1008    assertEquals(0, msg.repeatedFixed32.length);
1009    msg.repeatedFixed32 = new int[] { 123, 789, 456 };
1010    assertEquals(789, msg.repeatedFixed32[1]);
1011    assertEquals(456, msg.repeatedFixed32[2]);
1012    msg.clear();
1013    assertEquals(0, msg.repeatedFixed32.length);
1014    msg.clear()
1015       .repeatedFixed32 = new int[] { 456 };
1016    assertEquals(1, msg.repeatedFixed32.length);
1017    assertEquals(456, msg.repeatedFixed32[0]);
1018    msg.clear();
1019    assertEquals(0, msg.repeatedFixed32.length);
1020
1021    // Test 1 entry
1022    msg.clear()
1023       .repeatedFixed32 = new int[] { 123 };
1024    assertEquals(1, msg.repeatedFixed32.length);
1025    byte [] result = MessageNano.toByteArray(msg);
1026    int msgSerializedSize = msg.getSerializedSize();
1027    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1028    assertTrue(msgSerializedSize == 9);
1029    assertEquals(result.length, msgSerializedSize);
1030    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1031    assertEquals(1, newMsg.repeatedFixed32.length);
1032    assertEquals(123, newMsg.repeatedFixed32[0]);
1033
1034    // Test 2 entries
1035    msg.clear()
1036       .repeatedFixed32 = new int[] { 123, 456 };
1037    assertEquals(2, msg.repeatedFixed32.length);
1038    result = MessageNano.toByteArray(msg);
1039    msgSerializedSize = msg.getSerializedSize();
1040    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1041    assertTrue(msgSerializedSize == 15);
1042    assertEquals(result.length, msgSerializedSize);
1043
1044    newMsg = TestAllTypesNano.parseFrom(result);
1045    assertEquals(2, newMsg.repeatedFixed32.length);
1046    assertEquals(123, newMsg.repeatedFixed32[0]);
1047    assertEquals(456, newMsg.repeatedFixed32[1]);
1048  }
1049
1050  public void testNanoRepeatedFixed64() throws Exception {
1051    TestAllTypesNano msg = new TestAllTypesNano();
1052    assertEquals(0, msg.repeatedFixed64.length);
1053    msg.repeatedFixed64 = new long[] { 123, 789, 456 };
1054    assertEquals(789, msg.repeatedFixed64[1]);
1055    assertEquals(456, msg.repeatedFixed64[2]);
1056    msg.clear();
1057    assertEquals(0, msg.repeatedFixed64.length);
1058    msg.clear()
1059       .repeatedFixed64 = new long[] { 456 };
1060    assertEquals(1, msg.repeatedFixed64.length);
1061    assertEquals(456, msg.repeatedFixed64[0]);
1062    msg.clear();
1063    assertEquals(0, msg.repeatedFixed64.length);
1064
1065    // Test 1 entry
1066    msg.clear()
1067       .repeatedFixed64 = new long[] { 123 };
1068    assertEquals(1, msg.repeatedFixed64.length);
1069    byte [] result = MessageNano.toByteArray(msg);
1070    int msgSerializedSize = msg.getSerializedSize();
1071    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1072    assertTrue(msgSerializedSize == 13);
1073    assertEquals(result.length, msgSerializedSize);
1074    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1075    assertEquals(1, newMsg.repeatedFixed64.length);
1076    assertEquals(123, newMsg.repeatedFixed64[0]);
1077
1078    // Test 2 entries
1079    msg.clear()
1080       .repeatedFixed64 = new long[] { 123, 456 };
1081    assertEquals(2, msg.repeatedFixed64.length);
1082    result = MessageNano.toByteArray(msg);
1083    msgSerializedSize = msg.getSerializedSize();
1084    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1085    assertTrue(msgSerializedSize == 23);
1086    assertEquals(result.length, msgSerializedSize);
1087
1088    newMsg = TestAllTypesNano.parseFrom(result);
1089    assertEquals(2, newMsg.repeatedFixed64.length);
1090    assertEquals(123, newMsg.repeatedFixed64[0]);
1091    assertEquals(456, newMsg.repeatedFixed64[1]);
1092  }
1093
1094  public void testNanoRepeatedSfixed32() throws Exception {
1095    TestAllTypesNano msg = new TestAllTypesNano();
1096    assertEquals(0, msg.repeatedSfixed32.length);
1097    msg.repeatedSfixed32 = new int[] { 123, 789, 456 };
1098    assertEquals(789, msg.repeatedSfixed32[1]);
1099    assertEquals(456, msg.repeatedSfixed32[2]);
1100    msg.clear();
1101    assertEquals(0, msg.repeatedSfixed32.length);
1102    msg.clear()
1103       .repeatedSfixed32 = new int[] { 456 };
1104    assertEquals(1, msg.repeatedSfixed32.length);
1105    assertEquals(456, msg.repeatedSfixed32[0]);
1106    msg.clear();
1107    assertEquals(0, msg.repeatedSfixed32.length);
1108
1109    // Test 1 entry
1110    msg.clear()
1111       .repeatedSfixed32 = new int[] { 123 };
1112    assertEquals(1, msg.repeatedSfixed32.length);
1113    byte [] result = MessageNano.toByteArray(msg);
1114    int msgSerializedSize = msg.getSerializedSize();
1115    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1116    assertTrue(msgSerializedSize == 9);
1117    assertEquals(result.length, msgSerializedSize);
1118    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1119    assertEquals(1, newMsg.repeatedSfixed32.length);
1120    assertEquals(123, newMsg.repeatedSfixed32[0]);
1121
1122    // Test 2 entries
1123    msg.clear()
1124       .repeatedSfixed32 = new int[] { 123, 456 };
1125    assertEquals(2, msg.repeatedSfixed32.length);
1126    result = MessageNano.toByteArray(msg);
1127    msgSerializedSize = msg.getSerializedSize();
1128    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1129    assertTrue(msgSerializedSize == 15);
1130    assertEquals(result.length, msgSerializedSize);
1131
1132    newMsg = TestAllTypesNano.parseFrom(result);
1133    assertEquals(2, newMsg.repeatedSfixed32.length);
1134    assertEquals(123, newMsg.repeatedSfixed32[0]);
1135    assertEquals(456, newMsg.repeatedSfixed32[1]);
1136  }
1137
1138  public void testNanoRepeatedSfixed64() throws Exception {
1139    TestAllTypesNano msg = new TestAllTypesNano();
1140    assertEquals(0, msg.repeatedSfixed64.length);
1141    msg.repeatedSfixed64 = new long[] { 123, 789, 456 };
1142    assertEquals(789, msg.repeatedSfixed64[1]);
1143    assertEquals(456, msg.repeatedSfixed64[2]);
1144    msg.clear();
1145    assertEquals(0, msg.repeatedSfixed64.length);
1146    msg.clear()
1147       .repeatedSfixed64 = new long[] { 456 };
1148    assertEquals(1, msg.repeatedSfixed64.length);
1149    assertEquals(456, msg.repeatedSfixed64[0]);
1150    msg.clear();
1151    assertEquals(0, msg.repeatedSfixed64.length);
1152
1153    // Test 1 entry
1154    msg.clear()
1155       .repeatedSfixed64 = new long[] { 123 };
1156    assertEquals(1, msg.repeatedSfixed64.length);
1157    byte [] result = MessageNano.toByteArray(msg);
1158    int msgSerializedSize = msg.getSerializedSize();
1159    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1160    assertTrue(msgSerializedSize == 13);
1161    assertEquals(result.length, msgSerializedSize);
1162    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1163    assertEquals(1, newMsg.repeatedSfixed64.length);
1164    assertEquals(123, newMsg.repeatedSfixed64[0]);
1165
1166    // Test 2 entries
1167    msg.clear()
1168       .repeatedSfixed64 = new long[] { 123, 456 };
1169    assertEquals(2, msg.repeatedSfixed64.length);
1170    result = MessageNano.toByteArray(msg);
1171    msgSerializedSize = msg.getSerializedSize();
1172    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1173    assertTrue(msgSerializedSize == 23);
1174    assertEquals(result.length, msgSerializedSize);
1175
1176    newMsg = TestAllTypesNano.parseFrom(result);
1177    assertEquals(2, newMsg.repeatedSfixed64.length);
1178    assertEquals(123, newMsg.repeatedSfixed64[0]);
1179    assertEquals(456, newMsg.repeatedSfixed64[1]);
1180  }
1181
1182  public void testNanoRepeatedFloat() throws Exception {
1183    TestAllTypesNano msg = new TestAllTypesNano();
1184    assertEquals(0, msg.repeatedFloat.length);
1185    msg.repeatedFloat = new float[] { 123f, 789f, 456f };
1186    assertEquals(789f, msg.repeatedFloat[1]);
1187    assertEquals(456f, msg.repeatedFloat[2]);
1188    msg.clear();
1189    assertEquals(0, msg.repeatedFloat.length);
1190    msg.clear()
1191       .repeatedFloat = new float[] { 456f };
1192    assertEquals(1, msg.repeatedFloat.length);
1193    assertEquals(456f, msg.repeatedFloat[0]);
1194    msg.clear();
1195    assertEquals(0, msg.repeatedFloat.length);
1196
1197    // Test 1 entry
1198    msg.clear()
1199       .repeatedFloat = new float[] { 123f };
1200    assertEquals(1, msg.repeatedFloat.length);
1201    byte [] result = MessageNano.toByteArray(msg);
1202    int msgSerializedSize = msg.getSerializedSize();
1203    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1204    assertTrue(msgSerializedSize == 9);
1205    assertEquals(result.length, msgSerializedSize);
1206    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1207    assertEquals(1, newMsg.repeatedFloat.length);
1208    assertEquals(123f, newMsg.repeatedFloat[0]);
1209
1210    // Test 2 entries
1211    msg.clear()
1212       .repeatedFloat = new float[] { 123f, 456f };
1213    assertEquals(2, msg.repeatedFloat.length);
1214    result = MessageNano.toByteArray(msg);
1215    msgSerializedSize = msg.getSerializedSize();
1216    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1217    assertTrue(msgSerializedSize == 15);
1218    assertEquals(result.length, msgSerializedSize);
1219
1220    newMsg = TestAllTypesNano.parseFrom(result);
1221    assertEquals(2, newMsg.repeatedFloat.length);
1222    assertEquals(123f, newMsg.repeatedFloat[0]);
1223    assertEquals(456f, newMsg.repeatedFloat[1]);
1224  }
1225
1226  public void testNanoRepeatedDouble() throws Exception {
1227    TestAllTypesNano msg = new TestAllTypesNano();
1228    assertEquals(0, msg.repeatedDouble.length);
1229    msg.repeatedDouble = new double[] { 123.0, 789.0, 456.0 };
1230    assertEquals(789.0, msg.repeatedDouble[1]);
1231    assertEquals(456.0, msg.repeatedDouble[2]);
1232    msg.clear();
1233    assertEquals(0, msg.repeatedDouble.length);
1234    msg.clear()
1235       .repeatedDouble = new double[] { 456.0 };
1236    assertEquals(1, msg.repeatedDouble.length);
1237    assertEquals(456.0, msg.repeatedDouble[0]);
1238    msg.clear();
1239    assertEquals(0, msg.repeatedDouble.length);
1240
1241    // Test 1 entry
1242    msg.clear()
1243       .repeatedDouble = new double[] { 123.0 };
1244    assertEquals(1, msg.repeatedDouble.length);
1245    byte [] result = MessageNano.toByteArray(msg);
1246    int msgSerializedSize = msg.getSerializedSize();
1247    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1248    assertTrue(msgSerializedSize == 13);
1249    assertEquals(result.length, msgSerializedSize);
1250    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1251    assertEquals(1, newMsg.repeatedDouble.length);
1252    assertEquals(123.0, newMsg.repeatedDouble[0]);
1253
1254    // Test 2 entries
1255    msg.clear()
1256       .repeatedDouble = new double[] { 123.0, 456.0 };
1257    assertEquals(2, msg.repeatedDouble.length);
1258    result = MessageNano.toByteArray(msg);
1259    msgSerializedSize = msg.getSerializedSize();
1260    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1261    assertTrue(msgSerializedSize == 23);
1262    assertEquals(result.length, msgSerializedSize);
1263
1264    newMsg = TestAllTypesNano.parseFrom(result);
1265    assertEquals(2, newMsg.repeatedDouble.length);
1266    assertEquals(123.0, newMsg.repeatedDouble[0]);
1267    assertEquals(456.0, newMsg.repeatedDouble[1]);
1268  }
1269
1270  public void testNanoRepeatedBool() throws Exception {
1271    TestAllTypesNano msg = new TestAllTypesNano();
1272    assertEquals(0, msg.repeatedBool.length);
1273    msg.repeatedBool = new boolean[] { false, true, false };
1274    assertTrue(msg.repeatedBool[1]);
1275    assertFalse(msg.repeatedBool[2]);
1276    msg.clear();
1277    assertEquals(0, msg.repeatedBool.length);
1278    msg.clear()
1279       .repeatedBool = new boolean[] { true };
1280    assertEquals(1, msg.repeatedBool.length);
1281    assertTrue(msg.repeatedBool[0]);
1282    msg.clear();
1283    assertEquals(0, msg.repeatedBool.length);
1284
1285    // Test 1 entry
1286    msg.clear()
1287       .repeatedBool = new boolean[] { false };
1288    assertEquals(1, msg.repeatedBool.length);
1289    byte [] result = MessageNano.toByteArray(msg);
1290    int msgSerializedSize = msg.getSerializedSize();
1291    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1292    assertTrue(msgSerializedSize == 6);
1293    assertEquals(result.length, msgSerializedSize);
1294    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1295    assertEquals(1, newMsg.repeatedBool.length);
1296    assertFalse(newMsg.repeatedBool[0]);
1297
1298    // Test 2 entries
1299    msg.clear()
1300       .repeatedBool = new boolean[] { true, false };
1301    assertEquals(2, msg.repeatedBool.length);
1302    result = MessageNano.toByteArray(msg);
1303    msgSerializedSize = msg.getSerializedSize();
1304    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1305    assertTrue(msgSerializedSize == 9);
1306    assertEquals(result.length, msgSerializedSize);
1307
1308    newMsg = TestAllTypesNano.parseFrom(result);
1309    assertEquals(2, newMsg.repeatedBool.length);
1310    assertTrue(newMsg.repeatedBool[0]);
1311    assertFalse(newMsg.repeatedBool[1]);
1312  }
1313
1314  public void testNanoRepeatedString() throws Exception {
1315    TestAllTypesNano msg = new TestAllTypesNano();
1316    assertEquals(0, msg.repeatedString.length);
1317    msg.repeatedString = new String[] { "hello", "bye", "boo" };
1318    assertEquals("bye", msg.repeatedString[1]);
1319    assertEquals("boo", msg.repeatedString[2]);
1320    msg.clear();
1321    assertEquals(0, msg.repeatedString.length);
1322    msg.clear()
1323       .repeatedString = new String[] { "boo" };
1324    assertEquals(1, msg.repeatedString.length);
1325    assertEquals("boo", msg.repeatedString[0]);
1326    msg.clear();
1327    assertEquals(0, msg.repeatedString.length);
1328
1329    // Test 1 entry
1330    msg.clear()
1331       .repeatedString = new String[] { "" };
1332    assertEquals(1, msg.repeatedString.length);
1333    byte [] result = MessageNano.toByteArray(msg);
1334    int msgSerializedSize = msg.getSerializedSize();
1335    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1336    assertTrue(msgSerializedSize == 6);
1337    assertEquals(result.length, msgSerializedSize);
1338    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1339    assertEquals(1, newMsg.repeatedString.length);
1340    assertTrue(newMsg.repeatedString[0].isEmpty());
1341
1342    // Test 2 entries
1343    msg.clear()
1344       .repeatedString = new String[] { "hello", "world" };
1345    assertEquals(2, msg.repeatedString.length);
1346    result = MessageNano.toByteArray(msg);
1347    msgSerializedSize = msg.getSerializedSize();
1348    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1349    assertTrue(msgSerializedSize == 19);
1350    assertEquals(result.length, msgSerializedSize);
1351
1352    newMsg = TestAllTypesNano.parseFrom(result);
1353    assertEquals(2, newMsg.repeatedString.length);
1354    assertEquals("hello", newMsg.repeatedString[0]);
1355    assertEquals("world", newMsg.repeatedString[1]);
1356  }
1357
1358  public void testNanoRepeatedBytes() throws Exception {
1359    TestAllTypesNano msg = new TestAllTypesNano();
1360    assertEquals(0, msg.repeatedBytes.length);
1361    msg.repeatedBytes = new byte[][] {
1362        InternalNano.copyFromUtf8("hello"),
1363        InternalNano.copyFromUtf8("bye"),
1364        InternalNano.copyFromUtf8("boo")
1365    };
1366    assertEquals("bye", new String(msg.repeatedBytes[1], "UTF-8"));
1367    assertEquals("boo", new String(msg.repeatedBytes[2], "UTF-8"));
1368    msg.clear();
1369    assertEquals(0, msg.repeatedBytes.length);
1370    msg.clear()
1371       .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") };
1372    assertEquals(1, msg.repeatedBytes.length);
1373    assertEquals("boo", new String(msg.repeatedBytes[0], "UTF-8"));
1374    msg.clear();
1375    assertEquals(0, msg.repeatedBytes.length);
1376
1377    // Test 1 entry
1378    msg.clear()
1379       .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("") };
1380    assertEquals(1, msg.repeatedBytes.length);
1381    byte [] result = MessageNano.toByteArray(msg);
1382    int msgSerializedSize = msg.getSerializedSize();
1383    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1384    assertTrue(msgSerializedSize == 6);
1385    assertEquals(result.length, msgSerializedSize);
1386    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1387    assertEquals(1, newMsg.repeatedBytes.length);
1388    assertTrue(newMsg.repeatedBytes[0].length == 0);
1389
1390    // Test 2 entries
1391    msg.clear()
1392       .repeatedBytes = new byte[][] {
1393      InternalNano.copyFromUtf8("hello"),
1394      InternalNano.copyFromUtf8("world")
1395    };
1396    assertEquals(2, msg.repeatedBytes.length);
1397    result = MessageNano.toByteArray(msg);
1398    msgSerializedSize = msg.getSerializedSize();
1399    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1400    assertTrue(msgSerializedSize == 19);
1401    assertEquals(result.length, msgSerializedSize);
1402
1403    newMsg = TestAllTypesNano.parseFrom(result);
1404    assertEquals(2, newMsg.repeatedBytes.length);
1405    assertEquals("hello", new String(newMsg.repeatedBytes[0], "UTF-8"));
1406    assertEquals("world", new String(newMsg.repeatedBytes[1], "UTF-8"));
1407  }
1408
1409  public void testNanoRepeatedGroup() throws Exception {
1410    TestAllTypesNano msg = new TestAllTypesNano();
1411    TestAllTypesNano.RepeatedGroup group0 =
1412      new TestAllTypesNano.RepeatedGroup();
1413    group0.a = 0;
1414    TestAllTypesNano.RepeatedGroup group1 =
1415      new TestAllTypesNano.RepeatedGroup();
1416    group1.a = 1;
1417    TestAllTypesNano.RepeatedGroup group2 =
1418      new TestAllTypesNano.RepeatedGroup();
1419    group2.a = 2;
1420
1421    msg.repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0, group1, group2 };
1422    assertEquals(3, msg.repeatedGroup.length);
1423    assertEquals(0, msg.repeatedGroup[0].a);
1424    assertEquals(1, msg.repeatedGroup[1].a);
1425    assertEquals(2, msg.repeatedGroup[2].a);
1426    msg.clear();
1427    assertEquals(0, msg.repeatedGroup.length);
1428    msg.clear()
1429       .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group1 };
1430    assertEquals(1, msg.repeatedGroup.length);
1431    assertEquals(1, msg.repeatedGroup[0].a);
1432    msg.clear();
1433    assertEquals(0, msg.repeatedGroup.length);
1434
1435    // Test 1 entry
1436    msg.clear()
1437       .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0 };
1438    assertEquals(1, msg.repeatedGroup.length);
1439    byte [] result = MessageNano.toByteArray(msg);
1440    int msgSerializedSize = msg.getSerializedSize();
1441    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1442    assertTrue(msgSerializedSize == 7);
1443    assertEquals(result.length, msgSerializedSize);
1444    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1445    assertEquals(1, newMsg.repeatedGroup.length);
1446    assertEquals(0, newMsg.repeatedGroup[0].a);
1447
1448    // Test 2 entries
1449    msg.clear()
1450       .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0, group1 };
1451    assertEquals(2, msg.repeatedGroup.length);
1452    result = MessageNano.toByteArray(msg);
1453    msgSerializedSize = msg.getSerializedSize();
1454    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1455    assertTrue(msgSerializedSize == 14);
1456    assertEquals(result.length, msgSerializedSize);
1457
1458    newMsg = TestAllTypesNano.parseFrom(result);
1459    assertEquals(2, newMsg.repeatedGroup.length);
1460    assertEquals(0, newMsg.repeatedGroup[0].a);
1461    assertEquals(1, newMsg.repeatedGroup[1].a);
1462  }
1463
1464  public void testNanoRepeatedNestedMessage() throws Exception {
1465    TestAllTypesNano msg = new TestAllTypesNano();
1466    TestAllTypesNano.NestedMessage nestedMsg0 =
1467      new TestAllTypesNano.NestedMessage();
1468    nestedMsg0.bb = 0;
1469    TestAllTypesNano.NestedMessage nestedMsg1 =
1470      new TestAllTypesNano.NestedMessage();
1471    nestedMsg1.bb = 1;
1472    TestAllTypesNano.NestedMessage nestedMsg2 =
1473      new TestAllTypesNano.NestedMessage();
1474    nestedMsg2.bb = 2;
1475
1476    msg.repeatedNestedMessage =
1477        new TestAllTypesNano.NestedMessage[] { nestedMsg0, nestedMsg1, nestedMsg2 };
1478    assertEquals(3, msg.repeatedNestedMessage.length);
1479    assertEquals(0, msg.repeatedNestedMessage[0].bb);
1480    assertEquals(1, msg.repeatedNestedMessage[1].bb);
1481    assertEquals(2, msg.repeatedNestedMessage[2].bb);
1482    msg.clear();
1483    assertEquals(0, msg.repeatedNestedMessage.length);
1484    msg.clear()
1485       .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg1 };
1486    assertEquals(1, msg.repeatedNestedMessage.length);
1487    assertEquals(1, msg.repeatedNestedMessage[0].bb);
1488    msg.clear();
1489    assertEquals(0, msg.repeatedNestedMessage.length);
1490
1491    // Test 1 entry
1492    msg.clear()
1493       .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0 };
1494    assertEquals(1, msg.repeatedNestedMessage.length);
1495    byte [] result = MessageNano.toByteArray(msg);
1496    int msgSerializedSize = msg.getSerializedSize();
1497    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1498    assertTrue(msgSerializedSize == 6);
1499    assertEquals(result.length, msgSerializedSize);
1500    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1501    assertEquals(1, newMsg.repeatedNestedMessage.length);
1502    assertEquals(0, newMsg.repeatedNestedMessage[0].bb);
1503
1504    // Test 2 entries
1505    msg.clear()
1506       .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0, nestedMsg1 };
1507    assertEquals(2, msg.repeatedNestedMessage.length);
1508    result = MessageNano.toByteArray(msg);
1509    msgSerializedSize = msg.getSerializedSize();
1510    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1511    assertTrue(msgSerializedSize == 11);
1512    assertEquals(result.length, msgSerializedSize);
1513
1514    newMsg = TestAllTypesNano.parseFrom(result);
1515    assertEquals(2, newMsg.repeatedNestedMessage.length);
1516    assertEquals(0, newMsg.repeatedNestedMessage[0].bb);
1517    assertEquals(1, newMsg.repeatedNestedMessage[1].bb);
1518  }
1519
1520  public void testNanoRepeatedForeignMessage() throws Exception {
1521    TestAllTypesNano msg = new TestAllTypesNano();
1522    NanoOuterClass.ForeignMessageNano foreignMsg0 =
1523      new NanoOuterClass.ForeignMessageNano();
1524    foreignMsg0.c = 0;
1525    NanoOuterClass.ForeignMessageNano foreignMsg1 =
1526      new NanoOuterClass.ForeignMessageNano();
1527    foreignMsg1.c = 1;
1528    NanoOuterClass.ForeignMessageNano foreignMsg2 =
1529      new NanoOuterClass.ForeignMessageNano();
1530    foreignMsg2.c = 2;
1531
1532    msg.repeatedForeignMessage =
1533        new NanoOuterClass.ForeignMessageNano[] { foreignMsg0, foreignMsg1, foreignMsg2 };
1534    assertEquals(3, msg.repeatedForeignMessage.length);
1535    assertEquals(0, msg.repeatedForeignMessage[0].c);
1536    assertEquals(1, msg.repeatedForeignMessage[1].c);
1537    assertEquals(2, msg.repeatedForeignMessage[2].c);
1538    msg.clear();
1539    assertEquals(0, msg.repeatedForeignMessage.length);
1540    msg.clear()
1541       .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg1 };
1542    assertEquals(1, msg.repeatedForeignMessage.length);
1543    assertEquals(1, msg.repeatedForeignMessage[0].c);
1544    msg.clear();
1545    assertEquals(0, msg.repeatedForeignMessage.length);
1546
1547    // Test 1 entry
1548    msg.clear()
1549       .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg0 };
1550    assertEquals(1, msg.repeatedForeignMessage.length);
1551    byte [] result = MessageNano.toByteArray(msg);
1552    int msgSerializedSize = msg.getSerializedSize();
1553    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1554    assertTrue(msgSerializedSize == 6);
1555    assertEquals(result.length, msgSerializedSize);
1556    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1557    assertEquals(1, newMsg.repeatedForeignMessage.length);
1558    assertEquals(0, newMsg.repeatedForeignMessage[0].c);
1559
1560    // Test 2 entries
1561    msg.clear()
1562       .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg0, foreignMsg1 };
1563    assertEquals(2, msg.repeatedForeignMessage.length);
1564    result = MessageNano.toByteArray(msg);
1565    msgSerializedSize = msg.getSerializedSize();
1566    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1567    assertTrue(msgSerializedSize == 11);
1568    assertEquals(result.length, msgSerializedSize);
1569
1570    newMsg = TestAllTypesNano.parseFrom(result);
1571    assertEquals(2, newMsg.repeatedForeignMessage.length);
1572    assertEquals(0, newMsg.repeatedForeignMessage[0].c);
1573    assertEquals(1, newMsg.repeatedForeignMessage[1].c);
1574  }
1575
1576  public void testNanoRepeatedImportMessage() throws Exception {
1577    TestAllTypesNano msg = new TestAllTypesNano();
1578    UnittestImportNano.ImportMessageNano foreignMsg0 =
1579      new UnittestImportNano.ImportMessageNano();
1580    foreignMsg0.d = 0;
1581    UnittestImportNano.ImportMessageNano foreignMsg1 =
1582      new UnittestImportNano.ImportMessageNano();
1583    foreignMsg1.d = 1;
1584    UnittestImportNano.ImportMessageNano foreignMsg2 =
1585      new UnittestImportNano.ImportMessageNano();
1586    foreignMsg2.d = 2;
1587
1588    msg.repeatedImportMessage =
1589        new UnittestImportNano.ImportMessageNano[] { foreignMsg0, foreignMsg1, foreignMsg2 };
1590    assertEquals(3, msg.repeatedImportMessage.length);
1591    assertEquals(0, msg.repeatedImportMessage[0].d);
1592    assertEquals(1, msg.repeatedImportMessage[1].d);
1593    assertEquals(2, msg.repeatedImportMessage[2].d);
1594    msg.clear();
1595    assertEquals(0, msg.repeatedImportMessage.length);
1596    msg.clear()
1597       .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg1 };
1598    assertEquals(1, msg.repeatedImportMessage.length);
1599    assertEquals(1, msg.repeatedImportMessage[0].d);
1600    msg.clear();
1601    assertEquals(0, msg.repeatedImportMessage.length);
1602
1603    // Test 1 entry
1604    msg.clear()
1605       .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg0 };
1606    assertEquals(1, msg.repeatedImportMessage.length);
1607    byte [] result = MessageNano.toByteArray(msg);
1608    int msgSerializedSize = msg.getSerializedSize();
1609    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1610    assertTrue(msgSerializedSize == 6);
1611    assertEquals(result.length, msgSerializedSize);
1612    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1613    assertEquals(1, newMsg.repeatedImportMessage.length);
1614    assertEquals(0, newMsg.repeatedImportMessage[0].d);
1615
1616    // Test 2 entries
1617    msg.clear()
1618       .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg0, foreignMsg1 };
1619    assertEquals(2, msg.repeatedImportMessage.length);
1620    result = MessageNano.toByteArray(msg);
1621    msgSerializedSize = msg.getSerializedSize();
1622    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1623    assertTrue(msgSerializedSize == 11);
1624    assertEquals(result.length, msgSerializedSize);
1625
1626    newMsg = TestAllTypesNano.parseFrom(result);
1627    assertEquals(2, newMsg.repeatedImportMessage.length);
1628    assertEquals(0, newMsg.repeatedImportMessage[0].d);
1629    assertEquals(1, newMsg.repeatedImportMessage[1].d);
1630  }
1631
1632  public void testNanoRepeatedNestedEnum() throws Exception {
1633    TestAllTypesNano msg = new TestAllTypesNano();
1634    msg.repeatedNestedEnum = new int[] {
1635        TestAllTypesNano.FOO,
1636        TestAllTypesNano.BAR,
1637        TestAllTypesNano.BAZ
1638    };
1639    assertEquals(3, msg.repeatedNestedEnum.length);
1640    assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]);
1641    assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[1]);
1642    assertEquals(TestAllTypesNano.BAZ, msg.repeatedNestedEnum[2]);
1643    msg.clear();
1644    assertEquals(0, msg.repeatedNestedEnum.length);
1645    msg.clear()
1646       .repeatedNestedEnum = new int[] { TestAllTypesNano.BAR };
1647    assertEquals(1, msg.repeatedNestedEnum.length);
1648    assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[0]);
1649    msg.clear();
1650    assertEquals(0, msg.repeatedNestedEnum.length);
1651
1652    // Test 1 entry
1653    msg.clear()
1654       .repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
1655    byte [] result = MessageNano.toByteArray(msg);
1656    int msgSerializedSize = msg.getSerializedSize();
1657    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1658    assertTrue(msgSerializedSize == 6);
1659    assertEquals(result.length, msgSerializedSize);
1660    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1661    assertEquals(1, newMsg.repeatedNestedEnum.length);
1662    assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]);
1663
1664    // Test 2 entries
1665    msg.clear()
1666       .repeatedNestedEnum = new int[] { TestAllTypesNano.FOO, TestAllTypesNano.BAR };
1667    assertEquals(2, msg.repeatedNestedEnum.length);
1668    result = MessageNano.toByteArray(msg);
1669    msgSerializedSize = msg.getSerializedSize();
1670    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1671    assertTrue(msgSerializedSize == 9);
1672    assertEquals(result.length, msgSerializedSize);
1673
1674    newMsg = TestAllTypesNano.parseFrom(result);
1675    assertEquals(2, newMsg.repeatedNestedEnum.length);
1676    assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]);
1677    assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[1]);
1678  }
1679
1680  public void testNanoRepeatedForeignEnum() throws Exception {
1681    TestAllTypesNano msg = new TestAllTypesNano();
1682    msg.repeatedForeignEnum = new int[] {
1683        NanoOuterClass.FOREIGN_NANO_FOO,
1684        NanoOuterClass.FOREIGN_NANO_BAR,
1685        NanoOuterClass.FOREIGN_NANO_BAZ
1686    };
1687    assertEquals(3, msg.repeatedForeignEnum.length);
1688    assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]);
1689    assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[1]);
1690    assertEquals(NanoOuterClass.FOREIGN_NANO_BAZ, msg.repeatedForeignEnum[2]);
1691    msg.clear();
1692    assertEquals(0, msg.repeatedForeignEnum.length);
1693    msg.clear()
1694       .repeatedForeignEnum = new int[] { NanoOuterClass.FOREIGN_NANO_BAR };
1695    assertEquals(1, msg.repeatedForeignEnum.length);
1696    assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[0]);
1697    msg.clear();
1698    assertEquals(0, msg.repeatedForeignEnum.length);
1699
1700    // Test 1 entry
1701    msg.clear()
1702       .repeatedForeignEnum = new int[] { NanoOuterClass.FOREIGN_NANO_FOO };
1703    byte [] result = MessageNano.toByteArray(msg);
1704    int msgSerializedSize = msg.getSerializedSize();
1705    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1706    assertTrue(msgSerializedSize == 6);
1707    assertEquals(result.length, msgSerializedSize);
1708    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1709    assertEquals(1, newMsg.repeatedForeignEnum.length);
1710    assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]);
1711
1712    // Test 2 entries
1713    msg.clear()
1714       .repeatedForeignEnum = new int[] {
1715      NanoOuterClass.FOREIGN_NANO_FOO,
1716      NanoOuterClass.FOREIGN_NANO_BAR
1717    };
1718    assertEquals(2, msg.repeatedForeignEnum.length);
1719    result = MessageNano.toByteArray(msg);
1720    msgSerializedSize = msg.getSerializedSize();
1721    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1722    assertTrue(msgSerializedSize == 9);
1723    assertEquals(result.length, msgSerializedSize);
1724
1725    newMsg = TestAllTypesNano.parseFrom(result);
1726    assertEquals(2, newMsg.repeatedForeignEnum.length);
1727    assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]);
1728    assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[1]);
1729  }
1730
1731  public void testNanoRepeatedImportEnum() throws Exception {
1732    TestAllTypesNano msg = new TestAllTypesNano();
1733    msg.repeatedImportEnum = new int[] {
1734        UnittestImportNano.IMPORT_NANO_FOO,
1735        UnittestImportNano.IMPORT_NANO_BAR,
1736        UnittestImportNano.IMPORT_NANO_BAZ
1737    };
1738    assertEquals(3, msg.repeatedImportEnum.length);
1739    assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]);
1740    assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[1]);
1741    assertEquals(UnittestImportNano.IMPORT_NANO_BAZ, msg.repeatedImportEnum[2]);
1742    msg.clear();
1743    assertEquals(0, msg.repeatedImportEnum.length);
1744    msg.clear()
1745       .repeatedImportEnum = new int[] { UnittestImportNano.IMPORT_NANO_BAR };
1746    assertEquals(1, msg.repeatedImportEnum.length);
1747    assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[0]);
1748    msg.clear();
1749    assertEquals(0, msg.repeatedImportEnum.length);
1750
1751    // Test 1 entry
1752    msg.clear()
1753       .repeatedImportEnum = new int[] { UnittestImportNano.IMPORT_NANO_FOO };
1754    byte [] result = MessageNano.toByteArray(msg);
1755    int msgSerializedSize = msg.getSerializedSize();
1756    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1757    assertTrue(msgSerializedSize == 6);
1758    assertEquals(result.length, msgSerializedSize);
1759    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1760    assertEquals(1, newMsg.repeatedImportEnum.length);
1761    assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]);
1762
1763    // Test 2 entries
1764    msg.clear()
1765       .repeatedImportEnum = new int[] {
1766      UnittestImportNano.IMPORT_NANO_FOO,
1767      UnittestImportNano.IMPORT_NANO_BAR
1768    };
1769    assertEquals(2, msg.repeatedImportEnum.length);
1770    result = MessageNano.toByteArray(msg);
1771    msgSerializedSize = msg.getSerializedSize();
1772    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1773    assertTrue(msgSerializedSize == 9);
1774    assertEquals(result.length, msgSerializedSize);
1775
1776    newMsg = TestAllTypesNano.parseFrom(result);
1777    assertEquals(2, newMsg.repeatedImportEnum.length);
1778    assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]);
1779    assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[1]);
1780  }
1781
1782  public void testNanoRepeatedStringPiece() throws Exception {
1783    TestAllTypesNano msg = new TestAllTypesNano();
1784    assertEquals(0, msg.repeatedStringPiece.length);
1785    msg.repeatedStringPiece = new String[] { "hello", "bye", "boo" };
1786    assertEquals("bye", msg.repeatedStringPiece[1]);
1787    assertEquals("boo", msg.repeatedStringPiece[2]);
1788    msg.clear();
1789    assertEquals(0, msg.repeatedStringPiece.length);
1790    msg.clear()
1791       .repeatedStringPiece = new String[] { "boo" };
1792    assertEquals(1, msg.repeatedStringPiece.length);
1793    assertEquals("boo", msg.repeatedStringPiece[0]);
1794    msg.clear();
1795    assertEquals(0, msg.repeatedStringPiece.length);
1796
1797    // Test 1 entry
1798    msg.clear()
1799       .repeatedStringPiece = new String[] { "" };
1800    assertEquals(1, msg.repeatedStringPiece.length);
1801    byte [] result = MessageNano.toByteArray(msg);
1802    int msgSerializedSize = msg.getSerializedSize();
1803    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1804    assertTrue(msgSerializedSize == 6);
1805    assertEquals(result.length, msgSerializedSize);
1806    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1807    assertEquals(1, newMsg.repeatedStringPiece.length);
1808    assertTrue(newMsg.repeatedStringPiece[0].isEmpty());
1809
1810    // Test 2 entries
1811    msg.clear()
1812       .repeatedStringPiece = new String[] { "hello", "world" };
1813    assertEquals(2, msg.repeatedStringPiece.length);
1814    result = MessageNano.toByteArray(msg);
1815    msgSerializedSize = msg.getSerializedSize();
1816    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1817    assertTrue(msgSerializedSize == 19);
1818    assertEquals(result.length, msgSerializedSize);
1819
1820    newMsg = TestAllTypesNano.parseFrom(result);
1821    assertEquals(2, newMsg.repeatedStringPiece.length);
1822    assertEquals("hello", newMsg.repeatedStringPiece[0]);
1823    assertEquals("world", newMsg.repeatedStringPiece[1]);
1824  }
1825
1826  public void testNanoRepeatedCord() throws Exception {
1827    TestAllTypesNano msg = new TestAllTypesNano();
1828    assertEquals(0, msg.repeatedCord.length);
1829    msg.repeatedCord = new String[] { "hello", "bye", "boo" };
1830    assertEquals("bye", msg.repeatedCord[1]);
1831    assertEquals("boo", msg.repeatedCord[2]);
1832    msg.clear();
1833    assertEquals(0, msg.repeatedCord.length);
1834    msg.clear()
1835       .repeatedCord = new String[] { "boo" };
1836    assertEquals(1, msg.repeatedCord.length);
1837    assertEquals("boo", msg.repeatedCord[0]);
1838    msg.clear();
1839    assertEquals(0, msg.repeatedCord.length);
1840
1841    // Test 1 entry
1842    msg.clear()
1843       .repeatedCord = new String[] { "" };
1844    assertEquals(1, msg.repeatedCord.length);
1845    byte [] result = MessageNano.toByteArray(msg);
1846    int msgSerializedSize = msg.getSerializedSize();
1847    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1848    assertTrue(msgSerializedSize == 6);
1849    assertEquals(result.length, msgSerializedSize);
1850    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1851    assertEquals(1, newMsg.repeatedCord.length);
1852    assertTrue(newMsg.repeatedCord[0].isEmpty());
1853
1854    // Test 2 entries
1855    msg.clear()
1856       .repeatedCord = new String[] { "hello", "world" };
1857    assertEquals(2, msg.repeatedCord.length);
1858    result = MessageNano.toByteArray(msg);
1859    msgSerializedSize = msg.getSerializedSize();
1860    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1861    assertTrue(msgSerializedSize == 19);
1862    assertEquals(result.length, msgSerializedSize);
1863
1864    newMsg = TestAllTypesNano.parseFrom(result);
1865    assertEquals(2, newMsg.repeatedCord.length);
1866    assertEquals("hello", newMsg.repeatedCord[0]);
1867    assertEquals("world", newMsg.repeatedCord[1]);
1868  }
1869
1870  public void testNanoRepeatedPackedInt32() throws Exception {
1871    TestAllTypesNano msg = new TestAllTypesNano();
1872    assertEquals(0, msg.repeatedPackedInt32.length);
1873    msg.repeatedPackedInt32 = new int[] { 123, 789, 456 };
1874    assertEquals(789, msg.repeatedPackedInt32[1]);
1875    assertEquals(456, msg.repeatedPackedInt32[2]);
1876    msg.clear();
1877    assertEquals(0, msg.repeatedPackedInt32.length);
1878    msg.clear()
1879       .repeatedPackedInt32 = new int[] { 456 };
1880    assertEquals(1, msg.repeatedPackedInt32.length);
1881    assertEquals(456, msg.repeatedPackedInt32[0]);
1882    msg.clear();
1883    assertEquals(0, msg.repeatedPackedInt32.length);
1884
1885    // Test 1 entry
1886    msg.clear()
1887       .repeatedPackedInt32 = new int[] { 123 };
1888    assertEquals(1, msg.repeatedPackedInt32.length);
1889    byte [] result = MessageNano.toByteArray(msg);
1890    int msgSerializedSize = msg.getSerializedSize();
1891    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1892    assertTrue(msgSerializedSize == 7);
1893    assertEquals(result.length, msgSerializedSize);
1894    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1895    assertEquals(1, newMsg.repeatedPackedInt32.length);
1896    assertEquals(123, newMsg.repeatedPackedInt32[0]);
1897
1898    // Test 2 entries
1899    msg.clear()
1900       .repeatedPackedInt32 = new int[] { 123, 456 };
1901    assertEquals(2, msg.repeatedPackedInt32.length);
1902    result = MessageNano.toByteArray(msg);
1903    msgSerializedSize = msg.getSerializedSize();
1904    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1905    assertTrue(msgSerializedSize == 9);
1906    assertEquals(result.length, msgSerializedSize);
1907
1908    newMsg = TestAllTypesNano.parseFrom(result);
1909    assertEquals(2, newMsg.repeatedPackedInt32.length);
1910    assertEquals(123, newMsg.repeatedPackedInt32[0]);
1911    assertEquals(456, newMsg.repeatedPackedInt32[1]);
1912  }
1913
1914  public void testNanoRepeatedPackedSfixed64() throws Exception {
1915    TestAllTypesNano msg = new TestAllTypesNano();
1916    assertEquals(0, msg.repeatedPackedSfixed64.length);
1917    msg.repeatedPackedSfixed64 = new long[] { 123, 789, 456 };
1918    assertEquals(789, msg.repeatedPackedSfixed64[1]);
1919    assertEquals(456, msg.repeatedPackedSfixed64[2]);
1920    msg.clear();
1921    assertEquals(0, msg.repeatedPackedSfixed64.length);
1922    msg.clear()
1923       .repeatedPackedSfixed64 = new long[] { 456 };
1924    assertEquals(1, msg.repeatedPackedSfixed64.length);
1925    assertEquals(456, msg.repeatedPackedSfixed64[0]);
1926    msg.clear();
1927    assertEquals(0, msg.repeatedPackedSfixed64.length);
1928
1929    // Test 1 entry
1930    msg.clear()
1931       .repeatedPackedSfixed64 = new long[] { 123 };
1932    assertEquals(1, msg.repeatedPackedSfixed64.length);
1933    byte [] result = MessageNano.toByteArray(msg);
1934    int msgSerializedSize = msg.getSerializedSize();
1935    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1936    assertTrue(msgSerializedSize == 14);
1937    assertEquals(result.length, msgSerializedSize);
1938    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1939    assertEquals(1, newMsg.repeatedPackedSfixed64.length);
1940    assertEquals(123, newMsg.repeatedPackedSfixed64[0]);
1941
1942    // Test 2 entries
1943    msg.clear()
1944       .repeatedPackedSfixed64 = new long[] { 123, 456 };
1945    assertEquals(2, msg.repeatedPackedSfixed64.length);
1946    result = MessageNano.toByteArray(msg);
1947    msgSerializedSize = msg.getSerializedSize();
1948    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1949    assertTrue(msgSerializedSize == 22);
1950    assertEquals(result.length, msgSerializedSize);
1951
1952    newMsg = TestAllTypesNano.parseFrom(result);
1953    assertEquals(2, newMsg.repeatedPackedSfixed64.length);
1954    assertEquals(123, newMsg.repeatedPackedSfixed64[0]);
1955    assertEquals(456, newMsg.repeatedPackedSfixed64[1]);
1956  }
1957
1958  public void testNanoRepeatedPackedNestedEnum() throws Exception {
1959    TestAllTypesNano msg = new TestAllTypesNano();
1960    msg.repeatedPackedNestedEnum = new int[] {
1961        TestAllTypesNano.FOO,
1962        TestAllTypesNano.BAR,
1963        TestAllTypesNano.BAZ
1964    };
1965    assertEquals(3, msg.repeatedPackedNestedEnum.length);
1966    assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]);
1967    assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[1]);
1968    assertEquals(TestAllTypesNano.BAZ, msg.repeatedPackedNestedEnum[2]);
1969    msg.clear();
1970    assertEquals(0, msg.repeatedPackedNestedEnum.length);
1971    msg.clear()
1972       .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.BAR };
1973    assertEquals(1, msg.repeatedPackedNestedEnum.length);
1974    assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[0]);
1975    msg.clear();
1976    assertEquals(0, msg.repeatedPackedNestedEnum.length);
1977
1978    // Test 1 entry
1979    msg.clear()
1980       .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.FOO };
1981    byte [] result = MessageNano.toByteArray(msg);
1982    int msgSerializedSize = msg.getSerializedSize();
1983    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1984    assertTrue(msgSerializedSize == 7);
1985    assertEquals(result.length, msgSerializedSize);
1986    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
1987    assertEquals(1, newMsg.repeatedPackedNestedEnum.length);
1988    assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]);
1989
1990    // Test 2 entries
1991    msg.clear()
1992       .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.FOO, TestAllTypesNano.BAR };
1993    assertEquals(2, msg.repeatedPackedNestedEnum.length);
1994    result = MessageNano.toByteArray(msg);
1995    msgSerializedSize = msg.getSerializedSize();
1996    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
1997    assertTrue(msgSerializedSize == 8);
1998    assertEquals(result.length, msgSerializedSize);
1999
2000    newMsg = TestAllTypesNano.parseFrom(result);
2001    assertEquals(2, newMsg.repeatedPackedNestedEnum.length);
2002    assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]);
2003    assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[1]);
2004  }
2005
2006  public void testNanoRepeatedPackedSerializedSize() throws Exception {
2007    TestAllTypesNano msg = new TestAllTypesNano();
2008    msg.repeatedPackedInt32 = new int[] { 123, 789, 456 };
2009    int msgSerializedSize = msg.getSerializedSize();
2010    byte [] result = MessageNano.toByteArray(msg);
2011    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
2012    assertTrue(msgSerializedSize == 11);
2013    assertEquals(result.length, msgSerializedSize);
2014    TestAllTypesNano msg2 = new TestAllTypesNano();
2015    msg2.repeatedPackedInt32 = new int[] { 123, 789, 456 };
2016    byte [] result2 = new byte[msgSerializedSize];
2017    MessageNano.toByteArray(msg2, result2, 0, msgSerializedSize);
2018
2019    // Check equal size and content.
2020    assertEquals(msgSerializedSize, msg2.getSerializedSize());
2021    assertTrue(Arrays.equals(result, result2));
2022  }
2023
2024  public void testNanoRepeatedInt32ReMerge() throws Exception {
2025    TestAllTypesNano msg = new TestAllTypesNano();
2026    msg.repeatedInt32 = new int[] { 234 };
2027    byte [] result1 = MessageNano.toByteArray(msg);
2028
2029    msg.clear().optionalInt32 = 789;
2030    byte [] result2 = MessageNano.toByteArray(msg);
2031
2032    msg.clear().repeatedInt32 = new int[] { 123, 456 };
2033    byte [] result3 = MessageNano.toByteArray(msg);
2034
2035    // Concatenate the three serializations and read as one message.
2036    byte [] result = new byte[result1.length + result2.length + result3.length];
2037    System.arraycopy(result1, 0, result, 0, result1.length);
2038    System.arraycopy(result2, 0, result, result1.length, result2.length);
2039    System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length);
2040
2041    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
2042    assertEquals(789, newMsg.optionalInt32);
2043    assertEquals(3, newMsg.repeatedInt32.length);
2044    assertEquals(234, newMsg.repeatedInt32[0]);
2045    assertEquals(123, newMsg.repeatedInt32[1]);
2046    assertEquals(456, newMsg.repeatedInt32[2]);
2047  }
2048
2049  public void testNanoRepeatedNestedEnumReMerge() throws Exception {
2050    TestAllTypesNano msg = new TestAllTypesNano();
2051    msg.repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
2052    byte [] result1 = MessageNano.toByteArray(msg);
2053
2054    msg.clear().optionalInt32 = 789;
2055    byte [] result2 = MessageNano.toByteArray(msg);
2056
2057    msg.clear().repeatedNestedEnum = new int[] { TestAllTypesNano.BAR, TestAllTypesNano.FOO };
2058    byte [] result3 = MessageNano.toByteArray(msg);
2059
2060    // Concatenate the three serializations and read as one message.
2061    byte [] result = new byte[result1.length + result2.length + result3.length];
2062    System.arraycopy(result1, 0, result, 0, result1.length);
2063    System.arraycopy(result2, 0, result, result1.length, result2.length);
2064    System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length);
2065
2066    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
2067    assertEquals(789, newMsg.optionalInt32);
2068    assertEquals(3, newMsg.repeatedNestedEnum.length);
2069    assertEquals(TestAllTypesNano.FOO, newMsg.repeatedNestedEnum[0]);
2070    assertEquals(TestAllTypesNano.BAR, newMsg.repeatedNestedEnum[1]);
2071    assertEquals(TestAllTypesNano.FOO, newMsg.repeatedNestedEnum[2]);
2072  }
2073
2074  public void testNanoRepeatedNestedMessageReMerge() throws Exception {
2075    TestAllTypesNano msg = new TestAllTypesNano();
2076    TestAllTypesNano.NestedMessage nestedMsg0 =
2077      new TestAllTypesNano.NestedMessage();
2078    nestedMsg0.bb = 0;
2079    TestAllTypesNano.NestedMessage nestedMsg1 =
2080      new TestAllTypesNano.NestedMessage();
2081    nestedMsg1.bb = 1;
2082    TestAllTypesNano.NestedMessage nestedMsg2 =
2083      new TestAllTypesNano.NestedMessage();
2084    nestedMsg2.bb = 2;
2085
2086    msg.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0 };
2087    byte [] result1 = MessageNano.toByteArray(msg);
2088
2089    msg.clear().optionalInt32 = 789;
2090    byte [] result2 = MessageNano.toByteArray(msg);
2091
2092    msg.clear().repeatedNestedMessage =
2093        new TestAllTypesNano.NestedMessage[] { nestedMsg1, nestedMsg2 };
2094    byte [] result3 = MessageNano.toByteArray(msg);
2095
2096    // Concatenate the three serializations and read as one message.
2097    byte [] result = new byte[result1.length + result2.length + result3.length];
2098    System.arraycopy(result1, 0, result, 0, result1.length);
2099    System.arraycopy(result2, 0, result, result1.length, result2.length);
2100    System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length);
2101
2102    TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
2103    assertEquals(789, newMsg.optionalInt32);
2104    assertEquals(3, newMsg.repeatedNestedMessage.length);
2105    assertEquals(nestedMsg0.bb, newMsg.repeatedNestedMessage[0].bb);
2106    assertEquals(nestedMsg1.bb, newMsg.repeatedNestedMessage[1].bb);
2107    assertEquals(nestedMsg2.bb, newMsg.repeatedNestedMessage[2].bb);
2108  }
2109
2110  /**
2111   * Tests that invalid enum values from the wire are not accepted.
2112   */
2113  public void testNanoEnumValidity() throws Exception {
2114    final int invalid = 120;
2115    final int alsoInvalid = 121;
2116
2117    EnumValidity.M m = new EnumValidity.M();
2118    // Sanity check & baseline of the assertions for the first case below.
2119    assertEquals(EnumValidity.E.default_, m.optionalE);
2120    assertEquals(EnumValidity.E.BAZ, m.defaultE);
2121
2122    m.optionalE = invalid;
2123    m.defaultE = invalid;
2124    // E contains all valid values
2125    m.repeatedE = new int[] {EnumValidity.E.FOO, EnumValidity.E.BAR};
2126    m.packedE = new int[] {EnumValidity.E.FOO, EnumValidity.E.BAZ};
2127    // E2 contains some invalid values
2128    m.repeatedE2 = new int[] {invalid, EnumValidity.E.BAR, alsoInvalid};
2129    m.packedE2 = new int[] {EnumValidity.E.FOO, invalid, alsoInvalid};
2130    // E3 contains all invalid values
2131    m.repeatedE3 = new int[] {invalid, invalid};
2132    m.packedE3 = new int[] {alsoInvalid, alsoInvalid};
2133    byte[] serialized = MessageNano.toByteArray(m);
2134    // Sanity check that we do have all data in the byte array.
2135    assertEquals(31, serialized.length);
2136
2137    // Test 1: tests that invalid values aren't included in the deserialized message.
2138    EnumValidity.M deserialized = MessageNano.mergeFrom(new EnumValidity.M(), serialized);
2139    assertEquals(EnumValidity.E.default_, deserialized.optionalE);
2140    assertEquals(EnumValidity.E.BAZ, deserialized.defaultE);
2141    assertTrue(Arrays.equals(
2142        new int[] {EnumValidity.E.FOO, EnumValidity.E.BAR}, deserialized.repeatedE));
2143    assertTrue(Arrays.equals(
2144        new int[] {EnumValidity.E.FOO, EnumValidity.E.BAZ}, deserialized.packedE));
2145    assertTrue(Arrays.equals(
2146        new int[] {EnumValidity.E.BAR}, deserialized.repeatedE2));
2147    assertTrue(Arrays.equals(
2148        new int[] {EnumValidity.E.FOO}, deserialized.packedE2));
2149    assertEquals(0, deserialized.repeatedE3.length);
2150    assertEquals(0, deserialized.packedE3.length);
2151
2152    // Test 2: tests that invalid values do not override previous values in the field, including
2153    // arrays, including pre-existing invalid values.
2154    deserialized.optionalE = EnumValidity.E.BAR;
2155    deserialized.defaultE = alsoInvalid;
2156    deserialized.repeatedE = new int[] {EnumValidity.E.BAZ};
2157    deserialized.packedE = new int[] {EnumValidity.E.BAZ, alsoInvalid};
2158    deserialized.repeatedE2 = new int[] {invalid, alsoInvalid};
2159    deserialized.packedE2 = null;
2160    deserialized.repeatedE3 = null;
2161    deserialized.packedE3 = new int[0];
2162    MessageNano.mergeFrom(deserialized, serialized);
2163    assertEquals(EnumValidity.E.BAR, deserialized.optionalE);
2164    assertEquals(alsoInvalid, deserialized.defaultE);
2165    assertTrue(Arrays.equals(
2166        new int[] {EnumValidity.E.BAZ, /* + */ EnumValidity.E.FOO, EnumValidity.E.BAR},
2167        deserialized.repeatedE));
2168    assertTrue(Arrays.equals(
2169        new int[] {EnumValidity.E.BAZ, alsoInvalid, /* + */ EnumValidity.E.FOO, EnumValidity.E.BAZ},
2170        deserialized.packedE));
2171    assertTrue(Arrays.equals(
2172        new int[] {invalid, alsoInvalid, /* + */ EnumValidity.E.BAR},
2173        deserialized.repeatedE2));
2174    assertTrue(Arrays.equals(
2175        new int[] {/* <null> + */ EnumValidity.E.FOO},
2176        deserialized.packedE2));
2177    assertNull(deserialized.repeatedE3); // null + all invalid == null
2178    assertEquals(0, deserialized.packedE3.length); // empty + all invalid == empty
2179
2180    // Test 3: reading by alternative forms
2181    EnumValidity.Alt alt = MessageNano.mergeFrom(new EnumValidity.Alt(), serialized);
2182    assertEquals(EnumValidity.E.BAR, // last valid value in m.repeatedE2
2183        alt.repeatedE2AsOptional);
2184    assertTrue(Arrays.equals(new int[] {EnumValidity.E.FOO}, alt.packedE2AsNonPacked));
2185    assertEquals(0, alt.nonPackedE3AsPacked.length);
2186  }
2187
2188  /**
2189   * Tests the same as {@link #testNanoEnumValidity()} with accessor style. Repeated fields are
2190   * not re-tested here because they are not affected by the accessor style.
2191   */
2192  public void testNanoEnumValidityAccessors() throws Exception {
2193    final int invalid = 120;
2194    final int alsoInvalid = 121;
2195
2196    EnumValidityAccessors.M m = new EnumValidityAccessors.M();
2197    // Sanity check & baseline of the assertions for the first case below.
2198    assertEquals(EnumValidityAccessors.default_, m.getOptionalE());
2199    assertEquals(EnumValidityAccessors.BAZ, m.getDefaultE());
2200
2201    m.setOptionalE(invalid);
2202    m.setDefaultE(invalid);
2203    // Set repeatedE2 for Alt.repeatedE2AsOptional
2204    m.repeatedE2 = new int[] {invalid, EnumValidityAccessors.BAR, alsoInvalid};
2205    byte[] serialized = MessageNano.toByteArray(m);
2206    // Sanity check that we do have all data in the byte array.
2207    assertEquals(10, serialized.length);
2208
2209    // Test 1: tests that invalid values aren't included in the deserialized message.
2210    EnumValidityAccessors.M deserialized =
2211        MessageNano.mergeFrom(new EnumValidityAccessors.M(), serialized);
2212    assertEquals(EnumValidityAccessors.default_, deserialized.getOptionalE());
2213    assertEquals(EnumValidityAccessors.BAZ, deserialized.getDefaultE());
2214
2215    // Test 2: tests that invalid values do not override previous values in the field, including
2216    // pre-existing invalid values.
2217    deserialized.setOptionalE(EnumValidityAccessors.BAR);
2218    deserialized.setDefaultE(alsoInvalid);
2219    MessageNano.mergeFrom(deserialized, serialized);
2220    assertEquals(EnumValidityAccessors.BAR, deserialized.getOptionalE());
2221    assertEquals(alsoInvalid, deserialized.getDefaultE());
2222
2223    // Test 3: reading by alternative forms
2224    EnumValidityAccessors.Alt alt =
2225        MessageNano.mergeFrom(new EnumValidityAccessors.Alt(), serialized);
2226    assertEquals(EnumValidityAccessors.BAR, // last valid value in m.repeatedE2
2227        alt.getRepeatedE2AsOptional());
2228  }
2229
2230  /**
2231   * Tests that code generation correctly wraps a single message into its outer
2232   * class. The class {@code SingleMessageNano} is imported from the outer
2233   * class {@code UnittestSingleNano}, whose name is implicit. Any error would
2234   * cause this method to fail compilation.
2235   */
2236  public void testNanoSingle() throws Exception {
2237    SingleMessageNano msg = new SingleMessageNano();
2238    assertNotNull(msg);
2239  }
2240
2241  /**
2242   * Tests that code generation correctly skips generating the outer class if
2243   * unnecessary, letting a file-scope entity have the same name. The class
2244   * {@code MultipleNameClashNano} shares the same name with the file's outer
2245   * class defined explicitly, but the file contains no other entities and has
2246   * java_multiple_files set. Any error would cause this method to fail
2247   * compilation.
2248   */
2249  public void testNanoMultipleNameClash() throws Exception {
2250    MultipleNameClashNano msg = new MultipleNameClashNano();
2251    msg.field = 0;
2252  }
2253
2254  /**
2255   * Tests that code generation correctly handles enums in different scopes in
2256   * a source file with the option java_multiple_files set to true. Any error
2257   * would cause this method to fail compilation.
2258   */
2259  public void testNanoMultipleEnumScoping() throws Exception {
2260    FileScopeEnumRefNano msg1 = new FileScopeEnumRefNano();
2261    msg1.enumField = UnittestMultipleNano.ONE;
2262    MessageScopeEnumRefNano msg2 = new MessageScopeEnumRefNano();
2263    msg2.enumField = MessageScopeEnumRefNano.TWO;
2264  }
2265
2266  /**
2267   * Tests that code generation with mixed values of the java_multiple_files
2268   * options between the main source file and the imported source files would
2269   * generate correct references. Any error would cause this method to fail
2270   * compilation.
2271   */
2272  public void testNanoMultipleImportingNonMultiple() throws Exception {
2273    UnittestImportNano.ImportMessageNano importMsg = new UnittestImportNano.ImportMessageNano();
2274    MultipleImportingNonMultipleNano1 nano1 = new MultipleImportingNonMultipleNano1();
2275    nano1.field = importMsg;
2276    MultipleImportingNonMultipleNano2 nano2 = new MultipleImportingNonMultipleNano2();
2277    nano2.nano1 = nano1;
2278  }
2279
2280  public void testNanoDefaults() throws Exception {
2281    TestAllTypesNano msg = new TestAllTypesNano();
2282    for (int i = 0; i < 2; i++) {
2283      assertEquals(41, msg.defaultInt32);
2284      assertEquals(42, msg.defaultInt64);
2285      assertEquals(43, msg.defaultUint32);
2286      assertEquals(44, msg.defaultUint64);
2287      assertEquals(-45, msg.defaultSint32);
2288      assertEquals(46, msg.defaultSint64);
2289      assertEquals(47, msg.defaultFixed32);
2290      assertEquals(48, msg.defaultFixed64);
2291      assertEquals(49, msg.defaultSfixed32);
2292      assertEquals(-50, msg.defaultSfixed64);
2293      assertTrue(51.5f == msg.defaultFloat);
2294      assertTrue(52.0e3 == msg.defaultDouble);
2295      assertEquals(true, msg.defaultBool);
2296      assertEquals("hello", msg.defaultString);
2297      assertEquals("world", new String(msg.defaultBytes, "UTF-8"));
2298      assertEquals("dünya", msg.defaultStringNonascii);
2299      assertEquals("dünyab", new String(msg.defaultBytesNonascii, "UTF-8"));
2300      assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum);
2301      assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum);
2302      assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum);
2303      assertEquals(Float.POSITIVE_INFINITY, msg.defaultFloatInf);
2304      assertEquals(Float.NEGATIVE_INFINITY, msg.defaultFloatNegInf);
2305      assertEquals(Float.NaN, msg.defaultFloatNan);
2306      assertEquals(Double.POSITIVE_INFINITY, msg.defaultDoubleInf);
2307      assertEquals(Double.NEGATIVE_INFINITY, msg.defaultDoubleNegInf);
2308      assertEquals(Double.NaN, msg.defaultDoubleNan);
2309
2310      // Default values are not output, except for required fields.
2311      byte [] result = MessageNano.toByteArray(msg);
2312      int msgSerializedSize = msg.getSerializedSize();
2313      //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
2314      assertTrue(msgSerializedSize == 3);
2315      assertEquals(result.length, msgSerializedSize);
2316      msg.clear();
2317    }
2318  }
2319
2320  public void testNanoWithHasParseFrom() throws Exception {
2321    TestAllTypesNanoHas msg = null;
2322    // Test false on creation, after clear and upon empty parse.
2323    for (int i = 0; i < 3; i++) {
2324      if (i == 0) {
2325        msg = new TestAllTypesNanoHas();
2326      } else if (i == 1) {
2327        msg.clear();
2328      } else if (i == 2) {
2329        msg = TestAllTypesNanoHas.parseFrom(new byte[0]);
2330      }
2331      assertFalse(msg.hasOptionalInt32);
2332      assertFalse(msg.hasOptionalString);
2333      assertFalse(msg.hasOptionalBytes);
2334      assertFalse(msg.hasOptionalNestedEnum);
2335      assertFalse(msg.hasDefaultInt32);
2336      assertFalse(msg.hasDefaultString);
2337      assertFalse(msg.hasDefaultBytes);
2338      assertFalse(msg.hasDefaultFloatNan);
2339      assertFalse(msg.hasDefaultNestedEnum);
2340      assertFalse(msg.hasId);
2341      assertFalse(msg.hasRequiredEnum);
2342      msg.optionalInt32 = 123;
2343      msg.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage();
2344      msg.optionalNestedMessage.bb = 2;
2345      msg.optionalNestedEnum = TestAllTypesNano.BAZ;
2346    }
2347
2348    byte [] result = MessageNano.toByteArray(msg);
2349    int msgSerializedSize = msg.getSerializedSize();
2350    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
2351    assertTrue(msgSerializedSize == 10);
2352    assertEquals(result.length, msgSerializedSize);
2353
2354    // Has fields true upon parse.
2355    TestAllTypesNanoHas newMsg = TestAllTypesNanoHas.parseFrom(result);
2356    assertEquals(123, newMsg.optionalInt32);
2357    assertTrue(newMsg.hasOptionalInt32);
2358    assertEquals(2, newMsg.optionalNestedMessage.bb);
2359    assertTrue(newMsg.optionalNestedMessage.hasBb);
2360    assertEquals(TestAllTypesNanoHas.BAZ, newMsg.optionalNestedEnum);
2361    assertTrue(newMsg.hasOptionalNestedEnum);
2362  }
2363
2364  public void testNanoWithHasSerialize() throws Exception {
2365    TestAllTypesNanoHas msg = new TestAllTypesNanoHas();
2366    msg.hasOptionalInt32 = true;
2367    msg.hasOptionalString = true;
2368    msg.hasOptionalBytes = true;
2369    msg.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage();
2370    msg.optionalNestedMessage.hasBb = true;
2371    msg.hasOptionalNestedEnum = true;
2372    msg.hasDefaultInt32 = true;
2373    msg.hasDefaultString = true;
2374    msg.hasDefaultBytes = true;
2375    msg.hasDefaultFloatNan = true;
2376    msg.hasDefaultNestedEnum = true;
2377    msg.hasId = true;
2378    msg.hasRequiredEnum = true;
2379
2380    byte [] result = MessageNano.toByteArray(msg);
2381    int msgSerializedSize = msg.getSerializedSize();
2382    assertEquals(result.length, msgSerializedSize);
2383
2384    // Now deserialize and find that all fields are set and equal to their defaults.
2385    TestAllTypesNanoHas newMsg = TestAllTypesNanoHas.parseFrom(result);
2386    assertTrue(newMsg.hasOptionalInt32);
2387    assertTrue(newMsg.hasOptionalString);
2388    assertTrue(newMsg.hasOptionalBytes);
2389    assertTrue(newMsg.optionalNestedMessage.hasBb);
2390    assertTrue(newMsg.hasOptionalNestedEnum);
2391    assertTrue(newMsg.hasDefaultInt32);
2392    assertTrue(newMsg.hasDefaultString);
2393    assertTrue(newMsg.hasDefaultBytes);
2394    assertTrue(newMsg.hasDefaultFloatNan);
2395    assertTrue(newMsg.hasDefaultNestedEnum);
2396    assertTrue(newMsg.hasId);
2397    assertTrue(newMsg.hasRequiredEnum);
2398    assertEquals(0, newMsg.optionalInt32);
2399    assertEquals(0, newMsg.optionalString.length());
2400    assertEquals(0, newMsg.optionalBytes.length);
2401    assertEquals(0, newMsg.optionalNestedMessage.bb);
2402    assertEquals(TestAllTypesNanoHas.FOO, newMsg.optionalNestedEnum);
2403    assertEquals(41, newMsg.defaultInt32);
2404    assertEquals("hello", newMsg.defaultString);
2405    assertEquals("world", new String(newMsg.defaultBytes, "UTF-8"));
2406    assertEquals(TestAllTypesNanoHas.BAR, newMsg.defaultNestedEnum);
2407    assertEquals(Float.NaN, newMsg.defaultFloatNan);
2408    assertEquals(0, newMsg.id);
2409    assertEquals(TestAllTypesNanoHas.FOO, newMsg.requiredEnum);
2410  }
2411
2412  public void testNanoWithAccessorsBasic() throws Exception {
2413    TestNanoAccessors msg = new TestNanoAccessors();
2414
2415    // Makes sure required, repeated, and message fields are still public
2416    msg.id = 3;
2417    msg.repeatedBytes = new byte[2][3];
2418    msg.optionalNestedMessage = null;
2419
2420    // Test accessors
2421    assertEquals(0, msg.getOptionalInt32());
2422    assertFalse(msg.hasOptionalInt32());
2423    msg.setOptionalInt32(135);
2424    assertEquals(135, msg.getOptionalInt32());
2425    assertTrue(msg.hasOptionalInt32());
2426    msg.clearOptionalInt32();
2427    assertFalse(msg.hasOptionalInt32());
2428    msg.setOptionalInt32(0); // default value
2429    assertTrue(msg.hasOptionalInt32());
2430
2431    // Test NPE
2432    try {
2433      msg.setOptionalBytes(null);
2434      fail();
2435    } catch (NullPointerException expected) {}
2436    try {
2437      msg.setOptionalString(null);
2438      fail();
2439    } catch (NullPointerException expected) {}
2440
2441    // Test has bit on bytes field with defaults and clear() re-clones the default array
2442    assertFalse(msg.hasDefaultBytes());
2443    byte[] defaultBytes = msg.getDefaultBytes();
2444    msg.setDefaultBytes(defaultBytes);
2445    assertTrue(msg.hasDefaultBytes());
2446    msg.clearDefaultBytes();
2447    assertFalse(msg.hasDefaultBytes());
2448    defaultBytes[0]++; // modify original array
2449    assertFalse(Arrays.equals(defaultBytes, msg.getDefaultBytes()));
2450
2451    // Test has bits that require additional bit fields
2452    assertFalse(msg.hasBitFieldCheck());
2453    msg.setBitFieldCheck(0);
2454    assertTrue(msg.hasBitFieldCheck());
2455    assertFalse(msg.hasBeforeBitFieldCheck()); // checks bit field does not leak
2456    assertFalse(msg.hasAfterBitFieldCheck());
2457
2458    // Test clear() clears has bits
2459    msg.setOptionalString("hi");
2460    msg.setDefaultString("there");
2461    msg.clear();
2462    assertFalse(msg.hasOptionalString());
2463    assertFalse(msg.hasDefaultString());
2464    assertFalse(msg.hasBitFieldCheck());
2465
2466    // Test set() and clear() returns itself (compiles = success)
2467    msg.clear()
2468        .setOptionalInt32(3)
2469        .clearDefaultBytes()
2470        .setOptionalString("4");
2471  }
2472
2473  public void testNanoWithAccessorsParseFrom() throws Exception {
2474    TestNanoAccessors msg = null;
2475    // Test false on creation, after clear and upon empty parse.
2476    for (int i = 0; i < 3; i++) {
2477      if (i == 0) {
2478        msg = new TestNanoAccessors();
2479      } else if (i == 1) {
2480        msg.clear();
2481      } else if (i == 2) {
2482        msg = TestNanoAccessors.parseFrom(new byte[0]);
2483      }
2484      assertFalse(msg.hasOptionalInt32());
2485      assertFalse(msg.hasOptionalString());
2486      assertFalse(msg.hasOptionalBytes());
2487      assertFalse(msg.hasOptionalNestedEnum());
2488      assertFalse(msg.hasDefaultInt32());
2489      assertFalse(msg.hasDefaultString());
2490      assertFalse(msg.hasDefaultBytes());
2491      assertFalse(msg.hasDefaultFloatNan());
2492      assertFalse(msg.hasDefaultNestedEnum());
2493      msg.optionalNestedMessage = new TestNanoAccessors.NestedMessage();
2494      msg.optionalNestedMessage.setBb(2);
2495      msg.setOptionalNestedEnum(TestNanoAccessors.BAZ);
2496      msg.setDefaultInt32(msg.getDefaultInt32());
2497    }
2498
2499    byte [] result = MessageNano.toByteArray(msg);
2500    int msgSerializedSize = msg.getSerializedSize();
2501    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
2502    assertTrue(msgSerializedSize == 14);
2503    assertEquals(result.length, msgSerializedSize);
2504
2505    // Has fields true upon parse.
2506    TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(result);
2507    assertEquals(2, newMsg.optionalNestedMessage.getBb());
2508    assertTrue(newMsg.optionalNestedMessage.hasBb());
2509    assertEquals(TestNanoAccessors.BAZ, newMsg.getOptionalNestedEnum());
2510    assertTrue(newMsg.hasOptionalNestedEnum());
2511
2512    // Has field true on fields with explicit default values from wire.
2513    assertTrue(newMsg.hasDefaultInt32());
2514    assertEquals(41, newMsg.getDefaultInt32());
2515  }
2516
2517  public void testNanoWithAccessorsPublicFieldTypes() throws Exception {
2518    TestNanoAccessors msg = new TestNanoAccessors();
2519    assertNull(msg.optionalNestedMessage);
2520    assertEquals(0, msg.id);
2521    assertEquals(0, msg.repeatedNestedEnum.length);
2522
2523    TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(MessageNano.toByteArray(msg));
2524    assertNull(newMsg.optionalNestedMessage);
2525    assertEquals(0, newMsg.id);
2526    assertEquals(0, newMsg.repeatedNestedEnum.length);
2527
2528    TestNanoAccessors.NestedMessage nestedMessage = new TestNanoAccessors.NestedMessage();
2529    nestedMessage.setBb(5);
2530    newMsg.optionalNestedMessage = nestedMessage;
2531    newMsg.id = -1;
2532    newMsg.repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
2533
2534    TestNanoAccessors newMsg2 = TestNanoAccessors.parseFrom(MessageNano.toByteArray(newMsg));
2535    assertEquals(nestedMessage.getBb(), newMsg2.optionalNestedMessage.getBb());
2536    assertEquals(-1, newMsg2.id);
2537    assertEquals(TestAllTypesNano.FOO, newMsg2.repeatedNestedEnum[0]);
2538
2539    newMsg2.optionalNestedMessage = null;
2540    newMsg2.id = 0;
2541    newMsg2.repeatedNestedEnum = null;
2542
2543    TestNanoAccessors newMsg3 = TestNanoAccessors.parseFrom(MessageNano.toByteArray(newMsg2));
2544    assertNull(newMsg3.optionalNestedMessage);
2545    assertEquals(0, newMsg3.id);
2546    assertEquals(0, newMsg3.repeatedNestedEnum.length);
2547  }
2548
2549  public void testNanoWithAccessorsSerialize() throws Exception {
2550    TestNanoAccessors msg = new TestNanoAccessors();
2551    msg.setOptionalInt32(msg.getOptionalInt32());
2552    msg.setOptionalString(msg.getOptionalString());
2553    msg.setOptionalBytes(msg.getOptionalBytes());
2554    TestNanoAccessors.NestedMessage nestedMessage = new TestNanoAccessors.NestedMessage();
2555    nestedMessage.setBb(nestedMessage.getBb());
2556    msg.optionalNestedMessage = nestedMessage;
2557    msg.setOptionalNestedEnum(msg.getOptionalNestedEnum());
2558    msg.setDefaultInt32(msg.getDefaultInt32());
2559    msg.setDefaultString(msg.getDefaultString());
2560    msg.setDefaultBytes(msg.getDefaultBytes());
2561    msg.setDefaultFloatNan(msg.getDefaultFloatNan());
2562    msg.setDefaultNestedEnum(msg.getDefaultNestedEnum());
2563
2564    byte [] result = MessageNano.toByteArray(msg);
2565    int msgSerializedSize = msg.getSerializedSize();
2566    assertEquals(result.length, msgSerializedSize);
2567
2568    // Now deserialize and find that all fields are set and equal to their defaults.
2569    TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(result);
2570    assertTrue(newMsg.hasOptionalInt32());
2571    assertTrue(newMsg.hasOptionalString());
2572    assertTrue(newMsg.hasOptionalBytes());
2573    assertTrue(newMsg.optionalNestedMessage.hasBb());
2574    assertTrue(newMsg.hasOptionalNestedEnum());
2575    assertTrue(newMsg.hasDefaultInt32());
2576    assertTrue(newMsg.hasDefaultString());
2577    assertTrue(newMsg.hasDefaultBytes());
2578    assertTrue(newMsg.hasDefaultFloatNan());
2579    assertTrue(newMsg.hasDefaultNestedEnum());
2580    assertEquals(0, newMsg.getOptionalInt32());
2581    assertEquals(0, newMsg.getOptionalString().length());
2582    assertEquals(0, newMsg.getOptionalBytes().length);
2583    assertEquals(0, newMsg.optionalNestedMessage.getBb());
2584    assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum());
2585    assertEquals(41, newMsg.getDefaultInt32());
2586    assertEquals("hello", newMsg.getDefaultString());
2587    assertEquals("world", new String(newMsg.getDefaultBytes(), "UTF-8"));
2588    assertEquals(TestNanoAccessors.BAR, newMsg.getDefaultNestedEnum());
2589    assertEquals(Float.NaN, newMsg.getDefaultFloatNan());
2590    assertEquals(0, newMsg.id);
2591  }
2592
2593  public void testNanoJavaEnumStyle() throws Exception {
2594    EnumClassNanos.EnumClassNano msg = new EnumClassNanos.EnumClassNano();
2595    assertEquals(EnumClassNanos.FileScopeEnum.ONE, msg.one);
2596    assertEquals(EnumClassNanos.EnumClassNano.MessageScopeEnum.TWO, msg.two);
2597
2598    EnumClassNanoMultiple msg2 = new EnumClassNanoMultiple();
2599    assertEquals(FileScopeEnumMultiple.THREE, msg2.three);
2600    assertEquals(EnumClassNanoMultiple.MessageScopeEnumMultiple.FOUR, msg2.four);
2601  }
2602
2603  /**
2604   * Tests that fields with a default value of NaN are not serialized when
2605   * set to NaN. This is a special case as NaN != NaN, so normal equality
2606   * checks don't work.
2607   */
2608  public void testNanoNotANumberDefaults() throws Exception {
2609    TestAllTypesNano msg = new TestAllTypesNano();
2610    msg.defaultDoubleNan = 0;
2611    msg.defaultFloatNan = 0;
2612    byte[] result = MessageNano.toByteArray(msg);
2613    int msgSerializedSize = msg.getSerializedSize();
2614    assertTrue(result.length == msgSerializedSize);
2615    assertTrue(msgSerializedSize > 3);
2616
2617    msg.defaultDoubleNan = Double.NaN;
2618    msg.defaultFloatNan = Float.NaN;
2619    result = MessageNano.toByteArray(msg);
2620    msgSerializedSize = msg.getSerializedSize();
2621    assertEquals(3, result.length);
2622    assertEquals(3, msgSerializedSize);
2623  }
2624
2625  /**
2626   * Test that a bug in skipRawBytes() has been fixed:  if the skip skips
2627   * exactly up to a limit, this should not break things.
2628   */
2629  public void testSkipRawBytesBug() throws Exception {
2630    byte[] rawBytes = new byte[] { 1, 2 };
2631    CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(rawBytes);
2632
2633    int limit = input.pushLimit(1);
2634    input.skipRawBytes(1);
2635    input.popLimit(limit);
2636    assertEquals(2, input.readRawByte());
2637  }
2638
2639  /**
2640   * Test that a bug in skipRawBytes() has been fixed:  if the skip skips
2641   * past the end of a buffer with a limit that has been set past the end of
2642   * that buffer, this should not break things.
2643   */
2644  public void testSkipRawBytesPastEndOfBufferWithLimit() throws Exception {
2645    byte[] rawBytes = new byte[] { 1, 2, 3, 4, 5 };
2646    CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(rawBytes);
2647
2648    int limit = input.pushLimit(4);
2649    // In order to expose the bug we need to read at least one byte to prime the
2650    // buffer inside the CodedInputStream.
2651    assertEquals(1, input.readRawByte());
2652    // Skip to the end of the limit.
2653    input.skipRawBytes(3);
2654    assertTrue(input.isAtEnd());
2655    input.popLimit(limit);
2656    assertEquals(5, input.readRawByte());
2657  }
2658
2659  // Test a smattering of various proto types for printing
2660  public void testMessageNanoPrinter() {
2661    TestAllTypesNano msg = new TestAllTypesNano();
2662    msg.optionalInt32 = 14;
2663    msg.optionalFloat = 42.3f;
2664    msg.optionalString = "String \"with' both quotes";
2665    msg.optionalBytes = new byte[] {'"', '\0', 1, 8};
2666    msg.optionalGroup = new TestAllTypesNano.OptionalGroup();
2667    msg.optionalGroup.a = 15;
2668    msg.repeatedInt64 = new long[2];
2669    msg.repeatedInt64[0] = 1L;
2670    msg.repeatedInt64[1] = -1L;
2671    msg.repeatedBytes = new byte[2][];
2672    msg.repeatedBytes[1] = new byte[] {'h', 'e', 'l', 'l', 'o'};
2673    msg.repeatedGroup = new TestAllTypesNano.RepeatedGroup[2];
2674    msg.repeatedGroup[0] = new TestAllTypesNano.RepeatedGroup();
2675    msg.repeatedGroup[0].a = -27;
2676    msg.repeatedGroup[1] = new TestAllTypesNano.RepeatedGroup();
2677    msg.repeatedGroup[1].a = -72;
2678    msg.optionalNestedMessage = new TestAllTypesNano.NestedMessage();
2679    msg.optionalNestedMessage.bb = 7;
2680    msg.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[2];
2681    msg.repeatedNestedMessage[0] = new TestAllTypesNano.NestedMessage();
2682    msg.repeatedNestedMessage[0].bb = 77;
2683    msg.repeatedNestedMessage[1] = new TestAllTypesNano.NestedMessage();
2684    msg.repeatedNestedMessage[1].bb = 88;
2685    msg.optionalNestedEnum = TestAllTypesNano.BAZ;
2686    msg.repeatedNestedEnum = new int[2];
2687    msg.repeatedNestedEnum[0] = TestAllTypesNano.BAR;
2688    msg.repeatedNestedEnum[1] = TestAllTypesNano.FOO;
2689    msg.repeatedStringPiece = new String[] {null, "world"};
2690
2691    String protoPrint = msg.toString();
2692    assertTrue(protoPrint.contains("optional_int32: 14"));
2693    assertTrue(protoPrint.contains("optional_float: 42.3"));
2694    assertTrue(protoPrint.contains("optional_double: 0.0"));
2695    assertTrue(protoPrint.contains("optional_string: \"String \\u0022with\\u0027 both quotes\""));
2696    assertTrue(protoPrint.contains("optional_bytes: \"\\\"\\000\\001\\010\""));
2697    assertTrue(protoPrint.contains("optional_group <\n  a: 15\n>"));
2698
2699    assertTrue(protoPrint.contains("repeated_int64: 1\nrepeated_int64: -1"));
2700    assertFalse(protoPrint.contains("repeated_bytes: \"\"")); // null should be dropped
2701    assertTrue(protoPrint.contains("repeated_bytes: \"hello\""));
2702    assertTrue(protoPrint.contains("repeated_group <\n  a: -27\n>\n"
2703            + "repeated_group <\n  a: -72\n>"));
2704    assertTrue(protoPrint.contains("optional_nested_message <\n  bb: 7\n>"));
2705    assertTrue(protoPrint.contains("repeated_nested_message <\n  bb: 77\n>\n"
2706            + "repeated_nested_message <\n  bb: 88\n>"));
2707    assertTrue(protoPrint.contains("optional_nested_enum: 3"));
2708    assertTrue(protoPrint.contains("repeated_nested_enum: 2\nrepeated_nested_enum: 1"));
2709    assertTrue(protoPrint.contains("default_int32: 41"));
2710    assertTrue(protoPrint.contains("default_string: \"hello\""));
2711    assertFalse(protoPrint.contains("repeated_string_piece: \"\""));  // null should be dropped
2712    assertTrue(protoPrint.contains("repeated_string_piece: \"world\""));
2713  }
2714
2715  public void testMessageNanoPrinterAccessors() throws Exception {
2716    TestNanoAccessors msg = new TestNanoAccessors();
2717    msg.setOptionalInt32(13);
2718    msg.setOptionalString("foo");
2719    msg.setOptionalBytes(new byte[] {'"', '\0', 1, 8});
2720    msg.optionalNestedMessage = new TestNanoAccessors.NestedMessage();
2721    msg.optionalNestedMessage.setBb(7);
2722    msg.setOptionalNestedEnum(TestNanoAccessors.BAZ);
2723    msg.repeatedInt32 = new int[] { 1, -1 };
2724    msg.repeatedString = new String[] { "Hello", "world" };
2725    msg.repeatedBytes = new byte[2][];
2726    msg.repeatedBytes[1] = new byte[] {'h', 'e', 'l', 'l', 'o'};
2727    msg.repeatedNestedMessage = new TestNanoAccessors.NestedMessage[2];
2728    msg.repeatedNestedMessage[0] = new TestNanoAccessors.NestedMessage();
2729    msg.repeatedNestedMessage[0].setBb(5);
2730    msg.repeatedNestedMessage[1] = new TestNanoAccessors.NestedMessage();
2731    msg.repeatedNestedMessage[1].setBb(6);
2732    msg.repeatedNestedEnum = new int[] { TestNanoAccessors.FOO, TestNanoAccessors.BAR };
2733    msg.id = 33;
2734
2735    String protoPrint = msg.toString();
2736    assertTrue(protoPrint.contains("optional_int32: 13"));
2737    assertTrue(protoPrint.contains("optional_string: \"foo\""));
2738    assertTrue(protoPrint.contains("optional_bytes: \"\\\"\\000\\001\\010\""));
2739    assertTrue(protoPrint.contains("optional_nested_message <\n  bb: 7\n>"));
2740    assertTrue(protoPrint.contains("optional_nested_enum: 3"));
2741    assertTrue(protoPrint.contains("repeated_int32: 1\nrepeated_int32: -1"));
2742    assertTrue(protoPrint.contains("repeated_string: \"Hello\"\nrepeated_string: \"world\""));
2743    assertFalse(protoPrint.contains("repeated_bytes: \"\"")); // null should be dropped
2744    assertTrue(protoPrint.contains("repeated_bytes: \"hello\""));
2745    assertTrue(protoPrint.contains("repeated_nested_message <\n  bb: 5\n>\n"
2746            + "repeated_nested_message <\n  bb: 6\n>"));
2747    assertTrue(protoPrint.contains("repeated_nested_enum: 1\nrepeated_nested_enum: 2"));
2748    assertTrue(protoPrint.contains("id: 33"));
2749  }
2750
2751  public void testExtensions() throws Exception {
2752    Extensions.ExtendableMessage message = new Extensions.ExtendableMessage();
2753    message.field = 5;
2754    int[] int32s = {1, 2};
2755    int[] uint32s = {3, 4};
2756    int[] sint32s = {-5, -6};
2757    long[] int64s = {7, 8};
2758    long[] uint64s = {9, 10};
2759    long[] sint64s = {-11, -12};
2760    int[] fixed32s = {13, 14};
2761    int[] sfixed32s = {-15, -16};
2762    long[] fixed64s = {17, 18};
2763    long[] sfixed64s = {-19, -20};
2764    boolean[] bools = {true, false};
2765    float[] floats = {2.1f, 2.2f};
2766    double[] doubles = {2.3, 2.4};
2767    int[] enums = {Extensions.SECOND_VALUE, Extensions.FIRST_VALUE};
2768    String[] strings = {"vijfentwintig", "twenty-six"};
2769    byte[][] bytess = {{2, 7}, {2, 8}};
2770    AnotherMessage another1 = new AnotherMessage();
2771    another1.string = "er shi jiu";
2772    another1.value = false;
2773    AnotherMessage another2 = new AnotherMessage();
2774    another2.string = "trente";
2775    another2.value = true;
2776    AnotherMessage[] messages = {another1, another2};
2777    RepeatedExtensions.RepeatedGroup group1 = new RepeatedExtensions.RepeatedGroup();
2778    group1.a = 31;
2779    RepeatedExtensions.RepeatedGroup group2 = new RepeatedExtensions.RepeatedGroup();
2780    group2.a = 32;
2781    RepeatedExtensions.RepeatedGroup[] groups = {group1, group2};
2782    message.setExtension(RepeatedExtensions.repeatedInt32, int32s);
2783    message.setExtension(RepeatedExtensions.repeatedUint32, uint32s);
2784    message.setExtension(RepeatedExtensions.repeatedSint32, sint32s);
2785    message.setExtension(RepeatedExtensions.repeatedInt64, int64s);
2786    message.setExtension(RepeatedExtensions.repeatedUint64, uint64s);
2787    message.setExtension(RepeatedExtensions.repeatedSint64, sint64s);
2788    message.setExtension(RepeatedExtensions.repeatedFixed32, fixed32s);
2789    message.setExtension(RepeatedExtensions.repeatedSfixed32, sfixed32s);
2790    message.setExtension(RepeatedExtensions.repeatedFixed64, fixed64s);
2791    message.setExtension(RepeatedExtensions.repeatedSfixed64, sfixed64s);
2792    message.setExtension(RepeatedExtensions.repeatedBool, bools);
2793    message.setExtension(RepeatedExtensions.repeatedFloat, floats);
2794    message.setExtension(RepeatedExtensions.repeatedDouble, doubles);
2795    message.setExtension(RepeatedExtensions.repeatedEnum, enums);
2796    message.setExtension(RepeatedExtensions.repeatedString, strings);
2797    message.setExtension(RepeatedExtensions.repeatedBytes, bytess);
2798    message.setExtension(RepeatedExtensions.repeatedMessage, messages);
2799    message.setExtension(RepeatedExtensions.repeatedGroup, groups);
2800
2801    byte[] data = MessageNano.toByteArray(message);
2802    message = Extensions.ExtendableMessage.parseFrom(data);
2803    assertEquals(5, message.field);
2804
2805    // Test reading back using SingularExtensions: the retrieved value should equal the last
2806    // in each array.
2807    assertEquals(int32s[1], (int) message.getExtension(SingularExtensions.someInt32));
2808    assertEquals(uint32s[1], (int) message.getExtension(SingularExtensions.someUint32));
2809    assertEquals(sint32s[1], (int) message.getExtension(SingularExtensions.someSint32));
2810    assertEquals(int64s[1], (long) message.getExtension(SingularExtensions.someInt64));
2811    assertEquals(uint64s[1], (long) message.getExtension(SingularExtensions.someUint64));
2812    assertEquals(sint64s[1], (long) message.getExtension(SingularExtensions.someSint64));
2813    assertEquals(fixed32s[1], (int) message.getExtension(SingularExtensions.someFixed32));
2814    assertEquals(sfixed32s[1], (int) message.getExtension(SingularExtensions.someSfixed32));
2815    assertEquals(fixed64s[1], (long) message.getExtension(SingularExtensions.someFixed64));
2816    assertEquals(sfixed64s[1], (long) message.getExtension(SingularExtensions.someSfixed64));
2817    assertEquals(bools[1], (boolean) message.getExtension(SingularExtensions.someBool));
2818    assertEquals(floats[1], (float) message.getExtension(SingularExtensions.someFloat));
2819    assertEquals(doubles[1], (double) message.getExtension(SingularExtensions.someDouble));
2820    assertEquals(enums[1], (int) message.getExtension(SingularExtensions.someEnum));
2821    assertEquals(strings[1], message.getExtension(SingularExtensions.someString));
2822    assertTrue(Arrays.equals(bytess[1], message.getExtension(SingularExtensions.someBytes)));
2823    AnotherMessage deserializedMessage = message.getExtension(SingularExtensions.someMessage);
2824    assertEquals(another2.string, deserializedMessage.string);
2825    assertEquals(another2.value, deserializedMessage.value);
2826    assertEquals(group2.a, message.getExtension(SingularExtensions.someGroup).a);
2827
2828    // Test reading back using RepeatedExtensions: the arrays should be equal.
2829    assertTrue(Arrays.equals(int32s, message.getExtension(RepeatedExtensions.repeatedInt32)));
2830    assertTrue(Arrays.equals(uint32s, message.getExtension(RepeatedExtensions.repeatedUint32)));
2831    assertTrue(Arrays.equals(sint32s, message.getExtension(RepeatedExtensions.repeatedSint32)));
2832    assertTrue(Arrays.equals(int64s, message.getExtension(RepeatedExtensions.repeatedInt64)));
2833    assertTrue(Arrays.equals(uint64s, message.getExtension(RepeatedExtensions.repeatedUint64)));
2834    assertTrue(Arrays.equals(sint64s, message.getExtension(RepeatedExtensions.repeatedSint64)));
2835    assertTrue(Arrays.equals(fixed32s, message.getExtension(RepeatedExtensions.repeatedFixed32)));
2836    assertTrue(Arrays.equals(sfixed32s, message.getExtension(RepeatedExtensions.repeatedSfixed32)));
2837    assertTrue(Arrays.equals(fixed64s, message.getExtension(RepeatedExtensions.repeatedFixed64)));
2838    assertTrue(Arrays.equals(sfixed64s, message.getExtension(RepeatedExtensions.repeatedSfixed64)));
2839    assertTrue(Arrays.equals(bools, message.getExtension(RepeatedExtensions.repeatedBool)));
2840    assertTrue(Arrays.equals(floats, message.getExtension(RepeatedExtensions.repeatedFloat)));
2841    assertTrue(Arrays.equals(doubles, message.getExtension(RepeatedExtensions.repeatedDouble)));
2842    assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
2843    assertTrue(Arrays.equals(strings, message.getExtension(RepeatedExtensions.repeatedString)));
2844    byte[][] deserializedRepeatedBytes = message.getExtension(RepeatedExtensions.repeatedBytes);
2845    assertEquals(2, deserializedRepeatedBytes.length);
2846    assertTrue(Arrays.equals(bytess[0], deserializedRepeatedBytes[0]));
2847    assertTrue(Arrays.equals(bytess[1], deserializedRepeatedBytes[1]));
2848    AnotherMessage[] deserializedRepeatedMessage =
2849        message.getExtension(RepeatedExtensions.repeatedMessage);
2850    assertEquals(2, deserializedRepeatedMessage.length);
2851    assertEquals(another1.string, deserializedRepeatedMessage[0].string);
2852    assertEquals(another1.value, deserializedRepeatedMessage[0].value);
2853    assertEquals(another2.string, deserializedRepeatedMessage[1].string);
2854    assertEquals(another2.value, deserializedRepeatedMessage[1].value);
2855    RepeatedExtensions.RepeatedGroup[] deserializedRepeatedGroup =
2856        message.getExtension(RepeatedExtensions.repeatedGroup);
2857    assertEquals(2, deserializedRepeatedGroup.length);
2858    assertEquals(group1.a, deserializedRepeatedGroup[0].a);
2859    assertEquals(group2.a, deserializedRepeatedGroup[1].a);
2860
2861    // Test reading back using PackedExtensions: the arrays should be equal, even the fields
2862    // are non-packed.
2863    assertTrue(Arrays.equals(int32s, message.getExtension(PackedExtensions.packedInt32)));
2864    assertTrue(Arrays.equals(uint32s, message.getExtension(PackedExtensions.packedUint32)));
2865    assertTrue(Arrays.equals(sint32s, message.getExtension(PackedExtensions.packedSint32)));
2866    assertTrue(Arrays.equals(int64s, message.getExtension(PackedExtensions.packedInt64)));
2867    assertTrue(Arrays.equals(uint64s, message.getExtension(PackedExtensions.packedUint64)));
2868    assertTrue(Arrays.equals(sint64s, message.getExtension(PackedExtensions.packedSint64)));
2869    assertTrue(Arrays.equals(fixed32s, message.getExtension(PackedExtensions.packedFixed32)));
2870    assertTrue(Arrays.equals(sfixed32s, message.getExtension(PackedExtensions.packedSfixed32)));
2871    assertTrue(Arrays.equals(fixed64s, message.getExtension(PackedExtensions.packedFixed64)));
2872    assertTrue(Arrays.equals(sfixed64s, message.getExtension(PackedExtensions.packedSfixed64)));
2873    assertTrue(Arrays.equals(bools, message.getExtension(PackedExtensions.packedBool)));
2874    assertTrue(Arrays.equals(floats, message.getExtension(PackedExtensions.packedFloat)));
2875    assertTrue(Arrays.equals(doubles, message.getExtension(PackedExtensions.packedDouble)));
2876    assertTrue(Arrays.equals(enums, message.getExtension(PackedExtensions.packedEnum)));
2877
2878    // Now set the packable extension values using PackedExtensions so they're serialized packed.
2879    message.setExtension(PackedExtensions.packedInt32, int32s);
2880    message.setExtension(PackedExtensions.packedUint32, uint32s);
2881    message.setExtension(PackedExtensions.packedSint32, sint32s);
2882    message.setExtension(PackedExtensions.packedInt64, int64s);
2883    message.setExtension(PackedExtensions.packedUint64, uint64s);
2884    message.setExtension(PackedExtensions.packedSint64, sint64s);
2885    message.setExtension(PackedExtensions.packedFixed32, fixed32s);
2886    message.setExtension(PackedExtensions.packedSfixed32, sfixed32s);
2887    message.setExtension(PackedExtensions.packedFixed64, fixed64s);
2888    message.setExtension(PackedExtensions.packedSfixed64, sfixed64s);
2889    message.setExtension(PackedExtensions.packedBool, bools);
2890    message.setExtension(PackedExtensions.packedFloat, floats);
2891    message.setExtension(PackedExtensions.packedDouble, doubles);
2892    message.setExtension(PackedExtensions.packedEnum, enums);
2893
2894    // And read back using non-packed RepeatedExtensions.
2895    byte[] data2 = MessageNano.toByteArray(message);
2896    message = MessageNano.mergeFrom(new Extensions.ExtendableMessage(), data2);
2897    assertTrue(Arrays.equals(int32s, message.getExtension(RepeatedExtensions.repeatedInt32)));
2898    assertTrue(Arrays.equals(uint32s, message.getExtension(RepeatedExtensions.repeatedUint32)));
2899    assertTrue(Arrays.equals(sint32s, message.getExtension(RepeatedExtensions.repeatedSint32)));
2900    assertTrue(Arrays.equals(int64s, message.getExtension(RepeatedExtensions.repeatedInt64)));
2901    assertTrue(Arrays.equals(uint64s, message.getExtension(RepeatedExtensions.repeatedUint64)));
2902    assertTrue(Arrays.equals(sint64s, message.getExtension(RepeatedExtensions.repeatedSint64)));
2903    assertTrue(Arrays.equals(fixed32s, message.getExtension(RepeatedExtensions.repeatedFixed32)));
2904    assertTrue(Arrays.equals(sfixed32s, message.getExtension(RepeatedExtensions.repeatedSfixed32)));
2905    assertTrue(Arrays.equals(fixed64s, message.getExtension(RepeatedExtensions.repeatedFixed64)));
2906    assertTrue(Arrays.equals(sfixed64s, message.getExtension(RepeatedExtensions.repeatedSfixed64)));
2907    assertTrue(Arrays.equals(bools, message.getExtension(RepeatedExtensions.repeatedBool)));
2908    assertTrue(Arrays.equals(floats, message.getExtension(RepeatedExtensions.repeatedFloat)));
2909    assertTrue(Arrays.equals(doubles, message.getExtension(RepeatedExtensions.repeatedDouble)));
2910    assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
2911  }
2912
2913  public void testNullExtensions() throws Exception {
2914    // Check that clearing the extension on an empty message is a no-op.
2915    Extensions.ExtendableMessage message = new Extensions.ExtendableMessage();
2916    message.setExtension(SingularExtensions.someMessage, null);
2917    assertEquals(0, MessageNano.toByteArray(message).length);
2918
2919    // Check that the message is empty after setting and clearing an extension.
2920    AnotherMessage another = new AnotherMessage();
2921    message.setExtension(SingularExtensions.someMessage, another);
2922    assertTrue(MessageNano.toByteArray(message).length > 0);
2923    message.setExtension(SingularExtensions.someMessage, null);
2924    assertEquals(0, MessageNano.toByteArray(message).length);
2925  }
2926
2927  public void testUnknownFields() throws Exception {
2928    // Check that we roundtrip (serialize and deserialize) unrecognized fields.
2929    AnotherMessage message = new AnotherMessage();
2930    message.string = "Hello World";
2931    message.value = false;
2932
2933    byte[] bytes = MessageNano.toByteArray(message);
2934    int extraFieldSize = CodedOutputStream.computeStringSize(1001, "This is an unknown field");
2935    byte[] newBytes = new byte[bytes.length + extraFieldSize];
2936    System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
2937    CodedOutputStream.newInstance(newBytes, bytes.length, extraFieldSize).writeString(1001,
2938        "This is an unknown field");
2939
2940    // Deserialize with an unknown field.
2941    AnotherMessage deserialized = AnotherMessage.parseFrom(newBytes);
2942    byte[] serialized = MessageNano.toByteArray(deserialized);
2943
2944    assertEquals(newBytes.length, serialized.length);
2945
2946    // Clear, and make sure it clears everything.
2947    deserialized.clear();
2948    assertEquals(0, MessageNano.toByteArray(deserialized).length);
2949  }
2950
2951  public void testMergeFrom() throws Exception {
2952    SimpleMessageNano message = new SimpleMessageNano();
2953    message.d = 123;
2954    byte[] bytes = MessageNano.toByteArray(message);
2955
2956    SimpleMessageNano newMessage = MessageNano.mergeFrom(new SimpleMessageNano(), bytes);
2957    assertEquals(message.d, newMessage.d);
2958  }
2959
2960  public void testJavaKeyword() throws Exception {
2961    TestAllTypesNano msg = new TestAllTypesNano();
2962    msg.synchronized_ = 123;
2963    assertEquals(123, msg.synchronized_);
2964  }
2965
2966  public void testReferenceTypesForPrimitives() throws Exception {
2967    NanoReferenceTypes.TestAllTypesNano message = new NanoReferenceTypes.TestAllTypesNano();
2968
2969    // Base check - when nothing is set, we serialize nothing.
2970    assertHasWireData(message, false);
2971
2972    message.defaultBool = true;
2973    assertHasWireData(message, true);
2974
2975    message.defaultBool = false;
2976    assertHasWireData(message, true);
2977
2978    message.defaultBool = null;
2979    assertHasWireData(message, false);
2980
2981    message.defaultInt32 = 5;
2982    assertHasWireData(message, true);
2983
2984    message.defaultInt32 = null;
2985    assertHasWireData(message, false);
2986
2987    message.defaultInt64 = 123456L;
2988    assertHasWireData(message, true);
2989
2990    message.defaultInt64 = null;
2991    assertHasWireData(message, false);
2992
2993    message.defaultFloat = 1f;
2994    assertHasWireData(message, true);
2995
2996    message.defaultFloat = null;
2997    assertHasWireData(message, false);
2998
2999    message.defaultDouble = 2.1;
3000    assertHasWireData(message, true);
3001
3002    message.defaultDouble = null;
3003    assertHasWireData(message, false);
3004
3005    message.defaultString = "hello";
3006    assertHasWireData(message, true);
3007
3008    message.defaultString = null;
3009    assertHasWireData(message, false);
3010
3011    message.defaultBytes = new byte[] { 1, 2, 3 };
3012    assertHasWireData(message, true);
3013
3014    message.defaultBytes = null;
3015    assertHasWireData(message, false);
3016  }
3017
3018  public void testHashCodeEquals() throws Exception {
3019    // Complete equality:
3020    TestAllTypesNano a = createMessageForHashCodeEqualsTest();
3021    TestAllTypesNano aEquivalent = createMessageForHashCodeEqualsTest();
3022
3023    // Null and empty array for repeated fields equality:
3024    TestAllTypesNano b = createMessageForHashCodeEqualsTest();
3025    b.repeatedBool = null;
3026    b.repeatedFloat = new float[0];
3027    TestAllTypesNano bEquivalent = createMessageForHashCodeEqualsTest();
3028    bEquivalent.repeatedBool = new boolean[0];
3029    bEquivalent.repeatedFloat = null;
3030
3031    // Ref-element-type repeated fields use non-null subsequence equality:
3032    TestAllTypesNano c = createMessageForHashCodeEqualsTest();
3033    c.repeatedString = null;
3034    c.repeatedStringPiece = new String[] {null, "one", null, "two"};
3035    c.repeatedBytes = new byte[][] {{3, 4}, null};
3036    TestAllTypesNano cEquivalent = createMessageForHashCodeEqualsTest();
3037    cEquivalent.repeatedString = new String[3];
3038    cEquivalent.repeatedStringPiece = new String[] {"one", "two", null};
3039    cEquivalent.repeatedBytes = new byte[][] {{3, 4}};
3040
3041    // Complete equality for messages with has fields:
3042    TestAllTypesNanoHas d = createMessageWithHasForHashCodeEqualsTest();
3043    TestAllTypesNanoHas dEquivalent = createMessageWithHasForHashCodeEqualsTest();
3044
3045    // If has-fields exist, fields with the same default values but
3046    // different has-field values are different.
3047    TestAllTypesNanoHas e = createMessageWithHasForHashCodeEqualsTest();
3048    e.optionalInt32++; // make different from d
3049    e.hasDefaultString = false;
3050    TestAllTypesNanoHas eDifferent = createMessageWithHasForHashCodeEqualsTest();
3051    eDifferent.optionalInt32 = e.optionalInt32;
3052    eDifferent.hasDefaultString = true;
3053
3054    // Complete equality for messages with accessors:
3055    TestNanoAccessors f = createMessageWithAccessorsForHashCodeEqualsTest();
3056    TestNanoAccessors fEquivalent = createMessageWithAccessorsForHashCodeEqualsTest();
3057
3058    // If using accessors, explicitly setting a field to its default value
3059    // should make the message different.
3060    TestNanoAccessors g = createMessageWithAccessorsForHashCodeEqualsTest();
3061    g.setOptionalInt32(g.getOptionalInt32() + 1); // make different from f
3062    g.clearDefaultString();
3063    TestNanoAccessors gDifferent = createMessageWithAccessorsForHashCodeEqualsTest();
3064    gDifferent.setOptionalInt32(g.getOptionalInt32());
3065    gDifferent.setDefaultString(g.getDefaultString());
3066
3067    // Complete equality for reference typed messages:
3068    NanoReferenceTypes.TestAllTypesNano h = createRefTypedMessageForHashCodeEqualsTest();
3069    NanoReferenceTypes.TestAllTypesNano hEquivalent = createRefTypedMessageForHashCodeEqualsTest();
3070
3071    // Inequality of null and default value for reference typed messages:
3072    NanoReferenceTypes.TestAllTypesNano i = createRefTypedMessageForHashCodeEqualsTest();
3073    i.optionalInt32 = 1; // make different from h
3074    i.optionalFloat = null;
3075    NanoReferenceTypes.TestAllTypesNano iDifferent = createRefTypedMessageForHashCodeEqualsTest();
3076    iDifferent.optionalInt32 = i.optionalInt32;
3077    iDifferent.optionalFloat = 0.0f;
3078
3079    HashMap<MessageNano, String> hashMap = new HashMap<MessageNano, String>();
3080    hashMap.put(a, "a");
3081    hashMap.put(b, "b");
3082    hashMap.put(c, "c");
3083    hashMap.put(d, "d");
3084    hashMap.put(e, "e");
3085    hashMap.put(f, "f");
3086    hashMap.put(g, "g");
3087    hashMap.put(h, "h");
3088    hashMap.put(i, "i");
3089
3090    assertEquals(9, hashMap.size()); // a-i should be different from each other.
3091
3092    assertEquals("a", hashMap.get(a));
3093    assertEquals("a", hashMap.get(aEquivalent));
3094
3095    assertEquals("b", hashMap.get(b));
3096    assertEquals("b", hashMap.get(bEquivalent));
3097
3098    assertEquals("c", hashMap.get(c));
3099    assertEquals("c", hashMap.get(cEquivalent));
3100
3101    assertEquals("d", hashMap.get(d));
3102    assertEquals("d", hashMap.get(dEquivalent));
3103
3104    assertEquals("e", hashMap.get(e));
3105    assertNull(hashMap.get(eDifferent));
3106
3107    assertEquals("f", hashMap.get(f));
3108    assertEquals("f", hashMap.get(fEquivalent));
3109
3110    assertEquals("g", hashMap.get(g));
3111    assertNull(hashMap.get(gDifferent));
3112
3113    assertEquals("h", hashMap.get(h));
3114    assertEquals("h", hashMap.get(hEquivalent));
3115
3116    assertEquals("i", hashMap.get(i));
3117    assertNull(hashMap.get(iDifferent));
3118  }
3119
3120  private TestAllTypesNano createMessageForHashCodeEqualsTest() {
3121    TestAllTypesNano message = new TestAllTypesNano();
3122    message.optionalInt32 = 5;
3123    message.optionalInt64 = 777;
3124    message.optionalFloat = 1.0f;
3125    message.optionalDouble = 2.0;
3126    message.optionalBool = true;
3127    message.optionalString = "Hello";
3128    message.optionalBytes = new byte[] { 1, 2, 3 };
3129    message.optionalNestedMessage = new TestAllTypesNano.NestedMessage();
3130    message.optionalNestedMessage.bb = 27;
3131    message.optionalNestedEnum = TestAllTypesNano.BAR;
3132    message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
3133    message.repeatedInt64 = new long[] { 27L, 28L, 29L };
3134    message.repeatedFloat = new float[] { 5.0f, 6.0f };
3135    message.repeatedDouble = new double[] { 99.1, 22.5 };
3136    message.repeatedBool = new boolean[] { true, false, true };
3137    message.repeatedString = new String[] { "One", "Two" };
3138    message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
3139    message.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {
3140      message.optionalNestedMessage,
3141      message.optionalNestedMessage
3142    };
3143    message.repeatedNestedEnum = new int[] {
3144      TestAllTypesNano.BAR,
3145      TestAllTypesNano.BAZ
3146    };
3147    return message;
3148  }
3149
3150  private TestAllTypesNanoHas createMessageWithHasForHashCodeEqualsTest() {
3151    TestAllTypesNanoHas message = new TestAllTypesNanoHas();
3152    message.optionalInt32 = 5;
3153    message.optionalString = "Hello";
3154    message.optionalBytes = new byte[] { 1, 2, 3 };
3155    message.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage();
3156    message.optionalNestedMessage.bb = 27;
3157    message.optionalNestedEnum = TestAllTypesNano.BAR;
3158    message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
3159    message.repeatedString = new String[] { "One", "Two" };
3160    message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
3161    message.repeatedNestedMessage = new TestAllTypesNanoHas.NestedMessage[] {
3162      message.optionalNestedMessage,
3163      message.optionalNestedMessage
3164    };
3165    message.repeatedNestedEnum = new int[] {
3166      TestAllTypesNano.BAR,
3167      TestAllTypesNano.BAZ
3168    };
3169    return message;
3170  }
3171
3172  private TestNanoAccessors createMessageWithAccessorsForHashCodeEqualsTest() {
3173    TestNanoAccessors message = new TestNanoAccessors()
3174        .setOptionalInt32(5)
3175        .setOptionalString("Hello")
3176        .setOptionalBytes(new byte[] {1, 2, 3})
3177        .setOptionalNestedEnum(TestNanoAccessors.BAR);
3178    message.optionalNestedMessage = new TestNanoAccessors.NestedMessage().setBb(27);
3179    message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
3180    message.repeatedString = new String[] { "One", "Two" };
3181    message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
3182    message.repeatedNestedMessage = new TestNanoAccessors.NestedMessage[] {
3183      message.optionalNestedMessage,
3184      message.optionalNestedMessage
3185    };
3186    message.repeatedNestedEnum = new int[] {
3187      TestAllTypesNano.BAR,
3188      TestAllTypesNano.BAZ
3189    };
3190    return message;
3191  }
3192
3193  private NanoReferenceTypes.TestAllTypesNano createRefTypedMessageForHashCodeEqualsTest() {
3194    NanoReferenceTypes.TestAllTypesNano message = new NanoReferenceTypes.TestAllTypesNano();
3195    message.optionalInt32 = 5;
3196    message.optionalInt64 = 777L;
3197    message.optionalFloat = 1.0f;
3198    message.optionalDouble = 2.0;
3199    message.optionalBool = true;
3200    message.optionalString = "Hello";
3201    message.optionalBytes = new byte[] { 1, 2, 3 };
3202    message.optionalNestedMessage =
3203        new NanoReferenceTypes.TestAllTypesNano.NestedMessage();
3204    message.optionalNestedMessage.foo = 27;
3205    message.optionalNestedEnum = NanoReferenceTypes.TestAllTypesNano.BAR;
3206    message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
3207    message.repeatedInt64 = new long[] { 27L, 28L, 29L };
3208    message.repeatedFloat = new float[] { 5.0f, 6.0f };
3209    message.repeatedDouble = new double[] { 99.1, 22.5 };
3210    message.repeatedBool = new boolean[] { true, false, true };
3211    message.repeatedString = new String[] { "One", "Two" };
3212    message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
3213    message.repeatedNestedMessage =
3214        new NanoReferenceTypes.TestAllTypesNano.NestedMessage[] {
3215          message.optionalNestedMessage,
3216          message.optionalNestedMessage
3217        };
3218    message.repeatedNestedEnum = new int[] {
3219      NanoReferenceTypes.TestAllTypesNano.BAR,
3220      NanoReferenceTypes.TestAllTypesNano.BAZ
3221    };
3222    return message;
3223  }
3224
3225  public void testEqualsWithSpecialFloatingPointValues() throws Exception {
3226    // Checks that the nano implementation complies with Object.equals() when treating
3227    // floating point numbers, i.e. NaN == NaN and +0.0 != -0.0.
3228    // This test assumes that the generated equals() implementations are symmetric, so
3229    // there will only be one direction for each equality check.
3230
3231    TestAllTypesNano m1 = new TestAllTypesNano();
3232    m1.optionalFloat = Float.NaN;
3233    m1.optionalDouble = Double.NaN;
3234    TestAllTypesNano m2 = new TestAllTypesNano();
3235    m2.optionalFloat = Float.NaN;
3236    m2.optionalDouble = Double.NaN;
3237    assertTrue(m1.equals(m2));
3238    assertTrue(m1.equals(
3239        MessageNano.mergeFrom(new TestAllTypesNano(), MessageNano.toByteArray(m1))));
3240
3241    m1.optionalFloat = +0f;
3242    m2.optionalFloat = -0f;
3243    assertFalse(m1.equals(m2));
3244
3245    m1.optionalFloat = -0f;
3246    m1.optionalDouble = +0d;
3247    m2.optionalDouble = -0d;
3248    assertFalse(m1.equals(m2));
3249
3250    m1.optionalDouble = -0d;
3251    assertTrue(m1.equals(m2));
3252    assertFalse(m1.equals(new TestAllTypesNano())); // -0 does not equals() the default +0
3253    assertTrue(m1.equals(
3254        MessageNano.mergeFrom(new TestAllTypesNano(), MessageNano.toByteArray(m1))));
3255
3256    // -------
3257
3258    TestAllTypesNanoHas m3 = new TestAllTypesNanoHas();
3259    m3.optionalFloat = Float.NaN;
3260    m3.hasOptionalFloat = true;
3261    m3.optionalDouble = Double.NaN;
3262    m3.hasOptionalDouble = true;
3263    TestAllTypesNanoHas m4 = new TestAllTypesNanoHas();
3264    m4.optionalFloat = Float.NaN;
3265    m4.hasOptionalFloat = true;
3266    m4.optionalDouble = Double.NaN;
3267    m4.hasOptionalDouble = true;
3268    assertTrue(m3.equals(m4));
3269    assertTrue(m3.equals(
3270        MessageNano.mergeFrom(new TestAllTypesNanoHas(), MessageNano.toByteArray(m3))));
3271
3272    m3.optionalFloat = +0f;
3273    m4.optionalFloat = -0f;
3274    assertFalse(m3.equals(m4));
3275
3276    m3.optionalFloat = -0f;
3277    m3.optionalDouble = +0d;
3278    m4.optionalDouble = -0d;
3279    assertFalse(m3.equals(m4));
3280
3281    m3.optionalDouble = -0d;
3282    m3.hasOptionalFloat = false;  // -0 does not equals() the default +0,
3283    m3.hasOptionalDouble = false; // so these incorrect 'has' flags should be disregarded.
3284    assertTrue(m3.equals(m4));    // note: m4 has the 'has' flags set.
3285    assertFalse(m3.equals(new TestAllTypesNanoHas())); // note: the new message has +0 defaults
3286    assertTrue(m3.equals(
3287        MessageNano.mergeFrom(new TestAllTypesNanoHas(), MessageNano.toByteArray(m3))));
3288                                  // note: the deserialized message has the 'has' flags set.
3289
3290    // -------
3291
3292    TestNanoAccessors m5 = new TestNanoAccessors();
3293    m5.setOptionalFloat(Float.NaN);
3294    m5.setOptionalDouble(Double.NaN);
3295    TestNanoAccessors m6 = new TestNanoAccessors();
3296    m6.setOptionalFloat(Float.NaN);
3297    m6.setOptionalDouble(Double.NaN);
3298    assertTrue(m5.equals(m6));
3299    assertTrue(m5.equals(
3300        MessageNano.mergeFrom(new TestNanoAccessors(), MessageNano.toByteArray(m6))));
3301
3302    m5.setOptionalFloat(+0f);
3303    m6.setOptionalFloat(-0f);
3304    assertFalse(m5.equals(m6));
3305
3306    m5.setOptionalFloat(-0f);
3307    m5.setOptionalDouble(+0d);
3308    m6.setOptionalDouble(-0d);
3309    assertFalse(m5.equals(m6));
3310
3311    m5.setOptionalDouble(-0d);
3312    assertTrue(m5.equals(m6));
3313    assertFalse(m5.equals(new TestNanoAccessors()));
3314    assertTrue(m5.equals(
3315        MessageNano.mergeFrom(new TestNanoAccessors(), MessageNano.toByteArray(m6))));
3316
3317    // -------
3318
3319    NanoReferenceTypes.TestAllTypesNano m7 = new NanoReferenceTypes.TestAllTypesNano();
3320    m7.optionalFloat = Float.NaN;
3321    m7.optionalDouble = Double.NaN;
3322    NanoReferenceTypes.TestAllTypesNano m8 = new NanoReferenceTypes.TestAllTypesNano();
3323    m8.optionalFloat = Float.NaN;
3324    m8.optionalDouble = Double.NaN;
3325    assertTrue(m7.equals(m8));
3326    assertTrue(m7.equals(MessageNano.mergeFrom(
3327        new NanoReferenceTypes.TestAllTypesNano(), MessageNano.toByteArray(m7))));
3328
3329    m7.optionalFloat = +0f;
3330    m8.optionalFloat = -0f;
3331    assertFalse(m7.equals(m8));
3332
3333    m7.optionalFloat = -0f;
3334    m7.optionalDouble = +0d;
3335    m8.optionalDouble = -0d;
3336    assertFalse(m7.equals(m8));
3337
3338    m7.optionalDouble = -0d;
3339    assertTrue(m7.equals(m8));
3340    assertFalse(m7.equals(new NanoReferenceTypes.TestAllTypesNano()));
3341    assertTrue(m7.equals(MessageNano.mergeFrom(
3342        new NanoReferenceTypes.TestAllTypesNano(), MessageNano.toByteArray(m7))));
3343  }
3344
3345  public void testNullRepeatedFields() throws Exception {
3346    // Check that serialization after explicitly setting a repeated field
3347    // to null doesn't NPE.
3348    TestAllTypesNano message = new TestAllTypesNano();
3349    message.repeatedInt32 = null;
3350    MessageNano.toByteArray(message);  // should not NPE
3351    message.toString(); // should not NPE
3352
3353    message.repeatedNestedEnum = null;
3354    MessageNano.toByteArray(message);  // should not NPE
3355    message.toString(); // should not NPE
3356
3357    message.repeatedBytes = null;
3358    MessageNano.toByteArray(message); // should not NPE
3359    message.toString(); // should not NPE
3360
3361    message.repeatedNestedMessage = null;
3362    MessageNano.toByteArray(message); // should not NPE
3363    message.toString(); // should not NPE
3364
3365    message.repeatedPackedInt32 = null;
3366    MessageNano.toByteArray(message); // should not NPE
3367    message.toString(); // should not NPE
3368
3369    message.repeatedPackedNestedEnum = null;
3370    MessageNano.toByteArray(message); // should not NPE
3371    message.toString(); // should not NPE
3372
3373    // Create a second message to merge into message.
3374    TestAllTypesNano secondMessage = new TestAllTypesNano();
3375    secondMessage.repeatedInt32 = new int[] {1, 2, 3};
3376    secondMessage.repeatedNestedEnum = new int[] {
3377      TestAllTypesNano.FOO, TestAllTypesNano.BAR
3378    };
3379    secondMessage.repeatedBytes = new byte[][] {{1, 2}, {3, 4}};
3380    TestAllTypesNano.NestedMessage nested =
3381        new TestAllTypesNano.NestedMessage();
3382    nested.bb = 55;
3383    secondMessage.repeatedNestedMessage =
3384        new TestAllTypesNano.NestedMessage[] {nested};
3385    secondMessage.repeatedPackedInt32 = new int[] {1, 2, 3};
3386    secondMessage.repeatedPackedNestedEnum = new int[] {
3387        TestAllTypesNano.FOO, TestAllTypesNano.BAR
3388      };
3389
3390    // Should not NPE
3391    message.mergeFrom(CodedInputByteBufferNano.newInstance(
3392        MessageNano.toByteArray(secondMessage)));
3393    assertEquals(3, message.repeatedInt32.length);
3394    assertEquals(3, message.repeatedInt32[2]);
3395    assertEquals(2, message.repeatedNestedEnum.length);
3396    assertEquals(TestAllTypesNano.FOO, message.repeatedNestedEnum[0]);
3397    assertEquals(2, message.repeatedBytes.length);
3398    assertEquals(4, message.repeatedBytes[1][1]);
3399    assertEquals(1, message.repeatedNestedMessage.length);
3400    assertEquals(55, message.repeatedNestedMessage[0].bb);
3401    assertEquals(3, message.repeatedPackedInt32.length);
3402    assertEquals(2, message.repeatedPackedInt32[1]);
3403    assertEquals(2, message.repeatedPackedNestedEnum.length);
3404    assertEquals(TestAllTypesNano.BAR, message.repeatedPackedNestedEnum[1]);
3405  }
3406
3407  public void testNullRepeatedFieldElements() throws Exception {
3408    // Check that serialization with null array elements doesn't NPE.
3409    String string1 = "1";
3410    String string2 = "2";
3411    byte[] bytes1 = {3, 4};
3412    byte[] bytes2 = {5, 6};
3413    TestAllTypesNano.NestedMessage msg1 = new TestAllTypesNano.NestedMessage();
3414    msg1.bb = 7;
3415    TestAllTypesNano.NestedMessage msg2 = new TestAllTypesNano.NestedMessage();
3416    msg2.bb = 8;
3417
3418    TestAllTypesNano message = new TestAllTypesNano();
3419    message.repeatedString = new String[] {null, string1, string2};
3420    message.repeatedBytes = new byte[][] {bytes1, null, bytes2};
3421    message.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {msg1, msg2, null};
3422    message.repeatedGroup = new TestAllTypesNano.RepeatedGroup[] {null, null, null};
3423
3424    byte[] serialized = MessageNano.toByteArray(message); // should not NPE
3425    TestAllTypesNano deserialized = MessageNano.mergeFrom(new TestAllTypesNano(), serialized);
3426    assertEquals(2, deserialized.repeatedString.length);
3427    assertEquals(string1, deserialized.repeatedString[0]);
3428    assertEquals(string2, deserialized.repeatedString[1]);
3429    assertEquals(2, deserialized.repeatedBytes.length);
3430    assertTrue(Arrays.equals(bytes1, deserialized.repeatedBytes[0]));
3431    assertTrue(Arrays.equals(bytes2, deserialized.repeatedBytes[1]));
3432    assertEquals(2, deserialized.repeatedNestedMessage.length);
3433    assertEquals(msg1.bb, deserialized.repeatedNestedMessage[0].bb);
3434    assertEquals(msg2.bb, deserialized.repeatedNestedMessage[1].bb);
3435    assertEquals(0, deserialized.repeatedGroup.length);
3436  }
3437
3438  public void testRepeatedMerge() throws Exception {
3439    // Check that merging repeated fields cause the arrays to expand with
3440    // new data.
3441    TestAllTypesNano first = new TestAllTypesNano();
3442    first.repeatedInt32 = new int[] {1, 2, 3};
3443    TestAllTypesNano second = new TestAllTypesNano();
3444    second.repeatedInt32 = new int[] {4, 5};
3445    MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
3446    assertEquals(5, first.repeatedInt32.length);
3447    assertEquals(1, first.repeatedInt32[0]);
3448    assertEquals(4, first.repeatedInt32[3]);
3449
3450    first = new TestAllTypesNano();
3451    first.repeatedNestedEnum = new int[] {TestAllTypesNano.BAR};
3452    second = new TestAllTypesNano();
3453    second.repeatedNestedEnum = new int[] {TestAllTypesNano.FOO};
3454    MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
3455    assertEquals(2, first.repeatedNestedEnum.length);
3456    assertEquals(TestAllTypesNano.BAR, first.repeatedNestedEnum[0]);
3457    assertEquals(TestAllTypesNano.FOO, first.repeatedNestedEnum[1]);
3458
3459    first = new TestAllTypesNano();
3460    first.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {
3461      new TestAllTypesNano.NestedMessage()
3462    };
3463    first.repeatedNestedMessage[0].bb = 3;
3464    second = new TestAllTypesNano();
3465    second.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {
3466      new TestAllTypesNano.NestedMessage()
3467    };
3468    second.repeatedNestedMessage[0].bb = 5;
3469    MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
3470    assertEquals(2, first.repeatedNestedMessage.length);
3471    assertEquals(3, first.repeatedNestedMessage[0].bb);
3472    assertEquals(5, first.repeatedNestedMessage[1].bb);
3473
3474    first = new TestAllTypesNano();
3475    first.repeatedPackedSfixed64 = new long[] {-1, -2, -3};
3476    second = new TestAllTypesNano();
3477    second.repeatedPackedSfixed64 = new long[] {-4, -5};
3478    MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
3479    assertEquals(5, first.repeatedPackedSfixed64.length);
3480    assertEquals(-1, first.repeatedPackedSfixed64[0]);
3481    assertEquals(-4, first.repeatedPackedSfixed64[3]);
3482
3483    first = new TestAllTypesNano();
3484    first.repeatedPackedNestedEnum = new int[] {TestAllTypesNano.BAR};
3485    second = new TestAllTypesNano();
3486    second.repeatedPackedNestedEnum = new int[] {TestAllTypesNano.FOO};
3487    MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
3488    assertEquals(2, first.repeatedPackedNestedEnum.length);
3489    assertEquals(TestAllTypesNano.BAR, first.repeatedPackedNestedEnum[0]);
3490    assertEquals(TestAllTypesNano.FOO, first.repeatedPackedNestedEnum[1]);
3491
3492    // Now test repeated merging in a nested scope
3493    TestRepeatedMergeNano firstContainer = new TestRepeatedMergeNano();
3494    firstContainer.contained = new TestAllTypesNano();
3495    firstContainer.contained.repeatedInt32 = new int[] {10, 20};
3496    TestRepeatedMergeNano secondContainer = new TestRepeatedMergeNano();
3497    secondContainer.contained = new TestAllTypesNano();
3498    secondContainer.contained.repeatedInt32 = new int[] {30};
3499    MessageNano.mergeFrom(firstContainer, MessageNano.toByteArray(secondContainer));
3500    assertEquals(3, firstContainer.contained.repeatedInt32.length);
3501    assertEquals(20, firstContainer.contained.repeatedInt32[1]);
3502    assertEquals(30, firstContainer.contained.repeatedInt32[2]);
3503  }
3504
3505  public void testRepeatedPackables() throws Exception {
3506    // Check that repeated fields with packable types can accept both packed and unpacked
3507    // serialized forms.
3508    NanoRepeatedPackables.NonPacked nonPacked = new NanoRepeatedPackables.NonPacked();
3509    // Exaggerates the first values of varint-typed arrays. This is to test that the parsing code
3510    // of packed fields handles non-packed data correctly. If the code incorrectly thinks it is
3511    // reading from a packed tag, it will read the first value as the byte length of the field,
3512    // and the large number will cause the input to go out of bounds, thus capturing the error.
3513    nonPacked.int32S = new int[] {1000, 2, 3};
3514    nonPacked.int64S = new long[] {4000, 5, 6};
3515    nonPacked.uint32S = new int[] {7000, 8, 9};
3516    nonPacked.uint64S = new long[] {10000, 11, 12};
3517    nonPacked.sint32S = new int[] {13000, 14, 15};
3518    nonPacked.sint64S = new long[] {16000, 17, 18};
3519    nonPacked.fixed32S = new int[] {19, 20, 21};
3520    nonPacked.fixed64S = new long[] {22, 23, 24};
3521    nonPacked.sfixed32S = new int[] {25, 26, 27};
3522    nonPacked.sfixed64S = new long[] {28, 29, 30};
3523    nonPacked.floats = new float[] {31, 32, 33};
3524    nonPacked.doubles = new double[] {34, 35, 36};
3525    nonPacked.bools = new boolean[] {false, true};
3526    nonPacked.enums = new int[] {
3527      NanoRepeatedPackables.Enum.OPTION_ONE,
3528      NanoRepeatedPackables.Enum.OPTION_TWO,
3529    };
3530    nonPacked.noise = 13579;
3531
3532    byte[] nonPackedSerialized = MessageNano.toByteArray(nonPacked);
3533
3534    NanoRepeatedPackables.Packed packed =
3535        MessageNano.mergeFrom(new NanoRepeatedPackables.Packed(), nonPackedSerialized);
3536    assertRepeatedPackablesEqual(nonPacked, packed);
3537
3538    byte[] packedSerialized = MessageNano.toByteArray(packed);
3539    // Just a cautious check that the two serialized forms are different,
3540    // to make sure the remaining of this test is useful:
3541    assertFalse(Arrays.equals(nonPackedSerialized, packedSerialized));
3542
3543    nonPacked = MessageNano.mergeFrom(new NanoRepeatedPackables.NonPacked(), packedSerialized);
3544    assertRepeatedPackablesEqual(nonPacked, packed);
3545
3546    // Test mixed serialized form.
3547    byte[] mixedSerialized = new byte[nonPackedSerialized.length + packedSerialized.length];
3548    System.arraycopy(nonPackedSerialized, 0, mixedSerialized, 0, nonPackedSerialized.length);
3549    System.arraycopy(packedSerialized, 0,
3550        mixedSerialized, nonPackedSerialized.length, packedSerialized.length);
3551
3552    nonPacked = MessageNano.mergeFrom(new NanoRepeatedPackables.NonPacked(), mixedSerialized);
3553    packed = MessageNano.mergeFrom(new NanoRepeatedPackables.Packed(), mixedSerialized);
3554    assertRepeatedPackablesEqual(nonPacked, packed);
3555    assertTrue(Arrays.equals(new int[] {1000, 2, 3, 1000, 2, 3}, nonPacked.int32S));
3556    assertTrue(Arrays.equals(new int[] {13000, 14, 15, 13000, 14, 15}, nonPacked.sint32S));
3557    assertTrue(Arrays.equals(new int[] {25, 26, 27, 25, 26, 27}, nonPacked.sfixed32S));
3558    assertTrue(Arrays.equals(new boolean[] {false, true, false, true}, nonPacked.bools));
3559  }
3560
3561  private void assertRepeatedPackablesEqual(
3562      NanoRepeatedPackables.NonPacked nonPacked, NanoRepeatedPackables.Packed packed) {
3563    // Not using MessageNano.equals() -- that belongs to a separate test.
3564    assertTrue(Arrays.equals(nonPacked.int32S, packed.int32S));
3565    assertTrue(Arrays.equals(nonPacked.int64S, packed.int64S));
3566    assertTrue(Arrays.equals(nonPacked.uint32S, packed.uint32S));
3567    assertTrue(Arrays.equals(nonPacked.uint64S, packed.uint64S));
3568    assertTrue(Arrays.equals(nonPacked.sint32S, packed.sint32S));
3569    assertTrue(Arrays.equals(nonPacked.sint64S, packed.sint64S));
3570    assertTrue(Arrays.equals(nonPacked.fixed32S, packed.fixed32S));
3571    assertTrue(Arrays.equals(nonPacked.fixed64S, packed.fixed64S));
3572    assertTrue(Arrays.equals(nonPacked.sfixed32S, packed.sfixed32S));
3573    assertTrue(Arrays.equals(nonPacked.sfixed64S, packed.sfixed64S));
3574    assertTrue(Arrays.equals(nonPacked.floats, packed.floats));
3575    assertTrue(Arrays.equals(nonPacked.doubles, packed.doubles));
3576    assertTrue(Arrays.equals(nonPacked.bools, packed.bools));
3577    assertTrue(Arrays.equals(nonPacked.enums, packed.enums));
3578  }
3579
3580  private void assertHasWireData(MessageNano message, boolean expected) {
3581    byte[] bytes = MessageNano.toByteArray(message);
3582    int wireLength = bytes.length;
3583    if (expected) {
3584      assertFalse(wireLength == 0);
3585    } else {
3586      if (wireLength != 0) {
3587        fail("Expected no wire data for message \n" + message
3588            + "\nBut got:\n"
3589            + hexDump(bytes));
3590      }
3591    }
3592  }
3593
3594  private static String hexDump(byte[] bytes) {
3595    StringBuilder sb = new StringBuilder();
3596    for (byte b : bytes) {
3597      sb.append(String.format("%02x ", b));
3598    }
3599    return sb.toString();
3600  }
3601}
3602