Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers.Test/TestUtil.cs @ 4032

Last change on this file since 4032 was 3857, checked in by abeham, 15 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 96.9 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections.Generic;
37using System.Globalization;
38using System.IO;
39using System.Text;
40using System.Threading;
41using Google.ProtocolBuffers.TestProtos;
42using NUnit.Framework;
43
44namespace Google.ProtocolBuffers {
45  internal static class TestUtil {
46
47    private static string testDataDirectory;
48    private static ByteString goldenMessage = null;
49
50    internal static string TestDataDirectory {
51      get {
52        if (testDataDirectory != null) {
53          return testDataDirectory;
54        }
55
56        DirectoryInfo ancestor = new DirectoryInfo(".");
57        // Search each parent directory looking for "testdata".
58        while (ancestor != null) {
59          string candidate = Path.Combine(ancestor.FullName, "testdata");
60          if (Directory.Exists(candidate)) {
61            testDataDirectory = candidate;
62            return candidate;
63          }
64          ancestor = ancestor.Parent;
65        }
66        // TODO(jonskeet): Come up with a better exception to throw
67        throw new Exception("Unable to find directory containing test files");
68      }
69    }
70
71    internal static ByteString GoldenMessage {
72      get {
73        if (goldenMessage == null) {
74          goldenMessage = ReadBytesFromFile("golden_message");
75        }
76        return goldenMessage;
77      }
78    }
79
80    /// <summary>
81    /// Creates an unmodifiable ExtensionRegistry containing all the extensions
82    /// of TestAllExtensions.
83    /// </summary>
84    /// <returns></returns>
85    internal static ExtensionRegistry CreateExtensionRegistry() {
86      ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
87      RegisterAllExtensions(registry);
88      return registry.AsReadOnly();
89    }
90
91    /// <summary>
92    /// Registers all of the extensions in TestAllExtensions with the given
93    /// ExtensionRegistry.
94    /// </summary>
95    internal static void RegisterAllExtensions(ExtensionRegistry registry) {
96      registry.Add(UnitTestProtoFile.OptionalInt32Extension);
97      registry.Add(UnitTestProtoFile.OptionalInt64Extension);
98      registry.Add(UnitTestProtoFile.OptionalUint32Extension);
99      registry.Add(UnitTestProtoFile.OptionalUint64Extension);
100      registry.Add(UnitTestProtoFile.OptionalSint32Extension);
101      registry.Add(UnitTestProtoFile.OptionalSint64Extension);
102      registry.Add(UnitTestProtoFile.OptionalFixed32Extension);
103      registry.Add(UnitTestProtoFile.OptionalFixed64Extension);
104      registry.Add(UnitTestProtoFile.OptionalSfixed32Extension);
105      registry.Add(UnitTestProtoFile.OptionalSfixed64Extension);
106      registry.Add(UnitTestProtoFile.OptionalFloatExtension);
107      registry.Add(UnitTestProtoFile.OptionalDoubleExtension);
108      registry.Add(UnitTestProtoFile.OptionalBoolExtension);
109      registry.Add(UnitTestProtoFile.OptionalStringExtension);
110      registry.Add(UnitTestProtoFile.OptionalBytesExtension);
111      registry.Add(UnitTestProtoFile.OptionalGroupExtension);
112      registry.Add(UnitTestProtoFile.OptionalNestedMessageExtension);
113      registry.Add(UnitTestProtoFile.OptionalForeignMessageExtension);
114      registry.Add(UnitTestProtoFile.OptionalImportMessageExtension);
115      registry.Add(UnitTestProtoFile.OptionalNestedEnumExtension);
116      registry.Add(UnitTestProtoFile.OptionalForeignEnumExtension);
117      registry.Add(UnitTestProtoFile.OptionalImportEnumExtension);
118      registry.Add(UnitTestProtoFile.OptionalStringPieceExtension);
119      registry.Add(UnitTestProtoFile.OptionalCordExtension);
120
121      registry.Add(UnitTestProtoFile.RepeatedInt32Extension);
122      registry.Add(UnitTestProtoFile.RepeatedInt64Extension);
123      registry.Add(UnitTestProtoFile.RepeatedUint32Extension);
124      registry.Add(UnitTestProtoFile.RepeatedUint64Extension);
125      registry.Add(UnitTestProtoFile.RepeatedSint32Extension);
126      registry.Add(UnitTestProtoFile.RepeatedSint64Extension);
127      registry.Add(UnitTestProtoFile.RepeatedFixed32Extension);
128      registry.Add(UnitTestProtoFile.RepeatedFixed64Extension);
129      registry.Add(UnitTestProtoFile.RepeatedSfixed32Extension);
130      registry.Add(UnitTestProtoFile.RepeatedSfixed64Extension);
131      registry.Add(UnitTestProtoFile.RepeatedFloatExtension);
132      registry.Add(UnitTestProtoFile.RepeatedDoubleExtension);
133      registry.Add(UnitTestProtoFile.RepeatedBoolExtension);
134      registry.Add(UnitTestProtoFile.RepeatedStringExtension);
135      registry.Add(UnitTestProtoFile.RepeatedBytesExtension);
136      registry.Add(UnitTestProtoFile.RepeatedGroupExtension);
137      registry.Add(UnitTestProtoFile.RepeatedNestedMessageExtension);
138      registry.Add(UnitTestProtoFile.RepeatedForeignMessageExtension);
139      registry.Add(UnitTestProtoFile.RepeatedImportMessageExtension);
140      registry.Add(UnitTestProtoFile.RepeatedNestedEnumExtension);
141      registry.Add(UnitTestProtoFile.RepeatedForeignEnumExtension);
142      registry.Add(UnitTestProtoFile.RepeatedImportEnumExtension);
143      registry.Add(UnitTestProtoFile.RepeatedStringPieceExtension);
144      registry.Add(UnitTestProtoFile.RepeatedCordExtension);
145
146      registry.Add(UnitTestProtoFile.DefaultInt32Extension);
147      registry.Add(UnitTestProtoFile.DefaultInt64Extension);
148      registry.Add(UnitTestProtoFile.DefaultUint32Extension);
149      registry.Add(UnitTestProtoFile.DefaultUint64Extension);
150      registry.Add(UnitTestProtoFile.DefaultSint32Extension);
151      registry.Add(UnitTestProtoFile.DefaultSint64Extension);
152      registry.Add(UnitTestProtoFile.DefaultFixed32Extension);
153      registry.Add(UnitTestProtoFile.DefaultFixed64Extension);
154      registry.Add(UnitTestProtoFile.DefaultSfixed32Extension);
155      registry.Add(UnitTestProtoFile.DefaultSfixed64Extension);
156      registry.Add(UnitTestProtoFile.DefaultFloatExtension);
157      registry.Add(UnitTestProtoFile.DefaultDoubleExtension);
158      registry.Add(UnitTestProtoFile.DefaultBoolExtension);
159      registry.Add(UnitTestProtoFile.DefaultStringExtension);
160      registry.Add(UnitTestProtoFile.DefaultBytesExtension);
161      registry.Add(UnitTestProtoFile.DefaultNestedEnumExtension);
162      registry.Add(UnitTestProtoFile.DefaultForeignEnumExtension);
163      registry.Add(UnitTestProtoFile.DefaultImportEnumExtension);
164      registry.Add(UnitTestProtoFile.DefaultStringPieceExtension);
165      registry.Add(UnitTestProtoFile.DefaultCordExtension);
166
167      registry.Add(UnitTestProtoFile.PackedInt32Extension);
168      registry.Add(UnitTestProtoFile.PackedInt64Extension);
169      registry.Add(UnitTestProtoFile.PackedUint32Extension);
170      registry.Add(UnitTestProtoFile.PackedUint64Extension);
171      registry.Add(UnitTestProtoFile.PackedSint32Extension);
172      registry.Add(UnitTestProtoFile.PackedSint64Extension);
173      registry.Add(UnitTestProtoFile.PackedFixed32Extension);
174      registry.Add(UnitTestProtoFile.PackedFixed64Extension);
175      registry.Add(UnitTestProtoFile.PackedSfixed32Extension);
176      registry.Add(UnitTestProtoFile.PackedSfixed64Extension);
177      registry.Add(UnitTestProtoFile.PackedFloatExtension);
178      registry.Add(UnitTestProtoFile.PackedDoubleExtension);
179      registry.Add(UnitTestProtoFile.PackedBoolExtension);
180      registry.Add(UnitTestProtoFile.PackedEnumExtension);
181    }
182
183    internal static string ReadTextFromFile(string filePath) {
184      return ReadBytesFromFile(filePath).ToStringUtf8();
185    }
186
187    internal static ByteString ReadBytesFromFile(String filename) {
188      byte[] data = File.ReadAllBytes(Path.Combine(TestDataDirectory, filename));
189      return ByteString.CopyFrom(data);
190    }
191
192    /// <summary>
193    /// Helper to convert a String to ByteString.
194    /// </summary>
195    internal static ByteString ToBytes(String str) {
196      return ByteString.CopyFrom(Encoding.UTF8.GetBytes(str));
197    }
198
199    internal static TestAllTypes GetAllSet() {
200      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
201      SetAllFields(builder);
202      return builder.Build();
203    }
204
205    /// <summary>
206    /// Sets every field of the specified message to the values expected by
207    /// AssertAllFieldsSet.
208    /// </summary>
209    internal static void SetAllFields(TestAllTypes.Builder message) {
210      message.SetOptionalInt32(101);
211      message.SetOptionalInt64(102);
212      message.SetOptionalUint32(103);
213      message.SetOptionalUint64(104);
214      message.SetOptionalSint32(105);
215      message.SetOptionalSint64(106);
216      message.SetOptionalFixed32(107);
217      message.SetOptionalFixed64(108);
218      message.SetOptionalSfixed32(109);
219      message.SetOptionalSfixed64(110);
220      message.SetOptionalFloat(111);
221      message.SetOptionalDouble(112);
222      message.SetOptionalBool(true);
223      message.SetOptionalString("115");
224      message.SetOptionalBytes(ToBytes("116"));
225     
226      message.SetOptionalGroup(TestAllTypes.Types.OptionalGroup.CreateBuilder().SetA(117).Build());
227      message.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(118).Build());
228      message.SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(119).Build());
229      message.SetOptionalImportMessage(ImportMessage.CreateBuilder().SetD(120).Build());
230
231      message.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAZ);
232      message.SetOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ);
233      message.SetOptionalImportEnum(ImportEnum.IMPORT_BAZ);
234
235      message.SetOptionalStringPiece("124");
236      message.SetOptionalCord("125");
237
238      // -----------------------------------------------------------------
239
240      message.AddRepeatedInt32(201);
241      message.AddRepeatedInt64(202);
242      message.AddRepeatedUint32(203);
243      message.AddRepeatedUint64(204);
244      message.AddRepeatedSint32(205);
245      message.AddRepeatedSint64(206);
246      message.AddRepeatedFixed32(207);
247      message.AddRepeatedFixed64(208);
248      message.AddRepeatedSfixed32(209);
249      message.AddRepeatedSfixed64(210);
250      message.AddRepeatedFloat(211);
251      message.AddRepeatedDouble(212);
252      message.AddRepeatedBool(true);
253      message.AddRepeatedString("215");
254      message.AddRepeatedBytes(ToBytes("216"));
255
256      message.AddRepeatedGroup(TestAllTypes.Types.RepeatedGroup.CreateBuilder().SetA(217).Build());
257      message.AddRepeatedNestedMessage(TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(218).Build());
258      message.AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(219).Build());
259      message.AddRepeatedImportMessage(ImportMessage.CreateBuilder().SetD(220).Build());
260
261      message.AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.BAR);
262      message.AddRepeatedForeignEnum(ForeignEnum.FOREIGN_BAR);
263      message.AddRepeatedImportEnum(ImportEnum.IMPORT_BAR);
264
265      message.AddRepeatedStringPiece("224");
266      message.AddRepeatedCord("225");
267
268      // Add a second one of each field.
269      message.AddRepeatedInt32(301);
270      message.AddRepeatedInt64(302);
271      message.AddRepeatedUint32(303);
272      message.AddRepeatedUint64(304);
273      message.AddRepeatedSint32(305);
274      message.AddRepeatedSint64(306);
275      message.AddRepeatedFixed32(307);
276      message.AddRepeatedFixed64(308);
277      message.AddRepeatedSfixed32(309);
278      message.AddRepeatedSfixed64(310);
279      message.AddRepeatedFloat(311);
280      message.AddRepeatedDouble(312);
281      message.AddRepeatedBool(false);
282      message.AddRepeatedString("315");
283      message.AddRepeatedBytes(ToBytes("316"));
284
285      message.AddRepeatedGroup(TestAllTypes.Types.RepeatedGroup.CreateBuilder().SetA(317).Build());
286      message.AddRepeatedNestedMessage(TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(318).Build());
287      message.AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(319).Build());
288      message.AddRepeatedImportMessage(ImportMessage.CreateBuilder().SetD(320).Build());
289
290      message.AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.BAZ);
291      message.AddRepeatedForeignEnum(ForeignEnum.FOREIGN_BAZ);
292      message.AddRepeatedImportEnum(ImportEnum.IMPORT_BAZ);
293
294      message.AddRepeatedStringPiece("324");
295      message.AddRepeatedCord("325");
296
297      // -----------------------------------------------------------------
298
299      message.SetDefaultInt32(401);
300      message.SetDefaultInt64(402);
301      message.SetDefaultUint32(403);
302      message.SetDefaultUint64(404);
303      message.SetDefaultSint32(405);
304      message.SetDefaultSint64(406);
305      message.SetDefaultFixed32(407);
306      message.SetDefaultFixed64(408);
307      message.SetDefaultSfixed32(409);
308      message.SetDefaultSfixed64(410);
309      message.SetDefaultFloat(411);
310      message.SetDefaultDouble(412);
311      message.SetDefaultBool(false);
312      message.SetDefaultString("415");
313      message.SetDefaultBytes(ToBytes("416"));
314     
315      message.SetDefaultNestedEnum(TestAllTypes.Types.NestedEnum.FOO);
316      message.SetDefaultForeignEnum(ForeignEnum.FOREIGN_FOO);
317      message.SetDefaultImportEnum(ImportEnum.IMPORT_FOO);
318
319      message.SetDefaultStringPiece("424");
320      message.SetDefaultCord("425");
321    }
322
323    /// <summary>
324    /// Asserts that all fields of the specified message are set to the values
325    /// assigned by SetAllFields.
326    /// </summary>
327    internal static void AssertAllFieldsSet(TestAllTypes message) {
328      Assert.IsTrue(message.HasOptionalInt32);
329      Assert.IsTrue(message.HasOptionalInt64);
330      Assert.IsTrue(message.HasOptionalUint32);
331      Assert.IsTrue(message.HasOptionalUint64);
332      Assert.IsTrue(message.HasOptionalSint32);
333      Assert.IsTrue(message.HasOptionalSint64);
334      Assert.IsTrue(message.HasOptionalFixed32);
335      Assert.IsTrue(message.HasOptionalFixed64);
336      Assert.IsTrue(message.HasOptionalSfixed32);
337      Assert.IsTrue(message.HasOptionalSfixed64);
338      Assert.IsTrue(message.HasOptionalFloat);
339      Assert.IsTrue(message.HasOptionalDouble);
340      Assert.IsTrue(message.HasOptionalBool);
341      Assert.IsTrue(message.HasOptionalString);
342      Assert.IsTrue(message.HasOptionalBytes);
343
344      Assert.IsTrue(message.HasOptionalGroup);
345      Assert.IsTrue(message.HasOptionalNestedMessage);
346      Assert.IsTrue(message.HasOptionalForeignMessage);
347      Assert.IsTrue(message.HasOptionalImportMessage);
348
349      Assert.IsTrue(message.OptionalGroup.HasA);
350      Assert.IsTrue(message.OptionalNestedMessage.HasBb);
351      Assert.IsTrue(message.OptionalForeignMessage.HasC);
352      Assert.IsTrue(message.OptionalImportMessage.HasD);
353
354      Assert.IsTrue(message.HasOptionalNestedEnum);
355      Assert.IsTrue(message.HasOptionalForeignEnum);
356      Assert.IsTrue(message.HasOptionalImportEnum);
357
358      Assert.IsTrue(message.HasOptionalStringPiece);
359      Assert.IsTrue(message.HasOptionalCord);
360
361      Assert.AreEqual(101, message.OptionalInt32);
362      Assert.AreEqual(102, message.OptionalInt64);
363      Assert.AreEqual(103, message.OptionalUint32);
364      Assert.AreEqual(104, message.OptionalUint64);
365      Assert.AreEqual(105, message.OptionalSint32);
366      Assert.AreEqual(106, message.OptionalSint64);
367      Assert.AreEqual(107, message.OptionalFixed32);
368      Assert.AreEqual(108, message.OptionalFixed64);
369      Assert.AreEqual(109, message.OptionalSfixed32);
370      Assert.AreEqual(110, message.OptionalSfixed64);
371      Assert.AreEqual(111, message.OptionalFloat);
372      Assert.AreEqual(112, message.OptionalDouble);
373      Assert.AreEqual(true, message.OptionalBool);
374      Assert.AreEqual("115", message.OptionalString);
375      Assert.AreEqual(ToBytes("116"), message.OptionalBytes);
376
377      Assert.AreEqual(117, message.OptionalGroup.A);
378      Assert.AreEqual(118, message.OptionalNestedMessage.Bb);
379      Assert.AreEqual(119, message.OptionalForeignMessage.C);
380      Assert.AreEqual(120, message.OptionalImportMessage.D);
381
382      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ, message.OptionalNestedEnum);
383      Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.OptionalForeignEnum);
384      Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.OptionalImportEnum);
385
386      Assert.AreEqual("124", message.OptionalStringPiece);
387      Assert.AreEqual("125", message.OptionalCord);
388
389      // -----------------------------------------------------------------
390
391      Assert.AreEqual(2, message.RepeatedInt32Count);
392      Assert.AreEqual(2, message.RepeatedInt64Count);
393      Assert.AreEqual(2, message.RepeatedUint32Count);
394      Assert.AreEqual(2, message.RepeatedUint64Count);
395      Assert.AreEqual(2, message.RepeatedSint32Count);
396      Assert.AreEqual(2, message.RepeatedSint64Count);
397      Assert.AreEqual(2, message.RepeatedFixed32Count);
398      Assert.AreEqual(2, message.RepeatedFixed64Count);
399      Assert.AreEqual(2, message.RepeatedSfixed32Count);
400      Assert.AreEqual(2, message.RepeatedSfixed64Count);
401      Assert.AreEqual(2, message.RepeatedFloatCount);
402      Assert.AreEqual(2, message.RepeatedDoubleCount);
403      Assert.AreEqual(2, message.RepeatedBoolCount);
404      Assert.AreEqual(2, message.RepeatedStringCount);
405      Assert.AreEqual(2, message.RepeatedBytesCount);
406
407      Assert.AreEqual(2, message.RepeatedGroupCount         );
408      Assert.AreEqual(2, message.RepeatedNestedMessageCount );
409      Assert.AreEqual(2, message.RepeatedForeignMessageCount);
410      Assert.AreEqual(2, message.RepeatedImportMessageCount );
411      Assert.AreEqual(2, message.RepeatedNestedEnumCount    );
412      Assert.AreEqual(2, message.RepeatedForeignEnumCount   );
413      Assert.AreEqual(2, message.RepeatedImportEnumCount    );
414
415      Assert.AreEqual(2, message.RepeatedStringPieceCount);
416      Assert.AreEqual(2, message.RepeatedCordCount);
417
418      Assert.AreEqual(201, message.GetRepeatedInt32(0));
419      Assert.AreEqual(202, message.GetRepeatedInt64(0));
420      Assert.AreEqual(203, message.GetRepeatedUint32(0));
421      Assert.AreEqual(204, message.GetRepeatedUint64(0));
422      Assert.AreEqual(205, message.GetRepeatedSint32(0));
423      Assert.AreEqual(206, message.GetRepeatedSint64(0));
424      Assert.AreEqual(207, message.GetRepeatedFixed32(0));
425      Assert.AreEqual(208, message.GetRepeatedFixed64(0));
426      Assert.AreEqual(209, message.GetRepeatedSfixed32(0));
427      Assert.AreEqual(210, message.GetRepeatedSfixed64(0));
428      Assert.AreEqual(211, message.GetRepeatedFloat(0));
429      Assert.AreEqual(212, message.GetRepeatedDouble(0));
430      Assert.AreEqual(true , message.GetRepeatedBool(0));
431      Assert.AreEqual("215", message.GetRepeatedString(0));
432      Assert.AreEqual(ToBytes("216"), message.GetRepeatedBytes(0));
433
434      Assert.AreEqual(217, message.GetRepeatedGroup(0).A);
435      Assert.AreEqual(218, message.GetRepeatedNestedMessage (0).Bb);
436      Assert.AreEqual(219, message.GetRepeatedForeignMessage(0).C);
437      Assert.AreEqual(220, message.GetRepeatedImportMessage (0).D);
438
439      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetRepeatedNestedEnum (0));
440      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetRepeatedForeignEnum(0));
441      Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetRepeatedImportEnum(0));
442
443      Assert.AreEqual("224", message.GetRepeatedStringPiece(0));
444      Assert.AreEqual("225", message.GetRepeatedCord(0));
445
446      Assert.AreEqual(301, message.GetRepeatedInt32   (1));
447      Assert.AreEqual(302, message.GetRepeatedInt64   (1));
448      Assert.AreEqual(303, message.GetRepeatedUint32  (1));
449      Assert.AreEqual(304, message.GetRepeatedUint64  (1));
450      Assert.AreEqual(305, message.GetRepeatedSint32  (1));
451      Assert.AreEqual(306, message.GetRepeatedSint64  (1));
452      Assert.AreEqual(307, message.GetRepeatedFixed32 (1));
453      Assert.AreEqual(308, message.GetRepeatedFixed64 (1));
454      Assert.AreEqual(309, message.GetRepeatedSfixed32(1));
455      Assert.AreEqual(310, message.GetRepeatedSfixed64(1));
456      Assert.AreEqual(311, message.GetRepeatedFloat   (1), 0.0);
457      Assert.AreEqual(312, message.GetRepeatedDouble  (1), 0.0);
458      Assert.AreEqual(false, message.GetRepeatedBool    (1));
459      Assert.AreEqual("315", message.GetRepeatedString  (1));
460      Assert.AreEqual(ToBytes("316"), message.GetRepeatedBytes(1));
461
462      Assert.AreEqual(317, message.GetRepeatedGroup         (1).A);
463      Assert.AreEqual(318, message.GetRepeatedNestedMessage (1).Bb);
464      Assert.AreEqual(319, message.GetRepeatedForeignMessage(1).C);
465      Assert.AreEqual(320, message.GetRepeatedImportMessage (1).D);
466
467      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ, message.GetRepeatedNestedEnum (1));
468      Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetRepeatedForeignEnum(1));
469      Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.GetRepeatedImportEnum(1));
470
471      Assert.AreEqual("324", message.GetRepeatedStringPiece(1));
472      Assert.AreEqual("325", message.GetRepeatedCord(1));
473
474      // -----------------------------------------------------------------
475
476      Assert.IsTrue(message.HasDefaultInt32   );
477      Assert.IsTrue(message.HasDefaultInt64   );
478      Assert.IsTrue(message.HasDefaultUint32  );
479      Assert.IsTrue(message.HasDefaultUint64  );
480      Assert.IsTrue(message.HasDefaultSint32  );
481      Assert.IsTrue(message.HasDefaultSint64  );
482      Assert.IsTrue(message.HasDefaultFixed32 );
483      Assert.IsTrue(message.HasDefaultFixed64 );
484      Assert.IsTrue(message.HasDefaultSfixed32);
485      Assert.IsTrue(message.HasDefaultSfixed64);
486      Assert.IsTrue(message.HasDefaultFloat   );
487      Assert.IsTrue(message.HasDefaultDouble  );
488      Assert.IsTrue(message.HasDefaultBool    );
489      Assert.IsTrue(message.HasDefaultString  );
490      Assert.IsTrue(message.HasDefaultBytes   );
491
492      Assert.IsTrue(message.HasDefaultNestedEnum );
493      Assert.IsTrue(message.HasDefaultForeignEnum);
494      Assert.IsTrue(message.HasDefaultImportEnum );
495
496      Assert.IsTrue(message.HasDefaultStringPiece);
497      Assert.IsTrue(message.HasDefaultCord);
498
499      Assert.AreEqual(401, message.DefaultInt32);
500      Assert.AreEqual(402, message.DefaultInt64);
501      Assert.AreEqual(403, message.DefaultUint32);
502      Assert.AreEqual(404, message.DefaultUint64);
503      Assert.AreEqual(405, message.DefaultSint32);
504      Assert.AreEqual(406, message.DefaultSint64);
505      Assert.AreEqual(407, message.DefaultFixed32);
506      Assert.AreEqual(408, message.DefaultFixed64);
507      Assert.AreEqual(409, message.DefaultSfixed32);
508      Assert.AreEqual(410, message.DefaultSfixed64);
509      Assert.AreEqual(411, message.DefaultFloat);
510      Assert.AreEqual(412, message.DefaultDouble);
511      Assert.AreEqual(false, message.DefaultBool    );
512      Assert.AreEqual("415", message.DefaultString  );
513      Assert.AreEqual(ToBytes("416"), message.DefaultBytes);
514
515      Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.DefaultNestedEnum);
516      Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.DefaultForeignEnum);
517      Assert.AreEqual(ImportEnum.IMPORT_FOO, message.DefaultImportEnum);
518
519      Assert.AreEqual("424", message.DefaultStringPiece);
520      Assert.AreEqual("425", message.DefaultCord);
521    }
522
523    internal static void AssertClear(TestAllTypes message) {
524      // HasBlah() should initially be false for all optional fields.
525      Assert.IsFalse(message.HasOptionalInt32);
526      Assert.IsFalse(message.HasOptionalInt64);
527      Assert.IsFalse(message.HasOptionalUint32);
528      Assert.IsFalse(message.HasOptionalUint64);
529      Assert.IsFalse(message.HasOptionalSint32);
530      Assert.IsFalse(message.HasOptionalSint64);
531      Assert.IsFalse(message.HasOptionalFixed32);
532      Assert.IsFalse(message.HasOptionalFixed64);
533      Assert.IsFalse(message.HasOptionalSfixed32);
534      Assert.IsFalse(message.HasOptionalSfixed64);
535      Assert.IsFalse(message.HasOptionalFloat);
536      Assert.IsFalse(message.HasOptionalDouble);
537      Assert.IsFalse(message.HasOptionalBool);
538      Assert.IsFalse(message.HasOptionalString);
539      Assert.IsFalse(message.HasOptionalBytes);
540
541      Assert.IsFalse(message.HasOptionalGroup);
542      Assert.IsFalse(message.HasOptionalNestedMessage);
543      Assert.IsFalse(message.HasOptionalForeignMessage);
544      Assert.IsFalse(message.HasOptionalImportMessage);
545
546      Assert.IsFalse(message.HasOptionalNestedEnum);
547      Assert.IsFalse(message.HasOptionalForeignEnum);
548      Assert.IsFalse(message.HasOptionalImportEnum);
549
550      Assert.IsFalse(message.HasOptionalStringPiece);
551      Assert.IsFalse(message.HasOptionalCord);
552
553      // Optional fields without defaults are set to zero or something like it.
554      Assert.AreEqual(0, message.OptionalInt32);
555      Assert.AreEqual(0, message.OptionalInt64);
556      Assert.AreEqual(0, message.OptionalUint32);
557      Assert.AreEqual(0, message.OptionalUint64);
558      Assert.AreEqual(0, message.OptionalSint32);
559      Assert.AreEqual(0, message.OptionalSint64);
560      Assert.AreEqual(0, message.OptionalFixed32);
561      Assert.AreEqual(0, message.OptionalFixed64);
562      Assert.AreEqual(0, message.OptionalSfixed32);
563      Assert.AreEqual(0, message.OptionalSfixed64);
564      Assert.AreEqual(0, message.OptionalFloat);
565      Assert.AreEqual(0, message.OptionalDouble);
566      Assert.AreEqual(false, message.OptionalBool);
567      Assert.AreEqual("", message.OptionalString);
568      Assert.AreEqual(ByteString.Empty, message.OptionalBytes);
569
570      // Embedded messages should also be clear.
571      Assert.IsFalse(message.OptionalGroup.HasA);
572      Assert.IsFalse(message.OptionalNestedMessage.HasBb);
573      Assert.IsFalse(message.OptionalForeignMessage.HasC);
574      Assert.IsFalse(message.OptionalImportMessage.HasD);
575
576      Assert.AreEqual(0, message.OptionalGroup.A);
577      Assert.AreEqual(0, message.OptionalNestedMessage.Bb);
578      Assert.AreEqual(0, message.OptionalForeignMessage.C);
579      Assert.AreEqual(0, message.OptionalImportMessage.D);
580
581      // Enums without defaults are set to the first value in the enum.
582      Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
583      Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.OptionalForeignEnum);
584      Assert.AreEqual(ImportEnum.IMPORT_FOO, message.OptionalImportEnum);
585
586      Assert.AreEqual("", message.OptionalStringPiece);
587      Assert.AreEqual("", message.OptionalCord);
588
589      // Repeated fields are empty.
590      Assert.AreEqual(0, message.RepeatedInt32Count);
591      Assert.AreEqual(0, message.RepeatedInt64Count);
592      Assert.AreEqual(0, message.RepeatedUint32Count);
593      Assert.AreEqual(0, message.RepeatedUint64Count);
594      Assert.AreEqual(0, message.RepeatedSint32Count);
595      Assert.AreEqual(0, message.RepeatedSint64Count);
596      Assert.AreEqual(0, message.RepeatedFixed32Count);
597      Assert.AreEqual(0, message.RepeatedFixed64Count);
598      Assert.AreEqual(0, message.RepeatedSfixed32Count);
599      Assert.AreEqual(0, message.RepeatedSfixed64Count);
600      Assert.AreEqual(0, message.RepeatedFloatCount);
601      Assert.AreEqual(0, message.RepeatedDoubleCount);
602      Assert.AreEqual(0, message.RepeatedBoolCount);
603      Assert.AreEqual(0, message.RepeatedStringCount);
604      Assert.AreEqual(0, message.RepeatedBytesCount);
605
606      Assert.AreEqual(0, message.RepeatedGroupCount);
607      Assert.AreEqual(0, message.RepeatedNestedMessageCount);
608      Assert.AreEqual(0, message.RepeatedForeignMessageCount);
609      Assert.AreEqual(0, message.RepeatedImportMessageCount);
610      Assert.AreEqual(0, message.RepeatedNestedEnumCount);
611      Assert.AreEqual(0, message.RepeatedForeignEnumCount);
612      Assert.AreEqual(0, message.RepeatedImportEnumCount);
613
614      Assert.AreEqual(0, message.RepeatedStringPieceCount);
615      Assert.AreEqual(0, message.RepeatedCordCount);
616
617      // HasBlah() should also be false for all default fields.
618      Assert.IsFalse(message.HasDefaultInt32);
619      Assert.IsFalse(message.HasDefaultInt64);
620      Assert.IsFalse(message.HasDefaultUint32);
621      Assert.IsFalse(message.HasDefaultUint64);
622      Assert.IsFalse(message.HasDefaultSint32);
623      Assert.IsFalse(message.HasDefaultSint64);
624      Assert.IsFalse(message.HasDefaultFixed32);
625      Assert.IsFalse(message.HasDefaultFixed64);
626      Assert.IsFalse(message.HasDefaultSfixed32);
627      Assert.IsFalse(message.HasDefaultSfixed64);
628      Assert.IsFalse(message.HasDefaultFloat);
629      Assert.IsFalse(message.HasDefaultDouble);
630      Assert.IsFalse(message.HasDefaultBool);
631      Assert.IsFalse(message.HasDefaultString);
632      Assert.IsFalse(message.HasDefaultBytes);
633
634      Assert.IsFalse(message.HasDefaultNestedEnum);
635      Assert.IsFalse(message.HasDefaultForeignEnum);
636      Assert.IsFalse(message.HasDefaultImportEnum);
637
638      Assert.IsFalse(message.HasDefaultStringPiece);
639      Assert.IsFalse(message.HasDefaultCord);
640
641      // Fields with defaults have their default values (duh).
642      Assert.AreEqual(41, message.DefaultInt32);
643      Assert.AreEqual(42, message.DefaultInt64);
644      Assert.AreEqual(43, message.DefaultUint32);
645      Assert.AreEqual(44, message.DefaultUint64);
646      Assert.AreEqual(-45, message.DefaultSint32);
647      Assert.AreEqual(46, message.DefaultSint64);
648      Assert.AreEqual(47, message.DefaultFixed32);
649      Assert.AreEqual(48, message.DefaultFixed64);
650      Assert.AreEqual(49, message.DefaultSfixed32);
651      Assert.AreEqual(-50, message.DefaultSfixed64);
652      Assert.AreEqual(51.5, message.DefaultFloat, 0.0);
653      Assert.AreEqual(52e3, message.DefaultDouble, 0.0);
654      Assert.AreEqual(true, message.DefaultBool);
655      Assert.AreEqual("hello", message.DefaultString);
656      Assert.AreEqual(ToBytes("world"), message.DefaultBytes);
657
658      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.DefaultNestedEnum);
659      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.DefaultForeignEnum);
660      Assert.AreEqual(ImportEnum.IMPORT_BAR, message.DefaultImportEnum);
661
662      Assert.AreEqual("abc", message.DefaultStringPiece);
663      Assert.AreEqual("123", message.DefaultCord);
664    }
665
666    /// <summary>
667    /// Get a TestAllExtensions with all fields set as they would be by
668    /// SetAllExtensions(TestAllExtensions.Builder).
669    /// </summary>
670    internal static TestAllExtensions GetAllExtensionsSet() {
671      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
672      SetAllExtensions(builder);
673      return builder.Build();
674    }
675
676    public static TestPackedTypes GetPackedSet() {
677      TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
678      SetPackedFields(builder);
679      return builder.Build();
680    }
681
682    public static TestPackedExtensions GetPackedExtensionsSet() {
683      TestPackedExtensions.Builder builder = TestPackedExtensions.CreateBuilder();
684      SetPackedExtensions(builder);
685      return builder.Build();
686    }
687
688    /// <summary>
689    /// Sets every field of the specified builder to the values expected by
690    /// AssertAllExtensionsSet.
691    /// </summary>
692    internal static void SetAllExtensions(TestAllExtensions.Builder message) {
693      message.SetExtension(UnitTestProtoFile.OptionalInt32Extension, 101);
694      message.SetExtension(UnitTestProtoFile.OptionalInt64Extension, 102L);
695      message.SetExtension(UnitTestProtoFile.OptionalUint32Extension, 103U);
696      message.SetExtension(UnitTestProtoFile.OptionalUint64Extension, 104UL);
697      message.SetExtension(UnitTestProtoFile.OptionalSint32Extension, 105);
698      message.SetExtension(UnitTestProtoFile.OptionalSint64Extension, 106L);
699      message.SetExtension(UnitTestProtoFile.OptionalFixed32Extension, 107U);
700      message.SetExtension(UnitTestProtoFile.OptionalFixed64Extension, 108UL);
701      message.SetExtension(UnitTestProtoFile.OptionalSfixed32Extension, 109);
702      message.SetExtension(UnitTestProtoFile.OptionalSfixed64Extension, 110L);
703      message.SetExtension(UnitTestProtoFile.OptionalFloatExtension, 111F);
704      message.SetExtension(UnitTestProtoFile.OptionalDoubleExtension, 112D);
705      message.SetExtension(UnitTestProtoFile.OptionalBoolExtension, true);
706      message.SetExtension(UnitTestProtoFile.OptionalStringExtension, "115");
707      message.SetExtension(UnitTestProtoFile.OptionalBytesExtension, ToBytes("116"));
708
709      message.SetExtension(UnitTestProtoFile.OptionalGroupExtension, OptionalGroup_extension.CreateBuilder().SetA(117).Build());
710      message.SetExtension(UnitTestProtoFile.OptionalNestedMessageExtension, TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(118).Build());
711      message.SetExtension(UnitTestProtoFile.OptionalForeignMessageExtension, ForeignMessage.CreateBuilder().SetC(119).Build());
712      message.SetExtension(UnitTestProtoFile.OptionalImportMessageExtension, ImportMessage.CreateBuilder().SetD(120).Build());
713
714      message.SetExtension(UnitTestProtoFile.OptionalNestedEnumExtension, TestAllTypes.Types.NestedEnum.BAZ);
715      message.SetExtension(UnitTestProtoFile.OptionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ);
716      message.SetExtension(UnitTestProtoFile.OptionalImportEnumExtension, ImportEnum.IMPORT_BAZ);
717
718      message.SetExtension(UnitTestProtoFile.OptionalStringPieceExtension, "124");
719      message.SetExtension(UnitTestProtoFile.OptionalCordExtension, "125");
720
721      // -----------------------------------------------------------------
722
723      message.AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 201);
724      message.AddExtension(UnitTestProtoFile.RepeatedInt64Extension, 202L);
725      message.AddExtension(UnitTestProtoFile.RepeatedUint32Extension, 203U);
726      message.AddExtension(UnitTestProtoFile.RepeatedUint64Extension, 204UL);
727      message.AddExtension(UnitTestProtoFile.RepeatedSint32Extension, 205);
728      message.AddExtension(UnitTestProtoFile.RepeatedSint64Extension, 206L);
729      message.AddExtension(UnitTestProtoFile.RepeatedFixed32Extension, 207U);
730      message.AddExtension(UnitTestProtoFile.RepeatedFixed64Extension, 208UL);
731      message.AddExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 209);
732      message.AddExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 210L);
733      message.AddExtension(UnitTestProtoFile.RepeatedFloatExtension, 211F);
734      message.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 212D);
735      message.AddExtension(UnitTestProtoFile.RepeatedBoolExtension, true);
736      message.AddExtension(UnitTestProtoFile.RepeatedStringExtension, "215");
737      message.AddExtension(UnitTestProtoFile.RepeatedBytesExtension, ToBytes("216"));
738
739      message.AddExtension(UnitTestProtoFile.RepeatedGroupExtension, RepeatedGroup_extension.CreateBuilder().SetA(217).Build());
740      message.AddExtension(UnitTestProtoFile.RepeatedNestedMessageExtension, TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(218).Build());
741      message.AddExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, ForeignMessage.CreateBuilder().SetC(219).Build());
742      message.AddExtension(UnitTestProtoFile.RepeatedImportMessageExtension, ImportMessage.CreateBuilder().SetD(220).Build());
743
744      message.AddExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.BAR);
745      message.AddExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAR);
746      message.AddExtension(UnitTestProtoFile.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAR);
747
748      message.AddExtension(UnitTestProtoFile.RepeatedStringPieceExtension, "224");
749      message.AddExtension(UnitTestProtoFile.RepeatedCordExtension, "225");
750
751      // Add a second one of each field.
752      message.AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 301);
753      message.AddExtension(UnitTestProtoFile.RepeatedInt64Extension, 302L);
754      message.AddExtension(UnitTestProtoFile.RepeatedUint32Extension, 303U);
755      message.AddExtension(UnitTestProtoFile.RepeatedUint64Extension, 304UL);
756      message.AddExtension(UnitTestProtoFile.RepeatedSint32Extension, 305);
757      message.AddExtension(UnitTestProtoFile.RepeatedSint64Extension, 306L);
758      message.AddExtension(UnitTestProtoFile.RepeatedFixed32Extension, 307U);
759      message.AddExtension(UnitTestProtoFile.RepeatedFixed64Extension, 308UL);
760      message.AddExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 309);
761      message.AddExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 310L);
762      message.AddExtension(UnitTestProtoFile.RepeatedFloatExtension, 311F);
763      message.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 312D);
764      message.AddExtension(UnitTestProtoFile.RepeatedBoolExtension, false);
765      message.AddExtension(UnitTestProtoFile.RepeatedStringExtension, "315");
766      message.AddExtension(UnitTestProtoFile.RepeatedBytesExtension, ToBytes("316"));
767
768      message.AddExtension(UnitTestProtoFile.RepeatedGroupExtension, RepeatedGroup_extension.CreateBuilder().SetA(317).Build());
769      message.AddExtension(UnitTestProtoFile.RepeatedNestedMessageExtension, TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(318).Build());
770      message.AddExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, ForeignMessage.CreateBuilder().SetC(319).Build());
771      message.AddExtension(UnitTestProtoFile.RepeatedImportMessageExtension, ImportMessage.CreateBuilder().SetD(320).Build());
772
773      message.AddExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.BAZ);
774      message.AddExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ);
775      message.AddExtension(UnitTestProtoFile.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAZ);
776
777      message.AddExtension(UnitTestProtoFile.RepeatedStringPieceExtension, "324");
778      message.AddExtension(UnitTestProtoFile.RepeatedCordExtension, "325");
779
780      // -----------------------------------------------------------------
781
782      message.SetExtension(UnitTestProtoFile.DefaultInt32Extension, 401);
783      message.SetExtension(UnitTestProtoFile.DefaultInt64Extension, 402L);
784      message.SetExtension(UnitTestProtoFile.DefaultUint32Extension, 403U);
785      message.SetExtension(UnitTestProtoFile.DefaultUint64Extension, 404UL);
786      message.SetExtension(UnitTestProtoFile.DefaultSint32Extension, 405);
787      message.SetExtension(UnitTestProtoFile.DefaultSint64Extension, 406L);
788      message.SetExtension(UnitTestProtoFile.DefaultFixed32Extension, 407U);
789      message.SetExtension(UnitTestProtoFile.DefaultFixed64Extension, 408UL);
790      message.SetExtension(UnitTestProtoFile.DefaultSfixed32Extension, 409);
791      message.SetExtension(UnitTestProtoFile.DefaultSfixed64Extension, 410L);
792      message.SetExtension(UnitTestProtoFile.DefaultFloatExtension, 411F);
793      message.SetExtension(UnitTestProtoFile.DefaultDoubleExtension, 412D);
794      message.SetExtension(UnitTestProtoFile.DefaultBoolExtension, false);
795      message.SetExtension(UnitTestProtoFile.DefaultStringExtension, "415");
796      message.SetExtension(UnitTestProtoFile.DefaultBytesExtension, ToBytes("416"));
797
798      message.SetExtension(UnitTestProtoFile.DefaultNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO);
799      message.SetExtension(UnitTestProtoFile.DefaultForeignEnumExtension, ForeignEnum.FOREIGN_FOO);
800      message.SetExtension(UnitTestProtoFile.DefaultImportEnumExtension, ImportEnum.IMPORT_FOO);
801
802      message.SetExtension(UnitTestProtoFile.DefaultStringPieceExtension, "424");
803      message.SetExtension(UnitTestProtoFile.DefaultCordExtension, "425");
804    }
805
806    internal static void ModifyRepeatedFields(TestAllTypes.Builder message) {
807      message.SetRepeatedInt32(1, 501);
808      message.SetRepeatedInt64(1, 502);
809      message.SetRepeatedUint32(1, 503);
810      message.SetRepeatedUint64(1, 504);
811      message.SetRepeatedSint32(1, 505);
812      message.SetRepeatedSint64(1, 506);
813      message.SetRepeatedFixed32(1, 507);
814      message.SetRepeatedFixed64(1, 508);
815      message.SetRepeatedSfixed32(1, 509);
816      message.SetRepeatedSfixed64(1, 510);
817      message.SetRepeatedFloat(1, 511);
818      message.SetRepeatedDouble(1, 512);
819      message.SetRepeatedBool(1, true);
820      message.SetRepeatedString(1, "515");
821      message.SetRepeatedBytes(1, ToBytes("516"));
822
823      message.SetRepeatedGroup(1, TestAllTypes.Types.RepeatedGroup.CreateBuilder().SetA(517).Build());
824      message.SetRepeatedNestedMessage(1, TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(518).Build());
825      message.SetRepeatedForeignMessage(1, ForeignMessage.CreateBuilder().SetC(519).Build());
826      message.SetRepeatedImportMessage(1, ImportMessage.CreateBuilder().SetD(520).Build());
827
828      message.SetRepeatedNestedEnum(1, TestAllTypes.Types.NestedEnum.FOO);
829      message.SetRepeatedForeignEnum(1, ForeignEnum.FOREIGN_FOO);
830      message.SetRepeatedImportEnum(1, ImportEnum.IMPORT_FOO);
831
832      message.SetRepeatedStringPiece(1, "524");
833      message.SetRepeatedCord(1, "525");
834    }
835
836    internal static void AssertRepeatedFieldsModified(TestAllTypes message) {
837      // ModifyRepeatedFields only sets the second repeated element of each
838      // field.  In addition to verifying this, we also verify that the first
839      // element and size were *not* modified.
840      Assert.AreEqual(2, message.RepeatedInt32Count);
841      Assert.AreEqual(2, message.RepeatedInt64Count);
842      Assert.AreEqual(2, message.RepeatedUint32Count);
843      Assert.AreEqual(2, message.RepeatedUint64Count);
844      Assert.AreEqual(2, message.RepeatedSint32Count);
845      Assert.AreEqual(2, message.RepeatedSint64Count);
846      Assert.AreEqual(2, message.RepeatedFixed32Count);
847      Assert.AreEqual(2, message.RepeatedFixed64Count);
848      Assert.AreEqual(2, message.RepeatedSfixed32Count);
849      Assert.AreEqual(2, message.RepeatedSfixed64Count);
850      Assert.AreEqual(2, message.RepeatedFloatCount);
851      Assert.AreEqual(2, message.RepeatedDoubleCount);
852      Assert.AreEqual(2, message.RepeatedBoolCount);
853      Assert.AreEqual(2, message.RepeatedStringCount);
854      Assert.AreEqual(2, message.RepeatedBytesCount);
855
856      Assert.AreEqual(2, message.RepeatedGroupCount);
857      Assert.AreEqual(2, message.RepeatedNestedMessageCount);
858      Assert.AreEqual(2, message.RepeatedForeignMessageCount);
859      Assert.AreEqual(2, message.RepeatedImportMessageCount);
860      Assert.AreEqual(2, message.RepeatedNestedEnumCount);
861      Assert.AreEqual(2, message.RepeatedForeignEnumCount);
862      Assert.AreEqual(2, message.RepeatedImportEnumCount);
863
864      Assert.AreEqual(2, message.RepeatedStringPieceCount);
865      Assert.AreEqual(2, message.RepeatedCordCount);
866
867      Assert.AreEqual(201, message.GetRepeatedInt32(0));
868      Assert.AreEqual(202L, message.GetRepeatedInt64(0));
869      Assert.AreEqual(203U, message.GetRepeatedUint32(0));
870      Assert.AreEqual(204UL, message.GetRepeatedUint64(0));
871      Assert.AreEqual(205, message.GetRepeatedSint32(0));
872      Assert.AreEqual(206L, message.GetRepeatedSint64(0));
873      Assert.AreEqual(207U, message.GetRepeatedFixed32(0));
874      Assert.AreEqual(208UL, message.GetRepeatedFixed64(0));
875      Assert.AreEqual(209, message.GetRepeatedSfixed32(0));
876      Assert.AreEqual(210L, message.GetRepeatedSfixed64(0));
877      Assert.AreEqual(211F, message.GetRepeatedFloat(0));
878      Assert.AreEqual(212D, message.GetRepeatedDouble(0));
879      Assert.AreEqual(true, message.GetRepeatedBool(0));
880      Assert.AreEqual("215", message.GetRepeatedString(0));
881      Assert.AreEqual(ToBytes("216"), message.GetRepeatedBytes(0));
882
883      Assert.AreEqual(217, message.GetRepeatedGroup(0).A);
884      Assert.AreEqual(218, message.GetRepeatedNestedMessage(0).Bb);
885      Assert.AreEqual(219, message.GetRepeatedForeignMessage(0).C);
886      Assert.AreEqual(220, message.GetRepeatedImportMessage(0).D);
887
888      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetRepeatedNestedEnum(0));
889      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetRepeatedForeignEnum(0));
890      Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetRepeatedImportEnum(0));
891
892      Assert.AreEqual("224", message.GetRepeatedStringPiece(0));
893      Assert.AreEqual("225", message.GetRepeatedCord(0));
894
895      // Actually verify the second (modified) elements now.
896      Assert.AreEqual(501, message.GetRepeatedInt32(1));
897      Assert.AreEqual(502L, message.GetRepeatedInt64(1));
898      Assert.AreEqual(503U, message.GetRepeatedUint32(1));
899      Assert.AreEqual(504UL, message.GetRepeatedUint64(1));
900      Assert.AreEqual(505, message.GetRepeatedSint32(1));
901      Assert.AreEqual(506L, message.GetRepeatedSint64(1));
902      Assert.AreEqual(507U, message.GetRepeatedFixed32(1));
903      Assert.AreEqual(508UL, message.GetRepeatedFixed64(1));
904      Assert.AreEqual(509, message.GetRepeatedSfixed32(1));
905      Assert.AreEqual(510L, message.GetRepeatedSfixed64(1));
906      Assert.AreEqual(511F, message.GetRepeatedFloat(1));
907      Assert.AreEqual(512D, message.GetRepeatedDouble(1));
908      Assert.AreEqual(true, message.GetRepeatedBool(1));
909      Assert.AreEqual("515", message.GetRepeatedString(1));
910      Assert.AreEqual(ToBytes("516"), message.GetRepeatedBytes(1));
911
912      Assert.AreEqual(517, message.GetRepeatedGroup(1).A);
913      Assert.AreEqual(518, message.GetRepeatedNestedMessage(1).Bb);
914      Assert.AreEqual(519, message.GetRepeatedForeignMessage(1).C);
915      Assert.AreEqual(520, message.GetRepeatedImportMessage(1).D);
916
917      Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.GetRepeatedNestedEnum(1));
918      Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.GetRepeatedForeignEnum(1));
919      Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetRepeatedImportEnum(1));
920
921      Assert.AreEqual("524", message.GetRepeatedStringPiece(1));
922      Assert.AreEqual("525", message.GetRepeatedCord(1));
923    }
924
925    /// <summary>
926    /// Helper to assert that sequences are equal.
927    /// </summary>
928    internal static void AssertEqual<T>(IEnumerable<T> first, IEnumerable<T> second) {
929      using (IEnumerator<T> firstEnumerator = first.GetEnumerator()) {
930        foreach (T secondElement in second) {
931          Assert.IsTrue(firstEnumerator.MoveNext(), "First enumerator ran out of elements too early.");
932          Assert.AreEqual(firstEnumerator.Current, secondElement);
933        }
934        Assert.IsFalse(firstEnumerator.MoveNext(), "Second enumerator ran out of elements too early.");
935      }
936    }
937
938    internal static void AssertEqualBytes(byte[] expected, byte[] actual) {
939      Assert.AreEqual(ByteString.CopyFrom(expected), ByteString.CopyFrom(actual));
940    }
941
942    internal static void AssertAllExtensionsSet(TestAllExtensions message) {
943      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalInt32Extension   ));
944      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalInt64Extension   ));
945      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalUint32Extension  ));
946      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalUint64Extension  ));
947      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalSint32Extension  ));
948      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalSint64Extension  ));
949      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalFixed32Extension ));
950      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalFixed64Extension ));
951      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalSfixed32Extension));
952      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalSfixed64Extension));
953      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalFloatExtension   ));
954      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalDoubleExtension  ));
955      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalBoolExtension    ));
956      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalStringExtension  ));
957      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalBytesExtension   ));
958
959      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalGroupExtension         ));
960      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalNestedMessageExtension ));
961      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalForeignMessageExtension));
962      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalImportMessageExtension ));
963
964      Assert.IsTrue(message.GetExtension(UnitTestProtoFile.OptionalGroupExtension         ).HasA);
965      Assert.IsTrue(message.GetExtension(UnitTestProtoFile.OptionalNestedMessageExtension ).HasBb);
966      Assert.IsTrue(message.GetExtension(UnitTestProtoFile.OptionalForeignMessageExtension).HasC);
967      Assert.IsTrue(message.GetExtension(UnitTestProtoFile.OptionalImportMessageExtension ).HasD);
968
969      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalNestedEnumExtension ));
970      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalForeignEnumExtension));
971      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalImportEnumExtension ));
972
973      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalStringPieceExtension));
974      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.OptionalCordExtension));
975
976      Assert.AreEqual(101  , message.GetExtension(UnitTestProtoFile.OptionalInt32Extension   ));
977      Assert.AreEqual(102L , message.GetExtension(UnitTestProtoFile.OptionalInt64Extension   ));
978      Assert.AreEqual(103U  , message.GetExtension(UnitTestProtoFile.OptionalUint32Extension  ));
979      Assert.AreEqual(104UL , message.GetExtension(UnitTestProtoFile.OptionalUint64Extension  ));
980      Assert.AreEqual(105  , message.GetExtension(UnitTestProtoFile.OptionalSint32Extension  ));
981      Assert.AreEqual(106L , message.GetExtension(UnitTestProtoFile.OptionalSint64Extension  ));
982      Assert.AreEqual(107U  , message.GetExtension(UnitTestProtoFile.OptionalFixed32Extension ));
983      Assert.AreEqual(108UL , message.GetExtension(UnitTestProtoFile.OptionalFixed64Extension ));
984      Assert.AreEqual(109  , message.GetExtension(UnitTestProtoFile.OptionalSfixed32Extension));
985      Assert.AreEqual(110L , message.GetExtension(UnitTestProtoFile.OptionalSfixed64Extension));
986      Assert.AreEqual(111F , message.GetExtension(UnitTestProtoFile.OptionalFloatExtension   ));
987      Assert.AreEqual(112D , message.GetExtension(UnitTestProtoFile.OptionalDoubleExtension  ));
988      Assert.AreEqual(true , message.GetExtension(UnitTestProtoFile.OptionalBoolExtension    ));
989      Assert.AreEqual("115", message.GetExtension(UnitTestProtoFile.OptionalStringExtension  ));
990      Assert.AreEqual(ToBytes("116"), message.GetExtension(UnitTestProtoFile.OptionalBytesExtension));
991
992      Assert.AreEqual(117, message.GetExtension(UnitTestProtoFile.OptionalGroupExtension         ).A);
993      Assert.AreEqual(118, message.GetExtension(UnitTestProtoFile.OptionalNestedMessageExtension ).Bb);
994      Assert.AreEqual(119, message.GetExtension(UnitTestProtoFile.OptionalForeignMessageExtension).C);
995      Assert.AreEqual(120, message.GetExtension(UnitTestProtoFile.OptionalImportMessageExtension ).D);
996
997      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ, message.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
998      Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(UnitTestProtoFile.OptionalForeignEnumExtension));
999      Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.GetExtension(UnitTestProtoFile.OptionalImportEnumExtension));
1000
1001      Assert.AreEqual("124", message.GetExtension(UnitTestProtoFile.OptionalStringPieceExtension));
1002      Assert.AreEqual("125", message.GetExtension(UnitTestProtoFile.OptionalCordExtension));
1003
1004      // -----------------------------------------------------------------
1005
1006      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension   ));
1007      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedInt64Extension   ));
1008      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedUint32Extension  ));
1009      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedUint64Extension  ));
1010      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSint32Extension  ));
1011      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSint64Extension  ));
1012      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedFixed32Extension ));
1013      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedFixed64Extension ));
1014      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSfixed32Extension));
1015      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSfixed64Extension));
1016      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedFloatExtension   ));
1017      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension  ));
1018      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedBoolExtension    ));
1019      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedStringExtension  ));
1020      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedBytesExtension   ));
1021
1022      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedGroupExtension         ));
1023      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedNestedMessageExtension ));
1024      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedForeignMessageExtension));
1025      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedImportMessageExtension ));
1026      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedNestedEnumExtension    ));
1027      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedForeignEnumExtension   ));
1028      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedImportEnumExtension    ));
1029
1030      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedStringPieceExtension));
1031      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedCordExtension));
1032
1033      Assert.AreEqual(201  , message.GetExtension(UnitTestProtoFile.RepeatedInt32Extension   , 0));
1034      Assert.AreEqual(202L , message.GetExtension(UnitTestProtoFile.RepeatedInt64Extension   , 0));
1035      Assert.AreEqual(203U  , message.GetExtension(UnitTestProtoFile.RepeatedUint32Extension  , 0));
1036      Assert.AreEqual(204UL , message.GetExtension(UnitTestProtoFile.RepeatedUint64Extension  , 0));
1037      Assert.AreEqual(205  , message.GetExtension(UnitTestProtoFile.RepeatedSint32Extension  , 0));
1038      Assert.AreEqual(206L , message.GetExtension(UnitTestProtoFile.RepeatedSint64Extension  , 0));
1039      Assert.AreEqual(207U  , message.GetExtension(UnitTestProtoFile.RepeatedFixed32Extension , 0));
1040      Assert.AreEqual(208UL , message.GetExtension(UnitTestProtoFile.RepeatedFixed64Extension , 0));
1041      Assert.AreEqual(209  , message.GetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 0));
1042      Assert.AreEqual(210L , message.GetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 0));
1043      Assert.AreEqual(211F , message.GetExtension(UnitTestProtoFile.RepeatedFloatExtension   , 0));
1044      Assert.AreEqual(212D , message.GetExtension(UnitTestProtoFile.RepeatedDoubleExtension  , 0));
1045      Assert.AreEqual(true , message.GetExtension(UnitTestProtoFile.RepeatedBoolExtension    , 0));
1046      Assert.AreEqual("215", message.GetExtension(UnitTestProtoFile.RepeatedStringExtension  , 0));
1047      Assert.AreEqual(ToBytes("216"), message.GetExtension(UnitTestProtoFile.RepeatedBytesExtension, 0));
1048
1049      Assert.AreEqual(217, message.GetExtension(UnitTestProtoFile.RepeatedGroupExtension         , 0).A);
1050      Assert.AreEqual(218, message.GetExtension(UnitTestProtoFile.RepeatedNestedMessageExtension , 0).Bb);
1051      Assert.AreEqual(219, message.GetExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, 0).C);
1052      Assert.AreEqual(220, message.GetExtension(UnitTestProtoFile.RepeatedImportMessageExtension , 0).D);
1053
1054      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 0));
1055      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 0));
1056      Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 0));
1057
1058      Assert.AreEqual("224", message.GetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 0));
1059      Assert.AreEqual("225", message.GetExtension(UnitTestProtoFile.RepeatedCordExtension, 0));
1060
1061      Assert.AreEqual(301  , message.GetExtension(UnitTestProtoFile.RepeatedInt32Extension   , 1));
1062      Assert.AreEqual(302L , message.GetExtension(UnitTestProtoFile.RepeatedInt64Extension   , 1));
1063      Assert.AreEqual(303U  , message.GetExtension(UnitTestProtoFile.RepeatedUint32Extension  , 1));
1064      Assert.AreEqual(304UL , message.GetExtension(UnitTestProtoFile.RepeatedUint64Extension  , 1));
1065      Assert.AreEqual(305  , message.GetExtension(UnitTestProtoFile.RepeatedSint32Extension  , 1));
1066      Assert.AreEqual(306L , message.GetExtension(UnitTestProtoFile.RepeatedSint64Extension  , 1));
1067      Assert.AreEqual(307U  , message.GetExtension(UnitTestProtoFile.RepeatedFixed32Extension , 1));
1068      Assert.AreEqual(308UL , message.GetExtension(UnitTestProtoFile.RepeatedFixed64Extension , 1));
1069      Assert.AreEqual(309  , message.GetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 1));
1070      Assert.AreEqual(310L , message.GetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 1));
1071      Assert.AreEqual(311F , message.GetExtension(UnitTestProtoFile.RepeatedFloatExtension   , 1));
1072      Assert.AreEqual(312D , message.GetExtension(UnitTestProtoFile.RepeatedDoubleExtension  , 1));
1073      Assert.AreEqual(false, message.GetExtension(UnitTestProtoFile.RepeatedBoolExtension    , 1));
1074      Assert.AreEqual("315", message.GetExtension(UnitTestProtoFile.RepeatedStringExtension  , 1));
1075      Assert.AreEqual(ToBytes("316"), message.GetExtension(UnitTestProtoFile.RepeatedBytesExtension, 1));
1076
1077      Assert.AreEqual(317, message.GetExtension(UnitTestProtoFile.RepeatedGroupExtension         , 1).A);
1078      Assert.AreEqual(318, message.GetExtension(UnitTestProtoFile.RepeatedNestedMessageExtension , 1).Bb);
1079      Assert.AreEqual(319, message.GetExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, 1).C);
1080      Assert.AreEqual(320, message.GetExtension(UnitTestProtoFile.RepeatedImportMessageExtension , 1).D);
1081
1082      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ, message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 1));
1083      Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 1));
1084      Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.GetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 1));
1085
1086      Assert.AreEqual("324", message.GetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 1));
1087      Assert.AreEqual("325", message.GetExtension(UnitTestProtoFile.RepeatedCordExtension, 1));
1088
1089      // -----------------------------------------------------------------
1090
1091      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultInt32Extension   ));
1092      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultInt64Extension   ));
1093      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultUint32Extension  ));
1094      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultUint64Extension  ));
1095      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultSint32Extension  ));
1096      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultSint64Extension  ));
1097      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultFixed32Extension ));
1098      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultFixed64Extension ));
1099      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultSfixed32Extension));
1100      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultSfixed64Extension));
1101      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultFloatExtension   ));
1102      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultDoubleExtension  ));
1103      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultBoolExtension    ));
1104      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultStringExtension  ));
1105      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultBytesExtension   ));
1106
1107      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultNestedEnumExtension ));
1108      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultForeignEnumExtension));
1109      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultImportEnumExtension ));
1110
1111      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultStringPieceExtension));
1112      Assert.IsTrue(message.HasExtension(UnitTestProtoFile.DefaultCordExtension));
1113
1114      Assert.AreEqual(401  , message.GetExtension(UnitTestProtoFile.DefaultInt32Extension   ));
1115      Assert.AreEqual(402L , message.GetExtension(UnitTestProtoFile.DefaultInt64Extension   ));
1116      Assert.AreEqual(403U  , message.GetExtension(UnitTestProtoFile.DefaultUint32Extension  ));
1117      Assert.AreEqual(404UL , message.GetExtension(UnitTestProtoFile.DefaultUint64Extension  ));
1118      Assert.AreEqual(405  , message.GetExtension(UnitTestProtoFile.DefaultSint32Extension  ));
1119      Assert.AreEqual(406L , message.GetExtension(UnitTestProtoFile.DefaultSint64Extension  ));
1120      Assert.AreEqual(407U  , message.GetExtension(UnitTestProtoFile.DefaultFixed32Extension ));
1121      Assert.AreEqual(408UL , message.GetExtension(UnitTestProtoFile.DefaultFixed64Extension ));
1122      Assert.AreEqual(409  , message.GetExtension(UnitTestProtoFile.DefaultSfixed32Extension));
1123      Assert.AreEqual(410L , message.GetExtension(UnitTestProtoFile.DefaultSfixed64Extension));
1124      Assert.AreEqual(411F , message.GetExtension(UnitTestProtoFile.DefaultFloatExtension   ));
1125      Assert.AreEqual(412D , message.GetExtension(UnitTestProtoFile.DefaultDoubleExtension  ));
1126      Assert.AreEqual(false, message.GetExtension(UnitTestProtoFile.DefaultBoolExtension    ));
1127      Assert.AreEqual("415", message.GetExtension(UnitTestProtoFile.DefaultStringExtension  ));
1128      Assert.AreEqual(ToBytes("416"), message.GetExtension(UnitTestProtoFile.DefaultBytesExtension));
1129
1130      Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.GetExtension(UnitTestProtoFile.DefaultNestedEnumExtension ));
1131      Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.GetExtension(UnitTestProtoFile.DefaultForeignEnumExtension));
1132      Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetExtension(UnitTestProtoFile.DefaultImportEnumExtension));
1133
1134      Assert.AreEqual("424", message.GetExtension(UnitTestProtoFile.DefaultStringPieceExtension));
1135      Assert.AreEqual("425", message.GetExtension(UnitTestProtoFile.DefaultCordExtension));
1136    }
1137
1138    /// <summary>
1139    /// Modifies the repeated extensions of the given message to contain the values
1140    /// expected by AssertRepeatedExtensionsModified.
1141    /// </summary>
1142    internal static void ModifyRepeatedExtensions(TestAllExtensions.Builder message) {
1143      message.SetExtension(UnitTestProtoFile.RepeatedInt32Extension, 1, 501);
1144      message.SetExtension(UnitTestProtoFile.RepeatedInt64Extension, 1, 502L);
1145      message.SetExtension(UnitTestProtoFile.RepeatedUint32Extension, 1, 503U);
1146      message.SetExtension(UnitTestProtoFile.RepeatedUint64Extension, 1, 504UL);
1147      message.SetExtension(UnitTestProtoFile.RepeatedSint32Extension, 1, 505);
1148      message.SetExtension(UnitTestProtoFile.RepeatedSint64Extension, 1, 506L);
1149      message.SetExtension(UnitTestProtoFile.RepeatedFixed32Extension, 1, 507U);
1150      message.SetExtension(UnitTestProtoFile.RepeatedFixed64Extension, 1, 508UL);
1151      message.SetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 1, 509);
1152      message.SetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 1, 510L);
1153      message.SetExtension(UnitTestProtoFile.RepeatedFloatExtension, 1, 511F);
1154      message.SetExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1, 512D);
1155      message.SetExtension(UnitTestProtoFile.RepeatedBoolExtension, 1, true);
1156      message.SetExtension(UnitTestProtoFile.RepeatedStringExtension, 1, "515");
1157      message.SetExtension(UnitTestProtoFile.RepeatedBytesExtension, 1, ToBytes("516"));
1158
1159      message.SetExtension(UnitTestProtoFile.RepeatedGroupExtension, 1, RepeatedGroup_extension.CreateBuilder().SetA(517).Build());
1160      message.SetExtension(UnitTestProtoFile.RepeatedNestedMessageExtension, 1, TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(518).Build());
1161      message.SetExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, 1, ForeignMessage.CreateBuilder().SetC(519).Build());
1162      message.SetExtension(UnitTestProtoFile.RepeatedImportMessageExtension, 1, ImportMessage.CreateBuilder().SetD(520).Build());
1163
1164      message.SetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 1, TestAllTypes.Types.NestedEnum.FOO);
1165      message.SetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 1, ForeignEnum.FOREIGN_FOO);
1166      message.SetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 1, ImportEnum.IMPORT_FOO);
1167
1168      message.SetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 1, "524");
1169      message.SetExtension(UnitTestProtoFile.RepeatedCordExtension, 1, "525");
1170    }
1171
1172    /// <summary>
1173    /// Asserts that all repeated extensions are set to the values assigned by
1174    /// SetAllExtensions follwed by ModifyRepeatedExtensions.
1175    /// </summary>
1176    internal static void AssertRepeatedExtensionsModified(TestAllExtensions message) {
1177      // ModifyRepeatedFields only sets the second repeated element of each
1178      // field.  In addition to verifying this, we also verify that the first
1179      // element and size were *not* modified.
1180      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
1181      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedInt64Extension));
1182      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedUint32Extension));
1183      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedUint64Extension));
1184      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSint32Extension));
1185      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSint64Extension));
1186      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedFixed32Extension));
1187      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedFixed64Extension));
1188      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSfixed32Extension));
1189      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedSfixed64Extension));
1190      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedFloatExtension));
1191      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
1192      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedBoolExtension));
1193      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedStringExtension));
1194      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedBytesExtension));
1195
1196      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedGroupExtension));
1197      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedNestedMessageExtension));
1198      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedForeignMessageExtension));
1199      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedImportMessageExtension));
1200      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedNestedEnumExtension));
1201      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedForeignEnumExtension));
1202      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedImportEnumExtension));
1203
1204      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedStringPieceExtension));
1205      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.RepeatedCordExtension));
1206
1207      Assert.AreEqual(201, message.GetExtension(UnitTestProtoFile.RepeatedInt32Extension, 0));
1208      Assert.AreEqual(202L, message.GetExtension(UnitTestProtoFile.RepeatedInt64Extension, 0));
1209      Assert.AreEqual(203U, message.GetExtension(UnitTestProtoFile.RepeatedUint32Extension, 0));
1210      Assert.AreEqual(204UL, message.GetExtension(UnitTestProtoFile.RepeatedUint64Extension, 0));
1211      Assert.AreEqual(205, message.GetExtension(UnitTestProtoFile.RepeatedSint32Extension, 0));
1212      Assert.AreEqual(206L, message.GetExtension(UnitTestProtoFile.RepeatedSint64Extension, 0));
1213      Assert.AreEqual(207U, message.GetExtension(UnitTestProtoFile.RepeatedFixed32Extension, 0));
1214      Assert.AreEqual(208UL, message.GetExtension(UnitTestProtoFile.RepeatedFixed64Extension, 0));
1215      Assert.AreEqual(209, message.GetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 0));
1216      Assert.AreEqual(210L, message.GetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 0));
1217      Assert.AreEqual(211F, message.GetExtension(UnitTestProtoFile.RepeatedFloatExtension, 0));
1218      Assert.AreEqual(212D, message.GetExtension(UnitTestProtoFile.RepeatedDoubleExtension, 0));
1219      Assert.AreEqual(true, message.GetExtension(UnitTestProtoFile.RepeatedBoolExtension, 0));
1220      Assert.AreEqual("215", message.GetExtension(UnitTestProtoFile.RepeatedStringExtension, 0));
1221      Assert.AreEqual(ToBytes("216"), message.GetExtension(UnitTestProtoFile.RepeatedBytesExtension, 0));
1222
1223      Assert.AreEqual(217, message.GetExtension(UnitTestProtoFile.RepeatedGroupExtension, 0).A);
1224      Assert.AreEqual(218, message.GetExtension(UnitTestProtoFile.RepeatedNestedMessageExtension, 0).Bb);
1225      Assert.AreEqual(219, message.GetExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, 0).C);
1226      Assert.AreEqual(220, message.GetExtension(UnitTestProtoFile.RepeatedImportMessageExtension, 0).D);
1227
1228      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 0));
1229      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 0));
1230      Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 0));
1231
1232      Assert.AreEqual("224", message.GetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 0));
1233      Assert.AreEqual("225", message.GetExtension(UnitTestProtoFile.RepeatedCordExtension, 0));
1234
1235      // Actually verify the second (modified) elements now.
1236      Assert.AreEqual(501, message.GetExtension(UnitTestProtoFile.RepeatedInt32Extension, 1));
1237      Assert.AreEqual(502L, message.GetExtension(UnitTestProtoFile.RepeatedInt64Extension, 1));
1238      Assert.AreEqual(503U, message.GetExtension(UnitTestProtoFile.RepeatedUint32Extension, 1));
1239      Assert.AreEqual(504UL, message.GetExtension(UnitTestProtoFile.RepeatedUint64Extension, 1));
1240      Assert.AreEqual(505, message.GetExtension(UnitTestProtoFile.RepeatedSint32Extension, 1));
1241      Assert.AreEqual(506L, message.GetExtension(UnitTestProtoFile.RepeatedSint64Extension, 1));
1242      Assert.AreEqual(507U, message.GetExtension(UnitTestProtoFile.RepeatedFixed32Extension, 1));
1243      Assert.AreEqual(508UL, message.GetExtension(UnitTestProtoFile.RepeatedFixed64Extension, 1));
1244      Assert.AreEqual(509, message.GetExtension(UnitTestProtoFile.RepeatedSfixed32Extension, 1));
1245      Assert.AreEqual(510L, message.GetExtension(UnitTestProtoFile.RepeatedSfixed64Extension, 1));
1246      Assert.AreEqual(511F, message.GetExtension(UnitTestProtoFile.RepeatedFloatExtension, 1));
1247      Assert.AreEqual(512D, message.GetExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1));
1248      Assert.AreEqual(true, message.GetExtension(UnitTestProtoFile.RepeatedBoolExtension, 1));
1249      Assert.AreEqual("515", message.GetExtension(UnitTestProtoFile.RepeatedStringExtension, 1));
1250      Assert.AreEqual(ToBytes("516"), message.GetExtension(UnitTestProtoFile.RepeatedBytesExtension, 1));
1251
1252      Assert.AreEqual(517, message.GetExtension(UnitTestProtoFile.RepeatedGroupExtension, 1).A);
1253      Assert.AreEqual(518, message.GetExtension(UnitTestProtoFile.RepeatedNestedMessageExtension, 1).Bb);
1254      Assert.AreEqual(519, message.GetExtension(UnitTestProtoFile.RepeatedForeignMessageExtension, 1).C);
1255      Assert.AreEqual(520, message.GetExtension(UnitTestProtoFile.RepeatedImportMessageExtension, 1).D);
1256
1257      Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension, 1));
1258      Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.GetExtension(UnitTestProtoFile.RepeatedForeignEnumExtension, 1));
1259      Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetExtension(UnitTestProtoFile.RepeatedImportEnumExtension, 1));
1260
1261      Assert.AreEqual("524", message.GetExtension(UnitTestProtoFile.RepeatedStringPieceExtension, 1));
1262      Assert.AreEqual("525", message.GetExtension(UnitTestProtoFile.RepeatedCordExtension, 1));
1263    }
1264
1265    internal static void AssertExtensionsClear(TestAllExtensions message) {
1266      // HasBlah() should initially be false for all optional fields.
1267      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
1268      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalInt64Extension));
1269      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalUint32Extension));
1270      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalUint64Extension));
1271      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalSint32Extension));
1272      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalSint64Extension));
1273      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalFixed32Extension));
1274      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalFixed64Extension));
1275      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalSfixed32Extension));
1276      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalSfixed64Extension));
1277      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalFloatExtension));
1278      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalDoubleExtension));
1279      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalBoolExtension));
1280      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalStringExtension));
1281      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalBytesExtension));
1282
1283      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalGroupExtension));
1284      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalNestedMessageExtension));
1285      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalForeignMessageExtension));
1286      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalImportMessageExtension));
1287
1288      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
1289      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalForeignEnumExtension));
1290      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalImportEnumExtension));
1291
1292      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalStringPieceExtension));
1293      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.OptionalCordExtension));
1294
1295      // Optional fields without defaults are set to zero or something like it.
1296      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
1297      Assert.AreEqual(0L, message.GetExtension(UnitTestProtoFile.OptionalInt64Extension));
1298      Assert.AreEqual(0U, message.GetExtension(UnitTestProtoFile.OptionalUint32Extension));
1299      Assert.AreEqual(0UL, message.GetExtension(UnitTestProtoFile.OptionalUint64Extension));
1300      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalSint32Extension));
1301      Assert.AreEqual(0L, message.GetExtension(UnitTestProtoFile.OptionalSint64Extension));
1302      Assert.AreEqual(0U, message.GetExtension(UnitTestProtoFile.OptionalFixed32Extension));
1303      Assert.AreEqual(0UL, message.GetExtension(UnitTestProtoFile.OptionalFixed64Extension));
1304      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalSfixed32Extension));
1305      Assert.AreEqual(0L, message.GetExtension(UnitTestProtoFile.OptionalSfixed64Extension));
1306      Assert.AreEqual(0F, message.GetExtension(UnitTestProtoFile.OptionalFloatExtension));
1307      Assert.AreEqual(0D, message.GetExtension(UnitTestProtoFile.OptionalDoubleExtension));
1308      Assert.AreEqual(false, message.GetExtension(UnitTestProtoFile.OptionalBoolExtension));
1309      Assert.AreEqual("", message.GetExtension(UnitTestProtoFile.OptionalStringExtension));
1310      Assert.AreEqual(ByteString.Empty, message.GetExtension(UnitTestProtoFile.OptionalBytesExtension));
1311
1312      // Embedded messages should also be clear.
1313      Assert.IsFalse(message.GetExtension(UnitTestProtoFile.OptionalGroupExtension).HasA);
1314      Assert.IsFalse(message.GetExtension(UnitTestProtoFile.OptionalNestedMessageExtension).HasBb);
1315      Assert.IsFalse(message.GetExtension(UnitTestProtoFile.OptionalForeignMessageExtension).HasC);
1316      Assert.IsFalse(message.GetExtension(UnitTestProtoFile.OptionalImportMessageExtension).HasD);
1317
1318      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalGroupExtension).A);
1319      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalNestedMessageExtension).Bb);
1320      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalForeignMessageExtension).C);
1321      Assert.AreEqual(0, message.GetExtension(UnitTestProtoFile.OptionalImportMessageExtension).D);
1322
1323      // Enums without defaults are set to the first value in the enum.
1324      Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
1325      Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.GetExtension(UnitTestProtoFile.OptionalForeignEnumExtension));
1326      Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetExtension(UnitTestProtoFile.OptionalImportEnumExtension));
1327
1328      Assert.AreEqual("", message.GetExtension(UnitTestProtoFile.OptionalStringPieceExtension));
1329      Assert.AreEqual("", message.GetExtension(UnitTestProtoFile.OptionalCordExtension));
1330
1331      // Repeated fields are empty.
1332      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
1333      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedInt64Extension));
1334      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedUint32Extension));
1335      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedUint64Extension));
1336      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedSint32Extension));
1337      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedSint64Extension));
1338      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedFixed32Extension));
1339      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedFixed64Extension));
1340      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedSfixed32Extension));
1341      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedSfixed64Extension));
1342      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedFloatExtension));
1343      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
1344      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedBoolExtension));
1345      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedStringExtension));
1346      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedBytesExtension));
1347
1348      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedGroupExtension));
1349      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedNestedMessageExtension));
1350      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedForeignMessageExtension));
1351      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedImportMessageExtension));
1352      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedNestedEnumExtension));
1353      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedForeignEnumExtension));
1354      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedImportEnumExtension));
1355
1356      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedStringPieceExtension));
1357      Assert.AreEqual(0, message.GetExtensionCount(UnitTestProtoFile.RepeatedCordExtension));
1358
1359      // HasBlah() should also be false for all default fields.
1360      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultInt32Extension));
1361      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultInt64Extension));
1362      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultUint32Extension));
1363      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultUint64Extension));
1364      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultSint32Extension));
1365      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultSint64Extension));
1366      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultFixed32Extension));
1367      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultFixed64Extension));
1368      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultSfixed32Extension));
1369      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultSfixed64Extension));
1370      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultFloatExtension));
1371      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultDoubleExtension));
1372      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultBoolExtension));
1373      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultStringExtension));
1374      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultBytesExtension));
1375
1376      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultNestedEnumExtension));
1377      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultForeignEnumExtension));
1378      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultImportEnumExtension));
1379
1380      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultStringPieceExtension));
1381      Assert.IsFalse(message.HasExtension(UnitTestProtoFile.DefaultCordExtension));
1382
1383      // Fields with defaults have their default values (duh).
1384      Assert.AreEqual(41, message.GetExtension(UnitTestProtoFile.DefaultInt32Extension));
1385      Assert.AreEqual(42L, message.GetExtension(UnitTestProtoFile.DefaultInt64Extension));
1386      Assert.AreEqual(43U, message.GetExtension(UnitTestProtoFile.DefaultUint32Extension));
1387      Assert.AreEqual(44UL, message.GetExtension(UnitTestProtoFile.DefaultUint64Extension));
1388      Assert.AreEqual(-45, message.GetExtension(UnitTestProtoFile.DefaultSint32Extension));
1389      Assert.AreEqual(46L, message.GetExtension(UnitTestProtoFile.DefaultSint64Extension));
1390      Assert.AreEqual(47U, message.GetExtension(UnitTestProtoFile.DefaultFixed32Extension));
1391      Assert.AreEqual(48UL, message.GetExtension(UnitTestProtoFile.DefaultFixed64Extension));
1392      Assert.AreEqual(49, message.GetExtension(UnitTestProtoFile.DefaultSfixed32Extension));
1393      Assert.AreEqual(-50L, message.GetExtension(UnitTestProtoFile.DefaultSfixed64Extension));
1394      Assert.AreEqual(51.5F, message.GetExtension(UnitTestProtoFile.DefaultFloatExtension));
1395      Assert.AreEqual(52e3D, message.GetExtension(UnitTestProtoFile.DefaultDoubleExtension));
1396      Assert.AreEqual(true, message.GetExtension(UnitTestProtoFile.DefaultBoolExtension));
1397      Assert.AreEqual("hello", message.GetExtension(UnitTestProtoFile.DefaultStringExtension));
1398      Assert.AreEqual(ToBytes("world"), message.GetExtension(UnitTestProtoFile.DefaultBytesExtension));
1399
1400      Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetExtension(UnitTestProtoFile.DefaultNestedEnumExtension));
1401      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetExtension(UnitTestProtoFile.DefaultForeignEnumExtension));
1402      Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetExtension(UnitTestProtoFile.DefaultImportEnumExtension));
1403
1404      Assert.AreEqual("abc", message.GetExtension(UnitTestProtoFile.DefaultStringPieceExtension));
1405      Assert.AreEqual("123", message.GetExtension(UnitTestProtoFile.DefaultCordExtension));
1406    }
1407
1408    /// <summary>
1409    /// Set every field of the specified message to a unique value.
1410    /// </summary>
1411    public static void SetPackedFields(TestPackedTypes.Builder message) {
1412      message.AddPackedInt32(601);
1413      message.AddPackedInt64(602);
1414      message.AddPackedUint32(603);
1415      message.AddPackedUint64(604);
1416      message.AddPackedSint32(605);
1417      message.AddPackedSint64(606);
1418      message.AddPackedFixed32(607);
1419      message.AddPackedFixed64(608);
1420      message.AddPackedSfixed32(609);
1421      message.AddPackedSfixed64(610);
1422      message.AddPackedFloat(611);
1423      message.AddPackedDouble(612);
1424      message.AddPackedBool(true);
1425      message.AddPackedEnum(ForeignEnum.FOREIGN_BAR);
1426      // Add a second one of each field.
1427      message.AddPackedInt32(701);
1428      message.AddPackedInt64(702);
1429      message.AddPackedUint32(703);
1430      message.AddPackedUint64(704);
1431      message.AddPackedSint32(705);
1432      message.AddPackedSint64(706);
1433      message.AddPackedFixed32(707);
1434      message.AddPackedFixed64(708);
1435      message.AddPackedSfixed32(709);
1436      message.AddPackedSfixed64(710);
1437      message.AddPackedFloat(711);
1438      message.AddPackedDouble(712);
1439      message.AddPackedBool(false);
1440      message.AddPackedEnum(ForeignEnum.FOREIGN_BAZ);
1441    }
1442
1443    /// <summary>
1444    /// Asserts that all the fields of the specified message are set to the values assigned
1445    /// in SetPackedFields.
1446    /// </summary>
1447    public static void AssertPackedFieldsSet(TestPackedTypes message) {
1448      Assert.AreEqual(2, message.PackedInt32Count);
1449      Assert.AreEqual(2, message.PackedInt64Count);
1450      Assert.AreEqual(2, message.PackedUint32Count);
1451      Assert.AreEqual(2, message.PackedUint64Count);
1452      Assert.AreEqual(2, message.PackedSint32Count);
1453      Assert.AreEqual(2, message.PackedSint64Count);
1454      Assert.AreEqual(2, message.PackedFixed32Count);
1455      Assert.AreEqual(2, message.PackedFixed64Count);
1456      Assert.AreEqual(2, message.PackedSfixed32Count);
1457      Assert.AreEqual(2, message.PackedSfixed64Count);
1458      Assert.AreEqual(2, message.PackedFloatCount);
1459      Assert.AreEqual(2, message.PackedDoubleCount);
1460      Assert.AreEqual(2, message.PackedBoolCount);
1461      Assert.AreEqual(2, message.PackedEnumCount);
1462      Assert.AreEqual(601, message.GetPackedInt32(0));
1463      Assert.AreEqual(602, message.GetPackedInt64(0));
1464      Assert.AreEqual(603, message.GetPackedUint32(0));
1465      Assert.AreEqual(604, message.GetPackedUint64(0));
1466      Assert.AreEqual(605, message.GetPackedSint32(0));
1467      Assert.AreEqual(606, message.GetPackedSint64(0));
1468      Assert.AreEqual(607, message.GetPackedFixed32(0));
1469      Assert.AreEqual(608, message.GetPackedFixed64(0));
1470      Assert.AreEqual(609, message.GetPackedSfixed32(0));
1471      Assert.AreEqual(610, message.GetPackedSfixed64(0));
1472      Assert.AreEqual(611, message.GetPackedFloat(0), 0.0);
1473      Assert.AreEqual(612, message.GetPackedDouble(0), 0.0);
1474      Assert.AreEqual(true, message.GetPackedBool(0));
1475      Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetPackedEnum(0));
1476      Assert.AreEqual(701, message.GetPackedInt32(1));
1477      Assert.AreEqual(702, message.GetPackedInt64(1));
1478      Assert.AreEqual(703, message.GetPackedUint32(1));
1479      Assert.AreEqual(704, message.GetPackedUint64(1));
1480      Assert.AreEqual(705, message.GetPackedSint32(1));
1481      Assert.AreEqual(706, message.GetPackedSint64(1));
1482      Assert.AreEqual(707, message.GetPackedFixed32(1));
1483      Assert.AreEqual(708, message.GetPackedFixed64(1));
1484      Assert.AreEqual(709, message.GetPackedSfixed32(1));
1485      Assert.AreEqual(710, message.GetPackedSfixed64(1));
1486      Assert.AreEqual(711, message.GetPackedFloat(1), 0.0);
1487      Assert.AreEqual(712, message.GetPackedDouble(1), 0.0);
1488      Assert.AreEqual(false, message.GetPackedBool(1));
1489      Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetPackedEnum(1));
1490    }
1491
1492    public static void SetPackedExtensions(TestPackedExtensions.Builder message) {
1493      message.AddExtension(UnitTestProtoFile.PackedInt32Extension, 601);
1494      message.AddExtension(UnitTestProtoFile.PackedInt64Extension, 602L);
1495      message.AddExtension(UnitTestProtoFile.PackedUint32Extension, 603U);
1496      message.AddExtension(UnitTestProtoFile.PackedUint64Extension, 604UL);
1497      message.AddExtension(UnitTestProtoFile.PackedSint32Extension, 605);
1498      message.AddExtension(UnitTestProtoFile.PackedSint64Extension, 606L);
1499      message.AddExtension(UnitTestProtoFile.PackedFixed32Extension, 607U);
1500      message.AddExtension(UnitTestProtoFile.PackedFixed64Extension, 608UL);
1501      message.AddExtension(UnitTestProtoFile.PackedSfixed32Extension, 609);
1502      message.AddExtension(UnitTestProtoFile.PackedSfixed64Extension, 610L);
1503      message.AddExtension(UnitTestProtoFile.PackedFloatExtension, 611F);
1504      message.AddExtension(UnitTestProtoFile.PackedDoubleExtension, 612D);
1505      message.AddExtension(UnitTestProtoFile.PackedBoolExtension, true);
1506      message.AddExtension(UnitTestProtoFile.PackedEnumExtension, ForeignEnum.FOREIGN_BAR);
1507      // Add a second one of each field.
1508      message.AddExtension(UnitTestProtoFile.PackedInt32Extension, 701);
1509      message.AddExtension(UnitTestProtoFile.PackedInt64Extension, 702L);
1510      message.AddExtension(UnitTestProtoFile.PackedUint32Extension, 703U);
1511      message.AddExtension(UnitTestProtoFile.PackedUint64Extension, 704UL);
1512      message.AddExtension(UnitTestProtoFile.PackedSint32Extension, 705);
1513      message.AddExtension(UnitTestProtoFile.PackedSint64Extension, 706L);
1514      message.AddExtension(UnitTestProtoFile.PackedFixed32Extension, 707U);
1515      message.AddExtension(UnitTestProtoFile.PackedFixed64Extension, 708UL);
1516      message.AddExtension(UnitTestProtoFile.PackedSfixed32Extension, 709);
1517      message.AddExtension(UnitTestProtoFile.PackedSfixed64Extension, 710L);
1518      message.AddExtension(UnitTestProtoFile.PackedFloatExtension, 711F);
1519      message.AddExtension(UnitTestProtoFile.PackedDoubleExtension, 712D);
1520      message.AddExtension(UnitTestProtoFile.PackedBoolExtension, false);
1521      message.AddExtension(UnitTestProtoFile.PackedEnumExtension, ForeignEnum.FOREIGN_BAZ);
1522    }
1523
1524    public static void AssertPackedExtensionsSet(TestPackedExtensions message) {
1525      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedInt32Extension));
1526      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedInt64Extension));
1527      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedUint32Extension));
1528      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedUint64Extension));
1529      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedSint32Extension));
1530      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedSint64Extension));
1531      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedFixed32Extension));
1532      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedFixed64Extension));
1533      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedSfixed32Extension));
1534      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedSfixed64Extension));
1535      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedFloatExtension));
1536      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedDoubleExtension));
1537      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedBoolExtension));
1538      Assert.AreEqual(2, message.GetExtensionCount(UnitTestProtoFile.PackedEnumExtension));
1539      Assert.AreEqual(601, message.GetExtension(UnitTestProtoFile.PackedInt32Extension, 0));
1540      Assert.AreEqual(602L, message.GetExtension(UnitTestProtoFile.PackedInt64Extension, 0));
1541      Assert.AreEqual(603, message.GetExtension(UnitTestProtoFile.PackedUint32Extension, 0));
1542      Assert.AreEqual(604L, message.GetExtension(UnitTestProtoFile.PackedUint64Extension, 0));
1543      Assert.AreEqual(605, message.GetExtension(UnitTestProtoFile.PackedSint32Extension, 0));
1544      Assert.AreEqual(606L, message.GetExtension(UnitTestProtoFile.PackedSint64Extension, 0));
1545      Assert.AreEqual(607, message.GetExtension(UnitTestProtoFile.PackedFixed32Extension, 0));
1546      Assert.AreEqual(608L, message.GetExtension(UnitTestProtoFile.PackedFixed64Extension, 0));
1547      Assert.AreEqual(609, message.GetExtension(UnitTestProtoFile.PackedSfixed32Extension, 0));
1548      Assert.AreEqual(610L, message.GetExtension(UnitTestProtoFile.PackedSfixed64Extension, 0));
1549      Assert.AreEqual(611F, message.GetExtension(UnitTestProtoFile.PackedFloatExtension, 0));
1550      Assert.AreEqual(612D, message.GetExtension(UnitTestProtoFile.PackedDoubleExtension, 0));
1551      Assert.AreEqual(true, message.GetExtension(UnitTestProtoFile.PackedBoolExtension, 0));
1552      Assert.AreEqual(ForeignEnum.FOREIGN_BAR,
1553                            message.GetExtension(UnitTestProtoFile.PackedEnumExtension, 0));
1554      Assert.AreEqual(701, message.GetExtension(UnitTestProtoFile.PackedInt32Extension, 1));
1555      Assert.AreEqual(702L, message.GetExtension(UnitTestProtoFile.PackedInt64Extension, 1));
1556      Assert.AreEqual(703, message.GetExtension(UnitTestProtoFile.PackedUint32Extension, 1));
1557      Assert.AreEqual(704L, message.GetExtension(UnitTestProtoFile.PackedUint64Extension, 1));
1558      Assert.AreEqual(705, message.GetExtension(UnitTestProtoFile.PackedSint32Extension, 1));
1559      Assert.AreEqual(706L, message.GetExtension(UnitTestProtoFile.PackedSint64Extension, 1));
1560      Assert.AreEqual(707, message.GetExtension(UnitTestProtoFile.PackedFixed32Extension, 1));
1561      Assert.AreEqual(708L, message.GetExtension(UnitTestProtoFile.PackedFixed64Extension, 1));
1562      Assert.AreEqual(709, message.GetExtension(UnitTestProtoFile.PackedSfixed32Extension, 1));
1563      Assert.AreEqual(710L, message.GetExtension(UnitTestProtoFile.PackedSfixed64Extension, 1));
1564      Assert.AreEqual(711F, message.GetExtension(UnitTestProtoFile.PackedFloatExtension, 1));
1565      Assert.AreEqual(712D, message.GetExtension(UnitTestProtoFile.PackedDoubleExtension, 1));
1566      Assert.AreEqual(false, message.GetExtension(UnitTestProtoFile.PackedBoolExtension, 1));
1567      Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(UnitTestProtoFile.PackedEnumExtension, 1));
1568    }
1569
1570    private static ByteString goldenPackedFieldsMessage = null;
1571
1572    /// <summary>
1573    /// Get the bytes of the "golden packed fields message".  This is a serialized
1574    /// TestPackedTypes with all fields set as they would be by SetPackedFields,
1575    /// but it is loaded from a file on disk rather than generated dynamically.
1576    /// The file is actually generated by C++ code, so testing against it verifies compatibility
1577    /// with C++.
1578    /// </summary>
1579    public static ByteString GetGoldenPackedFieldsMessage() {
1580      if (goldenPackedFieldsMessage == null) {
1581        goldenPackedFieldsMessage = ReadBytesFromFile("golden_packed_fields_message");
1582      }
1583      return goldenPackedFieldsMessage;
1584    }
1585
1586    private static readonly string[] TestCultures = { "en-US", "en-GB", "fr-FR", "de-DE" };
1587
1588    public static void TestInMultipleCultures(Action test) {
1589      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
1590      foreach (string culture in TestCultures) {
1591        try {
1592          Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
1593          test();
1594        } finally {
1595          Thread.CurrentThread.CurrentCulture = originalCulture;
1596        }
1597      }
1598    }
1599   
1600    /// <summary>
1601    /// Helper to construct a byte array from a bunch of bytes.
1602    /// </summary>
1603    internal static byte[] Bytes(params byte[] bytesAsInts) {
1604      byte[] bytes = new byte[bytesAsInts.Length];
1605      for (int i = 0; i < bytesAsInts.Length; i++) {
1606        bytes[i] = (byte)bytesAsInts[i];
1607      }
1608      return bytes;
1609    }
1610
1611    internal static void AssertArgumentNullException(Action action) {
1612      try {
1613        action();
1614        Assert.Fail("Exception was not thrown");
1615      } catch (ArgumentNullException) {
1616        // We expect this exception.
1617      }
1618    }
1619  }
1620}
Note: See TracBrowser for help on using the repository browser.