• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Protocol Buffers - Google's data interchange format
2// Copyright 2015 Google Inc.  All rights reserved.
3// https://developers.google.com/protocol-buffers/
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
31#import "GPBTestUtilities.h"
32
33#import <objc/runtime.h>
34
35#import "GPBMessage.h"
36
37#import "google/protobuf/MapProto2Unittest.pbobjc.h"
38#import "google/protobuf/MapUnittest.pbobjc.h"
39#import "google/protobuf/Unittest.pbobjc.h"
40#import "google/protobuf/UnittestDropUnknownFields.pbobjc.h"
41#import "google/protobuf/UnittestPreserveUnknownEnum.pbobjc.h"
42#import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
43#import "google/protobuf/UnittestRuntimeProto3.pbobjc.h"
44
45static NSData *DataFromCStr(const char *str) {
46  return [NSData dataWithBytes:str length:strlen(str)];
47}
48
49@interface MessageSerializationTests : GPBTestCase
50@end
51
52@implementation MessageSerializationTests
53
54// TODO(thomasvl): Pull tests over from GPBMessageTests that are serialization
55// specific.
56
57- (void)testProto3SerializationHandlingDefaults {
58  // Proto2 covered in other tests.
59
60  Message3 *msg = [[Message3 alloc] init];
61
62  // Add defaults, no output.
63
64  NSData *data = [msg data];
65  XCTAssertEqual([data length], 0U);
66
67  // All zeros, still nothing.
68
69  msg.optionalInt32 = 0;
70  msg.optionalInt64 = 0;
71  msg.optionalUint32 = 0;
72  msg.optionalUint64 = 0;
73  msg.optionalSint32 = 0;
74  msg.optionalSint64 = 0;
75  msg.optionalFixed32 = 0;
76  msg.optionalFixed64 = 0;
77  msg.optionalSfixed32 = 0;
78  msg.optionalSfixed64 = 0;
79  msg.optionalFloat = 0.0f;
80  msg.optionalDouble = 0.0;
81  msg.optionalBool = NO;
82  msg.optionalString = @"";
83  msg.optionalBytes = [NSData data];
84  msg.optionalEnum = Message3_Enum_Foo;  // first value
85
86  data = [msg data];
87  XCTAssertEqual([data length], 0U);
88
89  // The two that also take nil as nothing.
90
91  msg.optionalString = nil;
92  msg.optionalBytes = nil;
93
94  data = [msg data];
95  XCTAssertEqual([data length], 0U);
96
97  // Set one field...
98
99  msg.optionalInt32 = 1;
100
101  data = [msg data];
102  const uint8_t expectedBytes[] = {0x08, 0x01};
103  NSData *expected = [NSData dataWithBytes:expectedBytes length:2];
104  XCTAssertEqualObjects(data, expected);
105
106  // Back to zero...
107
108  msg.optionalInt32 = 0;
109
110  data = [msg data];
111  XCTAssertEqual([data length], 0U);
112
113  [msg release];
114}
115
116- (void)testProto3DroppingUnknownFields {
117  DropUnknownsFooWithExtraFields *fooWithExtras =
118      [[DropUnknownsFooWithExtraFields alloc] init];
119
120  fooWithExtras.int32Value = 1;
121  fooWithExtras.enumValue = DropUnknownsFooWithExtraFields_NestedEnum_Baz;
122  fooWithExtras.extraInt32Value = 2;
123
124  NSData *data = [fooWithExtras data];
125  XCTAssertNotNil(data);
126  DropUnknownsFoo *foo = [DropUnknownsFoo parseFromData:data error:NULL];
127
128  XCTAssertEqual(foo.int32Value, 1);
129  XCTAssertEqual(foo.enumValue, DropUnknownsFoo_NestedEnum_Baz);
130  // Nothing should end up in the unknowns.
131  XCTAssertEqual([foo.unknownFields countOfFields], 0U);
132
133  [fooWithExtras release];
134  data = [foo data];
135  fooWithExtras =
136      [DropUnknownsFooWithExtraFields parseFromData:data error:NULL];
137  XCTAssertEqual(fooWithExtras.int32Value, 1);
138  XCTAssertEqual(fooWithExtras.enumValue,
139                 DropUnknownsFooWithExtraFields_NestedEnum_Baz);
140  // And the extra value is gone (back to the default).
141  XCTAssertEqual(fooWithExtras.extraInt32Value, 0);
142  XCTAssertEqual([foo.unknownFields countOfFields], 0U);
143}
144
145- (void)testProto2UnknownEnumToUnknownField {
146  Message3 *orig = [[Message3 alloc] init];
147
148  orig.optionalEnum = Message3_Enum_Extra3;
149  orig.repeatedEnumArray =
150      [GPBEnumArray arrayWithValidationFunction:Message3_Enum_IsValidValue
151                                       rawValue:Message3_Enum_Extra3];
152  orig.oneofEnum = Message3_Enum_Extra3;
153
154  NSData *data = [orig data];
155  XCTAssertNotNil(data);
156  Message2 *msg = [[Message2 alloc] initWithData:data error:NULL];
157
158  // None of the fields should be set.
159
160  XCTAssertFalse(msg.hasOptionalEnum);
161  XCTAssertEqual(msg.repeatedEnumArray.count, 0U);
162  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_GPBUnsetOneOfCase);
163
164  // All the values should be in unknown fields.
165
166  GPBUnknownFieldSet *unknownFields = msg.unknownFields;
167
168  XCTAssertEqual([unknownFields countOfFields], 3U);
169  XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OptionalEnum]);
170  XCTAssertTrue(
171      [unknownFields hasField:Message2_FieldNumber_RepeatedEnumArray]);
172  XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OneofEnum]);
173
174  GPBUnknownField *field =
175      [unknownFields getField:Message2_FieldNumber_OptionalEnum];
176  XCTAssertEqual(field.varintList.count, 1U);
177  XCTAssertEqual([field.varintList valueAtIndex:0],
178                 (uint64_t)Message3_Enum_Extra3);
179
180  field = [unknownFields getField:Message2_FieldNumber_RepeatedEnumArray];
181  XCTAssertEqual(field.varintList.count, 1U);
182  XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)Message3_Enum_Extra3);
183
184  field = [unknownFields getField:Message2_FieldNumber_OneofEnum];
185  XCTAssertEqual(field.varintList.count, 1U);
186  XCTAssertEqual([field.varintList valueAtIndex:0],
187                 (uint64_t)Message3_Enum_Extra3);
188
189  [msg release];
190  [orig release];
191}
192
193- (void)testProto3UnknownEnumPreserving {
194  UnknownEnumsMyMessagePlusExtra *orig =
195      [UnknownEnumsMyMessagePlusExtra message];
196
197  orig.e = UnknownEnumsMyEnumPlusExtra_EExtra;
198  orig.repeatedEArray = [GPBEnumArray
199      arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue
200                         rawValue:UnknownEnumsMyEnumPlusExtra_EExtra];
201  orig.repeatedPackedEArray = [GPBEnumArray
202      arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue
203                         rawValue:UnknownEnumsMyEnumPlusExtra_EExtra];
204  orig.oneofE1 = UnknownEnumsMyEnumPlusExtra_EExtra;
205
206  // Everything should be there via raw values.
207
208  NSData *data = [orig data];
209  XCTAssertNotNil(data);
210  UnknownEnumsMyMessage *msg =
211      [UnknownEnumsMyMessage parseFromData:data error:NULL];
212
213  XCTAssertEqual(msg.e, UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
214  XCTAssertEqual(UnknownEnumsMyMessage_E_RawValue(msg),
215                 UnknownEnumsMyEnumPlusExtra_EExtra);
216  XCTAssertEqual(msg.repeatedEArray.count, 1U);
217  XCTAssertEqual([msg.repeatedEArray valueAtIndex:0],
218                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
219  XCTAssertEqual([msg.repeatedEArray rawValueAtIndex:0],
220                 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra);
221  XCTAssertEqual(msg.repeatedPackedEArray.count, 1U);
222  XCTAssertEqual([msg.repeatedPackedEArray valueAtIndex:0],
223                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
224  XCTAssertEqual([msg.repeatedPackedEArray rawValueAtIndex:0],
225                 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra);
226  XCTAssertEqual(msg.oneofE1,
227                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
228  XCTAssertEqual(UnknownEnumsMyMessage_OneofE1_RawValue(msg),
229                 UnknownEnumsMyEnumPlusExtra_EExtra);
230
231  // Everything should go out and come back.
232
233  data = [msg data];
234  orig = [UnknownEnumsMyMessagePlusExtra parseFromData:data error:NULL];
235
236  XCTAssertEqual(orig.e, UnknownEnumsMyEnumPlusExtra_EExtra);
237  XCTAssertEqual(orig.repeatedEArray.count, 1U);
238  XCTAssertEqual([orig.repeatedEArray valueAtIndex:0],
239                 UnknownEnumsMyEnumPlusExtra_EExtra);
240  XCTAssertEqual(orig.repeatedPackedEArray.count, 1U);
241  XCTAssertEqual([orig.repeatedPackedEArray valueAtIndex:0],
242                 UnknownEnumsMyEnumPlusExtra_EExtra);
243  XCTAssertEqual(orig.oneofE1, UnknownEnumsMyEnumPlusExtra_EExtra);
244}
245
246//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF(MESSAGE, FIELD, VALUE)
247//%TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, )
248//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, EQ_SUFFIX)
249//%  {  // oneof##FIELD
250//%    MESSAGE *orig = [[MESSAGE alloc] init];
251//%    orig.oneof##FIELD = VALUE;
252//%    XCTAssertEqual(orig.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD);
253//%    NSData *data = [orig data];
254//%    XCTAssertNotNil(data);
255//%    MESSAGE *msg = [MESSAGE parseFromData:data error:NULL];
256//%    XCTAssertEqual(msg.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD);
257//%    XCTAssertEqual##EQ_SUFFIX(msg.oneof##FIELD, VALUE);
258//%    [orig release];
259//%  }
260//%
261//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOFS(SYNTAX, BOOL_NON_DEFAULT)
262//%- (void)testProto##SYNTAX##RoundTripOneof {
263//%
264//%GROUP_INIT##SYNTAX()  Message##SYNTAX *subMessage = [[Message##SYNTAX alloc] init];
265//%  XCTAssertNotNil(subMessage);
266//%  subMessage.optionalInt32 = 666;
267//%
268//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int32, 1)
269//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int64, 2)
270//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint32, 3U)
271//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint64, 4U)
272//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint32, 5)
273//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint64, 6)
274//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed32, 7U)
275//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed64, 8U)
276//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed32, 9)
277//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed64, 10)
278//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Float, 11.0f)
279//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Double, 12.0)
280//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Bool, BOOL_NON_DEFAULT)
281//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, String, @"foo", Objects)
282//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Bytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding], Objects)
283//%GROUP_TEST##SYNTAX()TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Message, subMessage, Objects)
284//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Enum, Message2_Enum_Bar)
285//%GROUP_CLEANUP##SYNTAX()  [subMessage release];
286//%}
287//%
288//%PDDM-DEFINE GROUP_INIT2()
289//%  Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init];
290//%  XCTAssertNotNil(group);
291//%  group.a = 777;
292//%
293//%PDDM-DEFINE GROUP_CLEANUP2()
294//%  [group release];
295//%
296//%PDDM-DEFINE GROUP_TEST2()
297//%TEST_ROUNDTRIP_ONEOF_ADV(Message2, Group, group, Objects)
298//%
299//%PDDM-DEFINE GROUP_INIT3()
300// Empty
301//%PDDM-DEFINE GROUP_CLEANUP3()
302// Empty
303//%PDDM-DEFINE GROUP_TEST3()
304//%  // Not "group" in proto3.
305//%
306//%
307//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(2, NO)
308// This block of code is generated, do not edit it directly.
309
310- (void)testProto2RoundTripOneof {
311
312  Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init];
313  XCTAssertNotNil(group);
314  group.a = 777;
315  Message2 *subMessage = [[Message2 alloc] init];
316  XCTAssertNotNil(subMessage);
317  subMessage.optionalInt32 = 666;
318
319  {  // oneofInt32
320    Message2 *orig = [[Message2 alloc] init];
321    orig.oneofInt32 = 1;
322    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
323    NSData *data = [orig data];
324    XCTAssertNotNil(data);
325    Message2 *msg = [Message2 parseFromData:data error:NULL];
326    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
327    XCTAssertEqual(msg.oneofInt32, 1);
328    [orig release];
329  }
330
331  {  // oneofInt64
332    Message2 *orig = [[Message2 alloc] init];
333    orig.oneofInt64 = 2;
334    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
335    NSData *data = [orig data];
336    XCTAssertNotNil(data);
337    Message2 *msg = [Message2 parseFromData:data error:NULL];
338    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
339    XCTAssertEqual(msg.oneofInt64, 2);
340    [orig release];
341  }
342
343  {  // oneofUint32
344    Message2 *orig = [[Message2 alloc] init];
345    orig.oneofUint32 = 3U;
346    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
347    NSData *data = [orig data];
348    XCTAssertNotNil(data);
349    Message2 *msg = [Message2 parseFromData:data error:NULL];
350    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
351    XCTAssertEqual(msg.oneofUint32, 3U);
352    [orig release];
353  }
354
355  {  // oneofUint64
356    Message2 *orig = [[Message2 alloc] init];
357    orig.oneofUint64 = 4U;
358    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
359    NSData *data = [orig data];
360    XCTAssertNotNil(data);
361    Message2 *msg = [Message2 parseFromData:data error:NULL];
362    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
363    XCTAssertEqual(msg.oneofUint64, 4U);
364    [orig release];
365  }
366
367  {  // oneofSint32
368    Message2 *orig = [[Message2 alloc] init];
369    orig.oneofSint32 = 5;
370    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
371    NSData *data = [orig data];
372    XCTAssertNotNil(data);
373    Message2 *msg = [Message2 parseFromData:data error:NULL];
374    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
375    XCTAssertEqual(msg.oneofSint32, 5);
376    [orig release];
377  }
378
379  {  // oneofSint64
380    Message2 *orig = [[Message2 alloc] init];
381    orig.oneofSint64 = 6;
382    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
383    NSData *data = [orig data];
384    XCTAssertNotNil(data);
385    Message2 *msg = [Message2 parseFromData:data error:NULL];
386    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
387    XCTAssertEqual(msg.oneofSint64, 6);
388    [orig release];
389  }
390
391  {  // oneofFixed32
392    Message2 *orig = [[Message2 alloc] init];
393    orig.oneofFixed32 = 7U;
394    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
395    NSData *data = [orig data];
396    XCTAssertNotNil(data);
397    Message2 *msg = [Message2 parseFromData:data error:NULL];
398    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
399    XCTAssertEqual(msg.oneofFixed32, 7U);
400    [orig release];
401  }
402
403  {  // oneofFixed64
404    Message2 *orig = [[Message2 alloc] init];
405    orig.oneofFixed64 = 8U;
406    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
407    NSData *data = [orig data];
408    XCTAssertNotNil(data);
409    Message2 *msg = [Message2 parseFromData:data error:NULL];
410    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
411    XCTAssertEqual(msg.oneofFixed64, 8U);
412    [orig release];
413  }
414
415  {  // oneofSfixed32
416    Message2 *orig = [[Message2 alloc] init];
417    orig.oneofSfixed32 = 9;
418    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
419    NSData *data = [orig data];
420    XCTAssertNotNil(data);
421    Message2 *msg = [Message2 parseFromData:data error:NULL];
422    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
423    XCTAssertEqual(msg.oneofSfixed32, 9);
424    [orig release];
425  }
426
427  {  // oneofSfixed64
428    Message2 *orig = [[Message2 alloc] init];
429    orig.oneofSfixed64 = 10;
430    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
431    NSData *data = [orig data];
432    XCTAssertNotNil(data);
433    Message2 *msg = [Message2 parseFromData:data error:NULL];
434    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
435    XCTAssertEqual(msg.oneofSfixed64, 10);
436    [orig release];
437  }
438
439  {  // oneofFloat
440    Message2 *orig = [[Message2 alloc] init];
441    orig.oneofFloat = 11.0f;
442    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
443    NSData *data = [orig data];
444    XCTAssertNotNil(data);
445    Message2 *msg = [Message2 parseFromData:data error:NULL];
446    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
447    XCTAssertEqual(msg.oneofFloat, 11.0f);
448    [orig release];
449  }
450
451  {  // oneofDouble
452    Message2 *orig = [[Message2 alloc] init];
453    orig.oneofDouble = 12.0;
454    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
455    NSData *data = [orig data];
456    XCTAssertNotNil(data);
457    Message2 *msg = [Message2 parseFromData:data error:NULL];
458    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
459    XCTAssertEqual(msg.oneofDouble, 12.0);
460    [orig release];
461  }
462
463  {  // oneofBool
464    Message2 *orig = [[Message2 alloc] init];
465    orig.oneofBool = NO;
466    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBool);
467    NSData *data = [orig data];
468    XCTAssertNotNil(data);
469    Message2 *msg = [Message2 parseFromData:data error:NULL];
470    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBool);
471    XCTAssertEqual(msg.oneofBool, NO);
472    [orig release];
473  }
474
475  {  // oneofString
476    Message2 *orig = [[Message2 alloc] init];
477    orig.oneofString = @"foo";
478    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofString);
479    NSData *data = [orig data];
480    XCTAssertNotNil(data);
481    Message2 *msg = [Message2 parseFromData:data error:NULL];
482    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofString);
483    XCTAssertEqualObjects(msg.oneofString, @"foo");
484    [orig release];
485  }
486
487  {  // oneofBytes
488    Message2 *orig = [[Message2 alloc] init];
489    orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
490    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
491    NSData *data = [orig data];
492    XCTAssertNotNil(data);
493    Message2 *msg = [Message2 parseFromData:data error:NULL];
494    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
495    XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
496    [orig release];
497  }
498
499  {  // oneofGroup
500    Message2 *orig = [[Message2 alloc] init];
501    orig.oneofGroup = group;
502    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
503    NSData *data = [orig data];
504    XCTAssertNotNil(data);
505    Message2 *msg = [Message2 parseFromData:data error:NULL];
506    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
507    XCTAssertEqualObjects(msg.oneofGroup, group);
508    [orig release];
509  }
510
511  {  // oneofMessage
512    Message2 *orig = [[Message2 alloc] init];
513    orig.oneofMessage = subMessage;
514    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
515    NSData *data = [orig data];
516    XCTAssertNotNil(data);
517    Message2 *msg = [Message2 parseFromData:data error:NULL];
518    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
519    XCTAssertEqualObjects(msg.oneofMessage, subMessage);
520    [orig release];
521  }
522
523  {  // oneofEnum
524    Message2 *orig = [[Message2 alloc] init];
525    orig.oneofEnum = Message2_Enum_Bar;
526    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
527    NSData *data = [orig data];
528    XCTAssertNotNil(data);
529    Message2 *msg = [Message2 parseFromData:data error:NULL];
530    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
531    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
532    [orig release];
533  }
534
535  [group release];
536  [subMessage release];
537}
538
539//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(3, YES)
540// This block of code is generated, do not edit it directly.
541
542- (void)testProto3RoundTripOneof {
543
544  Message3 *subMessage = [[Message3 alloc] init];
545  XCTAssertNotNil(subMessage);
546  subMessage.optionalInt32 = 666;
547
548  {  // oneofInt32
549    Message3 *orig = [[Message3 alloc] init];
550    orig.oneofInt32 = 1;
551    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
552    NSData *data = [orig data];
553    XCTAssertNotNil(data);
554    Message3 *msg = [Message3 parseFromData:data error:NULL];
555    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
556    XCTAssertEqual(msg.oneofInt32, 1);
557    [orig release];
558  }
559
560  {  // oneofInt64
561    Message3 *orig = [[Message3 alloc] init];
562    orig.oneofInt64 = 2;
563    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
564    NSData *data = [orig data];
565    XCTAssertNotNil(data);
566    Message3 *msg = [Message3 parseFromData:data error:NULL];
567    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
568    XCTAssertEqual(msg.oneofInt64, 2);
569    [orig release];
570  }
571
572  {  // oneofUint32
573    Message3 *orig = [[Message3 alloc] init];
574    orig.oneofUint32 = 3U;
575    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
576    NSData *data = [orig data];
577    XCTAssertNotNil(data);
578    Message3 *msg = [Message3 parseFromData:data error:NULL];
579    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
580    XCTAssertEqual(msg.oneofUint32, 3U);
581    [orig release];
582  }
583
584  {  // oneofUint64
585    Message3 *orig = [[Message3 alloc] init];
586    orig.oneofUint64 = 4U;
587    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
588    NSData *data = [orig data];
589    XCTAssertNotNil(data);
590    Message3 *msg = [Message3 parseFromData:data error:NULL];
591    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
592    XCTAssertEqual(msg.oneofUint64, 4U);
593    [orig release];
594  }
595
596  {  // oneofSint32
597    Message3 *orig = [[Message3 alloc] init];
598    orig.oneofSint32 = 5;
599    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
600    NSData *data = [orig data];
601    XCTAssertNotNil(data);
602    Message3 *msg = [Message3 parseFromData:data error:NULL];
603    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
604    XCTAssertEqual(msg.oneofSint32, 5);
605    [orig release];
606  }
607
608  {  // oneofSint64
609    Message3 *orig = [[Message3 alloc] init];
610    orig.oneofSint64 = 6;
611    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
612    NSData *data = [orig data];
613    XCTAssertNotNil(data);
614    Message3 *msg = [Message3 parseFromData:data error:NULL];
615    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
616    XCTAssertEqual(msg.oneofSint64, 6);
617    [orig release];
618  }
619
620  {  // oneofFixed32
621    Message3 *orig = [[Message3 alloc] init];
622    orig.oneofFixed32 = 7U;
623    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
624    NSData *data = [orig data];
625    XCTAssertNotNil(data);
626    Message3 *msg = [Message3 parseFromData:data error:NULL];
627    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
628    XCTAssertEqual(msg.oneofFixed32, 7U);
629    [orig release];
630  }
631
632  {  // oneofFixed64
633    Message3 *orig = [[Message3 alloc] init];
634    orig.oneofFixed64 = 8U;
635    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
636    NSData *data = [orig data];
637    XCTAssertNotNil(data);
638    Message3 *msg = [Message3 parseFromData:data error:NULL];
639    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
640    XCTAssertEqual(msg.oneofFixed64, 8U);
641    [orig release];
642  }
643
644  {  // oneofSfixed32
645    Message3 *orig = [[Message3 alloc] init];
646    orig.oneofSfixed32 = 9;
647    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
648    NSData *data = [orig data];
649    XCTAssertNotNil(data);
650    Message3 *msg = [Message3 parseFromData:data error:NULL];
651    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
652    XCTAssertEqual(msg.oneofSfixed32, 9);
653    [orig release];
654  }
655
656  {  // oneofSfixed64
657    Message3 *orig = [[Message3 alloc] init];
658    orig.oneofSfixed64 = 10;
659    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
660    NSData *data = [orig data];
661    XCTAssertNotNil(data);
662    Message3 *msg = [Message3 parseFromData:data error:NULL];
663    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
664    XCTAssertEqual(msg.oneofSfixed64, 10);
665    [orig release];
666  }
667
668  {  // oneofFloat
669    Message3 *orig = [[Message3 alloc] init];
670    orig.oneofFloat = 11.0f;
671    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
672    NSData *data = [orig data];
673    XCTAssertNotNil(data);
674    Message3 *msg = [Message3 parseFromData:data error:NULL];
675    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
676    XCTAssertEqual(msg.oneofFloat, 11.0f);
677    [orig release];
678  }
679
680  {  // oneofDouble
681    Message3 *orig = [[Message3 alloc] init];
682    orig.oneofDouble = 12.0;
683    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
684    NSData *data = [orig data];
685    XCTAssertNotNil(data);
686    Message3 *msg = [Message3 parseFromData:data error:NULL];
687    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
688    XCTAssertEqual(msg.oneofDouble, 12.0);
689    [orig release];
690  }
691
692  {  // oneofBool
693    Message3 *orig = [[Message3 alloc] init];
694    orig.oneofBool = YES;
695    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBool);
696    NSData *data = [orig data];
697    XCTAssertNotNil(data);
698    Message3 *msg = [Message3 parseFromData:data error:NULL];
699    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBool);
700    XCTAssertEqual(msg.oneofBool, YES);
701    [orig release];
702  }
703
704  {  // oneofString
705    Message3 *orig = [[Message3 alloc] init];
706    orig.oneofString = @"foo";
707    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofString);
708    NSData *data = [orig data];
709    XCTAssertNotNil(data);
710    Message3 *msg = [Message3 parseFromData:data error:NULL];
711    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofString);
712    XCTAssertEqualObjects(msg.oneofString, @"foo");
713    [orig release];
714  }
715
716  {  // oneofBytes
717    Message3 *orig = [[Message3 alloc] init];
718    orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
719    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
720    NSData *data = [orig data];
721    XCTAssertNotNil(data);
722    Message3 *msg = [Message3 parseFromData:data error:NULL];
723    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
724    XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
725    [orig release];
726  }
727
728  // Not "group" in proto3.
729
730  {  // oneofMessage
731    Message3 *orig = [[Message3 alloc] init];
732    orig.oneofMessage = subMessage;
733    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
734    NSData *data = [orig data];
735    XCTAssertNotNil(data);
736    Message3 *msg = [Message3 parseFromData:data error:NULL];
737    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
738    XCTAssertEqualObjects(msg.oneofMessage, subMessage);
739    [orig release];
740  }
741
742  {  // oneofEnum
743    Message3 *orig = [[Message3 alloc] init];
744    orig.oneofEnum = Message2_Enum_Bar;
745    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
746    NSData *data = [orig data];
747    XCTAssertNotNil(data);
748    Message3 *msg = [Message3 parseFromData:data error:NULL];
749    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
750    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
751    [orig release];
752  }
753
754  [subMessage release];
755}
756
757//%PDDM-EXPAND-END (2 expansions)
758
759- (void)testPackedUnpackedMessageParsing {
760  // packed is optional, a repeated field should parse when packed or unpacked.
761
762  TestPackedTypes *packedOrig = [TestPackedTypes message];
763  TestUnpackedTypes *unpackedOrig = [TestUnpackedTypes message];
764  [self setPackedFields:packedOrig repeatedCount:4];
765  [self setUnpackedFields:unpackedOrig repeatedCount:4];
766
767  NSData *packedData = [packedOrig data];
768  NSData *unpackedData = [unpackedOrig data];
769  XCTAssertNotNil(packedData);
770  XCTAssertNotNil(unpackedData);
771  XCTAssertNotEqualObjects(packedData, unpackedData,
772                           @"Data should differ (packed vs unpacked) use");
773
774  NSError *error = nil;
775  TestPackedTypes *packedParse =
776      [TestPackedTypes parseFromData:unpackedData error:&error];
777  XCTAssertNotNil(packedParse);
778  XCTAssertNil(error);
779  XCTAssertEqualObjects(packedParse, packedOrig);
780
781  error = nil;
782  TestUnpackedTypes *unpackedParsed =
783      [TestUnpackedTypes parseFromData:packedData error:&error];
784  XCTAssertNotNil(unpackedParsed);
785  XCTAssertNil(error);
786  XCTAssertEqualObjects(unpackedParsed, unpackedOrig);
787}
788
789- (void)testPackedUnpackedExtensionParsing {
790  // packed is optional, a repeated extension should parse when packed or
791  // unpacked.
792
793  TestPackedExtensions *packedOrig = [TestPackedExtensions message];
794  TestUnpackedExtensions *unpackedOrig = [TestUnpackedExtensions message];
795  [self setPackedExtensions:packedOrig repeatedCount:kGPBDefaultRepeatCount];
796  [self setUnpackedExtensions:unpackedOrig repeatedCount:kGPBDefaultRepeatCount];
797
798  NSData *packedData = [packedOrig data];
799  NSData *unpackedData = [unpackedOrig data];
800  XCTAssertNotNil(packedData);
801  XCTAssertNotNil(unpackedData);
802  XCTAssertNotEqualObjects(packedData, unpackedData,
803                           @"Data should differ (packed vs unpacked) use");
804
805  NSError *error = nil;
806  TestPackedExtensions *packedParse =
807      [TestPackedExtensions parseFromData:unpackedData
808                        extensionRegistry:[UnittestRoot extensionRegistry]
809                                    error:&error];
810  XCTAssertNotNil(packedParse);
811  XCTAssertNil(error);
812  XCTAssertEqualObjects(packedParse, packedOrig);
813
814  error = nil;
815  TestUnpackedExtensions *unpackedParsed =
816      [TestUnpackedExtensions parseFromData:packedData
817                          extensionRegistry:[UnittestRoot extensionRegistry]
818                                      error:&error];
819  XCTAssertNotNil(unpackedParsed);
820  XCTAssertNil(error);
821  XCTAssertEqualObjects(unpackedParsed, unpackedOrig);
822}
823
824- (void)testPackedExtensionVsFieldParsing {
825  // Extensions and fields end up on the wire the same way, so they can parse
826  // each other.
827
828  TestPackedTypes *fieldsOrig = [TestPackedTypes message];
829  TestPackedExtensions *extsOrig = [TestPackedExtensions message];
830  [self setPackedFields:fieldsOrig repeatedCount:kGPBDefaultRepeatCount];
831  [self setPackedExtensions:extsOrig repeatedCount:kGPBDefaultRepeatCount];
832
833  NSData *fieldsData = [fieldsOrig data];
834  NSData *extsData = [extsOrig data];
835  XCTAssertNotNil(fieldsData);
836  XCTAssertNotNil(extsData);
837  XCTAssertEqualObjects(fieldsData, extsData);
838
839  NSError *error = nil;
840  TestPackedTypes *fieldsParse =
841      [TestPackedTypes parseFromData:extsData error:&error];
842  XCTAssertNotNil(fieldsParse);
843  XCTAssertNil(error);
844  XCTAssertEqualObjects(fieldsParse, fieldsOrig);
845
846  error = nil;
847  TestPackedExtensions *extsParse =
848      [TestPackedExtensions parseFromData:fieldsData
849                        extensionRegistry:[UnittestRoot extensionRegistry]
850                                    error:&error];
851  XCTAssertNotNil(extsParse);
852  XCTAssertNil(error);
853  XCTAssertEqualObjects(extsParse, extsOrig);
854}
855
856- (void)testUnpackedExtensionVsFieldParsing {
857  // Extensions and fields end up on the wire the same way, so they can parse
858  // each other.
859
860  TestUnpackedTypes *fieldsOrig = [TestUnpackedTypes message];
861  TestUnpackedExtensions *extsOrig = [TestUnpackedExtensions message];
862  [self setUnpackedFields:fieldsOrig repeatedCount:3];
863  [self setUnpackedExtensions:extsOrig repeatedCount:3];
864
865  NSData *fieldsData = [fieldsOrig data];
866  NSData *extsData = [extsOrig data];
867  XCTAssertNotNil(fieldsData);
868  XCTAssertNotNil(extsData);
869  XCTAssertEqualObjects(fieldsData, extsData);
870
871  TestUnpackedTypes *fieldsParse =
872      [TestUnpackedTypes parseFromData:extsData error:NULL];
873  XCTAssertNotNil(fieldsParse);
874  XCTAssertEqualObjects(fieldsParse, fieldsOrig);
875
876  TestUnpackedExtensions *extsParse =
877      [TestUnpackedExtensions parseFromData:fieldsData
878                          extensionRegistry:[UnittestRoot extensionRegistry]
879                                      error:NULL];
880  XCTAssertNotNil(extsParse);
881  XCTAssertEqualObjects(extsParse, extsOrig);
882}
883
884#pragma mark - Subset from from map_tests.cc
885
886// TEST(GeneratedMapFieldTest, StandardWireFormat)
887- (void)testMap_StandardWireFormat {
888  NSData *data = DataFromCStr("\x0A\x04\x08\x01\x10\x01");
889
890  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
891  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
892  int32_t val = 666;
893  XCTAssertTrue([msg.mapInt32Int32 valueForKey:1 value:&val]);
894  XCTAssertEqual(val, 1);
895
896  [msg release];
897}
898
899// TEST(GeneratedMapFieldTest, UnorderedWireFormat)
900- (void)testMap_UnorderedWireFormat {
901  // put value before key in wire format
902  NSData *data = DataFromCStr("\x0A\x04\x10\x01\x08\x02");
903
904  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
905  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
906  int32_t val = 666;
907  XCTAssertTrue([msg.mapInt32Int32 valueForKey:2 value:&val]);
908  XCTAssertEqual(val, 1);
909
910  [msg release];
911}
912
913// TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat)
914- (void)testMap_DuplicatedKeyWireFormat {
915  // Two key fields in wire format
916  NSData *data = DataFromCStr("\x0A\x06\x08\x01\x08\x02\x10\x01");
917
918  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
919  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
920  int32_t val = 666;
921  XCTAssertTrue([msg.mapInt32Int32 valueForKey:2 value:&val]);
922  XCTAssertEqual(val, 1);
923
924  [msg release];
925}
926
927// TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat)
928- (void)testMap_DuplicatedValueWireFormat {
929  // Two value fields in wire format
930  NSData *data = DataFromCStr("\x0A\x06\x08\x01\x10\x01\x10\x02");
931
932  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
933  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
934  int32_t val = 666;
935  XCTAssertTrue([msg.mapInt32Int32 valueForKey:1 value:&val]);
936  XCTAssertEqual(val, 2);
937
938  [msg release];
939}
940
941// TEST(GeneratedMapFieldTest, MissedKeyWireFormat)
942- (void)testMap_MissedKeyWireFormat {
943  // No key field in wire format
944  NSData *data = DataFromCStr("\x0A\x02\x10\x01");
945
946  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
947  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
948  int32_t val = 666;
949  XCTAssertTrue([msg.mapInt32Int32 valueForKey:0 value:&val]);
950  XCTAssertEqual(val, 1);
951
952  [msg release];
953}
954
955// TEST(GeneratedMapFieldTest, MissedValueWireFormat)
956- (void)testMap_MissedValueWireFormat {
957  // No value field in wire format
958  NSData *data = DataFromCStr("\x0A\x02\x08\x01");
959
960  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
961  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
962  int32_t val = 666;
963  XCTAssertTrue([msg.mapInt32Int32 valueForKey:1 value:&val]);
964  XCTAssertEqual(val, 0);
965
966  [msg release];
967}
968
969// TEST(GeneratedMapFieldTest, UnknownFieldWireFormat)
970- (void)testMap_UnknownFieldWireFormat {
971  // Unknown field in wire format
972  NSData *data = DataFromCStr("\x0A\x06\x08\x02\x10\x03\x18\x01");
973
974  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
975  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
976  int32_t val = 666;
977  XCTAssertTrue([msg.mapInt32Int32 valueForKey:2 value:&val]);
978  XCTAssertEqual(val, 3);
979
980  [msg release];
981}
982
983// TEST(GeneratedMapFieldTest, CorruptedWireFormat)
984- (void)testMap_CorruptedWireFormat {
985  // corrupted data in wire format
986  NSData *data = DataFromCStr("\x0A\x06\x08\x02\x11\x03");
987
988  NSError *error = nil;
989  TestMap *msg = [TestMap parseFromData:data error:&error];
990  XCTAssertNil(msg);
991  XCTAssertNotNil(error);
992  XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain);
993  XCTAssertEqual(error.code, GPBMessageErrorCodeMalformedData);
994}
995
996// TEST(GeneratedMapFieldTest, Proto2UnknownEnum)
997- (void)testMap_Proto2UnknownEnum {
998  TestEnumMapPlusExtra *orig = [[TestEnumMapPlusExtra alloc] init];
999
1000  orig.knownMapField = [GPBInt32EnumDictionary
1001      dictionaryWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue];
1002  orig.unknownMapField = [GPBInt32EnumDictionary
1003      dictionaryWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue];
1004  [orig.knownMapField setValue:Proto2MapEnumPlusExtra_EProto2MapEnumFoo
1005                        forKey:0];
1006  [orig.unknownMapField setValue:Proto2MapEnumPlusExtra_EProto2MapEnumExtra
1007                          forKey:0];
1008
1009  NSData *data = [orig data];
1010  XCTAssertNotNil(data);
1011  TestEnumMap *msg1 = [TestEnumMap parseFromData:data error:NULL];
1012  XCTAssertEqual(msg1.knownMapField.count, 1U);
1013  int32_t val = -1;
1014  XCTAssertTrue([msg1.knownMapField valueForKey:0 value:&val]);
1015  XCTAssertEqual(val, Proto2MapEnum_Proto2MapEnumFoo);
1016  XCTAssertEqual(msg1.unknownFields.countOfFields, 1U);
1017
1018  data = [msg1 data];
1019  TestEnumMapPlusExtra *msg2 =
1020      [TestEnumMapPlusExtra parseFromData:data error:NULL];
1021  val = -1;
1022  XCTAssertEqual(msg2.knownMapField.count, 1U);
1023  XCTAssertTrue([msg2.knownMapField valueForKey:0 value:&val]);
1024  XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumFoo);
1025  val = -1;
1026  XCTAssertEqual(msg2.unknownMapField.count, 1U);
1027  XCTAssertTrue([msg2.unknownMapField valueForKey:0 value:&val]);
1028  XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumExtra);
1029  XCTAssertEqual(msg2.unknownFields.countOfFields, 0U);
1030
1031  XCTAssertEqualObjects(orig, msg2);
1032
1033  [orig release];
1034}
1035
1036#pragma mark - Map Round Tripping
1037
1038- (void)testProto2MapRoundTripping {
1039  Message2 *msg = [[Message2 alloc] init];
1040
1041  // Key/Value data should result in different byte lengths on wire to ensure
1042  // everything is right.
1043  [msg.mapInt32Int32 setValue:1000 forKey:200];
1044  [msg.mapInt32Int32 setValue:101 forKey:2001];
1045  [msg.mapInt64Int64 setValue:1002 forKey:202];
1046  [msg.mapInt64Int64 setValue:103 forKey:2003];
1047  [msg.mapUint32Uint32 setValue:1004 forKey:204];
1048  [msg.mapUint32Uint32 setValue:105 forKey:2005];
1049  [msg.mapUint64Uint64 setValue:1006 forKey:206];
1050  [msg.mapUint64Uint64 setValue:107 forKey:2007];
1051  [msg.mapSint32Sint32 setValue:1008 forKey:208];
1052  [msg.mapSint32Sint32 setValue:109 forKey:2009];
1053  [msg.mapSint64Sint64 setValue:1010 forKey:210];
1054  [msg.mapSint64Sint64 setValue:111 forKey:2011];
1055  [msg.mapFixed32Fixed32 setValue:1012 forKey:212];
1056  [msg.mapFixed32Fixed32 setValue:113 forKey:2013];
1057  [msg.mapFixed64Fixed64 setValue:1014 forKey:214];
1058  [msg.mapFixed64Fixed64 setValue:115 forKey:2015];
1059  [msg.mapSfixed32Sfixed32 setValue:1016 forKey:216];
1060  [msg.mapSfixed32Sfixed32 setValue:117 forKey:2017];
1061  [msg.mapSfixed64Sfixed64 setValue:1018 forKey:218];
1062  [msg.mapSfixed64Sfixed64 setValue:119 forKey:2019];
1063  [msg.mapInt32Float setValue:1020.f forKey:220];
1064  [msg.mapInt32Float setValue:121.f forKey:2021];
1065  [msg.mapInt32Double setValue:1022. forKey:222];
1066  [msg.mapInt32Double setValue:123. forKey:2023];
1067  [msg.mapBoolBool setValue:false forKey:true];
1068  [msg.mapBoolBool setValue:true forKey:false];
1069  msg.mapStringString[@"224"] = @"1024";
1070  msg.mapStringString[@"2025"] = @"125";
1071  msg.mapStringBytes[@"226"] = DataFromCStr("1026");
1072  msg.mapStringBytes[@"2027"] = DataFromCStr("127");
1073  Message2 *val1 = [[Message2 alloc] init];
1074  val1.optionalInt32 = 1028;
1075  Message2 *val2 = [[Message2 alloc] init];
1076  val2.optionalInt32 = 129;
1077  [msg.mapStringMessage setValue:val1 forKey:@"228"];
1078  [msg.mapStringMessage setValue:val2 forKey:@"2029"];
1079  [msg.mapInt32Bytes setObject:DataFromCStr("1030 bytes") forKey:230];
1080  [msg.mapInt32Bytes setObject:DataFromCStr("131") forKey:2031];
1081  [msg.mapInt32Enum setValue:Message2_Enum_Bar forKey:232];
1082  [msg.mapInt32Enum setValue:Message2_Enum_Baz forKey:2033];
1083  Message2 *val3 = [[Message2 alloc] init];
1084  val3.optionalInt32 = 1034;
1085  Message2 *val4 = [[Message2 alloc] init];
1086  val4.optionalInt32 = 135;
1087  [msg.mapInt32Message setObject:val3 forKey:234];
1088  [msg.mapInt32Message setObject:val4 forKey:2035];
1089
1090  NSData *data = [msg data];
1091  XCTAssertNotNil(data);
1092  Message2 *msg2 = [[Message2 alloc] initWithData:data error:NULL];
1093
1094  XCTAssertNotEqual(msg2, msg);  // Pointer comparison
1095  XCTAssertEqualObjects(msg2, msg);
1096
1097  [val4 release];
1098  [val3 release];
1099  [val2 release];
1100  [val1 release];
1101  [msg2 release];
1102  [msg release];
1103}
1104
1105@end
1106