1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
2ffe3c632Sopenharmony_ci// Copyright 2015 Google Inc.  All rights reserved.
3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
4ffe3c632Sopenharmony_ci//
5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
7ffe3c632Sopenharmony_ci// met:
8ffe3c632Sopenharmony_ci//
9ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
11ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
14ffe3c632Sopenharmony_ci// distribution.
15ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
17ffe3c632Sopenharmony_ci// this software without specific prior written permission.
18ffe3c632Sopenharmony_ci//
19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ffe3c632Sopenharmony_ci
31ffe3c632Sopenharmony_ci#import "GPBTestUtilities.h"
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ci#import <objc/runtime.h>
34ffe3c632Sopenharmony_ci
35ffe3c632Sopenharmony_ci#import "GPBMessage.h"
36ffe3c632Sopenharmony_ci
37ffe3c632Sopenharmony_ci#import "google/protobuf/MapProto2Unittest.pbobjc.h"
38ffe3c632Sopenharmony_ci#import "google/protobuf/MapUnittest.pbobjc.h"
39ffe3c632Sopenharmony_ci#import "google/protobuf/Unittest.pbobjc.h"
40ffe3c632Sopenharmony_ci#import "google/protobuf/UnittestDropUnknownFields.pbobjc.h"
41ffe3c632Sopenharmony_ci#import "google/protobuf/UnittestPreserveUnknownEnum.pbobjc.h"
42ffe3c632Sopenharmony_ci#import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
43ffe3c632Sopenharmony_ci#import "google/protobuf/UnittestRuntimeProto3.pbobjc.h"
44ffe3c632Sopenharmony_ci
45ffe3c632Sopenharmony_ci@interface MessageSerializationTests : GPBTestCase
46ffe3c632Sopenharmony_ci@end
47ffe3c632Sopenharmony_ci
48ffe3c632Sopenharmony_ci@implementation MessageSerializationTests
49ffe3c632Sopenharmony_ci
50ffe3c632Sopenharmony_ci// TODO(thomasvl): Pull tests over from GPBMessageTests that are serialization
51ffe3c632Sopenharmony_ci// specific.
52ffe3c632Sopenharmony_ci
53ffe3c632Sopenharmony_ci- (void)testProto3SerializationHandlingDefaults {
54ffe3c632Sopenharmony_ci  // Proto2 covered in other tests.
55ffe3c632Sopenharmony_ci
56ffe3c632Sopenharmony_ci  Message3 *msg = [[Message3 alloc] init];
57ffe3c632Sopenharmony_ci
58ffe3c632Sopenharmony_ci  // Add defaults, no output.
59ffe3c632Sopenharmony_ci
60ffe3c632Sopenharmony_ci  NSData *data = [msg data];
61ffe3c632Sopenharmony_ci  XCTAssertEqual([data length], 0U);
62ffe3c632Sopenharmony_ci
63ffe3c632Sopenharmony_ci  // All zeros, still nothing.
64ffe3c632Sopenharmony_ci
65ffe3c632Sopenharmony_ci  msg.optionalInt32 = 0;
66ffe3c632Sopenharmony_ci  msg.optionalInt64 = 0;
67ffe3c632Sopenharmony_ci  msg.optionalUint32 = 0;
68ffe3c632Sopenharmony_ci  msg.optionalUint64 = 0;
69ffe3c632Sopenharmony_ci  msg.optionalSint32 = 0;
70ffe3c632Sopenharmony_ci  msg.optionalSint64 = 0;
71ffe3c632Sopenharmony_ci  msg.optionalFixed32 = 0;
72ffe3c632Sopenharmony_ci  msg.optionalFixed64 = 0;
73ffe3c632Sopenharmony_ci  msg.optionalSfixed32 = 0;
74ffe3c632Sopenharmony_ci  msg.optionalSfixed64 = 0;
75ffe3c632Sopenharmony_ci  msg.optionalFloat = 0.0f;
76ffe3c632Sopenharmony_ci  msg.optionalDouble = 0.0;
77ffe3c632Sopenharmony_ci  msg.optionalBool = NO;
78ffe3c632Sopenharmony_ci  msg.optionalString = @"";
79ffe3c632Sopenharmony_ci  msg.optionalBytes = [NSData data];
80ffe3c632Sopenharmony_ci  msg.optionalEnum = Message3_Enum_Foo;  // first value
81ffe3c632Sopenharmony_ci
82ffe3c632Sopenharmony_ci  data = [msg data];
83ffe3c632Sopenharmony_ci  XCTAssertEqual([data length], 0U);
84ffe3c632Sopenharmony_ci
85ffe3c632Sopenharmony_ci  // The two that also take nil as nothing.
86ffe3c632Sopenharmony_ci
87ffe3c632Sopenharmony_ci  msg.optionalString = nil;
88ffe3c632Sopenharmony_ci  msg.optionalBytes = nil;
89ffe3c632Sopenharmony_ci
90ffe3c632Sopenharmony_ci  data = [msg data];
91ffe3c632Sopenharmony_ci  XCTAssertEqual([data length], 0U);
92ffe3c632Sopenharmony_ci
93ffe3c632Sopenharmony_ci  // Set one field...
94ffe3c632Sopenharmony_ci
95ffe3c632Sopenharmony_ci  msg.optionalInt32 = 1;
96ffe3c632Sopenharmony_ci
97ffe3c632Sopenharmony_ci  data = [msg data];
98ffe3c632Sopenharmony_ci  const uint8_t expectedBytes[] = {0x08, 0x01};
99ffe3c632Sopenharmony_ci  NSData *expected = [NSData dataWithBytes:expectedBytes length:2];
100ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(data, expected);
101ffe3c632Sopenharmony_ci
102ffe3c632Sopenharmony_ci  // Back to zero...
103ffe3c632Sopenharmony_ci
104ffe3c632Sopenharmony_ci  msg.optionalInt32 = 0;
105ffe3c632Sopenharmony_ci
106ffe3c632Sopenharmony_ci  data = [msg data];
107ffe3c632Sopenharmony_ci  XCTAssertEqual([data length], 0U);
108ffe3c632Sopenharmony_ci
109ffe3c632Sopenharmony_ci  [msg release];
110ffe3c632Sopenharmony_ci}
111ffe3c632Sopenharmony_ci
112ffe3c632Sopenharmony_ci- (void)testProto3SerializationHandlingOptionals {
113ffe3c632Sopenharmony_ci  //
114ffe3c632Sopenharmony_ci  // Proto3 optionals should be just like proto2, zero values also get serialized.
115ffe3c632Sopenharmony_ci  //
116ffe3c632Sopenharmony_ci
117ffe3c632Sopenharmony_ci//%PDDM-DEFINE PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(FIELD, ZERO_VALUE, EXPECTED_LEN)
118ffe3c632Sopenharmony_ci//%  {  // optional##FIELD
119ffe3c632Sopenharmony_ci//%    Message3Optional *msg = [[Message3Optional alloc] init];
120ffe3c632Sopenharmony_ci//%    NSData *data = [msg data];
121ffe3c632Sopenharmony_ci//%    XCTAssertEqual([data length], 0U);
122ffe3c632Sopenharmony_ci//%    msg.optional##FIELD = ZERO_VALUE;
123ffe3c632Sopenharmony_ci//%    data = [msg data];
124ffe3c632Sopenharmony_ci//%    XCTAssertEqual(data.length, EXPECTED_LEN);
125ffe3c632Sopenharmony_ci//%    NSError *err = nil;
126ffe3c632Sopenharmony_ci//%    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
127ffe3c632Sopenharmony_ci//%    XCTAssertNotNil(msg2);
128ffe3c632Sopenharmony_ci//%    XCTAssertNil(err);
129ffe3c632Sopenharmony_ci//%    XCTAssertTrue(msg2.hasOptional##FIELD);
130ffe3c632Sopenharmony_ci//%    XCTAssertEqualObjects(msg, msg2);
131ffe3c632Sopenharmony_ci//%    [msg release];
132ffe3c632Sopenharmony_ci//%  }
133ffe3c632Sopenharmony_ci//%
134ffe3c632Sopenharmony_ci//%PDDM-DEFINE PROTO3_TEST_SERIALIZE_OPTIONAL_FIELDS()
135ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Int32, 0, 2)
136ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Int64, 0, 2)
137ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Uint32, 0, 2)
138ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Uint64, 0, 2)
139ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sint32, 0, 2)
140ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sint64, 0, 2)
141ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Fixed32, 0, 5)
142ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Fixed64, 0, 9)
143ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sfixed32, 0, 5)
144ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sfixed64, 0, 9)
145ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Float, 0.0f, 5)
146ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Double, 0.0, 9)
147ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Bool, NO, 2)
148ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(String, @"", 2)
149ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Bytes, [NSData data], 2)
150ffe3c632Sopenharmony_ci//%  //
151ffe3c632Sopenharmony_ci//%  // Test doesn't apply to optionalMessage (no groups in proto3).
152ffe3c632Sopenharmony_ci//%  //
153ffe3c632Sopenharmony_ci//%
154ffe3c632Sopenharmony_ci//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Enum, Message3Optional_Enum_Foo, 3)
155ffe3c632Sopenharmony_ci//%PDDM-EXPAND PROTO3_TEST_SERIALIZE_OPTIONAL_FIELDS()
156ffe3c632Sopenharmony_ci// This block of code is generated, do not edit it directly.
157ffe3c632Sopenharmony_ci// clang-format off
158ffe3c632Sopenharmony_ci
159ffe3c632Sopenharmony_ci  {  // optionalInt32
160ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
161ffe3c632Sopenharmony_ci    NSData *data = [msg data];
162ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
163ffe3c632Sopenharmony_ci    msg.optionalInt32 = 0;
164ffe3c632Sopenharmony_ci    data = [msg data];
165ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
166ffe3c632Sopenharmony_ci    NSError *err = nil;
167ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
168ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
169ffe3c632Sopenharmony_ci    XCTAssertNil(err);
170ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalInt32);
171ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
172ffe3c632Sopenharmony_ci    [msg release];
173ffe3c632Sopenharmony_ci  }
174ffe3c632Sopenharmony_ci
175ffe3c632Sopenharmony_ci  {  // optionalInt64
176ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
177ffe3c632Sopenharmony_ci    NSData *data = [msg data];
178ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
179ffe3c632Sopenharmony_ci    msg.optionalInt64 = 0;
180ffe3c632Sopenharmony_ci    data = [msg data];
181ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
182ffe3c632Sopenharmony_ci    NSError *err = nil;
183ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
184ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
185ffe3c632Sopenharmony_ci    XCTAssertNil(err);
186ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalInt64);
187ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
188ffe3c632Sopenharmony_ci    [msg release];
189ffe3c632Sopenharmony_ci  }
190ffe3c632Sopenharmony_ci
191ffe3c632Sopenharmony_ci  {  // optionalUint32
192ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
193ffe3c632Sopenharmony_ci    NSData *data = [msg data];
194ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
195ffe3c632Sopenharmony_ci    msg.optionalUint32 = 0;
196ffe3c632Sopenharmony_ci    data = [msg data];
197ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
198ffe3c632Sopenharmony_ci    NSError *err = nil;
199ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
200ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
201ffe3c632Sopenharmony_ci    XCTAssertNil(err);
202ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalUint32);
203ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
204ffe3c632Sopenharmony_ci    [msg release];
205ffe3c632Sopenharmony_ci  }
206ffe3c632Sopenharmony_ci
207ffe3c632Sopenharmony_ci  {  // optionalUint64
208ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
209ffe3c632Sopenharmony_ci    NSData *data = [msg data];
210ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
211ffe3c632Sopenharmony_ci    msg.optionalUint64 = 0;
212ffe3c632Sopenharmony_ci    data = [msg data];
213ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
214ffe3c632Sopenharmony_ci    NSError *err = nil;
215ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
216ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
217ffe3c632Sopenharmony_ci    XCTAssertNil(err);
218ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalUint64);
219ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
220ffe3c632Sopenharmony_ci    [msg release];
221ffe3c632Sopenharmony_ci  }
222ffe3c632Sopenharmony_ci
223ffe3c632Sopenharmony_ci  {  // optionalSint32
224ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
225ffe3c632Sopenharmony_ci    NSData *data = [msg data];
226ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
227ffe3c632Sopenharmony_ci    msg.optionalSint32 = 0;
228ffe3c632Sopenharmony_ci    data = [msg data];
229ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
230ffe3c632Sopenharmony_ci    NSError *err = nil;
231ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
232ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
233ffe3c632Sopenharmony_ci    XCTAssertNil(err);
234ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalSint32);
235ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
236ffe3c632Sopenharmony_ci    [msg release];
237ffe3c632Sopenharmony_ci  }
238ffe3c632Sopenharmony_ci
239ffe3c632Sopenharmony_ci  {  // optionalSint64
240ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
241ffe3c632Sopenharmony_ci    NSData *data = [msg data];
242ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
243ffe3c632Sopenharmony_ci    msg.optionalSint64 = 0;
244ffe3c632Sopenharmony_ci    data = [msg data];
245ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
246ffe3c632Sopenharmony_ci    NSError *err = nil;
247ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
248ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
249ffe3c632Sopenharmony_ci    XCTAssertNil(err);
250ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalSint64);
251ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
252ffe3c632Sopenharmony_ci    [msg release];
253ffe3c632Sopenharmony_ci  }
254ffe3c632Sopenharmony_ci
255ffe3c632Sopenharmony_ci  {  // optionalFixed32
256ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
257ffe3c632Sopenharmony_ci    NSData *data = [msg data];
258ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
259ffe3c632Sopenharmony_ci    msg.optionalFixed32 = 0;
260ffe3c632Sopenharmony_ci    data = [msg data];
261ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 5);
262ffe3c632Sopenharmony_ci    NSError *err = nil;
263ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
264ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
265ffe3c632Sopenharmony_ci    XCTAssertNil(err);
266ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalFixed32);
267ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
268ffe3c632Sopenharmony_ci    [msg release];
269ffe3c632Sopenharmony_ci  }
270ffe3c632Sopenharmony_ci
271ffe3c632Sopenharmony_ci  {  // optionalFixed64
272ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
273ffe3c632Sopenharmony_ci    NSData *data = [msg data];
274ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
275ffe3c632Sopenharmony_ci    msg.optionalFixed64 = 0;
276ffe3c632Sopenharmony_ci    data = [msg data];
277ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 9);
278ffe3c632Sopenharmony_ci    NSError *err = nil;
279ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
280ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
281ffe3c632Sopenharmony_ci    XCTAssertNil(err);
282ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalFixed64);
283ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
284ffe3c632Sopenharmony_ci    [msg release];
285ffe3c632Sopenharmony_ci  }
286ffe3c632Sopenharmony_ci
287ffe3c632Sopenharmony_ci  {  // optionalSfixed32
288ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
289ffe3c632Sopenharmony_ci    NSData *data = [msg data];
290ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
291ffe3c632Sopenharmony_ci    msg.optionalSfixed32 = 0;
292ffe3c632Sopenharmony_ci    data = [msg data];
293ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 5);
294ffe3c632Sopenharmony_ci    NSError *err = nil;
295ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
296ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
297ffe3c632Sopenharmony_ci    XCTAssertNil(err);
298ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalSfixed32);
299ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
300ffe3c632Sopenharmony_ci    [msg release];
301ffe3c632Sopenharmony_ci  }
302ffe3c632Sopenharmony_ci
303ffe3c632Sopenharmony_ci  {  // optionalSfixed64
304ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
305ffe3c632Sopenharmony_ci    NSData *data = [msg data];
306ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
307ffe3c632Sopenharmony_ci    msg.optionalSfixed64 = 0;
308ffe3c632Sopenharmony_ci    data = [msg data];
309ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 9);
310ffe3c632Sopenharmony_ci    NSError *err = nil;
311ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
312ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
313ffe3c632Sopenharmony_ci    XCTAssertNil(err);
314ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalSfixed64);
315ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
316ffe3c632Sopenharmony_ci    [msg release];
317ffe3c632Sopenharmony_ci  }
318ffe3c632Sopenharmony_ci
319ffe3c632Sopenharmony_ci  {  // optionalFloat
320ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
321ffe3c632Sopenharmony_ci    NSData *data = [msg data];
322ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
323ffe3c632Sopenharmony_ci    msg.optionalFloat = 0.0f;
324ffe3c632Sopenharmony_ci    data = [msg data];
325ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 5);
326ffe3c632Sopenharmony_ci    NSError *err = nil;
327ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
328ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
329ffe3c632Sopenharmony_ci    XCTAssertNil(err);
330ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalFloat);
331ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
332ffe3c632Sopenharmony_ci    [msg release];
333ffe3c632Sopenharmony_ci  }
334ffe3c632Sopenharmony_ci
335ffe3c632Sopenharmony_ci  {  // optionalDouble
336ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
337ffe3c632Sopenharmony_ci    NSData *data = [msg data];
338ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
339ffe3c632Sopenharmony_ci    msg.optionalDouble = 0.0;
340ffe3c632Sopenharmony_ci    data = [msg data];
341ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 9);
342ffe3c632Sopenharmony_ci    NSError *err = nil;
343ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
344ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
345ffe3c632Sopenharmony_ci    XCTAssertNil(err);
346ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalDouble);
347ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
348ffe3c632Sopenharmony_ci    [msg release];
349ffe3c632Sopenharmony_ci  }
350ffe3c632Sopenharmony_ci
351ffe3c632Sopenharmony_ci  {  // optionalBool
352ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
353ffe3c632Sopenharmony_ci    NSData *data = [msg data];
354ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
355ffe3c632Sopenharmony_ci    msg.optionalBool = NO;
356ffe3c632Sopenharmony_ci    data = [msg data];
357ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
358ffe3c632Sopenharmony_ci    NSError *err = nil;
359ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
360ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
361ffe3c632Sopenharmony_ci    XCTAssertNil(err);
362ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalBool);
363ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
364ffe3c632Sopenharmony_ci    [msg release];
365ffe3c632Sopenharmony_ci  }
366ffe3c632Sopenharmony_ci
367ffe3c632Sopenharmony_ci  {  // optionalString
368ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
369ffe3c632Sopenharmony_ci    NSData *data = [msg data];
370ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
371ffe3c632Sopenharmony_ci    msg.optionalString = @"";
372ffe3c632Sopenharmony_ci    data = [msg data];
373ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
374ffe3c632Sopenharmony_ci    NSError *err = nil;
375ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
376ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
377ffe3c632Sopenharmony_ci    XCTAssertNil(err);
378ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalString);
379ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
380ffe3c632Sopenharmony_ci    [msg release];
381ffe3c632Sopenharmony_ci  }
382ffe3c632Sopenharmony_ci
383ffe3c632Sopenharmony_ci  {  // optionalBytes
384ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
385ffe3c632Sopenharmony_ci    NSData *data = [msg data];
386ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
387ffe3c632Sopenharmony_ci    msg.optionalBytes = [NSData data];
388ffe3c632Sopenharmony_ci    data = [msg data];
389ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 2);
390ffe3c632Sopenharmony_ci    NSError *err = nil;
391ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
392ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
393ffe3c632Sopenharmony_ci    XCTAssertNil(err);
394ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalBytes);
395ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
396ffe3c632Sopenharmony_ci    [msg release];
397ffe3c632Sopenharmony_ci  }
398ffe3c632Sopenharmony_ci
399ffe3c632Sopenharmony_ci  //
400ffe3c632Sopenharmony_ci  // Test doesn't apply to optionalMessage (no groups in proto3).
401ffe3c632Sopenharmony_ci  //
402ffe3c632Sopenharmony_ci
403ffe3c632Sopenharmony_ci  {  // optionalEnum
404ffe3c632Sopenharmony_ci    Message3Optional *msg = [[Message3Optional alloc] init];
405ffe3c632Sopenharmony_ci    NSData *data = [msg data];
406ffe3c632Sopenharmony_ci    XCTAssertEqual([data length], 0U);
407ffe3c632Sopenharmony_ci    msg.optionalEnum = Message3Optional_Enum_Foo;
408ffe3c632Sopenharmony_ci    data = [msg data];
409ffe3c632Sopenharmony_ci    XCTAssertEqual(data.length, 3);
410ffe3c632Sopenharmony_ci    NSError *err = nil;
411ffe3c632Sopenharmony_ci    Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err];
412ffe3c632Sopenharmony_ci    XCTAssertNotNil(msg2);
413ffe3c632Sopenharmony_ci    XCTAssertNil(err);
414ffe3c632Sopenharmony_ci    XCTAssertTrue(msg2.hasOptionalEnum);
415ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg, msg2);
416ffe3c632Sopenharmony_ci    [msg release];
417ffe3c632Sopenharmony_ci  }
418ffe3c632Sopenharmony_ci
419ffe3c632Sopenharmony_ci// clang-format on
420ffe3c632Sopenharmony_ci//%PDDM-EXPAND-END PROTO3_TEST_SERIALIZE_OPTIONAL_FIELDS()
421ffe3c632Sopenharmony_ci}
422ffe3c632Sopenharmony_ci
423ffe3c632Sopenharmony_ci- (void)testProto2UnknownEnumToUnknownField {
424ffe3c632Sopenharmony_ci  Message3 *orig = [[Message3 alloc] init];
425ffe3c632Sopenharmony_ci
426ffe3c632Sopenharmony_ci  orig.optionalEnum = Message3_Enum_Extra3;
427ffe3c632Sopenharmony_ci  orig.repeatedEnumArray =
428ffe3c632Sopenharmony_ci      [GPBEnumArray arrayWithValidationFunction:Message3_Enum_IsValidValue
429ffe3c632Sopenharmony_ci                                       rawValue:Message3_Enum_Extra3];
430ffe3c632Sopenharmony_ci  orig.oneofEnum = Message3_Enum_Extra3;
431ffe3c632Sopenharmony_ci
432ffe3c632Sopenharmony_ci  NSData *data = [orig data];
433ffe3c632Sopenharmony_ci  XCTAssertNotNil(data);
434ffe3c632Sopenharmony_ci  Message2 *msg = [[Message2 alloc] initWithData:data error:NULL];
435ffe3c632Sopenharmony_ci
436ffe3c632Sopenharmony_ci  // None of the fields should be set.
437ffe3c632Sopenharmony_ci
438ffe3c632Sopenharmony_ci  XCTAssertFalse(msg.hasOptionalEnum);
439ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.repeatedEnumArray.count, 0U);
440ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_GPBUnsetOneOfCase);
441ffe3c632Sopenharmony_ci
442ffe3c632Sopenharmony_ci  // All the values should be in unknown fields.
443ffe3c632Sopenharmony_ci
444ffe3c632Sopenharmony_ci  GPBUnknownFieldSet *unknownFields = msg.unknownFields;
445ffe3c632Sopenharmony_ci
446ffe3c632Sopenharmony_ci  XCTAssertEqual([unknownFields countOfFields], 3U);
447ffe3c632Sopenharmony_ci  XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OptionalEnum]);
448ffe3c632Sopenharmony_ci  XCTAssertTrue(
449ffe3c632Sopenharmony_ci      [unknownFields hasField:Message2_FieldNumber_RepeatedEnumArray]);
450ffe3c632Sopenharmony_ci  XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OneofEnum]);
451ffe3c632Sopenharmony_ci
452ffe3c632Sopenharmony_ci  GPBUnknownField *field =
453ffe3c632Sopenharmony_ci      [unknownFields getField:Message2_FieldNumber_OptionalEnum];
454ffe3c632Sopenharmony_ci  XCTAssertEqual(field.varintList.count, 1U);
455ffe3c632Sopenharmony_ci  XCTAssertEqual([field.varintList valueAtIndex:0],
456ffe3c632Sopenharmony_ci                 (uint64_t)Message3_Enum_Extra3);
457ffe3c632Sopenharmony_ci
458ffe3c632Sopenharmony_ci  field = [unknownFields getField:Message2_FieldNumber_RepeatedEnumArray];
459ffe3c632Sopenharmony_ci  XCTAssertEqual(field.varintList.count, 1U);
460ffe3c632Sopenharmony_ci  XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)Message3_Enum_Extra3);
461ffe3c632Sopenharmony_ci
462ffe3c632Sopenharmony_ci  field = [unknownFields getField:Message2_FieldNumber_OneofEnum];
463ffe3c632Sopenharmony_ci  XCTAssertEqual(field.varintList.count, 1U);
464ffe3c632Sopenharmony_ci  XCTAssertEqual([field.varintList valueAtIndex:0],
465ffe3c632Sopenharmony_ci                 (uint64_t)Message3_Enum_Extra3);
466ffe3c632Sopenharmony_ci
467ffe3c632Sopenharmony_ci  [msg release];
468ffe3c632Sopenharmony_ci  [orig release];
469ffe3c632Sopenharmony_ci}
470ffe3c632Sopenharmony_ci
471ffe3c632Sopenharmony_ci- (void)testProto3UnknownEnumPreserving {
472ffe3c632Sopenharmony_ci  UnknownEnumsMyMessagePlusExtra *orig =
473ffe3c632Sopenharmony_ci      [UnknownEnumsMyMessagePlusExtra message];
474ffe3c632Sopenharmony_ci
475ffe3c632Sopenharmony_ci  orig.e = UnknownEnumsMyEnumPlusExtra_EExtra;
476ffe3c632Sopenharmony_ci  orig.repeatedEArray = [GPBEnumArray
477ffe3c632Sopenharmony_ci      arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue
478ffe3c632Sopenharmony_ci                         rawValue:UnknownEnumsMyEnumPlusExtra_EExtra];
479ffe3c632Sopenharmony_ci  orig.repeatedPackedEArray = [GPBEnumArray
480ffe3c632Sopenharmony_ci      arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue
481ffe3c632Sopenharmony_ci                         rawValue:UnknownEnumsMyEnumPlusExtra_EExtra];
482ffe3c632Sopenharmony_ci  orig.oneofE1 = UnknownEnumsMyEnumPlusExtra_EExtra;
483ffe3c632Sopenharmony_ci
484ffe3c632Sopenharmony_ci  // Everything should be there via raw values.
485ffe3c632Sopenharmony_ci
486ffe3c632Sopenharmony_ci  NSData *data = [orig data];
487ffe3c632Sopenharmony_ci  XCTAssertNotNil(data);
488ffe3c632Sopenharmony_ci  UnknownEnumsMyMessage *msg =
489ffe3c632Sopenharmony_ci      [UnknownEnumsMyMessage parseFromData:data error:NULL];
490ffe3c632Sopenharmony_ci
491ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.e, UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
492ffe3c632Sopenharmony_ci  XCTAssertEqual(UnknownEnumsMyMessage_E_RawValue(msg),
493ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnumPlusExtra_EExtra);
494ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.repeatedEArray.count, 1U);
495ffe3c632Sopenharmony_ci  XCTAssertEqual([msg.repeatedEArray valueAtIndex:0],
496ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
497ffe3c632Sopenharmony_ci  XCTAssertEqual([msg.repeatedEArray rawValueAtIndex:0],
498ffe3c632Sopenharmony_ci                 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra);
499ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.repeatedPackedEArray.count, 1U);
500ffe3c632Sopenharmony_ci  XCTAssertEqual([msg.repeatedPackedEArray valueAtIndex:0],
501ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
502ffe3c632Sopenharmony_ci  XCTAssertEqual([msg.repeatedPackedEArray rawValueAtIndex:0],
503ffe3c632Sopenharmony_ci                 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra);
504ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.oneofE1,
505ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
506ffe3c632Sopenharmony_ci  XCTAssertEqual(UnknownEnumsMyMessage_OneofE1_RawValue(msg),
507ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnumPlusExtra_EExtra);
508ffe3c632Sopenharmony_ci
509ffe3c632Sopenharmony_ci  // Everything should go out and come back.
510ffe3c632Sopenharmony_ci
511ffe3c632Sopenharmony_ci  data = [msg data];
512ffe3c632Sopenharmony_ci  orig = [UnknownEnumsMyMessagePlusExtra parseFromData:data error:NULL];
513ffe3c632Sopenharmony_ci
514ffe3c632Sopenharmony_ci  XCTAssertEqual(orig.e, UnknownEnumsMyEnumPlusExtra_EExtra);
515ffe3c632Sopenharmony_ci  XCTAssertEqual(orig.repeatedEArray.count, 1U);
516ffe3c632Sopenharmony_ci  XCTAssertEqual([orig.repeatedEArray valueAtIndex:0],
517ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnumPlusExtra_EExtra);
518ffe3c632Sopenharmony_ci  XCTAssertEqual(orig.repeatedPackedEArray.count, 1U);
519ffe3c632Sopenharmony_ci  XCTAssertEqual([orig.repeatedPackedEArray valueAtIndex:0],
520ffe3c632Sopenharmony_ci                 UnknownEnumsMyEnumPlusExtra_EExtra);
521ffe3c632Sopenharmony_ci  XCTAssertEqual(orig.oneofE1, UnknownEnumsMyEnumPlusExtra_EExtra);
522ffe3c632Sopenharmony_ci}
523ffe3c632Sopenharmony_ci
524ffe3c632Sopenharmony_ci//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF(MESSAGE, FIELD, VALUE)
525ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, )
526ffe3c632Sopenharmony_ci//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, EQ_SUFFIX)
527ffe3c632Sopenharmony_ci//%  {  // oneof##FIELD
528ffe3c632Sopenharmony_ci//%    MESSAGE *orig = [[MESSAGE alloc] init];
529ffe3c632Sopenharmony_ci//%    orig.oneof##FIELD = VALUE;
530ffe3c632Sopenharmony_ci//%    XCTAssertEqual(orig.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD);
531ffe3c632Sopenharmony_ci//%    NSData *data = [orig data];
532ffe3c632Sopenharmony_ci//%    XCTAssertNotNil(data);
533ffe3c632Sopenharmony_ci//%    MESSAGE *msg = [MESSAGE parseFromData:data error:NULL];
534ffe3c632Sopenharmony_ci//%    XCTAssertEqual(msg.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD);
535ffe3c632Sopenharmony_ci//%    XCTAssertEqual##EQ_SUFFIX(msg.oneof##FIELD, VALUE);
536ffe3c632Sopenharmony_ci//%    [orig release];
537ffe3c632Sopenharmony_ci//%  }
538ffe3c632Sopenharmony_ci//%
539ffe3c632Sopenharmony_ci//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOFS(SYNTAX, BOOL_NON_DEFAULT)
540ffe3c632Sopenharmony_ci//%- (void)testProto##SYNTAX##RoundTripOneof {
541ffe3c632Sopenharmony_ci//%
542ffe3c632Sopenharmony_ci//%GROUP_INIT##SYNTAX()  Message##SYNTAX *subMessage = [[Message##SYNTAX alloc] init];
543ffe3c632Sopenharmony_ci//%  XCTAssertNotNil(subMessage);
544ffe3c632Sopenharmony_ci//%  subMessage.optionalInt32 = 666;
545ffe3c632Sopenharmony_ci//%
546ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int32, 1)
547ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int64, 2)
548ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint32, 3U)
549ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint64, 4U)
550ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint32, 5)
551ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint64, 6)
552ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed32, 7U)
553ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed64, 8U)
554ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed32, 9)
555ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed64, 10)
556ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Float, 11.0f)
557ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Double, 12.0)
558ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Bool, BOOL_NON_DEFAULT)
559ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, String, @"foo", Objects)
560ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Bytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding], Objects)
561ffe3c632Sopenharmony_ci//%GROUP_TEST##SYNTAX()TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Message, subMessage, Objects)
562ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Enum, Message2_Enum_Bar)
563ffe3c632Sopenharmony_ci//%GROUP_CLEANUP##SYNTAX()  [subMessage release];
564ffe3c632Sopenharmony_ci//%}
565ffe3c632Sopenharmony_ci//%
566ffe3c632Sopenharmony_ci//%PDDM-DEFINE GROUP_INIT2()
567ffe3c632Sopenharmony_ci//%  Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init];
568ffe3c632Sopenharmony_ci//%  XCTAssertNotNil(group);
569ffe3c632Sopenharmony_ci//%  group.a = 777;
570ffe3c632Sopenharmony_ci//%
571ffe3c632Sopenharmony_ci//%PDDM-DEFINE GROUP_CLEANUP2()
572ffe3c632Sopenharmony_ci//%  [group release];
573ffe3c632Sopenharmony_ci//%
574ffe3c632Sopenharmony_ci//%PDDM-DEFINE GROUP_TEST2()
575ffe3c632Sopenharmony_ci//%TEST_ROUNDTRIP_ONEOF_ADV(Message2, Group, group, Objects)
576ffe3c632Sopenharmony_ci//%
577ffe3c632Sopenharmony_ci//%PDDM-DEFINE GROUP_INIT3()
578ffe3c632Sopenharmony_ci// Empty
579ffe3c632Sopenharmony_ci//%PDDM-DEFINE GROUP_CLEANUP3()
580ffe3c632Sopenharmony_ci// Empty
581ffe3c632Sopenharmony_ci//%PDDM-DEFINE GROUP_TEST3()
582ffe3c632Sopenharmony_ci//%  // Not "group" in proto3.
583ffe3c632Sopenharmony_ci//%
584ffe3c632Sopenharmony_ci//%
585ffe3c632Sopenharmony_ci//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(2, NO)
586ffe3c632Sopenharmony_ci// This block of code is generated, do not edit it directly.
587ffe3c632Sopenharmony_ci// clang-format off
588ffe3c632Sopenharmony_ci
589ffe3c632Sopenharmony_ci- (void)testProto2RoundTripOneof {
590ffe3c632Sopenharmony_ci
591ffe3c632Sopenharmony_ci  Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init];
592ffe3c632Sopenharmony_ci  XCTAssertNotNil(group);
593ffe3c632Sopenharmony_ci  group.a = 777;
594ffe3c632Sopenharmony_ci  Message2 *subMessage = [[Message2 alloc] init];
595ffe3c632Sopenharmony_ci  XCTAssertNotNil(subMessage);
596ffe3c632Sopenharmony_ci  subMessage.optionalInt32 = 666;
597ffe3c632Sopenharmony_ci
598ffe3c632Sopenharmony_ci  {  // oneofInt32
599ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
600ffe3c632Sopenharmony_ci    orig.oneofInt32 = 1;
601ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
602ffe3c632Sopenharmony_ci    NSData *data = [orig data];
603ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
604ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
605ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
606ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofInt32, 1);
607ffe3c632Sopenharmony_ci    [orig release];
608ffe3c632Sopenharmony_ci  }
609ffe3c632Sopenharmony_ci
610ffe3c632Sopenharmony_ci  {  // oneofInt64
611ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
612ffe3c632Sopenharmony_ci    orig.oneofInt64 = 2;
613ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
614ffe3c632Sopenharmony_ci    NSData *data = [orig data];
615ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
616ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
617ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
618ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofInt64, 2);
619ffe3c632Sopenharmony_ci    [orig release];
620ffe3c632Sopenharmony_ci  }
621ffe3c632Sopenharmony_ci
622ffe3c632Sopenharmony_ci  {  // oneofUint32
623ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
624ffe3c632Sopenharmony_ci    orig.oneofUint32 = 3U;
625ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
626ffe3c632Sopenharmony_ci    NSData *data = [orig data];
627ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
628ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
629ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
630ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofUint32, 3U);
631ffe3c632Sopenharmony_ci    [orig release];
632ffe3c632Sopenharmony_ci  }
633ffe3c632Sopenharmony_ci
634ffe3c632Sopenharmony_ci  {  // oneofUint64
635ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
636ffe3c632Sopenharmony_ci    orig.oneofUint64 = 4U;
637ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
638ffe3c632Sopenharmony_ci    NSData *data = [orig data];
639ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
640ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
641ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
642ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofUint64, 4U);
643ffe3c632Sopenharmony_ci    [orig release];
644ffe3c632Sopenharmony_ci  }
645ffe3c632Sopenharmony_ci
646ffe3c632Sopenharmony_ci  {  // oneofSint32
647ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
648ffe3c632Sopenharmony_ci    orig.oneofSint32 = 5;
649ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
650ffe3c632Sopenharmony_ci    NSData *data = [orig data];
651ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
652ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
653ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
654ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSint32, 5);
655ffe3c632Sopenharmony_ci    [orig release];
656ffe3c632Sopenharmony_ci  }
657ffe3c632Sopenharmony_ci
658ffe3c632Sopenharmony_ci  {  // oneofSint64
659ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
660ffe3c632Sopenharmony_ci    orig.oneofSint64 = 6;
661ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
662ffe3c632Sopenharmony_ci    NSData *data = [orig data];
663ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
664ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
665ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
666ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSint64, 6);
667ffe3c632Sopenharmony_ci    [orig release];
668ffe3c632Sopenharmony_ci  }
669ffe3c632Sopenharmony_ci
670ffe3c632Sopenharmony_ci  {  // oneofFixed32
671ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
672ffe3c632Sopenharmony_ci    orig.oneofFixed32 = 7U;
673ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
674ffe3c632Sopenharmony_ci    NSData *data = [orig data];
675ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
676ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
677ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
678ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofFixed32, 7U);
679ffe3c632Sopenharmony_ci    [orig release];
680ffe3c632Sopenharmony_ci  }
681ffe3c632Sopenharmony_ci
682ffe3c632Sopenharmony_ci  {  // oneofFixed64
683ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
684ffe3c632Sopenharmony_ci    orig.oneofFixed64 = 8U;
685ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
686ffe3c632Sopenharmony_ci    NSData *data = [orig data];
687ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
688ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
689ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
690ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofFixed64, 8U);
691ffe3c632Sopenharmony_ci    [orig release];
692ffe3c632Sopenharmony_ci  }
693ffe3c632Sopenharmony_ci
694ffe3c632Sopenharmony_ci  {  // oneofSfixed32
695ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
696ffe3c632Sopenharmony_ci    orig.oneofSfixed32 = 9;
697ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
698ffe3c632Sopenharmony_ci    NSData *data = [orig data];
699ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
700ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
701ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
702ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSfixed32, 9);
703ffe3c632Sopenharmony_ci    [orig release];
704ffe3c632Sopenharmony_ci  }
705ffe3c632Sopenharmony_ci
706ffe3c632Sopenharmony_ci  {  // oneofSfixed64
707ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
708ffe3c632Sopenharmony_ci    orig.oneofSfixed64 = 10;
709ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
710ffe3c632Sopenharmony_ci    NSData *data = [orig data];
711ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
712ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
713ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
714ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSfixed64, 10);
715ffe3c632Sopenharmony_ci    [orig release];
716ffe3c632Sopenharmony_ci  }
717ffe3c632Sopenharmony_ci
718ffe3c632Sopenharmony_ci  {  // oneofFloat
719ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
720ffe3c632Sopenharmony_ci    orig.oneofFloat = 11.0f;
721ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
722ffe3c632Sopenharmony_ci    NSData *data = [orig data];
723ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
724ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
725ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
726ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofFloat, 11.0f);
727ffe3c632Sopenharmony_ci    [orig release];
728ffe3c632Sopenharmony_ci  }
729ffe3c632Sopenharmony_ci
730ffe3c632Sopenharmony_ci  {  // oneofDouble
731ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
732ffe3c632Sopenharmony_ci    orig.oneofDouble = 12.0;
733ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
734ffe3c632Sopenharmony_ci    NSData *data = [orig data];
735ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
736ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
737ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
738ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofDouble, 12.0);
739ffe3c632Sopenharmony_ci    [orig release];
740ffe3c632Sopenharmony_ci  }
741ffe3c632Sopenharmony_ci
742ffe3c632Sopenharmony_ci  {  // oneofBool
743ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
744ffe3c632Sopenharmony_ci    orig.oneofBool = NO;
745ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBool);
746ffe3c632Sopenharmony_ci    NSData *data = [orig data];
747ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
748ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
749ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBool);
750ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofBool, NO);
751ffe3c632Sopenharmony_ci    [orig release];
752ffe3c632Sopenharmony_ci  }
753ffe3c632Sopenharmony_ci
754ffe3c632Sopenharmony_ci  {  // oneofString
755ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
756ffe3c632Sopenharmony_ci    orig.oneofString = @"foo";
757ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofString);
758ffe3c632Sopenharmony_ci    NSData *data = [orig data];
759ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
760ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
761ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofString);
762ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofString, @"foo");
763ffe3c632Sopenharmony_ci    [orig release];
764ffe3c632Sopenharmony_ci  }
765ffe3c632Sopenharmony_ci
766ffe3c632Sopenharmony_ci  {  // oneofBytes
767ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
768ffe3c632Sopenharmony_ci    orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
769ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
770ffe3c632Sopenharmony_ci    NSData *data = [orig data];
771ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
772ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
773ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
774ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
775ffe3c632Sopenharmony_ci    [orig release];
776ffe3c632Sopenharmony_ci  }
777ffe3c632Sopenharmony_ci
778ffe3c632Sopenharmony_ci  {  // oneofGroup
779ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
780ffe3c632Sopenharmony_ci    orig.oneofGroup = group;
781ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
782ffe3c632Sopenharmony_ci    NSData *data = [orig data];
783ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
784ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
785ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
786ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofGroup, group);
787ffe3c632Sopenharmony_ci    [orig release];
788ffe3c632Sopenharmony_ci  }
789ffe3c632Sopenharmony_ci
790ffe3c632Sopenharmony_ci  {  // oneofMessage
791ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
792ffe3c632Sopenharmony_ci    orig.oneofMessage = subMessage;
793ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
794ffe3c632Sopenharmony_ci    NSData *data = [orig data];
795ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
796ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
797ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
798ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofMessage, subMessage);
799ffe3c632Sopenharmony_ci    [orig release];
800ffe3c632Sopenharmony_ci  }
801ffe3c632Sopenharmony_ci
802ffe3c632Sopenharmony_ci  {  // oneofEnum
803ffe3c632Sopenharmony_ci    Message2 *orig = [[Message2 alloc] init];
804ffe3c632Sopenharmony_ci    orig.oneofEnum = Message2_Enum_Bar;
805ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
806ffe3c632Sopenharmony_ci    NSData *data = [orig data];
807ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
808ffe3c632Sopenharmony_ci    Message2 *msg = [Message2 parseFromData:data error:NULL];
809ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
810ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
811ffe3c632Sopenharmony_ci    [orig release];
812ffe3c632Sopenharmony_ci  }
813ffe3c632Sopenharmony_ci
814ffe3c632Sopenharmony_ci  [group release];
815ffe3c632Sopenharmony_ci  [subMessage release];
816ffe3c632Sopenharmony_ci}
817ffe3c632Sopenharmony_ci
818ffe3c632Sopenharmony_ci// clang-format on
819ffe3c632Sopenharmony_ci//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(3, YES)
820ffe3c632Sopenharmony_ci// This block of code is generated, do not edit it directly.
821ffe3c632Sopenharmony_ci// clang-format off
822ffe3c632Sopenharmony_ci
823ffe3c632Sopenharmony_ci- (void)testProto3RoundTripOneof {
824ffe3c632Sopenharmony_ci
825ffe3c632Sopenharmony_ci  Message3 *subMessage = [[Message3 alloc] init];
826ffe3c632Sopenharmony_ci  XCTAssertNotNil(subMessage);
827ffe3c632Sopenharmony_ci  subMessage.optionalInt32 = 666;
828ffe3c632Sopenharmony_ci
829ffe3c632Sopenharmony_ci  {  // oneofInt32
830ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
831ffe3c632Sopenharmony_ci    orig.oneofInt32 = 1;
832ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
833ffe3c632Sopenharmony_ci    NSData *data = [orig data];
834ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
835ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
836ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
837ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofInt32, 1);
838ffe3c632Sopenharmony_ci    [orig release];
839ffe3c632Sopenharmony_ci  }
840ffe3c632Sopenharmony_ci
841ffe3c632Sopenharmony_ci  {  // oneofInt64
842ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
843ffe3c632Sopenharmony_ci    orig.oneofInt64 = 2;
844ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
845ffe3c632Sopenharmony_ci    NSData *data = [orig data];
846ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
847ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
848ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
849ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofInt64, 2);
850ffe3c632Sopenharmony_ci    [orig release];
851ffe3c632Sopenharmony_ci  }
852ffe3c632Sopenharmony_ci
853ffe3c632Sopenharmony_ci  {  // oneofUint32
854ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
855ffe3c632Sopenharmony_ci    orig.oneofUint32 = 3U;
856ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
857ffe3c632Sopenharmony_ci    NSData *data = [orig data];
858ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
859ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
860ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
861ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofUint32, 3U);
862ffe3c632Sopenharmony_ci    [orig release];
863ffe3c632Sopenharmony_ci  }
864ffe3c632Sopenharmony_ci
865ffe3c632Sopenharmony_ci  {  // oneofUint64
866ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
867ffe3c632Sopenharmony_ci    orig.oneofUint64 = 4U;
868ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
869ffe3c632Sopenharmony_ci    NSData *data = [orig data];
870ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
871ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
872ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
873ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofUint64, 4U);
874ffe3c632Sopenharmony_ci    [orig release];
875ffe3c632Sopenharmony_ci  }
876ffe3c632Sopenharmony_ci
877ffe3c632Sopenharmony_ci  {  // oneofSint32
878ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
879ffe3c632Sopenharmony_ci    orig.oneofSint32 = 5;
880ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
881ffe3c632Sopenharmony_ci    NSData *data = [orig data];
882ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
883ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
884ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
885ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSint32, 5);
886ffe3c632Sopenharmony_ci    [orig release];
887ffe3c632Sopenharmony_ci  }
888ffe3c632Sopenharmony_ci
889ffe3c632Sopenharmony_ci  {  // oneofSint64
890ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
891ffe3c632Sopenharmony_ci    orig.oneofSint64 = 6;
892ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
893ffe3c632Sopenharmony_ci    NSData *data = [orig data];
894ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
895ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
896ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
897ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSint64, 6);
898ffe3c632Sopenharmony_ci    [orig release];
899ffe3c632Sopenharmony_ci  }
900ffe3c632Sopenharmony_ci
901ffe3c632Sopenharmony_ci  {  // oneofFixed32
902ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
903ffe3c632Sopenharmony_ci    orig.oneofFixed32 = 7U;
904ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
905ffe3c632Sopenharmony_ci    NSData *data = [orig data];
906ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
907ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
908ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
909ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofFixed32, 7U);
910ffe3c632Sopenharmony_ci    [orig release];
911ffe3c632Sopenharmony_ci  }
912ffe3c632Sopenharmony_ci
913ffe3c632Sopenharmony_ci  {  // oneofFixed64
914ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
915ffe3c632Sopenharmony_ci    orig.oneofFixed64 = 8U;
916ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
917ffe3c632Sopenharmony_ci    NSData *data = [orig data];
918ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
919ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
920ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
921ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofFixed64, 8U);
922ffe3c632Sopenharmony_ci    [orig release];
923ffe3c632Sopenharmony_ci  }
924ffe3c632Sopenharmony_ci
925ffe3c632Sopenharmony_ci  {  // oneofSfixed32
926ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
927ffe3c632Sopenharmony_ci    orig.oneofSfixed32 = 9;
928ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
929ffe3c632Sopenharmony_ci    NSData *data = [orig data];
930ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
931ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
932ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
933ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSfixed32, 9);
934ffe3c632Sopenharmony_ci    [orig release];
935ffe3c632Sopenharmony_ci  }
936ffe3c632Sopenharmony_ci
937ffe3c632Sopenharmony_ci  {  // oneofSfixed64
938ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
939ffe3c632Sopenharmony_ci    orig.oneofSfixed64 = 10;
940ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
941ffe3c632Sopenharmony_ci    NSData *data = [orig data];
942ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
943ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
944ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
945ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofSfixed64, 10);
946ffe3c632Sopenharmony_ci    [orig release];
947ffe3c632Sopenharmony_ci  }
948ffe3c632Sopenharmony_ci
949ffe3c632Sopenharmony_ci  {  // oneofFloat
950ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
951ffe3c632Sopenharmony_ci    orig.oneofFloat = 11.0f;
952ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
953ffe3c632Sopenharmony_ci    NSData *data = [orig data];
954ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
955ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
956ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
957ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofFloat, 11.0f);
958ffe3c632Sopenharmony_ci    [orig release];
959ffe3c632Sopenharmony_ci  }
960ffe3c632Sopenharmony_ci
961ffe3c632Sopenharmony_ci  {  // oneofDouble
962ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
963ffe3c632Sopenharmony_ci    orig.oneofDouble = 12.0;
964ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
965ffe3c632Sopenharmony_ci    NSData *data = [orig data];
966ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
967ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
968ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
969ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofDouble, 12.0);
970ffe3c632Sopenharmony_ci    [orig release];
971ffe3c632Sopenharmony_ci  }
972ffe3c632Sopenharmony_ci
973ffe3c632Sopenharmony_ci  {  // oneofBool
974ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
975ffe3c632Sopenharmony_ci    orig.oneofBool = YES;
976ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBool);
977ffe3c632Sopenharmony_ci    NSData *data = [orig data];
978ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
979ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
980ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBool);
981ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofBool, YES);
982ffe3c632Sopenharmony_ci    [orig release];
983ffe3c632Sopenharmony_ci  }
984ffe3c632Sopenharmony_ci
985ffe3c632Sopenharmony_ci  {  // oneofString
986ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
987ffe3c632Sopenharmony_ci    orig.oneofString = @"foo";
988ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofString);
989ffe3c632Sopenharmony_ci    NSData *data = [orig data];
990ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
991ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
992ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofString);
993ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofString, @"foo");
994ffe3c632Sopenharmony_ci    [orig release];
995ffe3c632Sopenharmony_ci  }
996ffe3c632Sopenharmony_ci
997ffe3c632Sopenharmony_ci  {  // oneofBytes
998ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
999ffe3c632Sopenharmony_ci    orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
1000ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
1001ffe3c632Sopenharmony_ci    NSData *data = [orig data];
1002ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
1003ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
1004ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
1005ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
1006ffe3c632Sopenharmony_ci    [orig release];
1007ffe3c632Sopenharmony_ci  }
1008ffe3c632Sopenharmony_ci
1009ffe3c632Sopenharmony_ci  // Not "group" in proto3.
1010ffe3c632Sopenharmony_ci
1011ffe3c632Sopenharmony_ci  {  // oneofMessage
1012ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
1013ffe3c632Sopenharmony_ci    orig.oneofMessage = subMessage;
1014ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
1015ffe3c632Sopenharmony_ci    NSData *data = [orig data];
1016ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
1017ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
1018ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
1019ffe3c632Sopenharmony_ci    XCTAssertEqualObjects(msg.oneofMessage, subMessage);
1020ffe3c632Sopenharmony_ci    [orig release];
1021ffe3c632Sopenharmony_ci  }
1022ffe3c632Sopenharmony_ci
1023ffe3c632Sopenharmony_ci  {  // oneofEnum
1024ffe3c632Sopenharmony_ci    Message3 *orig = [[Message3 alloc] init];
1025ffe3c632Sopenharmony_ci    orig.oneofEnum = Message2_Enum_Bar;
1026ffe3c632Sopenharmony_ci    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
1027ffe3c632Sopenharmony_ci    NSData *data = [orig data];
1028ffe3c632Sopenharmony_ci    XCTAssertNotNil(data);
1029ffe3c632Sopenharmony_ci    Message3 *msg = [Message3 parseFromData:data error:NULL];
1030ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
1031ffe3c632Sopenharmony_ci    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
1032ffe3c632Sopenharmony_ci    [orig release];
1033ffe3c632Sopenharmony_ci  }
1034ffe3c632Sopenharmony_ci
1035ffe3c632Sopenharmony_ci  [subMessage release];
1036ffe3c632Sopenharmony_ci}
1037ffe3c632Sopenharmony_ci
1038ffe3c632Sopenharmony_ci// clang-format on
1039ffe3c632Sopenharmony_ci//%PDDM-EXPAND-END (2 expansions)
1040ffe3c632Sopenharmony_ci
1041ffe3c632Sopenharmony_ci- (void)testPackedUnpackedMessageParsing {
1042ffe3c632Sopenharmony_ci  // packed is optional, a repeated field should parse when packed or unpacked.
1043ffe3c632Sopenharmony_ci
1044ffe3c632Sopenharmony_ci  TestPackedTypes *packedOrig = [TestPackedTypes message];
1045ffe3c632Sopenharmony_ci  TestUnpackedTypes *unpackedOrig = [TestUnpackedTypes message];
1046ffe3c632Sopenharmony_ci  [self setPackedFields:packedOrig repeatedCount:4];
1047ffe3c632Sopenharmony_ci  [self setUnpackedFields:unpackedOrig repeatedCount:4];
1048ffe3c632Sopenharmony_ci
1049ffe3c632Sopenharmony_ci  NSData *packedData = [packedOrig data];
1050ffe3c632Sopenharmony_ci  NSData *unpackedData = [unpackedOrig data];
1051ffe3c632Sopenharmony_ci  XCTAssertNotNil(packedData);
1052ffe3c632Sopenharmony_ci  XCTAssertNotNil(unpackedData);
1053ffe3c632Sopenharmony_ci  XCTAssertNotEqualObjects(packedData, unpackedData,
1054ffe3c632Sopenharmony_ci                           @"Data should differ (packed vs unpacked) use");
1055ffe3c632Sopenharmony_ci
1056ffe3c632Sopenharmony_ci  NSError *error = nil;
1057ffe3c632Sopenharmony_ci  TestPackedTypes *packedParse =
1058ffe3c632Sopenharmony_ci      [TestPackedTypes parseFromData:unpackedData error:&error];
1059ffe3c632Sopenharmony_ci  XCTAssertNotNil(packedParse);
1060ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1061ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(packedParse, packedOrig);
1062ffe3c632Sopenharmony_ci
1063ffe3c632Sopenharmony_ci  error = nil;
1064ffe3c632Sopenharmony_ci  TestUnpackedTypes *unpackedParsed =
1065ffe3c632Sopenharmony_ci      [TestUnpackedTypes parseFromData:packedData error:&error];
1066ffe3c632Sopenharmony_ci  XCTAssertNotNil(unpackedParsed);
1067ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1068ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(unpackedParsed, unpackedOrig);
1069ffe3c632Sopenharmony_ci}
1070ffe3c632Sopenharmony_ci
1071ffe3c632Sopenharmony_ci- (void)testPackedUnpackedExtensionParsing {
1072ffe3c632Sopenharmony_ci  // packed is optional, a repeated extension should parse when packed or
1073ffe3c632Sopenharmony_ci  // unpacked.
1074ffe3c632Sopenharmony_ci
1075ffe3c632Sopenharmony_ci  TestPackedExtensions *packedOrig = [TestPackedExtensions message];
1076ffe3c632Sopenharmony_ci  TestUnpackedExtensions *unpackedOrig = [TestUnpackedExtensions message];
1077ffe3c632Sopenharmony_ci  [self setPackedExtensions:packedOrig repeatedCount:kGPBDefaultRepeatCount];
1078ffe3c632Sopenharmony_ci  [self setUnpackedExtensions:unpackedOrig repeatedCount:kGPBDefaultRepeatCount];
1079ffe3c632Sopenharmony_ci
1080ffe3c632Sopenharmony_ci  NSData *packedData = [packedOrig data];
1081ffe3c632Sopenharmony_ci  NSData *unpackedData = [unpackedOrig data];
1082ffe3c632Sopenharmony_ci  XCTAssertNotNil(packedData);
1083ffe3c632Sopenharmony_ci  XCTAssertNotNil(unpackedData);
1084ffe3c632Sopenharmony_ci  XCTAssertNotEqualObjects(packedData, unpackedData,
1085ffe3c632Sopenharmony_ci                           @"Data should differ (packed vs unpacked) use");
1086ffe3c632Sopenharmony_ci
1087ffe3c632Sopenharmony_ci  NSError *error = nil;
1088ffe3c632Sopenharmony_ci  TestPackedExtensions *packedParse =
1089ffe3c632Sopenharmony_ci      [TestPackedExtensions parseFromData:unpackedData
1090ffe3c632Sopenharmony_ci                        extensionRegistry:[UnittestRoot extensionRegistry]
1091ffe3c632Sopenharmony_ci                                    error:&error];
1092ffe3c632Sopenharmony_ci  XCTAssertNotNil(packedParse);
1093ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1094ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(packedParse, packedOrig);
1095ffe3c632Sopenharmony_ci
1096ffe3c632Sopenharmony_ci  error = nil;
1097ffe3c632Sopenharmony_ci  TestUnpackedExtensions *unpackedParsed =
1098ffe3c632Sopenharmony_ci      [TestUnpackedExtensions parseFromData:packedData
1099ffe3c632Sopenharmony_ci                          extensionRegistry:[UnittestRoot extensionRegistry]
1100ffe3c632Sopenharmony_ci                                      error:&error];
1101ffe3c632Sopenharmony_ci  XCTAssertNotNil(unpackedParsed);
1102ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1103ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(unpackedParsed, unpackedOrig);
1104ffe3c632Sopenharmony_ci}
1105ffe3c632Sopenharmony_ci
1106ffe3c632Sopenharmony_ci- (void)testPackedExtensionVsFieldParsing {
1107ffe3c632Sopenharmony_ci  // Extensions and fields end up on the wire the same way, so they can parse
1108ffe3c632Sopenharmony_ci  // each other.
1109ffe3c632Sopenharmony_ci
1110ffe3c632Sopenharmony_ci  TestPackedTypes *fieldsOrig = [TestPackedTypes message];
1111ffe3c632Sopenharmony_ci  TestPackedExtensions *extsOrig = [TestPackedExtensions message];
1112ffe3c632Sopenharmony_ci  [self setPackedFields:fieldsOrig repeatedCount:kGPBDefaultRepeatCount];
1113ffe3c632Sopenharmony_ci  [self setPackedExtensions:extsOrig repeatedCount:kGPBDefaultRepeatCount];
1114ffe3c632Sopenharmony_ci
1115ffe3c632Sopenharmony_ci  NSData *fieldsData = [fieldsOrig data];
1116ffe3c632Sopenharmony_ci  NSData *extsData = [extsOrig data];
1117ffe3c632Sopenharmony_ci  XCTAssertNotNil(fieldsData);
1118ffe3c632Sopenharmony_ci  XCTAssertNotNil(extsData);
1119ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(fieldsData, extsData);
1120ffe3c632Sopenharmony_ci
1121ffe3c632Sopenharmony_ci  NSError *error = nil;
1122ffe3c632Sopenharmony_ci  TestPackedTypes *fieldsParse =
1123ffe3c632Sopenharmony_ci      [TestPackedTypes parseFromData:extsData error:&error];
1124ffe3c632Sopenharmony_ci  XCTAssertNotNil(fieldsParse);
1125ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1126ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(fieldsParse, fieldsOrig);
1127ffe3c632Sopenharmony_ci
1128ffe3c632Sopenharmony_ci  error = nil;
1129ffe3c632Sopenharmony_ci  TestPackedExtensions *extsParse =
1130ffe3c632Sopenharmony_ci      [TestPackedExtensions parseFromData:fieldsData
1131ffe3c632Sopenharmony_ci                        extensionRegistry:[UnittestRoot extensionRegistry]
1132ffe3c632Sopenharmony_ci                                    error:&error];
1133ffe3c632Sopenharmony_ci  XCTAssertNotNil(extsParse);
1134ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1135ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(extsParse, extsOrig);
1136ffe3c632Sopenharmony_ci}
1137ffe3c632Sopenharmony_ci
1138ffe3c632Sopenharmony_ci- (void)testUnpackedExtensionVsFieldParsing {
1139ffe3c632Sopenharmony_ci  // Extensions and fields end up on the wire the same way, so they can parse
1140ffe3c632Sopenharmony_ci  // each other.
1141ffe3c632Sopenharmony_ci
1142ffe3c632Sopenharmony_ci  TestUnpackedTypes *fieldsOrig = [TestUnpackedTypes message];
1143ffe3c632Sopenharmony_ci  TestUnpackedExtensions *extsOrig = [TestUnpackedExtensions message];
1144ffe3c632Sopenharmony_ci  [self setUnpackedFields:fieldsOrig repeatedCount:3];
1145ffe3c632Sopenharmony_ci  [self setUnpackedExtensions:extsOrig repeatedCount:3];
1146ffe3c632Sopenharmony_ci
1147ffe3c632Sopenharmony_ci  NSData *fieldsData = [fieldsOrig data];
1148ffe3c632Sopenharmony_ci  NSData *extsData = [extsOrig data];
1149ffe3c632Sopenharmony_ci  XCTAssertNotNil(fieldsData);
1150ffe3c632Sopenharmony_ci  XCTAssertNotNil(extsData);
1151ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(fieldsData, extsData);
1152ffe3c632Sopenharmony_ci
1153ffe3c632Sopenharmony_ci  TestUnpackedTypes *fieldsParse =
1154ffe3c632Sopenharmony_ci      [TestUnpackedTypes parseFromData:extsData error:NULL];
1155ffe3c632Sopenharmony_ci  XCTAssertNotNil(fieldsParse);
1156ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(fieldsParse, fieldsOrig);
1157ffe3c632Sopenharmony_ci
1158ffe3c632Sopenharmony_ci  TestUnpackedExtensions *extsParse =
1159ffe3c632Sopenharmony_ci      [TestUnpackedExtensions parseFromData:fieldsData
1160ffe3c632Sopenharmony_ci                          extensionRegistry:[UnittestRoot extensionRegistry]
1161ffe3c632Sopenharmony_ci                                      error:NULL];
1162ffe3c632Sopenharmony_ci  XCTAssertNotNil(extsParse);
1163ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(extsParse, extsOrig);
1164ffe3c632Sopenharmony_ci}
1165ffe3c632Sopenharmony_ci
1166ffe3c632Sopenharmony_ci- (void)testErrorSubsectionInvalidLimit {
1167ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr(
1168ffe3c632Sopenharmony_ci      "\x0A\x08\x0A\x07\x12\x04\x72\x02\x4B\x50\x12\x04\x72\x02\x4B\x50");
1169ffe3c632Sopenharmony_ci  NSError *error = nil;
1170ffe3c632Sopenharmony_ci  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
1171ffe3c632Sopenharmony_ci                                                        error:&error];
1172ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1173ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1174ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1175ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSubsectionLimit);
1176ffe3c632Sopenharmony_ci}
1177ffe3c632Sopenharmony_ci
1178ffe3c632Sopenharmony_ci- (void)testErrorSubsectionLimitReached {
1179ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x06\x12\x03\x72\x02\x4B\x50");
1180ffe3c632Sopenharmony_ci  NSError *error = nil;
1181ffe3c632Sopenharmony_ci  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
1182ffe3c632Sopenharmony_ci                                                        error:&error];
1183ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1184ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1185ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1186ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorSubsectionLimitReached);
1187ffe3c632Sopenharmony_ci}
1188ffe3c632Sopenharmony_ci
1189ffe3c632Sopenharmony_ci- (void)testErrorInvalidVarint {
1190ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x72\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
1191ffe3c632Sopenharmony_ci  NSError *error = nil;
1192ffe3c632Sopenharmony_ci  TestAllTypes *msg = [TestAllTypes parseFromData:data error:&error];
1193ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1194ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1195ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1196ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidVarInt);
1197ffe3c632Sopenharmony_ci}
1198ffe3c632Sopenharmony_ci
1199ffe3c632Sopenharmony_ci- (void)testErrorInvalidUTF8 {
1200ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x72\x04\xF4\xFF\xFF\xFF");
1201ffe3c632Sopenharmony_ci  NSError *error = nil;
1202ffe3c632Sopenharmony_ci  TestAllTypes *msg = [TestAllTypes parseFromData:data error:&error];
1203ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1204ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1205ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1206ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidUTF8);
1207ffe3c632Sopenharmony_ci}
1208ffe3c632Sopenharmony_ci
1209ffe3c632Sopenharmony_ci- (void)testErrorInvalidSize {
1210ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x72\x03\x4B\x50");
1211ffe3c632Sopenharmony_ci  NSError *error = nil;
1212ffe3c632Sopenharmony_ci  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
1213ffe3c632Sopenharmony_ci                                                        error:&error];
1214ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1215ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1216ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1217ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSize);
1218ffe3c632Sopenharmony_ci}
1219ffe3c632Sopenharmony_ci
1220ffe3c632Sopenharmony_ci- (void)testErrorInvalidTag {
1221ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0F");
1222ffe3c632Sopenharmony_ci  NSError *error = nil;
1223ffe3c632Sopenharmony_ci  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
1224ffe3c632Sopenharmony_ci                                                        error:&error];
1225ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1226ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1227ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1228ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag);
1229ffe3c632Sopenharmony_ci}
1230ffe3c632Sopenharmony_ci
1231ffe3c632Sopenharmony_ci- (void)testZeroFieldNum {
1232ffe3c632Sopenharmony_ci  // These are ConformanceTestSuite::TestIllegalTags.
1233ffe3c632Sopenharmony_ci
1234ffe3c632Sopenharmony_ci  const char *tests[] = {
1235ffe3c632Sopenharmony_ci    "\1DEADBEEF",
1236ffe3c632Sopenharmony_ci    "\2\1\1",
1237ffe3c632Sopenharmony_ci    "\3\4",
1238ffe3c632Sopenharmony_ci    "\5DEAD"
1239ffe3c632Sopenharmony_ci  };
1240ffe3c632Sopenharmony_ci
1241ffe3c632Sopenharmony_ci  for (size_t i = 0; i < GPBARRAYSIZE(tests); ++i) {
1242ffe3c632Sopenharmony_ci    NSData *data = DataFromCStr(tests[i]);
1243ffe3c632Sopenharmony_ci
1244ffe3c632Sopenharmony_ci    {
1245ffe3c632Sopenharmony_ci      // Message from proto2 syntax file
1246ffe3c632Sopenharmony_ci      NSError *error = nil;
1247ffe3c632Sopenharmony_ci      Message2 *msg = [Message2 parseFromData:data error:&error];
1248ffe3c632Sopenharmony_ci      XCTAssertNil(msg, @"i = %zd", i);
1249ffe3c632Sopenharmony_ci      XCTAssertNotNil(error, @"i = %zd", i);
1250ffe3c632Sopenharmony_ci      XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain, @"i = %zd", i);
1251ffe3c632Sopenharmony_ci      XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag, @"i = %zd", i);
1252ffe3c632Sopenharmony_ci    }
1253ffe3c632Sopenharmony_ci
1254ffe3c632Sopenharmony_ci    {
1255ffe3c632Sopenharmony_ci      // Message from proto3 syntax file
1256ffe3c632Sopenharmony_ci      NSError *error = nil;
1257ffe3c632Sopenharmony_ci      Message3 *msg = [Message3 parseFromData:data error:&error];
1258ffe3c632Sopenharmony_ci      XCTAssertNil(msg, @"i = %zd", i);
1259ffe3c632Sopenharmony_ci      XCTAssertNotNil(error, @"i = %zd", i);
1260ffe3c632Sopenharmony_ci      XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain, @"i = %zd", i);
1261ffe3c632Sopenharmony_ci      XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag, @"i = %zd", i);
1262ffe3c632Sopenharmony_ci    }
1263ffe3c632Sopenharmony_ci  }
1264ffe3c632Sopenharmony_ci}
1265ffe3c632Sopenharmony_ci
1266ffe3c632Sopenharmony_ci- (void)testErrorRecursionDepthReached {
1267ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr(
1268ffe3c632Sopenharmony_ci      "\x0A\xF2\x01\x0A\xEF\x01\x0A\xEC\x01\x0A\xE9\x01\x0A\xE6\x01"
1269ffe3c632Sopenharmony_ci      "\x0A\xE3\x01\x0A\xE0\x01\x0A\xDD\x01\x0A\xDA\x01\x0A\xD7\x01"
1270ffe3c632Sopenharmony_ci      "\x0A\xD4\x01\x0A\xD1\x01\x0A\xCE\x01\x0A\xCB\x01\x0A\xC8\x01"
1271ffe3c632Sopenharmony_ci      "\x0A\xC5\x01\x0A\xC2\x01\x0A\xBF\x01\x0A\xBC\x01\x0A\xB9\x01"
1272ffe3c632Sopenharmony_ci      "\x0A\xB6\x01\x0A\xB3\x01\x0A\xB0\x01\x0A\xAD\x01\x0A\xAA\x01"
1273ffe3c632Sopenharmony_ci      "\x0A\xA7\x01\x0A\xA4\x01\x0A\xA1\x01\x0A\x9E\x01\x0A\x9B\x01"
1274ffe3c632Sopenharmony_ci      "\x0A\x98\x01\x0A\x95\x01\x0A\x92\x01\x0A\x8F\x01\x0A\x8C\x01"
1275ffe3c632Sopenharmony_ci      "\x0A\x89\x01\x0A\x86\x01\x0A\x83\x01\x0A\x80\x01\x0A\x7E"
1276ffe3c632Sopenharmony_ci      "\x0A\x7C\x0A\x7A\x0A\x78\x0A\x76\x0A\x74\x0A\x72\x0A\x70"
1277ffe3c632Sopenharmony_ci      "\x0A\x6E\x0A\x6C\x0A\x6A\x0A\x68\x0A\x66\x0A\x64\x0A\x62"
1278ffe3c632Sopenharmony_ci      "\x0A\x60\x0A\x5E\x0A\x5C\x0A\x5A\x0A\x58\x0A\x56\x0A\x54"
1279ffe3c632Sopenharmony_ci      "\x0A\x52\x0A\x50\x0A\x4E\x0A\x4C\x0A\x4A\x0A\x48\x0A\x46"
1280ffe3c632Sopenharmony_ci      "\x0A\x44\x0A\x42\x0A\x40\x0A\x3E\x0A\x3C\x0A\x3A\x0A\x38"
1281ffe3c632Sopenharmony_ci      "\x0A\x36\x0A\x34\x0A\x32\x0A\x30\x0A\x2E\x0A\x2C\x0A\x2A"
1282ffe3c632Sopenharmony_ci      "\x0A\x28\x0A\x26\x0A\x24\x0A\x22\x0A\x20\x0A\x1E\x0A\x1C"
1283ffe3c632Sopenharmony_ci      "\x0A\x1A\x0A\x18\x0A\x16\x0A\x14\x0A\x12\x0A\x10\x0A\x0E"
1284ffe3c632Sopenharmony_ci      "\x0A\x0C\x0A\x0A\x0A\x08\x0A\x06\x12\x04\x72\x02\x4B\x50");
1285ffe3c632Sopenharmony_ci  NSError *error = nil;
1286ffe3c632Sopenharmony_ci  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
1287ffe3c632Sopenharmony_ci                                                        error:&error];
1288ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1289ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1290ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1291ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorRecursionDepthExceeded);
1292ffe3c632Sopenharmony_ci}
1293ffe3c632Sopenharmony_ci
1294ffe3c632Sopenharmony_ci- (void)testParseDelimitedDataWithNegativeSize {
1295ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\xFF\xFF\xFF\xFF\x0F");
1296ffe3c632Sopenharmony_ci  GPBCodedInputStream *input = [GPBCodedInputStream streamWithData:data];
1297ffe3c632Sopenharmony_ci  NSError *error;
1298ffe3c632Sopenharmony_ci  [GPBMessage parseDelimitedFromCodedInputStream:input
1299ffe3c632Sopenharmony_ci                               extensionRegistry:nil
1300ffe3c632Sopenharmony_ci                                           error:&error];
1301ffe3c632Sopenharmony_ci  XCTAssertNil(error);
1302ffe3c632Sopenharmony_ci}
1303ffe3c632Sopenharmony_ci
1304ffe3c632Sopenharmony_ci#ifdef DEBUG
1305ffe3c632Sopenharmony_ci- (void)testErrorMissingRequiredField {
1306ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("");
1307ffe3c632Sopenharmony_ci  NSError *error = nil;
1308ffe3c632Sopenharmony_ci  TestRequired *msg = [TestRequired parseFromData:data error:&error];
1309ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1310ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1311ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain);
1312ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBMessageErrorCodeMissingRequiredField);
1313ffe3c632Sopenharmony_ci}
1314ffe3c632Sopenharmony_ci#endif
1315ffe3c632Sopenharmony_ci
1316ffe3c632Sopenharmony_ci#pragma mark - Subset from from map_tests.cc
1317ffe3c632Sopenharmony_ci
1318ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, StandardWireFormat)
1319ffe3c632Sopenharmony_ci- (void)testMap_StandardWireFormat {
1320ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x04\x08\x01\x10\x01");
1321ffe3c632Sopenharmony_ci
1322ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1323ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1324ffe3c632Sopenharmony_ci  int32_t val = 666;
1325ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
1326ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 1);
1327ffe3c632Sopenharmony_ci
1328ffe3c632Sopenharmony_ci  [msg release];
1329ffe3c632Sopenharmony_ci}
1330ffe3c632Sopenharmony_ci
1331ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, UnorderedWireFormat)
1332ffe3c632Sopenharmony_ci- (void)testMap_UnorderedWireFormat {
1333ffe3c632Sopenharmony_ci  // put value before key in wire format
1334ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x04\x10\x01\x08\x02");
1335ffe3c632Sopenharmony_ci
1336ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1337ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1338ffe3c632Sopenharmony_ci  int32_t val = 666;
1339ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
1340ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 1);
1341ffe3c632Sopenharmony_ci
1342ffe3c632Sopenharmony_ci  [msg release];
1343ffe3c632Sopenharmony_ci}
1344ffe3c632Sopenharmony_ci
1345ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat)
1346ffe3c632Sopenharmony_ci- (void)testMap_DuplicatedKeyWireFormat {
1347ffe3c632Sopenharmony_ci  // Two key fields in wire format
1348ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x06\x08\x01\x08\x02\x10\x01");
1349ffe3c632Sopenharmony_ci
1350ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1351ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1352ffe3c632Sopenharmony_ci  int32_t val = 666;
1353ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
1354ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 1);
1355ffe3c632Sopenharmony_ci
1356ffe3c632Sopenharmony_ci  [msg release];
1357ffe3c632Sopenharmony_ci}
1358ffe3c632Sopenharmony_ci
1359ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat)
1360ffe3c632Sopenharmony_ci- (void)testMap_DuplicatedValueWireFormat {
1361ffe3c632Sopenharmony_ci  // Two value fields in wire format
1362ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x06\x08\x01\x10\x01\x10\x02");
1363ffe3c632Sopenharmony_ci
1364ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1365ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1366ffe3c632Sopenharmony_ci  int32_t val = 666;
1367ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
1368ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 2);
1369ffe3c632Sopenharmony_ci
1370ffe3c632Sopenharmony_ci  [msg release];
1371ffe3c632Sopenharmony_ci}
1372ffe3c632Sopenharmony_ci
1373ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, MissedKeyWireFormat)
1374ffe3c632Sopenharmony_ci- (void)testMap_MissedKeyWireFormat {
1375ffe3c632Sopenharmony_ci  // No key field in wire format
1376ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x02\x10\x01");
1377ffe3c632Sopenharmony_ci
1378ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1379ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1380ffe3c632Sopenharmony_ci  int32_t val = 666;
1381ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:0]);
1382ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 1);
1383ffe3c632Sopenharmony_ci
1384ffe3c632Sopenharmony_ci  [msg release];
1385ffe3c632Sopenharmony_ci}
1386ffe3c632Sopenharmony_ci
1387ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, MissedValueWireFormat)
1388ffe3c632Sopenharmony_ci- (void)testMap_MissedValueWireFormat {
1389ffe3c632Sopenharmony_ci  // No value field in wire format
1390ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x02\x08\x01");
1391ffe3c632Sopenharmony_ci
1392ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1393ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1394ffe3c632Sopenharmony_ci  int32_t val = 666;
1395ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
1396ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 0);
1397ffe3c632Sopenharmony_ci
1398ffe3c632Sopenharmony_ci  [msg release];
1399ffe3c632Sopenharmony_ci}
1400ffe3c632Sopenharmony_ci
1401ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, UnknownFieldWireFormat)
1402ffe3c632Sopenharmony_ci- (void)testMap_UnknownFieldWireFormat {
1403ffe3c632Sopenharmony_ci  // Unknown field in wire format
1404ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x06\x08\x02\x10\x03\x18\x01");
1405ffe3c632Sopenharmony_ci
1406ffe3c632Sopenharmony_ci  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1407ffe3c632Sopenharmony_ci  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1408ffe3c632Sopenharmony_ci  int32_t val = 666;
1409ffe3c632Sopenharmony_ci  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
1410ffe3c632Sopenharmony_ci  XCTAssertEqual(val, 3);
1411ffe3c632Sopenharmony_ci
1412ffe3c632Sopenharmony_ci  [msg release];
1413ffe3c632Sopenharmony_ci}
1414ffe3c632Sopenharmony_ci
1415ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, CorruptedWireFormat)
1416ffe3c632Sopenharmony_ci- (void)testMap_CorruptedWireFormat {
1417ffe3c632Sopenharmony_ci  // corrupted data in wire format
1418ffe3c632Sopenharmony_ci  NSData *data = DataFromCStr("\x0A\x06\x08\x02\x11\x03");
1419ffe3c632Sopenharmony_ci
1420ffe3c632Sopenharmony_ci  NSError *error = nil;
1421ffe3c632Sopenharmony_ci  TestMap *msg = [TestMap parseFromData:data error:&error];
1422ffe3c632Sopenharmony_ci  XCTAssertNil(msg);
1423ffe3c632Sopenharmony_ci  XCTAssertNotNil(error);
1424ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1425ffe3c632Sopenharmony_ci  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSubsectionLimit);
1426ffe3c632Sopenharmony_ci}
1427ffe3c632Sopenharmony_ci
1428ffe3c632Sopenharmony_ci// TEST(GeneratedMapFieldTest, Proto2UnknownEnum)
1429ffe3c632Sopenharmony_ci- (void)testMap_Proto2UnknownEnum {
1430ffe3c632Sopenharmony_ci  TestEnumMapPlusExtra *orig = [[TestEnumMapPlusExtra alloc] init];
1431ffe3c632Sopenharmony_ci
1432ffe3c632Sopenharmony_ci  orig.knownMapField = [[[GPBInt32EnumDictionary alloc]
1433ffe3c632Sopenharmony_ci      initWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue] autorelease];
1434ffe3c632Sopenharmony_ci  orig.unknownMapField = [[[GPBInt32EnumDictionary alloc]
1435ffe3c632Sopenharmony_ci      initWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue] autorelease];
1436ffe3c632Sopenharmony_ci  [orig.knownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumFoo
1437ffe3c632Sopenharmony_ci                       forKey:0];
1438ffe3c632Sopenharmony_ci  [orig.unknownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumExtra
1439ffe3c632Sopenharmony_ci                         forKey:0];
1440ffe3c632Sopenharmony_ci
1441ffe3c632Sopenharmony_ci  NSData *data = [orig data];
1442ffe3c632Sopenharmony_ci  XCTAssertNotNil(data);
1443ffe3c632Sopenharmony_ci  TestEnumMap *msg1 = [TestEnumMap parseFromData:data error:NULL];
1444ffe3c632Sopenharmony_ci  XCTAssertEqual(msg1.knownMapField.count, 1U);
1445ffe3c632Sopenharmony_ci  int32_t val = -1;
1446ffe3c632Sopenharmony_ci  XCTAssertTrue([msg1.knownMapField getEnum:&val forKey:0]);
1447ffe3c632Sopenharmony_ci  XCTAssertEqual(val, Proto2MapEnum_Proto2MapEnumFoo);
1448ffe3c632Sopenharmony_ci  XCTAssertEqual(msg1.unknownFields.countOfFields, 1U);
1449ffe3c632Sopenharmony_ci
1450ffe3c632Sopenharmony_ci  data = [msg1 data];
1451ffe3c632Sopenharmony_ci  TestEnumMapPlusExtra *msg2 =
1452ffe3c632Sopenharmony_ci      [TestEnumMapPlusExtra parseFromData:data error:NULL];
1453ffe3c632Sopenharmony_ci  val = -1;
1454ffe3c632Sopenharmony_ci  XCTAssertEqual(msg2.knownMapField.count, 1U);
1455ffe3c632Sopenharmony_ci  XCTAssertTrue([msg2.knownMapField getEnum:&val forKey:0]);
1456ffe3c632Sopenharmony_ci  XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumFoo);
1457ffe3c632Sopenharmony_ci  val = -1;
1458ffe3c632Sopenharmony_ci  XCTAssertEqual(msg2.unknownMapField.count, 1U);
1459ffe3c632Sopenharmony_ci  XCTAssertTrue([msg2.unknownMapField getEnum:&val forKey:0]);
1460ffe3c632Sopenharmony_ci  XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumExtra);
1461ffe3c632Sopenharmony_ci  XCTAssertEqual(msg2.unknownFields.countOfFields, 0U);
1462ffe3c632Sopenharmony_ci
1463ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(orig, msg2);
1464ffe3c632Sopenharmony_ci
1465ffe3c632Sopenharmony_ci  [orig release];
1466ffe3c632Sopenharmony_ci}
1467ffe3c632Sopenharmony_ci
1468ffe3c632Sopenharmony_ci#pragma mark - Map Round Tripping
1469ffe3c632Sopenharmony_ci
1470ffe3c632Sopenharmony_ci- (void)testProto2MapRoundTripping {
1471ffe3c632Sopenharmony_ci  Message2 *msg = [[Message2 alloc] init];
1472ffe3c632Sopenharmony_ci
1473ffe3c632Sopenharmony_ci  // Key/Value data should result in different byte lengths on wire to ensure
1474ffe3c632Sopenharmony_ci  // everything is right.
1475ffe3c632Sopenharmony_ci  [msg.mapInt32Int32 setInt32:1000 forKey:200];
1476ffe3c632Sopenharmony_ci  [msg.mapInt32Int32 setInt32:101 forKey:2001];
1477ffe3c632Sopenharmony_ci  [msg.mapInt64Int64 setInt64:1002 forKey:202];
1478ffe3c632Sopenharmony_ci  [msg.mapInt64Int64 setInt64:103 forKey:2003];
1479ffe3c632Sopenharmony_ci  [msg.mapInt64Int64 setInt64:4294967296 forKey:4294967297];
1480ffe3c632Sopenharmony_ci  [msg.mapUint32Uint32 setUInt32:1004 forKey:204];
1481ffe3c632Sopenharmony_ci  [msg.mapUint32Uint32 setUInt32:105 forKey:2005];
1482ffe3c632Sopenharmony_ci  [msg.mapUint64Uint64 setUInt64:1006 forKey:206];
1483ffe3c632Sopenharmony_ci  [msg.mapUint64Uint64 setUInt64:107 forKey:2007];
1484ffe3c632Sopenharmony_ci  [msg.mapUint64Uint64 setUInt64:4294967298 forKey:4294967299];
1485ffe3c632Sopenharmony_ci  [msg.mapSint32Sint32 setInt32:1008 forKey:208];
1486ffe3c632Sopenharmony_ci  [msg.mapSint32Sint32 setInt32:109 forKey:2009];
1487ffe3c632Sopenharmony_ci  [msg.mapSint64Sint64 setInt64:1010 forKey:210];
1488ffe3c632Sopenharmony_ci  [msg.mapSint64Sint64 setInt64:111 forKey:2011];
1489ffe3c632Sopenharmony_ci  [msg.mapSint64Sint64 setInt64:4294967300 forKey:4294967301];
1490ffe3c632Sopenharmony_ci  [msg.mapFixed32Fixed32 setUInt32:1012 forKey:212];
1491ffe3c632Sopenharmony_ci  [msg.mapFixed32Fixed32 setUInt32:113 forKey:2013];
1492ffe3c632Sopenharmony_ci  [msg.mapFixed64Fixed64 setUInt64:1014 forKey:214];
1493ffe3c632Sopenharmony_ci  [msg.mapFixed64Fixed64 setUInt64:115 forKey:2015];
1494ffe3c632Sopenharmony_ci  [msg.mapFixed64Fixed64 setUInt64:4294967302 forKey:4294967303];
1495ffe3c632Sopenharmony_ci  [msg.mapSfixed32Sfixed32 setInt32:1016 forKey:216];
1496ffe3c632Sopenharmony_ci  [msg.mapSfixed32Sfixed32 setInt32:117 forKey:2017];
1497ffe3c632Sopenharmony_ci  [msg.mapSfixed64Sfixed64 setInt64:1018 forKey:218];
1498ffe3c632Sopenharmony_ci  [msg.mapSfixed64Sfixed64 setInt64:119 forKey:2019];
1499ffe3c632Sopenharmony_ci  [msg.mapSfixed64Sfixed64 setInt64:4294967304 forKey:4294967305];
1500ffe3c632Sopenharmony_ci  [msg.mapInt32Float setFloat:1020.f forKey:220];
1501ffe3c632Sopenharmony_ci  [msg.mapInt32Float setFloat:121.f forKey:2021];
1502ffe3c632Sopenharmony_ci  [msg.mapInt32Double setDouble:1022. forKey:222];
1503ffe3c632Sopenharmony_ci  [msg.mapInt32Double setDouble:123. forKey:2023];
1504ffe3c632Sopenharmony_ci  [msg.mapBoolBool setBool:false forKey:true];
1505ffe3c632Sopenharmony_ci  [msg.mapBoolBool setBool:true forKey:false];
1506ffe3c632Sopenharmony_ci  msg.mapStringString[@"224"] = @"1024";
1507ffe3c632Sopenharmony_ci  msg.mapStringString[@"2025"] = @"125";
1508ffe3c632Sopenharmony_ci  msg.mapStringBytes[@"226"] = DataFromCStr("1026");
1509ffe3c632Sopenharmony_ci  msg.mapStringBytes[@"2027"] = DataFromCStr("127");
1510ffe3c632Sopenharmony_ci  Message2 *val1 = [[Message2 alloc] init];
1511ffe3c632Sopenharmony_ci  val1.optionalInt32 = 1028;
1512ffe3c632Sopenharmony_ci  Message2 *val2 = [[Message2 alloc] init];
1513ffe3c632Sopenharmony_ci  val2.optionalInt32 = 129;
1514ffe3c632Sopenharmony_ci  [msg.mapStringMessage setObject:val1 forKey:@"228"];
1515ffe3c632Sopenharmony_ci  [msg.mapStringMessage setObject:val2 forKey:@"2029"];
1516ffe3c632Sopenharmony_ci  [msg.mapInt32Bytes setObject:DataFromCStr("1030 bytes") forKey:230];
1517ffe3c632Sopenharmony_ci  [msg.mapInt32Bytes setObject:DataFromCStr("131") forKey:2031];
1518ffe3c632Sopenharmony_ci  [msg.mapInt32Enum setEnum:Message2_Enum_Bar forKey:232];
1519ffe3c632Sopenharmony_ci  [msg.mapInt32Enum setEnum:Message2_Enum_Baz forKey:2033];
1520ffe3c632Sopenharmony_ci  Message2 *val3 = [[Message2 alloc] init];
1521ffe3c632Sopenharmony_ci  val3.optionalInt32 = 1034;
1522ffe3c632Sopenharmony_ci  Message2 *val4 = [[Message2 alloc] init];
1523ffe3c632Sopenharmony_ci  val4.optionalInt32 = 135;
1524ffe3c632Sopenharmony_ci  [msg.mapInt32Message setObject:val3 forKey:234];
1525ffe3c632Sopenharmony_ci  [msg.mapInt32Message setObject:val4 forKey:2035];
1526ffe3c632Sopenharmony_ci
1527ffe3c632Sopenharmony_ci  NSData *data = [msg data];
1528ffe3c632Sopenharmony_ci  XCTAssertNotNil(data);
1529ffe3c632Sopenharmony_ci  Message2 *msg2 = [[Message2 alloc] initWithData:data error:NULL];
1530ffe3c632Sopenharmony_ci
1531ffe3c632Sopenharmony_ci  XCTAssertNotEqual(msg2, msg);  // Pointer comparison
1532ffe3c632Sopenharmony_ci  XCTAssertEqualObjects(msg2, msg);
1533ffe3c632Sopenharmony_ci
1534ffe3c632Sopenharmony_ci  [val4 release];
1535ffe3c632Sopenharmony_ci  [val3 release];
1536ffe3c632Sopenharmony_ci  [val2 release];
1537ffe3c632Sopenharmony_ci  [val1 release];
1538ffe3c632Sopenharmony_ci  [msg2 release];
1539ffe3c632Sopenharmony_ci  [msg release];
1540ffe3c632Sopenharmony_ci}
1541ffe3c632Sopenharmony_ci
1542ffe3c632Sopenharmony_ci@end
1543