• 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/MapUnittest.pbobjc.h"
38#import "google/protobuf/Unittest.pbobjc.h"
39#import "google/protobuf/UnittestObjcStartup.pbobjc.h"
40#import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
41#import "google/protobuf/UnittestRuntimeProto3.pbobjc.h"
42
43@interface MessageRuntimeTests : GPBTestCase
44@end
45
46@implementation MessageRuntimeTests
47
48// TODO(thomasvl): Pull tests over from GPBMessageTests that are runtime
49// specific.
50
51- (void)testStartupOrdering {
52  // Just have to create a message.  Nothing else uses the classes from
53  // this file, so the first selector invoked on the class will initialize
54  // it, which also initializes the root.
55  TestObjCStartupMessage *message = [TestObjCStartupMessage message];
56  XCTAssertNotNil(message);
57}
58
59- (void)testProto2HasMethodSupport {
60  NSArray *names = @[
61    @"Int32",
62    @"Int64",
63    @"Uint32",
64    @"Uint64",
65    @"Sint32",
66    @"Sint64",
67    @"Fixed32",
68    @"Fixed64",
69    @"Sfixed32",
70    @"Sfixed64",
71    @"Float",
72    @"Double",
73    @"Bool",
74    @"String",
75    @"Bytes",
76    @"Group",
77    @"Message",
78    @"Enum",
79  ];
80
81  // Proto2 gets:
82
83  // Single fields - has*/setHas* is valid.
84
85  for (NSString *name in names) {
86    // build the selector, i.e. - hasOptionalInt32/setHasOptionalInt32:
87    SEL hasSel = NSSelectorFromString(
88        [NSString stringWithFormat:@"hasOptional%@", name]);
89    SEL setHasSel = NSSelectorFromString(
90        [NSString stringWithFormat:@"setHasOptional%@:", name]);
91    XCTAssertTrue([Message2 instancesRespondToSelector:hasSel], @"field: %@",
92                  name);
93    XCTAssertTrue([Message2 instancesRespondToSelector:setHasSel], @"field: %@",
94                  name);
95  }
96
97  // Repeated fields
98  //  - no has*/setHas*
99  //  - *Count
100
101  for (NSString *name in names) {
102    // build the selector, i.e. - hasRepeatedInt32Array/setHasRepeatedInt32Array:
103    SEL hasSel = NSSelectorFromString(
104        [NSString stringWithFormat:@"hasRepeated%@Array", name]);
105    SEL setHasSel = NSSelectorFromString(
106        [NSString stringWithFormat:@"setHasRepeated%@Array:", name]);
107    XCTAssertFalse([Message2 instancesRespondToSelector:hasSel], @"field: %@",
108                   name);
109    XCTAssertFalse([Message2 instancesRespondToSelector:setHasSel],
110                   @"field: %@", name);
111    // build the selector, i.e. - repeatedInt32Array_Count
112    SEL countSel = NSSelectorFromString(
113        [NSString stringWithFormat:@"repeated%@Array_Count", name]);
114    XCTAssertTrue([Message2 instancesRespondToSelector:countSel], @"field: %@",
115                   name);
116  }
117
118  // OneOf fields - no has*/setHas*
119
120  for (NSString *name in names) {
121    // build the selector, i.e. - hasOneofInt32/setHasOneofInt32:
122    SEL hasSel =
123        NSSelectorFromString([NSString stringWithFormat:@"hasOneof%@", name]);
124    SEL setHasSel = NSSelectorFromString(
125        [NSString stringWithFormat:@"setHasOneof%@:", name]);
126    XCTAssertFalse([Message2 instancesRespondToSelector:hasSel], @"field: %@",
127                   name);
128    XCTAssertFalse([Message2 instancesRespondToSelector:setHasSel],
129                   @"field: %@", name);
130  }
131
132  // map<> fields
133  //  - no has*/setHas*
134  //  - *Count
135
136  NSArray *mapNames = @[
137    @"Int32Int32",
138    @"Int64Int64",
139    @"Uint32Uint32",
140    @"Uint64Uint64",
141    @"Sint32Sint32",
142    @"Sint64Sint64",
143    @"Fixed32Fixed32",
144    @"Fixed64Fixed64",
145    @"Sfixed32Sfixed32",
146    @"Sfixed64Sfixed64",
147    @"Int32Float",
148    @"Int32Double",
149    @"BoolBool",
150    @"StringString",
151    @"StringBytes",
152    @"StringMessage",
153    @"Int32Bytes",
154    @"Int32Enum",
155    @"Int32Message",
156  ];
157
158  for (NSString *name in mapNames) {
159    // build the selector, i.e. - hasMapInt32Int32/setHasMapInt32Int32:
160    SEL hasSel = NSSelectorFromString(
161        [NSString stringWithFormat:@"hasMap%@", name]);
162    SEL setHasSel = NSSelectorFromString(
163        [NSString stringWithFormat:@"setHasMap%@:", name]);
164    XCTAssertFalse([Message2 instancesRespondToSelector:hasSel], @"field: %@",
165                   name);
166    XCTAssertFalse([Message2 instancesRespondToSelector:setHasSel],
167                   @"field: %@", name);
168    // build the selector, i.e. - mapInt32Int32Count
169    SEL countSel = NSSelectorFromString(
170        [NSString stringWithFormat:@"map%@_Count", name]);
171    XCTAssertTrue([Message2 instancesRespondToSelector:countSel], @"field: %@",
172                   name);
173  }
174
175}
176
177- (void)testProto3HasMethodSupport {
178  NSArray *names = @[
179    @"Int32",
180    @"Int64",
181    @"Uint32",
182    @"Uint64",
183    @"Sint32",
184    @"Sint64",
185    @"Fixed32",
186    @"Fixed64",
187    @"Sfixed32",
188    @"Sfixed64",
189    @"Float",
190    @"Double",
191    @"Bool",
192    @"String",
193    @"Bytes",
194    @"Message",
195    @"Enum",
196  ];
197
198  // Proto3 gets:
199
200  // Single fields
201  //  - has*/setHas* invalid for primative types.
202  //  - has*/setHas* valid for Message.
203
204  for (NSString *name in names) {
205    // build the selector, i.e. - hasOptionalInt32/setHasOptionalInt32:
206    SEL hasSel = NSSelectorFromString(
207        [NSString stringWithFormat:@"hasOptional%@", name]);
208    SEL setHasSel = NSSelectorFromString(
209        [NSString stringWithFormat:@"setHasOptional%@:", name]);
210    if ([name isEqual:@"Message"]) {
211      // Sub messages/groups are the exception.
212      XCTAssertTrue([Message3 instancesRespondToSelector:hasSel], @"field: %@",
213                    name);
214      XCTAssertTrue([Message3 instancesRespondToSelector:setHasSel],
215                    @"field: %@", name);
216    } else {
217      XCTAssertFalse([Message3 instancesRespondToSelector:hasSel], @"field: %@",
218                     name);
219      XCTAssertFalse([Message3 instancesRespondToSelector:setHasSel],
220                     @"field: %@", name);
221    }
222  }
223
224  // Repeated fields
225  //  - no has*/setHas*
226  //  - *Count
227
228  for (NSString *name in names) {
229    // build the selector, i.e. - hasRepeatedInt32Array/setHasRepeatedInt32Array:
230    SEL hasSel = NSSelectorFromString(
231        [NSString stringWithFormat:@"hasRepeated%@Array", name]);
232    SEL setHasSel = NSSelectorFromString(
233        [NSString stringWithFormat:@"setHasRepeated%@Array:", name]);
234    XCTAssertFalse([Message3 instancesRespondToSelector:hasSel], @"field: %@",
235                   name);
236    XCTAssertFalse([Message3 instancesRespondToSelector:setHasSel],
237                   @"field: %@", name);
238    // build the selector, i.e. - repeatedInt32Array_Count
239    SEL countSel = NSSelectorFromString(
240        [NSString stringWithFormat:@"repeated%@Array_Count", name]);
241    XCTAssertTrue([Message2 instancesRespondToSelector:countSel], @"field: %@",
242                  name);
243  }
244
245  // OneOf fields - no has*/setHas*
246
247  for (NSString *name in names) {
248    // build the selector, i.e. - hasOneofInt32/setHasOneofInt32:
249    SEL hasSel =
250        NSSelectorFromString([NSString stringWithFormat:@"hasOneof%@", name]);
251    SEL setHasSel = NSSelectorFromString(
252        [NSString stringWithFormat:@"setHasOneof%@:", name]);
253    XCTAssertFalse([Message2 instancesRespondToSelector:hasSel], @"field: %@",
254                   name);
255    XCTAssertFalse([Message2 instancesRespondToSelector:setHasSel],
256                   @"field: %@", name);
257  }
258
259  // map<> fields
260  //  - no has*/setHas*
261  //  - *Count
262
263  NSArray *mapNames = @[
264    @"Int32Int32",
265    @"Int64Int64",
266    @"Uint32Uint32",
267    @"Uint64Uint64",
268    @"Sint32Sint32",
269    @"Sint64Sint64",
270    @"Fixed32Fixed32",
271    @"Fixed64Fixed64",
272    @"Sfixed32Sfixed32",
273    @"Sfixed64Sfixed64",
274    @"Int32Float",
275    @"Int32Double",
276    @"BoolBool",
277    @"StringString",
278    @"StringBytes",
279    @"StringMessage",
280    @"Int32Bytes",
281    @"Int32Enum",
282    @"Int32Message",
283  ];
284
285  for (NSString *name in mapNames) {
286    // build the selector, i.e. - hasMapInt32Int32/setHasMapInt32Int32:
287    SEL hasSel = NSSelectorFromString(
288        [NSString stringWithFormat:@"hasMap%@", name]);
289    SEL setHasSel = NSSelectorFromString(
290        [NSString stringWithFormat:@"setHasMap%@:", name]);
291    XCTAssertFalse([Message2 instancesRespondToSelector:hasSel], @"field: %@",
292                   name);
293    XCTAssertFalse([Message2 instancesRespondToSelector:setHasSel],
294                   @"field: %@", name);
295    // build the selector, i.e. - mapInt32Int32Count
296    SEL countSel = NSSelectorFromString(
297        [NSString stringWithFormat:@"map%@_Count", name]);
298    XCTAssertTrue([Message2 instancesRespondToSelector:countSel], @"field: %@",
299                   name);
300  }
301}
302
303- (void)testProto2SingleFieldHasBehavior {
304  //
305  // Setting to any value including the default value (0) should result has*
306  // being true.
307  //
308
309//%PDDM-DEFINE PROTO2_TEST_HAS_FIELD(FIELD, NON_ZERO_VALUE, ZERO_VALUE)
310//%  {  // optional##FIELD :: NON_ZERO_VALUE
311//%    Message2 *msg = [[Message2 alloc] init];
312//%    XCTAssertFalse(msg.hasOptional##FIELD);
313//%    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
314//%    msg.optional##FIELD = NON_ZERO_VALUE;
315//%    XCTAssertTrue(msg.hasOptional##FIELD);
316//%    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
317//%    [msg release];
318//%  }
319//%  {  // optional##FIELD :: ZERO_VALUE
320//%    Message2 *msg = [[Message2 alloc] init];
321//%    XCTAssertFalse(msg.hasOptional##FIELD);
322//%    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
323//%    msg.optional##FIELD = ZERO_VALUE;
324//%    XCTAssertTrue(msg.hasOptional##FIELD);
325//%    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
326//%    [msg release];
327//%  }
328//%
329//%PDDM-DEFINE PROTO2_TEST_HAS_FIELDS()
330//%PROTO2_TEST_HAS_FIELD(Int32, 1, 0)
331//%PROTO2_TEST_HAS_FIELD(Int64, 1, 0)
332//%PROTO2_TEST_HAS_FIELD(Uint32, 1, 0)
333//%PROTO2_TEST_HAS_FIELD(Uint64, 1, 0)
334//%PROTO2_TEST_HAS_FIELD(Sint32, 1, 0)
335//%PROTO2_TEST_HAS_FIELD(Sint64, 1, 0)
336//%PROTO2_TEST_HAS_FIELD(Fixed32, 1, 0)
337//%PROTO2_TEST_HAS_FIELD(Fixed64, 1, 0)
338//%PROTO2_TEST_HAS_FIELD(Sfixed32, 1, 0)
339//%PROTO2_TEST_HAS_FIELD(Sfixed64, 1, 0)
340//%PROTO2_TEST_HAS_FIELD(Float, 1.0f, 0.0f)
341//%PROTO2_TEST_HAS_FIELD(Double, 1.0, 0.0)
342//%PROTO2_TEST_HAS_FIELD(Bool, YES, NO)
343//%PROTO2_TEST_HAS_FIELD(String, @"foo", @"")
344//%PROTO2_TEST_HAS_FIELD(Bytes, [@"foo" dataUsingEncoding:NSUTF8StringEncoding], [NSData data])
345//%  //
346//%  // Test doesn't apply to optionalGroup/optionalMessage.
347//%  //
348//%
349//%PROTO2_TEST_HAS_FIELD(Enum, Message2_Enum_Bar, Message2_Enum_Foo)
350//%PDDM-EXPAND PROTO2_TEST_HAS_FIELDS()
351// This block of code is generated, do not edit it directly.
352
353  {  // optionalInt32 :: 1
354    Message2 *msg = [[Message2 alloc] init];
355    XCTAssertFalse(msg.hasOptionalInt32);
356    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt32));
357    msg.optionalInt32 = 1;
358    XCTAssertTrue(msg.hasOptionalInt32);
359    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt32));
360    [msg release];
361  }
362  {  // optionalInt32 :: 0
363    Message2 *msg = [[Message2 alloc] init];
364    XCTAssertFalse(msg.hasOptionalInt32);
365    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt32));
366    msg.optionalInt32 = 0;
367    XCTAssertTrue(msg.hasOptionalInt32);
368    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt32));
369    [msg release];
370  }
371
372  {  // optionalInt64 :: 1
373    Message2 *msg = [[Message2 alloc] init];
374    XCTAssertFalse(msg.hasOptionalInt64);
375    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt64));
376    msg.optionalInt64 = 1;
377    XCTAssertTrue(msg.hasOptionalInt64);
378    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt64));
379    [msg release];
380  }
381  {  // optionalInt64 :: 0
382    Message2 *msg = [[Message2 alloc] init];
383    XCTAssertFalse(msg.hasOptionalInt64);
384    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt64));
385    msg.optionalInt64 = 0;
386    XCTAssertTrue(msg.hasOptionalInt64);
387    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalInt64));
388    [msg release];
389  }
390
391  {  // optionalUint32 :: 1
392    Message2 *msg = [[Message2 alloc] init];
393    XCTAssertFalse(msg.hasOptionalUint32);
394    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint32));
395    msg.optionalUint32 = 1;
396    XCTAssertTrue(msg.hasOptionalUint32);
397    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint32));
398    [msg release];
399  }
400  {  // optionalUint32 :: 0
401    Message2 *msg = [[Message2 alloc] init];
402    XCTAssertFalse(msg.hasOptionalUint32);
403    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint32));
404    msg.optionalUint32 = 0;
405    XCTAssertTrue(msg.hasOptionalUint32);
406    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint32));
407    [msg release];
408  }
409
410  {  // optionalUint64 :: 1
411    Message2 *msg = [[Message2 alloc] init];
412    XCTAssertFalse(msg.hasOptionalUint64);
413    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint64));
414    msg.optionalUint64 = 1;
415    XCTAssertTrue(msg.hasOptionalUint64);
416    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint64));
417    [msg release];
418  }
419  {  // optionalUint64 :: 0
420    Message2 *msg = [[Message2 alloc] init];
421    XCTAssertFalse(msg.hasOptionalUint64);
422    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint64));
423    msg.optionalUint64 = 0;
424    XCTAssertTrue(msg.hasOptionalUint64);
425    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalUint64));
426    [msg release];
427  }
428
429  {  // optionalSint32 :: 1
430    Message2 *msg = [[Message2 alloc] init];
431    XCTAssertFalse(msg.hasOptionalSint32);
432    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint32));
433    msg.optionalSint32 = 1;
434    XCTAssertTrue(msg.hasOptionalSint32);
435    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint32));
436    [msg release];
437  }
438  {  // optionalSint32 :: 0
439    Message2 *msg = [[Message2 alloc] init];
440    XCTAssertFalse(msg.hasOptionalSint32);
441    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint32));
442    msg.optionalSint32 = 0;
443    XCTAssertTrue(msg.hasOptionalSint32);
444    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint32));
445    [msg release];
446  }
447
448  {  // optionalSint64 :: 1
449    Message2 *msg = [[Message2 alloc] init];
450    XCTAssertFalse(msg.hasOptionalSint64);
451    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint64));
452    msg.optionalSint64 = 1;
453    XCTAssertTrue(msg.hasOptionalSint64);
454    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint64));
455    [msg release];
456  }
457  {  // optionalSint64 :: 0
458    Message2 *msg = [[Message2 alloc] init];
459    XCTAssertFalse(msg.hasOptionalSint64);
460    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint64));
461    msg.optionalSint64 = 0;
462    XCTAssertTrue(msg.hasOptionalSint64);
463    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSint64));
464    [msg release];
465  }
466
467  {  // optionalFixed32 :: 1
468    Message2 *msg = [[Message2 alloc] init];
469    XCTAssertFalse(msg.hasOptionalFixed32);
470    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed32));
471    msg.optionalFixed32 = 1;
472    XCTAssertTrue(msg.hasOptionalFixed32);
473    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed32));
474    [msg release];
475  }
476  {  // optionalFixed32 :: 0
477    Message2 *msg = [[Message2 alloc] init];
478    XCTAssertFalse(msg.hasOptionalFixed32);
479    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed32));
480    msg.optionalFixed32 = 0;
481    XCTAssertTrue(msg.hasOptionalFixed32);
482    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed32));
483    [msg release];
484  }
485
486  {  // optionalFixed64 :: 1
487    Message2 *msg = [[Message2 alloc] init];
488    XCTAssertFalse(msg.hasOptionalFixed64);
489    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed64));
490    msg.optionalFixed64 = 1;
491    XCTAssertTrue(msg.hasOptionalFixed64);
492    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed64));
493    [msg release];
494  }
495  {  // optionalFixed64 :: 0
496    Message2 *msg = [[Message2 alloc] init];
497    XCTAssertFalse(msg.hasOptionalFixed64);
498    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed64));
499    msg.optionalFixed64 = 0;
500    XCTAssertTrue(msg.hasOptionalFixed64);
501    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFixed64));
502    [msg release];
503  }
504
505  {  // optionalSfixed32 :: 1
506    Message2 *msg = [[Message2 alloc] init];
507    XCTAssertFalse(msg.hasOptionalSfixed32);
508    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed32));
509    msg.optionalSfixed32 = 1;
510    XCTAssertTrue(msg.hasOptionalSfixed32);
511    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed32));
512    [msg release];
513  }
514  {  // optionalSfixed32 :: 0
515    Message2 *msg = [[Message2 alloc] init];
516    XCTAssertFalse(msg.hasOptionalSfixed32);
517    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed32));
518    msg.optionalSfixed32 = 0;
519    XCTAssertTrue(msg.hasOptionalSfixed32);
520    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed32));
521    [msg release];
522  }
523
524  {  // optionalSfixed64 :: 1
525    Message2 *msg = [[Message2 alloc] init];
526    XCTAssertFalse(msg.hasOptionalSfixed64);
527    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed64));
528    msg.optionalSfixed64 = 1;
529    XCTAssertTrue(msg.hasOptionalSfixed64);
530    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed64));
531    [msg release];
532  }
533  {  // optionalSfixed64 :: 0
534    Message2 *msg = [[Message2 alloc] init];
535    XCTAssertFalse(msg.hasOptionalSfixed64);
536    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed64));
537    msg.optionalSfixed64 = 0;
538    XCTAssertTrue(msg.hasOptionalSfixed64);
539    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalSfixed64));
540    [msg release];
541  }
542
543  {  // optionalFloat :: 1.0f
544    Message2 *msg = [[Message2 alloc] init];
545    XCTAssertFalse(msg.hasOptionalFloat);
546    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFloat));
547    msg.optionalFloat = 1.0f;
548    XCTAssertTrue(msg.hasOptionalFloat);
549    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFloat));
550    [msg release];
551  }
552  {  // optionalFloat :: 0.0f
553    Message2 *msg = [[Message2 alloc] init];
554    XCTAssertFalse(msg.hasOptionalFloat);
555    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFloat));
556    msg.optionalFloat = 0.0f;
557    XCTAssertTrue(msg.hasOptionalFloat);
558    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalFloat));
559    [msg release];
560  }
561
562  {  // optionalDouble :: 1.0
563    Message2 *msg = [[Message2 alloc] init];
564    XCTAssertFalse(msg.hasOptionalDouble);
565    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalDouble));
566    msg.optionalDouble = 1.0;
567    XCTAssertTrue(msg.hasOptionalDouble);
568    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalDouble));
569    [msg release];
570  }
571  {  // optionalDouble :: 0.0
572    Message2 *msg = [[Message2 alloc] init];
573    XCTAssertFalse(msg.hasOptionalDouble);
574    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalDouble));
575    msg.optionalDouble = 0.0;
576    XCTAssertTrue(msg.hasOptionalDouble);
577    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalDouble));
578    [msg release];
579  }
580
581  {  // optionalBool :: YES
582    Message2 *msg = [[Message2 alloc] init];
583    XCTAssertFalse(msg.hasOptionalBool);
584    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBool));
585    msg.optionalBool = YES;
586    XCTAssertTrue(msg.hasOptionalBool);
587    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBool));
588    [msg release];
589  }
590  {  // optionalBool :: NO
591    Message2 *msg = [[Message2 alloc] init];
592    XCTAssertFalse(msg.hasOptionalBool);
593    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBool));
594    msg.optionalBool = NO;
595    XCTAssertTrue(msg.hasOptionalBool);
596    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBool));
597    [msg release];
598  }
599
600  {  // optionalString :: @"foo"
601    Message2 *msg = [[Message2 alloc] init];
602    XCTAssertFalse(msg.hasOptionalString);
603    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
604    msg.optionalString = @"foo";
605    XCTAssertTrue(msg.hasOptionalString);
606    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
607    [msg release];
608  }
609  {  // optionalString :: @""
610    Message2 *msg = [[Message2 alloc] init];
611    XCTAssertFalse(msg.hasOptionalString);
612    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
613    msg.optionalString = @"";
614    XCTAssertTrue(msg.hasOptionalString);
615    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
616    [msg release];
617  }
618
619  {  // optionalBytes :: [@"foo" dataUsingEncoding:NSUTF8StringEncoding]
620    Message2 *msg = [[Message2 alloc] init];
621    XCTAssertFalse(msg.hasOptionalBytes);
622    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
623    msg.optionalBytes = [@"foo" dataUsingEncoding:NSUTF8StringEncoding];
624    XCTAssertTrue(msg.hasOptionalBytes);
625    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
626    [msg release];
627  }
628  {  // optionalBytes :: [NSData data]
629    Message2 *msg = [[Message2 alloc] init];
630    XCTAssertFalse(msg.hasOptionalBytes);
631    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
632    msg.optionalBytes = [NSData data];
633    XCTAssertTrue(msg.hasOptionalBytes);
634    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
635    [msg release];
636  }
637
638  //
639  // Test doesn't apply to optionalGroup/optionalMessage.
640  //
641
642  {  // optionalEnum :: Message2_Enum_Bar
643    Message2 *msg = [[Message2 alloc] init];
644    XCTAssertFalse(msg.hasOptionalEnum);
645    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalEnum));
646    msg.optionalEnum = Message2_Enum_Bar;
647    XCTAssertTrue(msg.hasOptionalEnum);
648    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalEnum));
649    [msg release];
650  }
651  {  // optionalEnum :: Message2_Enum_Foo
652    Message2 *msg = [[Message2 alloc] init];
653    XCTAssertFalse(msg.hasOptionalEnum);
654    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalEnum));
655    msg.optionalEnum = Message2_Enum_Foo;
656    XCTAssertTrue(msg.hasOptionalEnum);
657    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalEnum));
658    [msg release];
659  }
660
661//%PDDM-EXPAND-END PROTO2_TEST_HAS_FIELDS()
662}
663
664- (void)testProto3SingleFieldHasBehavior {
665  //
666  // Setting to any value including the default value (0) should result has*
667  // being true.
668  //
669
670//%PDDM-DEFINE PROTO3_TEST_HAS_FIELD(FIELD, NON_ZERO_VALUE, ZERO_VALUE)
671//%  {  // optional##FIELD
672//%    Message3 *msg = [[Message3 alloc] init];
673//%    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_Optional##FIELD));
674//%    msg.optional##FIELD = NON_ZERO_VALUE;
675//%    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_Optional##FIELD));
676//%    msg.optional##FIELD = ZERO_VALUE;
677//%    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_Optional##FIELD));
678//%    [msg release];
679//%  }
680//%
681//%PDDM-DEFINE PROTO3_TEST_HAS_FIELDS()
682//%PROTO3_TEST_HAS_FIELD(Int32, 1, 0)
683//%PROTO3_TEST_HAS_FIELD(Int64, 1, 0)
684//%PROTO3_TEST_HAS_FIELD(Uint32, 1, 0)
685//%PROTO3_TEST_HAS_FIELD(Uint64, 1, 0)
686//%PROTO3_TEST_HAS_FIELD(Sint32, 1, 0)
687//%PROTO3_TEST_HAS_FIELD(Sint64, 1, 0)
688//%PROTO3_TEST_HAS_FIELD(Fixed32, 1, 0)
689//%PROTO3_TEST_HAS_FIELD(Fixed64, 1, 0)
690//%PROTO3_TEST_HAS_FIELD(Sfixed32, 1, 0)
691//%PROTO3_TEST_HAS_FIELD(Sfixed64, 1, 0)
692//%PROTO3_TEST_HAS_FIELD(Float, 1.0f, 0.0f)
693//%PROTO3_TEST_HAS_FIELD(Double, 1.0, 0.0)
694//%PROTO3_TEST_HAS_FIELD(Bool, YES, NO)
695//%PROTO3_TEST_HAS_FIELD(String, @"foo", @"")
696//%PROTO3_TEST_HAS_FIELD(Bytes, [@"foo" dataUsingEncoding:NSUTF8StringEncoding], [NSData data])
697//%  //
698//%  // Test doesn't apply to optionalGroup/optionalMessage.
699//%  //
700//%
701//%PROTO3_TEST_HAS_FIELD(Enum, Message3_Enum_Bar, Message3_Enum_Foo)
702//%PDDM-EXPAND PROTO3_TEST_HAS_FIELDS()
703// This block of code is generated, do not edit it directly.
704
705  {  // optionalInt32
706    Message3 *msg = [[Message3 alloc] init];
707    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalInt32));
708    msg.optionalInt32 = 1;
709    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalInt32));
710    msg.optionalInt32 = 0;
711    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalInt32));
712    [msg release];
713  }
714
715  {  // optionalInt64
716    Message3 *msg = [[Message3 alloc] init];
717    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalInt64));
718    msg.optionalInt64 = 1;
719    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalInt64));
720    msg.optionalInt64 = 0;
721    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalInt64));
722    [msg release];
723  }
724
725  {  // optionalUint32
726    Message3 *msg = [[Message3 alloc] init];
727    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalUint32));
728    msg.optionalUint32 = 1;
729    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalUint32));
730    msg.optionalUint32 = 0;
731    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalUint32));
732    [msg release];
733  }
734
735  {  // optionalUint64
736    Message3 *msg = [[Message3 alloc] init];
737    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalUint64));
738    msg.optionalUint64 = 1;
739    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalUint64));
740    msg.optionalUint64 = 0;
741    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalUint64));
742    [msg release];
743  }
744
745  {  // optionalSint32
746    Message3 *msg = [[Message3 alloc] init];
747    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSint32));
748    msg.optionalSint32 = 1;
749    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSint32));
750    msg.optionalSint32 = 0;
751    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSint32));
752    [msg release];
753  }
754
755  {  // optionalSint64
756    Message3 *msg = [[Message3 alloc] init];
757    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSint64));
758    msg.optionalSint64 = 1;
759    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSint64));
760    msg.optionalSint64 = 0;
761    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSint64));
762    [msg release];
763  }
764
765  {  // optionalFixed32
766    Message3 *msg = [[Message3 alloc] init];
767    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFixed32));
768    msg.optionalFixed32 = 1;
769    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFixed32));
770    msg.optionalFixed32 = 0;
771    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFixed32));
772    [msg release];
773  }
774
775  {  // optionalFixed64
776    Message3 *msg = [[Message3 alloc] init];
777    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFixed64));
778    msg.optionalFixed64 = 1;
779    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFixed64));
780    msg.optionalFixed64 = 0;
781    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFixed64));
782    [msg release];
783  }
784
785  {  // optionalSfixed32
786    Message3 *msg = [[Message3 alloc] init];
787    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSfixed32));
788    msg.optionalSfixed32 = 1;
789    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSfixed32));
790    msg.optionalSfixed32 = 0;
791    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSfixed32));
792    [msg release];
793  }
794
795  {  // optionalSfixed64
796    Message3 *msg = [[Message3 alloc] init];
797    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSfixed64));
798    msg.optionalSfixed64 = 1;
799    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSfixed64));
800    msg.optionalSfixed64 = 0;
801    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalSfixed64));
802    [msg release];
803  }
804
805  {  // optionalFloat
806    Message3 *msg = [[Message3 alloc] init];
807    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFloat));
808    msg.optionalFloat = 1.0f;
809    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFloat));
810    msg.optionalFloat = 0.0f;
811    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalFloat));
812    [msg release];
813  }
814
815  {  // optionalDouble
816    Message3 *msg = [[Message3 alloc] init];
817    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalDouble));
818    msg.optionalDouble = 1.0;
819    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalDouble));
820    msg.optionalDouble = 0.0;
821    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalDouble));
822    [msg release];
823  }
824
825  {  // optionalBool
826    Message3 *msg = [[Message3 alloc] init];
827    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBool));
828    msg.optionalBool = YES;
829    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBool));
830    msg.optionalBool = NO;
831    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBool));
832    [msg release];
833  }
834
835  {  // optionalString
836    Message3 *msg = [[Message3 alloc] init];
837    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalString));
838    msg.optionalString = @"foo";
839    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalString));
840    msg.optionalString = @"";
841    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalString));
842    [msg release];
843  }
844
845  {  // optionalBytes
846    Message3 *msg = [[Message3 alloc] init];
847    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBytes));
848    msg.optionalBytes = [@"foo" dataUsingEncoding:NSUTF8StringEncoding];
849    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBytes));
850    msg.optionalBytes = [NSData data];
851    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBytes));
852    [msg release];
853  }
854
855  //
856  // Test doesn't apply to optionalGroup/optionalMessage.
857  //
858
859  {  // optionalEnum
860    Message3 *msg = [[Message3 alloc] init];
861    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalEnum));
862    msg.optionalEnum = Message3_Enum_Bar;
863    XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalEnum));
864    msg.optionalEnum = Message3_Enum_Foo;
865    XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalEnum));
866    [msg release];
867  }
868
869//%PDDM-EXPAND-END PROTO3_TEST_HAS_FIELDS()
870}
871
872- (void)testAccessingProto2UnknownEnumValues {
873  Message2 *msg = [[Message2 alloc] init];
874
875  // Set it to something non zero, try and confirm it doesn't change.
876
877  msg.optionalEnum = Message2_Enum_Bar;
878  XCTAssertThrowsSpecificNamed(msg.optionalEnum = 666, NSException,
879                               NSInvalidArgumentException);
880  XCTAssertEqual(msg.optionalEnum, Message2_Enum_Bar);
881
882  msg.oneofEnum = Message2_Enum_Bar;
883  XCTAssertThrowsSpecificNamed(msg.oneofEnum = 666, NSException,
884                               NSInvalidArgumentException);
885  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
886
887  [msg release];
888}
889
890- (void)testAccessingProto3UnknownEnumValues {
891  Message3 *msg = [[Message3 alloc] init];
892
893  // Set it to something non zero, try and confirm it doesn't change.
894
895  msg.optionalEnum = Message3_Enum_Bar;
896  XCTAssertThrowsSpecificNamed(msg.optionalEnum = 666, NSException,
897                               NSInvalidArgumentException);
898  XCTAssertEqual(msg.optionalEnum, Message3_Enum_Bar);
899
900  msg.oneofEnum = Message3_Enum_Bar;
901  XCTAssertThrowsSpecificNamed(msg.oneofEnum = 666, NSException,
902                               NSInvalidArgumentException);
903  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Bar);
904
905  // Set via raw api to confirm it works.
906
907  SetMessage3_OptionalEnum_RawValue(msg, 666);
908  XCTAssertEqual(msg.optionalEnum,
909                 Message3_Enum_GPBUnrecognizedEnumeratorValue);
910  XCTAssertEqual(Message3_OptionalEnum_RawValue(msg), 666);
911
912  SetMessage3_OneofEnum_RawValue(msg, 666);
913  XCTAssertEqual(msg.oneofEnum, Message3_Enum_GPBUnrecognizedEnumeratorValue);
914  XCTAssertEqual(Message3_OneofEnum_RawValue(msg), 666);
915
916  [msg release];
917}
918
919- (void)testProto2OneofBasicBehaviors {
920  Message2 *msg = [[Message2 alloc] init];
921
922  NSString *oneofStringDefault = @"string";
923  NSData *oneofBytesDefault = [@"data" dataUsingEncoding:NSUTF8StringEncoding];
924
925  // Nothing set.
926  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_GPBUnsetOneOfCase);
927  XCTAssertEqual(msg.oneofInt32, 100);
928  XCTAssertEqual(msg.oneofInt64, 101);
929  XCTAssertEqual(msg.oneofUint32, 102U);
930  XCTAssertEqual(msg.oneofUint64, 103U);
931  XCTAssertEqual(msg.oneofSint32, 104);
932  XCTAssertEqual(msg.oneofSint64, 105);
933  XCTAssertEqual(msg.oneofFixed32, 106U);
934  XCTAssertEqual(msg.oneofFixed64, 107U);
935  XCTAssertEqual(msg.oneofSfixed32, 108);
936  XCTAssertEqual(msg.oneofSfixed64, 109);
937  XCTAssertEqual(msg.oneofFloat, 110.0f);
938  XCTAssertEqual(msg.oneofDouble, 111.0);
939  XCTAssertEqual(msg.oneofBool, YES);
940  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
941  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
942  XCTAssertNotNil(msg.oneofGroup);
943  XCTAssertNotNil(msg.oneofMessage);
944  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
945
946  // Set, check the case, check everyone has default but the one, confirm case
947  // didn't change.
948
949  msg.oneofInt32 = 1;
950  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
951  XCTAssertEqual(msg.oneofInt32, 1);
952  XCTAssertEqual(msg.oneofInt64, 101);
953  XCTAssertEqual(msg.oneofUint32, 102U);
954  XCTAssertEqual(msg.oneofUint64, 103U);
955  XCTAssertEqual(msg.oneofSint32, 104);
956  XCTAssertEqual(msg.oneofSint64, 105);
957  XCTAssertEqual(msg.oneofFixed32, 106U);
958  XCTAssertEqual(msg.oneofFixed64, 107U);
959  XCTAssertEqual(msg.oneofSfixed32, 108);
960  XCTAssertEqual(msg.oneofSfixed64, 109);
961  XCTAssertEqual(msg.oneofFloat, 110.0f);
962  XCTAssertEqual(msg.oneofDouble, 111.0);
963  XCTAssertEqual(msg.oneofBool, YES);
964  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
965  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
966  XCTAssertNotNil(msg.oneofGroup);
967  XCTAssertNotNil(msg.oneofMessage);
968  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
969  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
970
971  msg.oneofInt64 = 2;
972  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
973  XCTAssertEqual(msg.oneofInt32, 100);
974  XCTAssertEqual(msg.oneofInt64, 2);
975  XCTAssertEqual(msg.oneofUint32, 102U);
976  XCTAssertEqual(msg.oneofUint64, 103U);
977  XCTAssertEqual(msg.oneofSint32, 104);
978  XCTAssertEqual(msg.oneofSint64, 105);
979  XCTAssertEqual(msg.oneofFixed32, 106U);
980  XCTAssertEqual(msg.oneofFixed64, 107U);
981  XCTAssertEqual(msg.oneofSfixed32, 108);
982  XCTAssertEqual(msg.oneofSfixed64, 109);
983  XCTAssertEqual(msg.oneofFloat, 110.0f);
984  XCTAssertEqual(msg.oneofDouble, 111.0);
985  XCTAssertEqual(msg.oneofBool, YES);
986  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
987  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
988  XCTAssertNotNil(msg.oneofGroup);
989  XCTAssertNotNil(msg.oneofMessage);
990  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
991  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
992
993  msg.oneofUint32 = 3;
994  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
995  XCTAssertEqual(msg.oneofInt32, 100);
996  XCTAssertEqual(msg.oneofInt64, 101);
997  XCTAssertEqual(msg.oneofUint32, 3U);
998  XCTAssertEqual(msg.oneofUint64, 103U);
999  XCTAssertEqual(msg.oneofSint32, 104);
1000  XCTAssertEqual(msg.oneofSint64, 105);
1001  XCTAssertEqual(msg.oneofFixed32, 106U);
1002  XCTAssertEqual(msg.oneofFixed64, 107U);
1003  XCTAssertEqual(msg.oneofSfixed32, 108);
1004  XCTAssertEqual(msg.oneofSfixed64, 109);
1005  XCTAssertEqual(msg.oneofFloat, 110.0f);
1006  XCTAssertEqual(msg.oneofDouble, 111.0);
1007  XCTAssertEqual(msg.oneofBool, YES);
1008  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1009  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1010  XCTAssertNotNil(msg.oneofGroup);
1011  XCTAssertNotNil(msg.oneofMessage);
1012  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1013  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
1014
1015  msg.oneofUint64 = 4;
1016  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
1017  XCTAssertEqual(msg.oneofInt32, 100);
1018  XCTAssertEqual(msg.oneofInt64, 101);
1019  XCTAssertEqual(msg.oneofUint32, 102U);
1020  XCTAssertEqual(msg.oneofUint64, 4U);
1021  XCTAssertEqual(msg.oneofSint32, 104);
1022  XCTAssertEqual(msg.oneofSint64, 105);
1023  XCTAssertEqual(msg.oneofFixed32, 106U);
1024  XCTAssertEqual(msg.oneofFixed64, 107U);
1025  XCTAssertEqual(msg.oneofSfixed32, 108);
1026  XCTAssertEqual(msg.oneofSfixed64, 109);
1027  XCTAssertEqual(msg.oneofFloat, 110.0f);
1028  XCTAssertEqual(msg.oneofDouble, 111.0);
1029  XCTAssertEqual(msg.oneofBool, YES);
1030  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1031  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1032  XCTAssertNotNil(msg.oneofGroup);
1033  XCTAssertNotNil(msg.oneofMessage);
1034  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1035  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
1036
1037  msg.oneofSint32 = 5;
1038  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
1039  XCTAssertEqual(msg.oneofInt32, 100);
1040  XCTAssertEqual(msg.oneofInt64, 101);
1041  XCTAssertEqual(msg.oneofUint32, 102U);
1042  XCTAssertEqual(msg.oneofUint64, 103U);
1043  XCTAssertEqual(msg.oneofSint32, 5);
1044  XCTAssertEqual(msg.oneofSint64, 105);
1045  XCTAssertEqual(msg.oneofFixed32, 106U);
1046  XCTAssertEqual(msg.oneofFixed64, 107U);
1047  XCTAssertEqual(msg.oneofSfixed32, 108);
1048  XCTAssertEqual(msg.oneofSfixed64, 109);
1049  XCTAssertEqual(msg.oneofFloat, 110.0f);
1050  XCTAssertEqual(msg.oneofDouble, 111.0);
1051  XCTAssertEqual(msg.oneofBool, YES);
1052  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1053  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1054  XCTAssertNotNil(msg.oneofGroup);
1055  XCTAssertNotNil(msg.oneofMessage);
1056  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1057  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
1058
1059  msg.oneofSint64 = 6;
1060  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
1061  XCTAssertEqual(msg.oneofInt32, 100);
1062  XCTAssertEqual(msg.oneofInt64, 101);
1063  XCTAssertEqual(msg.oneofUint32, 102U);
1064  XCTAssertEqual(msg.oneofUint64, 103U);
1065  XCTAssertEqual(msg.oneofSint32, 104);
1066  XCTAssertEqual(msg.oneofSint64, 6);
1067  XCTAssertEqual(msg.oneofFixed32, 106U);
1068  XCTAssertEqual(msg.oneofFixed64, 107U);
1069  XCTAssertEqual(msg.oneofSfixed32, 108);
1070  XCTAssertEqual(msg.oneofSfixed64, 109);
1071  XCTAssertEqual(msg.oneofFloat, 110.0f);
1072  XCTAssertEqual(msg.oneofDouble, 111.0);
1073  XCTAssertEqual(msg.oneofBool, YES);
1074  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1075  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1076  XCTAssertNotNil(msg.oneofGroup);
1077  XCTAssertNotNil(msg.oneofMessage);
1078  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1079  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
1080
1081  msg.oneofFixed32 = 7;
1082  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
1083  XCTAssertEqual(msg.oneofInt32, 100);
1084  XCTAssertEqual(msg.oneofInt64, 101);
1085  XCTAssertEqual(msg.oneofUint32, 102U);
1086  XCTAssertEqual(msg.oneofUint64, 103U);
1087  XCTAssertEqual(msg.oneofSint32, 104);
1088  XCTAssertEqual(msg.oneofSint64, 105);
1089  XCTAssertEqual(msg.oneofFixed32, 7U);
1090  XCTAssertEqual(msg.oneofFixed64, 107U);
1091  XCTAssertEqual(msg.oneofSfixed32, 108);
1092  XCTAssertEqual(msg.oneofSfixed64, 109);
1093  XCTAssertEqual(msg.oneofFloat, 110.0f);
1094  XCTAssertEqual(msg.oneofDouble, 111.0);
1095  XCTAssertEqual(msg.oneofBool, YES);
1096  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1097  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1098  XCTAssertNotNil(msg.oneofGroup);
1099  XCTAssertNotNil(msg.oneofMessage);
1100  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1101  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
1102
1103  msg.oneofFixed64 = 8;
1104  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
1105  XCTAssertEqual(msg.oneofInt32, 100);
1106  XCTAssertEqual(msg.oneofInt64, 101);
1107  XCTAssertEqual(msg.oneofUint32, 102U);
1108  XCTAssertEqual(msg.oneofUint64, 103U);
1109  XCTAssertEqual(msg.oneofSint32, 104);
1110  XCTAssertEqual(msg.oneofSint64, 105);
1111  XCTAssertEqual(msg.oneofFixed32, 106U);
1112  XCTAssertEqual(msg.oneofFixed64, 8U);
1113  XCTAssertEqual(msg.oneofSfixed32, 108);
1114  XCTAssertEqual(msg.oneofSfixed64, 109);
1115  XCTAssertEqual(msg.oneofFloat, 110.0f);
1116  XCTAssertEqual(msg.oneofDouble, 111.0);
1117  XCTAssertEqual(msg.oneofBool, YES);
1118  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1119  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1120  XCTAssertNotNil(msg.oneofGroup);
1121  XCTAssertNotNil(msg.oneofMessage);
1122  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1123  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
1124
1125  msg.oneofSfixed32 = 9;
1126  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
1127  XCTAssertEqual(msg.oneofInt32, 100);
1128  XCTAssertEqual(msg.oneofInt64, 101);
1129  XCTAssertEqual(msg.oneofUint32, 102U);
1130  XCTAssertEqual(msg.oneofUint64, 103U);
1131  XCTAssertEqual(msg.oneofSint32, 104);
1132  XCTAssertEqual(msg.oneofSint64, 105);
1133  XCTAssertEqual(msg.oneofFixed32, 106U);
1134  XCTAssertEqual(msg.oneofFixed64, 107U);
1135  XCTAssertEqual(msg.oneofSfixed32, 9);
1136  XCTAssertEqual(msg.oneofSfixed64, 109);
1137  XCTAssertEqual(msg.oneofFloat, 110.0f);
1138  XCTAssertEqual(msg.oneofDouble, 111.0);
1139  XCTAssertEqual(msg.oneofBool, YES);
1140  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1141  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1142  XCTAssertNotNil(msg.oneofGroup);
1143  XCTAssertNotNil(msg.oneofMessage);
1144  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1145  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
1146
1147  msg.oneofSfixed64 = 10;
1148  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
1149  XCTAssertEqual(msg.oneofInt32, 100);
1150  XCTAssertEqual(msg.oneofInt64, 101);
1151  XCTAssertEqual(msg.oneofUint32, 102U);
1152  XCTAssertEqual(msg.oneofUint64, 103U);
1153  XCTAssertEqual(msg.oneofSint32, 104);
1154  XCTAssertEqual(msg.oneofSint64, 105);
1155  XCTAssertEqual(msg.oneofFixed32, 106U);
1156  XCTAssertEqual(msg.oneofFixed64, 107U);
1157  XCTAssertEqual(msg.oneofSfixed32, 108);
1158  XCTAssertEqual(msg.oneofSfixed64, 10);
1159  XCTAssertEqual(msg.oneofFloat, 110.0f);
1160  XCTAssertEqual(msg.oneofDouble, 111.0);
1161  XCTAssertEqual(msg.oneofBool, YES);
1162  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1163  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1164  XCTAssertNotNil(msg.oneofGroup);
1165  XCTAssertNotNil(msg.oneofMessage);
1166  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1167  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
1168
1169  msg.oneofFloat = 11.0f;
1170  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
1171  XCTAssertEqual(msg.oneofInt32, 100);
1172  XCTAssertEqual(msg.oneofInt64, 101);
1173  XCTAssertEqual(msg.oneofUint32, 102U);
1174  XCTAssertEqual(msg.oneofUint64, 103U);
1175  XCTAssertEqual(msg.oneofSint32, 104);
1176  XCTAssertEqual(msg.oneofSint64, 105);
1177  XCTAssertEqual(msg.oneofFixed32, 106U);
1178  XCTAssertEqual(msg.oneofFixed64, 107U);
1179  XCTAssertEqual(msg.oneofSfixed32, 108);
1180  XCTAssertEqual(msg.oneofSfixed64, 109);
1181  XCTAssertEqual(msg.oneofFloat, 11.0f);
1182  XCTAssertEqual(msg.oneofDouble, 111.0);
1183  XCTAssertEqual(msg.oneofBool, YES);
1184  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1185  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1186  XCTAssertNotNil(msg.oneofGroup);
1187  XCTAssertNotNil(msg.oneofMessage);
1188  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1189  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
1190
1191  msg.oneofDouble = 12.0;
1192  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
1193  XCTAssertEqual(msg.oneofInt32, 100);
1194  XCTAssertEqual(msg.oneofInt64, 101);
1195  XCTAssertEqual(msg.oneofUint32, 102U);
1196  XCTAssertEqual(msg.oneofUint64, 103U);
1197  XCTAssertEqual(msg.oneofSint32, 104);
1198  XCTAssertEqual(msg.oneofSint64, 105);
1199  XCTAssertEqual(msg.oneofFixed32, 106U);
1200  XCTAssertEqual(msg.oneofFixed64, 107U);
1201  XCTAssertEqual(msg.oneofSfixed32, 108);
1202  XCTAssertEqual(msg.oneofSfixed64, 109);
1203  XCTAssertEqual(msg.oneofFloat, 110.0f);
1204  XCTAssertEqual(msg.oneofDouble, 12.0);
1205  XCTAssertEqual(msg.oneofBool, YES);
1206  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1207  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1208  XCTAssertNotNil(msg.oneofGroup);
1209  XCTAssertNotNil(msg.oneofMessage);
1210  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1211  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
1212
1213  msg.oneofBool = NO;
1214  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBool);
1215  XCTAssertEqual(msg.oneofInt32, 100);
1216  XCTAssertEqual(msg.oneofInt64, 101);
1217  XCTAssertEqual(msg.oneofUint32, 102U);
1218  XCTAssertEqual(msg.oneofUint64, 103U);
1219  XCTAssertEqual(msg.oneofSint32, 104);
1220  XCTAssertEqual(msg.oneofSint64, 105);
1221  XCTAssertEqual(msg.oneofFixed32, 106U);
1222  XCTAssertEqual(msg.oneofFixed64, 107U);
1223  XCTAssertEqual(msg.oneofSfixed32, 108);
1224  XCTAssertEqual(msg.oneofSfixed64, 109);
1225  XCTAssertEqual(msg.oneofFloat, 110.0f);
1226  XCTAssertEqual(msg.oneofDouble, 111.0);
1227  XCTAssertEqual(msg.oneofBool, NO);
1228  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1229  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1230  XCTAssertNotNil(msg.oneofGroup);
1231  XCTAssertNotNil(msg.oneofMessage);
1232  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1233  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBool);
1234
1235  msg.oneofString = @"foo";
1236  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofString);
1237  XCTAssertEqual(msg.oneofInt32, 100);
1238  XCTAssertEqual(msg.oneofInt64, 101);
1239  XCTAssertEqual(msg.oneofUint32, 102U);
1240  XCTAssertEqual(msg.oneofUint64, 103U);
1241  XCTAssertEqual(msg.oneofSint32, 104);
1242  XCTAssertEqual(msg.oneofSint64, 105);
1243  XCTAssertEqual(msg.oneofFixed32, 106U);
1244  XCTAssertEqual(msg.oneofFixed64, 107U);
1245  XCTAssertEqual(msg.oneofSfixed32, 108);
1246  XCTAssertEqual(msg.oneofSfixed64, 109);
1247  XCTAssertEqual(msg.oneofFloat, 110.0f);
1248  XCTAssertEqual(msg.oneofDouble, 111.0);
1249  XCTAssertEqual(msg.oneofBool, YES);
1250  XCTAssertEqualObjects(msg.oneofString, @"foo");
1251  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1252  XCTAssertNotNil(msg.oneofGroup);
1253  XCTAssertNotNil(msg.oneofMessage);
1254  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1255  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofString);
1256
1257  msg.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
1258  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
1259  XCTAssertEqual(msg.oneofInt32, 100);
1260  XCTAssertEqual(msg.oneofInt64, 101);
1261  XCTAssertEqual(msg.oneofUint32, 102U);
1262  XCTAssertEqual(msg.oneofUint64, 103U);
1263  XCTAssertEqual(msg.oneofSint32, 104);
1264  XCTAssertEqual(msg.oneofSint64, 105);
1265  XCTAssertEqual(msg.oneofFixed32, 106U);
1266  XCTAssertEqual(msg.oneofFixed64, 107U);
1267  XCTAssertEqual(msg.oneofSfixed32, 108);
1268  XCTAssertEqual(msg.oneofSfixed64, 109);
1269  XCTAssertEqual(msg.oneofFloat, 110.0f);
1270  XCTAssertEqual(msg.oneofDouble, 111.0);
1271  XCTAssertEqual(msg.oneofBool, YES);
1272  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1273  XCTAssertEqualObjects(msg.oneofBytes,
1274                        [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
1275  XCTAssertNotNil(msg.oneofGroup);
1276  XCTAssertNotNil(msg.oneofMessage);
1277  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1278  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
1279
1280  Message2_OneofGroup *group = [Message2_OneofGroup message];
1281  msg.oneofGroup = group;
1282  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
1283  XCTAssertEqual(msg.oneofInt32, 100);
1284  XCTAssertEqual(msg.oneofInt64, 101);
1285  XCTAssertEqual(msg.oneofUint32, 102U);
1286  XCTAssertEqual(msg.oneofUint64, 103U);
1287  XCTAssertEqual(msg.oneofSint32, 104);
1288  XCTAssertEqual(msg.oneofSint64, 105);
1289  XCTAssertEqual(msg.oneofFixed32, 106U);
1290  XCTAssertEqual(msg.oneofFixed64, 107U);
1291  XCTAssertEqual(msg.oneofSfixed32, 108);
1292  XCTAssertEqual(msg.oneofSfixed64, 109);
1293  XCTAssertEqual(msg.oneofFloat, 110.0f);
1294  XCTAssertEqual(msg.oneofDouble, 111.0);
1295  XCTAssertEqual(msg.oneofBool, YES);
1296  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1297  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1298  XCTAssertEqual(msg.oneofGroup, group);  // Pointer compare.
1299  XCTAssertNotNil(msg.oneofMessage);
1300  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1301  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
1302
1303  Message2 *subMessage = [Message2 message];
1304  msg.oneofMessage = subMessage;
1305  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
1306  XCTAssertEqual(msg.oneofInt32, 100);
1307  XCTAssertEqual(msg.oneofInt64, 101);
1308  XCTAssertEqual(msg.oneofUint32, 102U);
1309  XCTAssertEqual(msg.oneofUint64, 103U);
1310  XCTAssertEqual(msg.oneofSint32, 104);
1311  XCTAssertEqual(msg.oneofSint64, 105);
1312  XCTAssertEqual(msg.oneofFixed32, 106U);
1313  XCTAssertEqual(msg.oneofFixed64, 107U);
1314  XCTAssertEqual(msg.oneofSfixed32, 108);
1315  XCTAssertEqual(msg.oneofSfixed64, 109);
1316  XCTAssertEqual(msg.oneofFloat, 110.0f);
1317  XCTAssertEqual(msg.oneofDouble, 111.0);
1318  XCTAssertEqual(msg.oneofBool, YES);
1319  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1320  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1321  XCTAssertNotNil(msg.oneofGroup);
1322  XCTAssertNotEqual(msg.oneofGroup, group);      // Pointer compare.
1323  XCTAssertEqual(msg.oneofMessage, subMessage);  // Pointer compare.
1324  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz);
1325  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
1326
1327  msg.oneofEnum = Message2_Enum_Bar;
1328  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
1329  XCTAssertEqual(msg.oneofInt32, 100);
1330  XCTAssertEqual(msg.oneofInt64, 101);
1331  XCTAssertEqual(msg.oneofUint32, 102U);
1332  XCTAssertEqual(msg.oneofUint64, 103U);
1333  XCTAssertEqual(msg.oneofSint32, 104);
1334  XCTAssertEqual(msg.oneofSint64, 105);
1335  XCTAssertEqual(msg.oneofFixed32, 106U);
1336  XCTAssertEqual(msg.oneofFixed64, 107U);
1337  XCTAssertEqual(msg.oneofSfixed32, 108);
1338  XCTAssertEqual(msg.oneofSfixed64, 109);
1339  XCTAssertEqual(msg.oneofFloat, 110.0f);
1340  XCTAssertEqual(msg.oneofDouble, 111.0);
1341  XCTAssertEqual(msg.oneofBool, YES);
1342  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1343  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1344  XCTAssertNotNil(msg.oneofGroup);
1345  XCTAssertNotEqual(msg.oneofGroup, group);  // Pointer compare.
1346  XCTAssertNotNil(msg.oneofMessage);
1347  XCTAssertNotEqual(msg.oneofMessage, subMessage);  // Pointer compare.
1348  XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
1349  XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
1350
1351  // Test setting/calling clear clearing.
1352
1353  [msg release];
1354  msg = [[Message2 alloc] init];
1355
1356  uint32_t values[] = {
1357    Message2_O_OneOfCase_OneofInt32,
1358    Message2_O_OneOfCase_OneofInt64,
1359    Message2_O_OneOfCase_OneofUint32,
1360    Message2_O_OneOfCase_OneofUint64,
1361    Message2_O_OneOfCase_OneofSint32,
1362    Message2_O_OneOfCase_OneofSint64,
1363    Message2_O_OneOfCase_OneofFixed32,
1364    Message2_O_OneOfCase_OneofFixed64,
1365    Message2_O_OneOfCase_OneofSfixed32,
1366    Message2_O_OneOfCase_OneofSfixed64,
1367    Message2_O_OneOfCase_OneofFloat,
1368    Message2_O_OneOfCase_OneofDouble,
1369    Message2_O_OneOfCase_OneofBool,
1370    Message2_O_OneOfCase_OneofString,
1371    Message2_O_OneOfCase_OneofBytes,
1372    Message2_O_OneOfCase_OneofGroup,
1373    Message2_O_OneOfCase_OneofMessage,
1374    Message2_O_OneOfCase_OneofEnum,
1375  };
1376
1377  for (size_t i = 0; i < GPBARRAYSIZE(values); ++i) {
1378    switch (values[i]) {
1379      case Message2_O_OneOfCase_OneofInt32:
1380        msg.oneofInt32 = 1;
1381        break;
1382      case Message2_O_OneOfCase_OneofInt64:
1383        msg.oneofInt64 = 2;
1384        break;
1385      case Message2_O_OneOfCase_OneofUint32:
1386        msg.oneofUint32 = 3;
1387        break;
1388      case Message2_O_OneOfCase_OneofUint64:
1389        msg.oneofUint64 = 4;
1390        break;
1391      case Message2_O_OneOfCase_OneofSint32:
1392        msg.oneofSint32 = 5;
1393        break;
1394      case Message2_O_OneOfCase_OneofSint64:
1395        msg.oneofSint64 = 6;
1396        break;
1397      case Message2_O_OneOfCase_OneofFixed32:
1398        msg.oneofFixed32 = 7;
1399        break;
1400      case Message2_O_OneOfCase_OneofFixed64:
1401        msg.oneofFixed64 = 8;
1402        break;
1403      case Message2_O_OneOfCase_OneofSfixed32:
1404        msg.oneofSfixed32 = 9;
1405        break;
1406      case Message2_O_OneOfCase_OneofSfixed64:
1407        msg.oneofSfixed64 = 10;
1408        break;
1409      case Message2_O_OneOfCase_OneofFloat:
1410        msg.oneofFloat = 11.0f;
1411        break;
1412      case Message2_O_OneOfCase_OneofDouble:
1413        msg.oneofDouble = 12.0;
1414        break;
1415      case Message2_O_OneOfCase_OneofBool:
1416        msg.oneofBool = YES;
1417        break;
1418      case Message2_O_OneOfCase_OneofString:
1419        msg.oneofString = @"foo";
1420        break;
1421      case Message2_O_OneOfCase_OneofBytes:
1422        msg.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
1423        break;
1424      case Message2_O_OneOfCase_OneofGroup:
1425        msg.oneofGroup = group;
1426        break;
1427      case Message2_O_OneOfCase_OneofMessage:
1428        msg.oneofMessage = subMessage;
1429        break;
1430      case Message2_O_OneOfCase_OneofEnum:
1431        msg.oneofEnum = Message2_Enum_Bar;
1432        break;
1433      default:
1434        XCTFail(@"shouldn't happen, loop: %zd, value: %d", i, values[i]);
1435        break;
1436    }
1437
1438    XCTAssertEqual(msg.oOneOfCase, values[i], "Loop: %zd", i);
1439    // No need to check the value was set, the above tests did that.
1440    Message2_ClearOOneOfCase(msg);
1441    // Nothing in the case.
1442    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_GPBUnsetOneOfCase,
1443                   "Loop: %zd", i);
1444    // Confirm everything is back to defaults after a clear.
1445    XCTAssertEqual(msg.oneofInt32, 100, "Loop: %zd", i);
1446    XCTAssertEqual(msg.oneofInt64, 101, "Loop: %zd", i);
1447    XCTAssertEqual(msg.oneofUint32, 102U, "Loop: %zd", i);
1448    XCTAssertEqual(msg.oneofUint64, 103U, "Loop: %zd", i);
1449    XCTAssertEqual(msg.oneofSint32, 104, "Loop: %zd", i);
1450    XCTAssertEqual(msg.oneofSint64, 105, "Loop: %zd", i);
1451    XCTAssertEqual(msg.oneofFixed32, 106U, "Loop: %zd", i);
1452    XCTAssertEqual(msg.oneofFixed64, 107U, "Loop: %zd", i);
1453    XCTAssertEqual(msg.oneofSfixed32, 108, "Loop: %zd", i);
1454    XCTAssertEqual(msg.oneofSfixed64, 109, "Loop: %zd", i);
1455    XCTAssertEqual(msg.oneofFloat, 110.0f, "Loop: %zd", i);
1456    XCTAssertEqual(msg.oneofDouble, 111.0, "Loop: %zd", i);
1457    XCTAssertEqual(msg.oneofBool, YES, "Loop: %zd", i);
1458    XCTAssertEqualObjects(msg.oneofString, oneofStringDefault, "Loop: %zd", i);
1459    XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault, "Loop: %zd", i);
1460    XCTAssertNotNil(msg.oneofGroup, "Loop: %zd", i);
1461    XCTAssertNotEqual(msg.oneofGroup, group, "Loop: %zd",
1462                      i);  // Pointer compare.
1463    XCTAssertNotNil(msg.oneofMessage, "Loop: %zd", i);
1464    XCTAssertNotEqual(msg.oneofMessage, subMessage, "Loop: %zd",
1465                      i);  // Pointer compare.
1466    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Baz, "Loop: %zd", i);
1467  }
1468
1469  [msg release];
1470}
1471
1472- (void)testProto3OneofBasicBehaviors {
1473  Message3 *msg = [[Message3 alloc] init];
1474
1475  NSString *oneofStringDefault = @"";
1476  NSData *oneofBytesDefault = [NSData data];
1477
1478  // Nothing set.
1479  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_GPBUnsetOneOfCase);
1480  XCTAssertEqual(msg.oneofInt32, 0);
1481  XCTAssertEqual(msg.oneofInt64, 0);
1482  XCTAssertEqual(msg.oneofUint32, 0U);
1483  XCTAssertEqual(msg.oneofUint64, 0U);
1484  XCTAssertEqual(msg.oneofSint32, 0);
1485  XCTAssertEqual(msg.oneofSint64, 0);
1486  XCTAssertEqual(msg.oneofFixed32, 0U);
1487  XCTAssertEqual(msg.oneofFixed64, 0U);
1488  XCTAssertEqual(msg.oneofSfixed32, 0);
1489  XCTAssertEqual(msg.oneofSfixed64, 0);
1490  XCTAssertEqual(msg.oneofFloat, 0.0f);
1491  XCTAssertEqual(msg.oneofDouble, 0.0);
1492  XCTAssertEqual(msg.oneofBool, NO);
1493  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1494  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1495  XCTAssertNotNil(msg.oneofMessage);
1496  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1497
1498  // Set, check the case, check everyone has default but the one, confirm case
1499  // didn't change.
1500
1501  msg.oneofInt32 = 1;
1502  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
1503  XCTAssertEqual(msg.oneofInt32, 1);
1504  XCTAssertEqual(msg.oneofInt64, 0);
1505  XCTAssertEqual(msg.oneofUint32, 0U);
1506  XCTAssertEqual(msg.oneofUint64, 0U);
1507  XCTAssertEqual(msg.oneofSint32, 0);
1508  XCTAssertEqual(msg.oneofSint64, 0);
1509  XCTAssertEqual(msg.oneofFixed32, 0U);
1510  XCTAssertEqual(msg.oneofFixed64, 0U);
1511  XCTAssertEqual(msg.oneofSfixed32, 0);
1512  XCTAssertEqual(msg.oneofSfixed64, 0);
1513  XCTAssertEqual(msg.oneofFloat, 0.0f);
1514  XCTAssertEqual(msg.oneofDouble, 0.0);
1515  XCTAssertEqual(msg.oneofBool, NO);
1516  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1517  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1518  XCTAssertNotNil(msg.oneofMessage);
1519  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1520  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
1521
1522  msg.oneofInt64 = 2;
1523  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
1524  XCTAssertEqual(msg.oneofInt32, 0);
1525  XCTAssertEqual(msg.oneofInt64, 2);
1526  XCTAssertEqual(msg.oneofUint32, 0U);
1527  XCTAssertEqual(msg.oneofUint64, 0U);
1528  XCTAssertEqual(msg.oneofSint32, 0);
1529  XCTAssertEqual(msg.oneofSint64, 0);
1530  XCTAssertEqual(msg.oneofFixed32, 0U);
1531  XCTAssertEqual(msg.oneofFixed64, 0U);
1532  XCTAssertEqual(msg.oneofSfixed32, 0);
1533  XCTAssertEqual(msg.oneofSfixed64, 0);
1534  XCTAssertEqual(msg.oneofFloat, 0.0f);
1535  XCTAssertEqual(msg.oneofDouble, 0.0);
1536  XCTAssertEqual(msg.oneofBool, NO);
1537  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1538  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1539  XCTAssertNotNil(msg.oneofMessage);
1540  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1541  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
1542
1543  msg.oneofUint32 = 3;
1544  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
1545  XCTAssertEqual(msg.oneofInt32, 0);
1546  XCTAssertEqual(msg.oneofInt64, 0);
1547  XCTAssertEqual(msg.oneofUint32, 3U);
1548  XCTAssertEqual(msg.oneofUint64, 0U);
1549  XCTAssertEqual(msg.oneofSint32, 0);
1550  XCTAssertEqual(msg.oneofSint64, 0);
1551  XCTAssertEqual(msg.oneofFixed32, 0U);
1552  XCTAssertEqual(msg.oneofFixed64, 0U);
1553  XCTAssertEqual(msg.oneofSfixed32, 0);
1554  XCTAssertEqual(msg.oneofSfixed64, 0);
1555  XCTAssertEqual(msg.oneofFloat, 0.0f);
1556  XCTAssertEqual(msg.oneofDouble, 0.0);
1557  XCTAssertEqual(msg.oneofBool, NO);
1558  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1559  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1560  XCTAssertNotNil(msg.oneofMessage);
1561  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1562  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
1563
1564  msg.oneofUint64 = 4;
1565  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
1566  XCTAssertEqual(msg.oneofInt32, 0);
1567  XCTAssertEqual(msg.oneofInt64, 0);
1568  XCTAssertEqual(msg.oneofUint32, 0U);
1569  XCTAssertEqual(msg.oneofUint64, 4U);
1570  XCTAssertEqual(msg.oneofSint32, 0);
1571  XCTAssertEqual(msg.oneofSint64, 0);
1572  XCTAssertEqual(msg.oneofFixed32, 0U);
1573  XCTAssertEqual(msg.oneofFixed64, 0U);
1574  XCTAssertEqual(msg.oneofSfixed32, 0);
1575  XCTAssertEqual(msg.oneofSfixed64, 0);
1576  XCTAssertEqual(msg.oneofFloat, 0.0f);
1577  XCTAssertEqual(msg.oneofDouble, 0.0);
1578  XCTAssertEqual(msg.oneofBool, NO);
1579  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1580  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1581  XCTAssertNotNil(msg.oneofMessage);
1582  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1583  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
1584
1585  msg.oneofSint32 = 5;
1586  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
1587  XCTAssertEqual(msg.oneofInt32, 0);
1588  XCTAssertEqual(msg.oneofInt64, 0);
1589  XCTAssertEqual(msg.oneofUint32, 0U);
1590  XCTAssertEqual(msg.oneofUint64, 0U);
1591  XCTAssertEqual(msg.oneofSint32, 5);
1592  XCTAssertEqual(msg.oneofSint64, 0);
1593  XCTAssertEqual(msg.oneofFixed32, 0U);
1594  XCTAssertEqual(msg.oneofFixed64, 0U);
1595  XCTAssertEqual(msg.oneofSfixed32, 0);
1596  XCTAssertEqual(msg.oneofSfixed64, 0);
1597  XCTAssertEqual(msg.oneofFloat, 0.0f);
1598  XCTAssertEqual(msg.oneofDouble, 0.0);
1599  XCTAssertEqual(msg.oneofBool, NO);
1600  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1601  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1602  XCTAssertNotNil(msg.oneofMessage);
1603  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1604  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
1605
1606  msg.oneofSint64 = 6;
1607  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
1608  XCTAssertEqual(msg.oneofInt32, 0);
1609  XCTAssertEqual(msg.oneofInt64, 0);
1610  XCTAssertEqual(msg.oneofUint32, 0U);
1611  XCTAssertEqual(msg.oneofUint64, 0U);
1612  XCTAssertEqual(msg.oneofSint32, 0);
1613  XCTAssertEqual(msg.oneofSint64, 6);
1614  XCTAssertEqual(msg.oneofFixed32, 0U);
1615  XCTAssertEqual(msg.oneofFixed64, 0U);
1616  XCTAssertEqual(msg.oneofSfixed32, 0);
1617  XCTAssertEqual(msg.oneofSfixed64, 0);
1618  XCTAssertEqual(msg.oneofFloat, 0.0f);
1619  XCTAssertEqual(msg.oneofDouble, 0.0);
1620  XCTAssertEqual(msg.oneofBool, NO);
1621  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1622  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1623  XCTAssertNotNil(msg.oneofMessage);
1624  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1625  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
1626
1627  msg.oneofFixed32 = 7;
1628  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
1629  XCTAssertEqual(msg.oneofInt32, 0);
1630  XCTAssertEqual(msg.oneofInt64, 0);
1631  XCTAssertEqual(msg.oneofUint32, 0U);
1632  XCTAssertEqual(msg.oneofUint64, 0U);
1633  XCTAssertEqual(msg.oneofSint32, 0);
1634  XCTAssertEqual(msg.oneofSint64, 0);
1635  XCTAssertEqual(msg.oneofFixed32, 7U);
1636  XCTAssertEqual(msg.oneofFixed64, 0U);
1637  XCTAssertEqual(msg.oneofSfixed32, 0);
1638  XCTAssertEqual(msg.oneofSfixed64, 0);
1639  XCTAssertEqual(msg.oneofFloat, 0.0f);
1640  XCTAssertEqual(msg.oneofDouble, 0.0);
1641  XCTAssertEqual(msg.oneofBool, NO);
1642  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1643  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1644  XCTAssertNotNil(msg.oneofMessage);
1645  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1646  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
1647
1648  msg.oneofFixed64 = 8;
1649  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
1650  XCTAssertEqual(msg.oneofInt32, 0);
1651  XCTAssertEqual(msg.oneofInt64, 0);
1652  XCTAssertEqual(msg.oneofUint32, 0U);
1653  XCTAssertEqual(msg.oneofUint64, 0U);
1654  XCTAssertEqual(msg.oneofSint32, 0);
1655  XCTAssertEqual(msg.oneofSint64, 0);
1656  XCTAssertEqual(msg.oneofFixed32, 0U);
1657  XCTAssertEqual(msg.oneofFixed64, 8U);
1658  XCTAssertEqual(msg.oneofSfixed32, 0);
1659  XCTAssertEqual(msg.oneofSfixed64, 0);
1660  XCTAssertEqual(msg.oneofFloat, 0.0f);
1661  XCTAssertEqual(msg.oneofDouble, 0.0);
1662  XCTAssertEqual(msg.oneofBool, NO);
1663  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1664  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1665  XCTAssertNotNil(msg.oneofMessage);
1666  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1667  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
1668
1669  msg.oneofSfixed32 = 9;
1670  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
1671  XCTAssertEqual(msg.oneofInt32, 0);
1672  XCTAssertEqual(msg.oneofInt64, 0);
1673  XCTAssertEqual(msg.oneofUint32, 0U);
1674  XCTAssertEqual(msg.oneofUint64, 0U);
1675  XCTAssertEqual(msg.oneofSint32, 0);
1676  XCTAssertEqual(msg.oneofSint64, 0);
1677  XCTAssertEqual(msg.oneofFixed32, 0U);
1678  XCTAssertEqual(msg.oneofFixed64, 0U);
1679  XCTAssertEqual(msg.oneofSfixed32, 9);
1680  XCTAssertEqual(msg.oneofSfixed64, 0);
1681  XCTAssertEqual(msg.oneofFloat, 0.0f);
1682  XCTAssertEqual(msg.oneofDouble, 0.0);
1683  XCTAssertEqual(msg.oneofBool, NO);
1684  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1685  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1686  XCTAssertNotNil(msg.oneofMessage);
1687  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1688  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
1689
1690  msg.oneofSfixed64 = 10;
1691  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
1692  XCTAssertEqual(msg.oneofInt32, 0);
1693  XCTAssertEqual(msg.oneofInt64, 0);
1694  XCTAssertEqual(msg.oneofUint32, 0U);
1695  XCTAssertEqual(msg.oneofUint64, 0U);
1696  XCTAssertEqual(msg.oneofSint32, 0);
1697  XCTAssertEqual(msg.oneofSint64, 0);
1698  XCTAssertEqual(msg.oneofFixed32, 0U);
1699  XCTAssertEqual(msg.oneofFixed64, 0U);
1700  XCTAssertEqual(msg.oneofSfixed32, 0);
1701  XCTAssertEqual(msg.oneofSfixed64, 10);
1702  XCTAssertEqual(msg.oneofFloat, 0.0f);
1703  XCTAssertEqual(msg.oneofDouble, 0.0);
1704  XCTAssertEqual(msg.oneofBool, NO);
1705  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1706  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1707  XCTAssertNotNil(msg.oneofMessage);
1708  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1709  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
1710
1711  msg.oneofFloat = 11.0f;
1712  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
1713  XCTAssertEqual(msg.oneofInt32, 0);
1714  XCTAssertEqual(msg.oneofInt64, 0);
1715  XCTAssertEqual(msg.oneofUint32, 0U);
1716  XCTAssertEqual(msg.oneofUint64, 0U);
1717  XCTAssertEqual(msg.oneofSint32, 0);
1718  XCTAssertEqual(msg.oneofSint64, 0);
1719  XCTAssertEqual(msg.oneofFixed32, 0U);
1720  XCTAssertEqual(msg.oneofFixed64, 0U);
1721  XCTAssertEqual(msg.oneofSfixed32, 0);
1722  XCTAssertEqual(msg.oneofSfixed64, 0);
1723  XCTAssertEqual(msg.oneofFloat, 11.0f);
1724  XCTAssertEqual(msg.oneofDouble, 0.0);
1725  XCTAssertEqual(msg.oneofBool, NO);
1726  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1727  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1728  XCTAssertNotNil(msg.oneofMessage);
1729  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1730  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
1731
1732  msg.oneofDouble = 12.0;
1733  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
1734  XCTAssertEqual(msg.oneofInt32, 0);
1735  XCTAssertEqual(msg.oneofInt64, 0);
1736  XCTAssertEqual(msg.oneofUint32, 0U);
1737  XCTAssertEqual(msg.oneofUint64, 0U);
1738  XCTAssertEqual(msg.oneofSint32, 0);
1739  XCTAssertEqual(msg.oneofSint64, 0);
1740  XCTAssertEqual(msg.oneofFixed32, 0U);
1741  XCTAssertEqual(msg.oneofFixed64, 0U);
1742  XCTAssertEqual(msg.oneofSfixed32, 0);
1743  XCTAssertEqual(msg.oneofSfixed64, 0);
1744  XCTAssertEqual(msg.oneofFloat, 0.0f);
1745  XCTAssertEqual(msg.oneofDouble, 12.0);
1746  XCTAssertEqual(msg.oneofBool, NO);
1747  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1748  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1749  XCTAssertNotNil(msg.oneofMessage);
1750  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1751  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
1752
1753  msg.oneofBool = YES;
1754  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBool);
1755  XCTAssertEqual(msg.oneofInt32, 0);
1756  XCTAssertEqual(msg.oneofInt64, 0);
1757  XCTAssertEqual(msg.oneofUint32, 0U);
1758  XCTAssertEqual(msg.oneofUint64, 0U);
1759  XCTAssertEqual(msg.oneofSint32, 0);
1760  XCTAssertEqual(msg.oneofSint64, 0);
1761  XCTAssertEqual(msg.oneofFixed32, 0U);
1762  XCTAssertEqual(msg.oneofFixed64, 0U);
1763  XCTAssertEqual(msg.oneofSfixed32, 0);
1764  XCTAssertEqual(msg.oneofSfixed64, 0);
1765  XCTAssertEqual(msg.oneofFloat, 0.0f);
1766  XCTAssertEqual(msg.oneofDouble, 0.0);
1767  XCTAssertEqual(msg.oneofBool, YES);
1768  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1769  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1770  XCTAssertNotNil(msg.oneofMessage);
1771  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1772  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBool);
1773
1774  msg.oneofString = @"foo";
1775  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofString);
1776  XCTAssertEqual(msg.oneofInt32, 0);
1777  XCTAssertEqual(msg.oneofInt64, 0);
1778  XCTAssertEqual(msg.oneofUint32, 0U);
1779  XCTAssertEqual(msg.oneofUint64, 0U);
1780  XCTAssertEqual(msg.oneofSint32, 0);
1781  XCTAssertEqual(msg.oneofSint64, 0);
1782  XCTAssertEqual(msg.oneofFixed32, 0U);
1783  XCTAssertEqual(msg.oneofFixed64, 0U);
1784  XCTAssertEqual(msg.oneofSfixed32, 0);
1785  XCTAssertEqual(msg.oneofSfixed64, 0);
1786  XCTAssertEqual(msg.oneofFloat, 0.0f);
1787  XCTAssertEqual(msg.oneofDouble, 0.0);
1788  XCTAssertEqual(msg.oneofBool, NO);
1789  XCTAssertEqualObjects(msg.oneofString, @"foo");
1790  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1791  XCTAssertNotNil(msg.oneofMessage);
1792  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1793  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofString);
1794
1795  msg.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
1796  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
1797  XCTAssertEqual(msg.oneofInt32, 0);
1798  XCTAssertEqual(msg.oneofInt64, 0);
1799  XCTAssertEqual(msg.oneofUint32, 0U);
1800  XCTAssertEqual(msg.oneofUint64, 0U);
1801  XCTAssertEqual(msg.oneofSint32, 0);
1802  XCTAssertEqual(msg.oneofSint64, 0);
1803  XCTAssertEqual(msg.oneofFixed32, 0U);
1804  XCTAssertEqual(msg.oneofFixed64, 0U);
1805  XCTAssertEqual(msg.oneofSfixed32, 0);
1806  XCTAssertEqual(msg.oneofSfixed64, 0);
1807  XCTAssertEqual(msg.oneofFloat, 0.0f);
1808  XCTAssertEqual(msg.oneofDouble, 0.0);
1809  XCTAssertEqual(msg.oneofBool, NO);
1810  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1811  XCTAssertEqualObjects(msg.oneofBytes,
1812                        [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
1813  XCTAssertNotNil(msg.oneofMessage);
1814  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1815  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
1816
1817  Message3 *subMessage = [Message3 message];
1818  msg.oneofMessage = subMessage;
1819  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
1820  XCTAssertEqual(msg.oneofInt32, 0);
1821  XCTAssertEqual(msg.oneofInt64, 0);
1822  XCTAssertEqual(msg.oneofUint32, 0U);
1823  XCTAssertEqual(msg.oneofUint64, 0U);
1824  XCTAssertEqual(msg.oneofSint32, 0);
1825  XCTAssertEqual(msg.oneofSint64, 0);
1826  XCTAssertEqual(msg.oneofFixed32, 0U);
1827  XCTAssertEqual(msg.oneofFixed64, 0U);
1828  XCTAssertEqual(msg.oneofSfixed32, 0);
1829  XCTAssertEqual(msg.oneofSfixed64, 0);
1830  XCTAssertEqual(msg.oneofFloat, 0.0f);
1831  XCTAssertEqual(msg.oneofDouble, 0.0);
1832  XCTAssertEqual(msg.oneofBool, NO);
1833  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1834  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1835  XCTAssertEqual(msg.oneofMessage, subMessage);  // Pointer compare.
1836  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo);
1837  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
1838
1839  msg.oneofEnum = Message3_Enum_Bar;
1840  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
1841  XCTAssertEqual(msg.oneofInt32, 0);
1842  XCTAssertEqual(msg.oneofInt64, 0);
1843  XCTAssertEqual(msg.oneofUint32, 0U);
1844  XCTAssertEqual(msg.oneofUint64, 0U);
1845  XCTAssertEqual(msg.oneofSint32, 0);
1846  XCTAssertEqual(msg.oneofSint64, 0);
1847  XCTAssertEqual(msg.oneofFixed32, 0U);
1848  XCTAssertEqual(msg.oneofFixed64, 0U);
1849  XCTAssertEqual(msg.oneofSfixed32, 0);
1850  XCTAssertEqual(msg.oneofSfixed64, 0);
1851  XCTAssertEqual(msg.oneofFloat, 0.0f);
1852  XCTAssertEqual(msg.oneofDouble, 0.0);
1853  XCTAssertEqual(msg.oneofBool, NO);
1854  XCTAssertEqualObjects(msg.oneofString, oneofStringDefault);
1855  XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault);
1856  XCTAssertNotNil(msg.oneofMessage);
1857  XCTAssertNotEqual(msg.oneofMessage, subMessage);  // Pointer compare.
1858  XCTAssertEqual(msg.oneofEnum, Message3_Enum_Bar);
1859  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
1860
1861  // Test setting/calling clear clearing.
1862
1863  [msg release];
1864  msg = [[Message3 alloc] init];
1865
1866  uint32_t values[] = {
1867    Message3_O_OneOfCase_OneofInt32,
1868    Message3_O_OneOfCase_OneofInt64,
1869    Message3_O_OneOfCase_OneofUint32,
1870    Message3_O_OneOfCase_OneofUint64,
1871    Message3_O_OneOfCase_OneofSint32,
1872    Message3_O_OneOfCase_OneofSint64,
1873    Message3_O_OneOfCase_OneofFixed32,
1874    Message3_O_OneOfCase_OneofFixed64,
1875    Message3_O_OneOfCase_OneofSfixed32,
1876    Message3_O_OneOfCase_OneofSfixed64,
1877    Message3_O_OneOfCase_OneofFloat,
1878    Message3_O_OneOfCase_OneofDouble,
1879    Message3_O_OneOfCase_OneofBool,
1880    Message3_O_OneOfCase_OneofString,
1881    Message3_O_OneOfCase_OneofBytes,
1882    Message3_O_OneOfCase_OneofMessage,
1883    Message3_O_OneOfCase_OneofEnum,
1884  };
1885
1886  for (size_t i = 0; i < GPBARRAYSIZE(values); ++i) {
1887    switch (values[i]) {
1888      case Message3_O_OneOfCase_OneofInt32:
1889        msg.oneofInt32 = 1;
1890        break;
1891      case Message3_O_OneOfCase_OneofInt64:
1892        msg.oneofInt64 = 2;
1893        break;
1894      case Message3_O_OneOfCase_OneofUint32:
1895        msg.oneofUint32 = 3;
1896        break;
1897      case Message3_O_OneOfCase_OneofUint64:
1898        msg.oneofUint64 = 4;
1899        break;
1900      case Message3_O_OneOfCase_OneofSint32:
1901        msg.oneofSint32 = 5;
1902        break;
1903      case Message3_O_OneOfCase_OneofSint64:
1904        msg.oneofSint64 = 6;
1905        break;
1906      case Message3_O_OneOfCase_OneofFixed32:
1907        msg.oneofFixed32 = 7;
1908        break;
1909      case Message3_O_OneOfCase_OneofFixed64:
1910        msg.oneofFixed64 = 8;
1911        break;
1912      case Message3_O_OneOfCase_OneofSfixed32:
1913        msg.oneofSfixed32 = 9;
1914        break;
1915      case Message3_O_OneOfCase_OneofSfixed64:
1916        msg.oneofSfixed64 = 10;
1917        break;
1918      case Message3_O_OneOfCase_OneofFloat:
1919        msg.oneofFloat = 11.0f;
1920        break;
1921      case Message3_O_OneOfCase_OneofDouble:
1922        msg.oneofDouble = 12.0;
1923        break;
1924      case Message3_O_OneOfCase_OneofBool:
1925        msg.oneofBool = YES;
1926        break;
1927      case Message3_O_OneOfCase_OneofString:
1928        msg.oneofString = @"foo";
1929        break;
1930      case Message3_O_OneOfCase_OneofBytes:
1931        msg.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
1932        break;
1933      case Message3_O_OneOfCase_OneofMessage:
1934        msg.oneofMessage = subMessage;
1935        break;
1936      case Message3_O_OneOfCase_OneofEnum:
1937        msg.oneofEnum = Message3_Enum_Baz;
1938        break;
1939      default:
1940        XCTFail(@"shouldn't happen, loop: %zd, value: %d", i, values[i]);
1941        break;
1942    }
1943
1944    XCTAssertEqual(msg.oOneOfCase, values[i], "Loop: %zd", i);
1945    // No need to check the value was set, the above tests did that.
1946    Message3_ClearOOneOfCase(msg);
1947    // Nothing in the case.
1948    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_GPBUnsetOneOfCase,
1949                   "Loop: %zd", i);
1950    // Confirm everything is back to defaults after a clear.
1951    XCTAssertEqual(msg.oneofInt32, 0, "Loop: %zd", i);
1952    XCTAssertEqual(msg.oneofInt64, 0, "Loop: %zd", i);
1953    XCTAssertEqual(msg.oneofUint32, 0U, "Loop: %zd", i);
1954    XCTAssertEqual(msg.oneofUint64, 0U, "Loop: %zd", i);
1955    XCTAssertEqual(msg.oneofSint32, 0, "Loop: %zd", i);
1956    XCTAssertEqual(msg.oneofSint64, 0, "Loop: %zd", i);
1957    XCTAssertEqual(msg.oneofFixed32, 0U, "Loop: %zd", i);
1958    XCTAssertEqual(msg.oneofFixed64, 0U, "Loop: %zd", i);
1959    XCTAssertEqual(msg.oneofSfixed32, 0, "Loop: %zd", i);
1960    XCTAssertEqual(msg.oneofSfixed64, 0, "Loop: %zd", i);
1961    XCTAssertEqual(msg.oneofFloat, 0.0f, "Loop: %zd", i);
1962    XCTAssertEqual(msg.oneofDouble, 0.0, "Loop: %zd", i);
1963    XCTAssertEqual(msg.oneofBool, NO, "Loop: %zd", i);
1964    XCTAssertEqualObjects(msg.oneofString, oneofStringDefault, "Loop: %zd", i);
1965    XCTAssertEqualObjects(msg.oneofBytes, oneofBytesDefault, "Loop: %zd", i);
1966    XCTAssertNotNil(msg.oneofMessage, "Loop: %zd", i);
1967    XCTAssertNotEqual(msg.oneofMessage, subMessage, "Loop: %zd",
1968                      i);  // Pointer compare.
1969    XCTAssertEqual(msg.oneofEnum, Message3_Enum_Foo, "Loop: %zd", i);
1970  }
1971
1972  [msg release];
1973}
1974
1975- (void)testCopyingMakesUniqueObjects {
1976  const int repeatCount = 5;
1977  TestAllTypes *msg1 = [TestAllTypes message];
1978  [self setAllFields:msg1 repeatedCount:repeatCount];
1979
1980  TestAllTypes *msg2 = [[msg1 copy] autorelease];
1981
1982  XCTAssertNotEqual(msg1, msg2);      // Ptr compare, new object.
1983  XCTAssertEqualObjects(msg1, msg2);  // Equal values.
1984
1985  // Pointer comparisions, different objects.
1986
1987  XCTAssertNotEqual(msg1.optionalGroup, msg2.optionalGroup);
1988  XCTAssertNotEqual(msg1.optionalNestedMessage, msg2.optionalNestedMessage);
1989  XCTAssertNotEqual(msg1.optionalForeignMessage, msg2.optionalForeignMessage);
1990  XCTAssertNotEqual(msg1.optionalImportMessage, msg2.optionalImportMessage);
1991
1992  XCTAssertNotEqual(msg1.repeatedInt32Array, msg2.repeatedInt32Array);
1993  XCTAssertNotEqual(msg1.repeatedInt64Array, msg2.repeatedInt64Array);
1994  XCTAssertNotEqual(msg1.repeatedUint32Array, msg2.repeatedUint32Array);
1995  XCTAssertNotEqual(msg1.repeatedUint64Array, msg2.repeatedUint64Array);
1996  XCTAssertNotEqual(msg1.repeatedSint32Array, msg2.repeatedSint32Array);
1997  XCTAssertNotEqual(msg1.repeatedSint64Array, msg2.repeatedSint64Array);
1998  XCTAssertNotEqual(msg1.repeatedFixed32Array, msg2.repeatedFixed32Array);
1999  XCTAssertNotEqual(msg1.repeatedFixed64Array, msg2.repeatedFixed64Array);
2000  XCTAssertNotEqual(msg1.repeatedSfixed32Array, msg2.repeatedSfixed32Array);
2001  XCTAssertNotEqual(msg1.repeatedSfixed64Array, msg2.repeatedSfixed64Array);
2002  XCTAssertNotEqual(msg1.repeatedFloatArray, msg2.repeatedFloatArray);
2003  XCTAssertNotEqual(msg1.repeatedDoubleArray, msg2.repeatedDoubleArray);
2004  XCTAssertNotEqual(msg1.repeatedBoolArray, msg2.repeatedBoolArray);
2005  XCTAssertNotEqual(msg1.repeatedStringArray, msg2.repeatedStringArray);
2006  XCTAssertNotEqual(msg1.repeatedBytesArray, msg2.repeatedBytesArray);
2007  XCTAssertNotEqual(msg1.repeatedGroupArray, msg2.repeatedGroupArray);
2008  XCTAssertNotEqual(msg1.repeatedNestedMessageArray,
2009                    msg2.repeatedNestedMessageArray);
2010  XCTAssertNotEqual(msg1.repeatedForeignMessageArray,
2011                    msg2.repeatedForeignMessageArray);
2012  XCTAssertNotEqual(msg1.repeatedImportMessageArray,
2013                    msg2.repeatedImportMessageArray);
2014  XCTAssertNotEqual(msg1.repeatedNestedEnumArray, msg2.repeatedNestedEnumArray);
2015  XCTAssertNotEqual(msg1.repeatedForeignEnumArray,
2016                    msg2.repeatedForeignEnumArray);
2017  XCTAssertNotEqual(msg1.repeatedImportEnumArray, msg2.repeatedImportEnumArray);
2018  XCTAssertNotEqual(msg1.repeatedStringPieceArray,
2019                    msg2.repeatedStringPieceArray);
2020  XCTAssertNotEqual(msg1.repeatedCordArray, msg2.repeatedCordArray);
2021
2022  for (int i = 0; i < repeatCount; i++) {
2023    XCTAssertNotEqual(msg1.repeatedNestedMessageArray[i],
2024                      msg2.repeatedNestedMessageArray[i]);
2025    XCTAssertNotEqual(msg1.repeatedForeignMessageArray[i],
2026                      msg2.repeatedForeignMessageArray[i]);
2027    XCTAssertNotEqual(msg1.repeatedImportMessageArray[i],
2028                      msg2.repeatedImportMessageArray[i]);
2029  }
2030}
2031
2032- (void)testCopyingMapsMakesUniqueObjects {
2033  TestMap *msg1 = [TestMap message];
2034  [self setAllMapFields:msg1 numEntries:5];
2035
2036  TestMap *msg2 = [[msg1 copy] autorelease];
2037
2038  XCTAssertNotEqual(msg1, msg2);      // Ptr compare, new object.
2039  XCTAssertEqualObjects(msg1, msg2);  // Equal values.
2040
2041  // Pointer comparisions, different objects.
2042  XCTAssertNotEqual(msg1.mapInt32Int32, msg2.mapInt32Int32);
2043  XCTAssertNotEqual(msg1.mapInt64Int64, msg2.mapInt64Int64);
2044  XCTAssertNotEqual(msg1.mapUint32Uint32, msg2.mapUint32Uint32);
2045  XCTAssertNotEqual(msg1.mapUint64Uint64, msg2.mapUint64Uint64);
2046  XCTAssertNotEqual(msg1.mapSint32Sint32, msg2.mapSint32Sint32);
2047  XCTAssertNotEqual(msg1.mapSint64Sint64, msg2.mapSint64Sint64);
2048  XCTAssertNotEqual(msg1.mapFixed32Fixed32, msg2.mapFixed32Fixed32);
2049  XCTAssertNotEqual(msg1.mapFixed64Fixed64, msg2.mapFixed64Fixed64);
2050  XCTAssertNotEqual(msg1.mapSfixed32Sfixed32, msg2.mapSfixed32Sfixed32);
2051  XCTAssertNotEqual(msg1.mapSfixed64Sfixed64, msg2.mapSfixed64Sfixed64);
2052  XCTAssertNotEqual(msg1.mapInt32Float, msg2.mapInt32Float);
2053  XCTAssertNotEqual(msg1.mapInt32Double, msg2.mapInt32Double);
2054  XCTAssertNotEqual(msg1.mapBoolBool, msg2.mapBoolBool);
2055  XCTAssertNotEqual(msg1.mapStringString, msg2.mapStringString);
2056  XCTAssertNotEqual(msg1.mapInt32Bytes, msg2.mapInt32Bytes);
2057  XCTAssertNotEqual(msg1.mapInt32Enum, msg2.mapInt32Enum);
2058  XCTAssertNotEqual(msg1.mapInt32ForeignMessage, msg2.mapInt32ForeignMessage);
2059
2060  // Ensure the messages are unique per map.
2061  [msg1.mapInt32ForeignMessage
2062      enumerateKeysAndObjectsUsingBlock:^(int32_t key, id value, BOOL *stop) {
2063#pragma unused(stop)
2064        ForeignMessage *subMsg2 = [msg2.mapInt32ForeignMessage objectForKey:key];
2065        XCTAssertNotEqual(value, subMsg2);  // Ptr compare, new object.
2066      }];
2067}
2068
2069- (void)test_GPBGetMessageRepeatedField {
2070  TestAllTypes *message = [TestAllTypes message];
2071  GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"repeatedStringArray"];
2072  XCTAssertNotNil(fieldDescriptor);
2073  NSMutableArray *fieldArray = GPBGetMessageRepeatedField(message, fieldDescriptor);
2074  XCTAssertNotNil(fieldArray);  // Should have autocreated.
2075  XCTAssertTrue(fieldArray == message.repeatedStringArray);  // Same pointer
2076}
2077
2078- (void)test_GPBSetMessageRepeatedField {
2079  TestAllTypes *message = [TestAllTypes message];
2080  GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"repeatedStringArray"];
2081  XCTAssertNotNil(fieldDescriptor);
2082
2083  NSMutableArray *fieldArray = [NSMutableArray arrayWithObject:@"foo"];
2084  GPBSetMessageRepeatedField(message, fieldDescriptor, fieldArray);
2085  XCTAssertTrue(fieldArray == message.repeatedStringArray);  // Same pointer
2086  XCTAssertEqualObjects(@"foo", message.repeatedStringArray.firstObject);
2087}
2088
2089- (void)test_GPBGetMessageMapField {
2090  TestMap *message = [TestMap message];
2091  GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"mapStringString"];
2092  XCTAssertNotNil(fieldDescriptor);
2093  NSMutableDictionary *fieldMap = GPBGetMessageMapField(message, fieldDescriptor);
2094  XCTAssertNotNil(fieldMap);  // Should have autocreated.
2095  XCTAssertTrue(fieldMap == message.mapStringString);  // Same pointer
2096}
2097
2098- (void)test_GPBSetMessageMapField {
2099  TestMap *message = [TestMap message];
2100  GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"mapStringString"];
2101  XCTAssertNotNil(fieldDescriptor);
2102
2103  NSMutableDictionary *fieldMap = [NSMutableDictionary dictionaryWithObject:@"bar" forKey:@"foo"];
2104  GPBSetMessageMapField(message, fieldDescriptor, fieldMap);
2105  XCTAssertTrue(fieldMap == message.mapStringString);  // Same pointer
2106  XCTAssertEqualObjects(@"bar", message.mapStringString[@"foo"]);
2107}
2108
2109#pragma mark - Subset from from map_tests.cc
2110
2111// TEST(GeneratedMapFieldTest, IsInitialized)
2112- (void)testMap_IsInitialized {
2113  TestRequiredMessageMap *msg = [[TestRequiredMessageMap alloc] init];
2114
2115  // Add an uninitialized message.
2116  TestRequired *subMsg = [[TestRequired alloc] init];
2117  [msg.mapField setObject:subMsg forKey:0];
2118  XCTAssertFalse(msg.initialized);
2119
2120  // Initialize uninitialized message
2121  subMsg.a = 0;
2122  subMsg.b = 0;
2123  subMsg.c = 0;
2124  XCTAssertTrue(msg.initialized);
2125
2126  [subMsg release];
2127  [msg release];
2128}
2129
2130@end
2131