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