#region Copyright notice and license

// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;

namespace Google.ProtocolBuffers
{
    internal static class TestUtil
    {
        private static ByteString goldenMessage = null;

        internal static ByteString GoldenMessage
        {
            get
            {
                if (goldenMessage == null)
                {
                    goldenMessage = ByteString.CopyFrom(TestResources.golden_message);
                }
                return goldenMessage;
            }
        }

        private static ByteString goldenPackedFieldsMessage = null;

        /// <summary>
        /// Get the bytes of the "golden packed fields message".  This is a serialized
        /// TestPackedTypes with all fields set as they would be by SetPackedFields,
        /// but it is loaded from a file on disk rather than generated dynamically.
        /// The file is actually generated by C++ code, so testing against it verifies compatibility
        /// with C++.
        /// </summary>
        public static ByteString GetGoldenPackedFieldsMessage()
        {
            if (goldenPackedFieldsMessage == null)
            {
                goldenPackedFieldsMessage = ByteString.CopyFrom(TestResources.golden_packed_fields_message);
            }
            return goldenPackedFieldsMessage;
        }

        /// <summary>
        /// Creates an unmodifiable ExtensionRegistry containing all the extensions
        /// of TestAllExtensions.
        /// </summary>
        /// <returns></returns>
        internal static ExtensionRegistry CreateExtensionRegistry()
        {
            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
            RegisterAllExtensions(registry);
            return registry.AsReadOnly();
        }

        /// <summary>
        /// Registers all of the extensions in TestAllExtensions with the given
        /// ExtensionRegistry.
        /// </summary>
        internal static void RegisterAllExtensions(ExtensionRegistry registry)
        {
            registry.Add(Unittest.OptionalInt32Extension);
            registry.Add(Unittest.OptionalInt64Extension);
            registry.Add(Unittest.OptionalUint32Extension);
            registry.Add(Unittest.OptionalUint64Extension);
            registry.Add(Unittest.OptionalSint32Extension);
            registry.Add(Unittest.OptionalSint64Extension);
            registry.Add(Unittest.OptionalFixed32Extension);
            registry.Add(Unittest.OptionalFixed64Extension);
            registry.Add(Unittest.OptionalSfixed32Extension);
            registry.Add(Unittest.OptionalSfixed64Extension);
            registry.Add(Unittest.OptionalFloatExtension);
            registry.Add(Unittest.OptionalDoubleExtension);
            registry.Add(Unittest.OptionalBoolExtension);
            registry.Add(Unittest.OptionalStringExtension);
            registry.Add(Unittest.OptionalBytesExtension);
            registry.Add(Unittest.OptionalGroupExtension);
            registry.Add(Unittest.OptionalNestedMessageExtension);
            registry.Add(Unittest.OptionalForeignMessageExtension);
            registry.Add(Unittest.OptionalImportMessageExtension);
            registry.Add(Unittest.OptionalNestedEnumExtension);
            registry.Add(Unittest.OptionalForeignEnumExtension);
            registry.Add(Unittest.OptionalImportEnumExtension);
            registry.Add(Unittest.OptionalStringPieceExtension);
            registry.Add(Unittest.OptionalCordExtension);

            registry.Add(Unittest.RepeatedInt32Extension);
            registry.Add(Unittest.RepeatedInt64Extension);
            registry.Add(Unittest.RepeatedUint32Extension);
            registry.Add(Unittest.RepeatedUint64Extension);
            registry.Add(Unittest.RepeatedSint32Extension);
            registry.Add(Unittest.RepeatedSint64Extension);
            registry.Add(Unittest.RepeatedFixed32Extension);
            registry.Add(Unittest.RepeatedFixed64Extension);
            registry.Add(Unittest.RepeatedSfixed32Extension);
            registry.Add(Unittest.RepeatedSfixed64Extension);
            registry.Add(Unittest.RepeatedFloatExtension);
            registry.Add(Unittest.RepeatedDoubleExtension);
            registry.Add(Unittest.RepeatedBoolExtension);
            registry.Add(Unittest.RepeatedStringExtension);
            registry.Add(Unittest.RepeatedBytesExtension);
            registry.Add(Unittest.RepeatedGroupExtension);
            registry.Add(Unittest.RepeatedNestedMessageExtension);
            registry.Add(Unittest.RepeatedForeignMessageExtension);
            registry.Add(Unittest.RepeatedImportMessageExtension);
            registry.Add(Unittest.RepeatedNestedEnumExtension);
            registry.Add(Unittest.RepeatedForeignEnumExtension);
            registry.Add(Unittest.RepeatedImportEnumExtension);
            registry.Add(Unittest.RepeatedStringPieceExtension);
            registry.Add(Unittest.RepeatedCordExtension);

            registry.Add(Unittest.DefaultInt32Extension);
            registry.Add(Unittest.DefaultInt64Extension);
            registry.Add(Unittest.DefaultUint32Extension);
            registry.Add(Unittest.DefaultUint64Extension);
            registry.Add(Unittest.DefaultSint32Extension);
            registry.Add(Unittest.DefaultSint64Extension);
            registry.Add(Unittest.DefaultFixed32Extension);
            registry.Add(Unittest.DefaultFixed64Extension);
            registry.Add(Unittest.DefaultSfixed32Extension);
            registry.Add(Unittest.DefaultSfixed64Extension);
            registry.Add(Unittest.DefaultFloatExtension);
            registry.Add(Unittest.DefaultDoubleExtension);
            registry.Add(Unittest.DefaultBoolExtension);
            registry.Add(Unittest.DefaultStringExtension);
            registry.Add(Unittest.DefaultBytesExtension);
            registry.Add(Unittest.DefaultNestedEnumExtension);
            registry.Add(Unittest.DefaultForeignEnumExtension);
            registry.Add(Unittest.DefaultImportEnumExtension);
            registry.Add(Unittest.DefaultStringPieceExtension);
            registry.Add(Unittest.DefaultCordExtension);

            registry.Add(Unittest.PackedInt32Extension);
            registry.Add(Unittest.PackedInt64Extension);
            registry.Add(Unittest.PackedUint32Extension);
            registry.Add(Unittest.PackedUint64Extension);
            registry.Add(Unittest.PackedSint32Extension);
            registry.Add(Unittest.PackedSint64Extension);
            registry.Add(Unittest.PackedFixed32Extension);
            registry.Add(Unittest.PackedFixed64Extension);
            registry.Add(Unittest.PackedSfixed32Extension);
            registry.Add(Unittest.PackedSfixed64Extension);
            registry.Add(Unittest.PackedFloatExtension);
            registry.Add(Unittest.PackedDoubleExtension);
            registry.Add(Unittest.PackedBoolExtension);
            registry.Add(Unittest.PackedEnumExtension);

            registry.Add(Unittest.OneofUint32Extension);
            registry.Add(Unittest.OneofNestedMessageExtension);
            registry.Add(Unittest.OneofStringExtension);
            registry.Add(Unittest.OneofBytesExtension);
        }

        /// <summary>
        /// Helper to convert a String to ByteString.
        /// </summary>
        internal static ByteString ToBytes(String str)
        {
            return ByteString.CopyFrom(Encoding.UTF8.GetBytes(str));
        }

        internal static TestAllTypes GetAllSet()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            SetAllFields(builder);
            return builder.Build();
        }

        /// <summary>
        /// Sets every field of the specified message to the values expected by
        /// AssertAllFieldsSet.
        /// </summary>
        internal static void SetAllFields(TestAllTypes.Builder message)
        {
            message.SetOptionalInt32(101);
            message.SetOptionalInt64(102);
            message.SetOptionalUint32(103);
            message.SetOptionalUint64(104);
            message.SetOptionalSint32(105);
            message.SetOptionalSint64(106);
            message.SetOptionalFixed32(107);
            message.SetOptionalFixed64(108);
            message.SetOptionalSfixed32(109);
            message.SetOptionalSfixed64(110);
            message.SetOptionalFloat(111);
            message.SetOptionalDouble(112);
            message.SetOptionalBool(true);
            message.SetOptionalString("115");
            message.SetOptionalBytes(ToBytes("116"));

            message.SetOptionalGroup(TestAllTypes.Types.OptionalGroup.CreateBuilder().SetA(117).Build());
            message.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(118).Build());
            message.SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(119).Build());
            message.SetOptionalImportMessage(ImportMessage.CreateBuilder().SetD(120).Build());

            message.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAZ);
            message.SetOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ);
            message.SetOptionalImportEnum(ImportEnum.IMPORT_BAZ);

            message.SetOptionalStringPiece("124");
            message.SetOptionalCord("125");

            // -----------------------------------------------------------------

            message.AddRepeatedInt32(201);
            message.AddRepeatedInt64(202);
            message.AddRepeatedUint32(203);
            message.AddRepeatedUint64(204);
            message.AddRepeatedSint32(205);
            message.AddRepeatedSint64(206);
            message.AddRepeatedFixed32(207);
            message.AddRepeatedFixed64(208);
            message.AddRepeatedSfixed32(209);
            message.AddRepeatedSfixed64(210);
            message.AddRepeatedFloat(211);
            message.AddRepeatedDouble(212);
            message.AddRepeatedBool(true);
            message.AddRepeatedString("215");
            message.AddRepeatedBytes(ToBytes("216"));

            message.AddRepeatedGroup(TestAllTypes.Types.RepeatedGroup.CreateBuilder().SetA(217).Build());
            message.AddRepeatedNestedMessage(TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(218).Build());
            message.AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(219).Build());
            message.AddRepeatedImportMessage(ImportMessage.CreateBuilder().SetD(220).Build());

            message.AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.BAR);
            message.AddRepeatedForeignEnum(ForeignEnum.FOREIGN_BAR);
            message.AddRepeatedImportEnum(ImportEnum.IMPORT_BAR);

            message.AddRepeatedStringPiece("224");
            message.AddRepeatedCord("225");

            // Add a second one of each field.
            message.AddRepeatedInt32(301);
            message.AddRepeatedInt64(302);
            message.AddRepeatedUint32(303);
            message.AddRepeatedUint64(304);
            message.AddRepeatedSint32(305);
            message.AddRepeatedSint64(306);
            message.AddRepeatedFixed32(307);
            message.AddRepeatedFixed64(308);
            message.AddRepeatedSfixed32(309);
            message.AddRepeatedSfixed64(310);
            message.AddRepeatedFloat(311);
            message.AddRepeatedDouble(312);
            message.AddRepeatedBool(false);
            message.AddRepeatedString("315");
            message.AddRepeatedBytes(ToBytes("316"));

            message.AddRepeatedGroup(TestAllTypes.Types.RepeatedGroup.CreateBuilder().SetA(317).Build());
            message.AddRepeatedNestedMessage(TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(318).Build());
            message.AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(319).Build());
            message.AddRepeatedImportMessage(ImportMessage.CreateBuilder().SetD(320).Build());

            message.AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.BAZ);
            message.AddRepeatedForeignEnum(ForeignEnum.FOREIGN_BAZ);
            message.AddRepeatedImportEnum(ImportEnum.IMPORT_BAZ);

            message.AddRepeatedStringPiece("324");
            message.AddRepeatedCord("325");

            // -----------------------------------------------------------------

            message.SetDefaultInt32(401);
            message.SetDefaultInt64(402);
            message.SetDefaultUint32(403);
            message.SetDefaultUint64(404);
            message.SetDefaultSint32(405);
            message.SetDefaultSint64(406);
            message.SetDefaultFixed32(407);
            message.SetDefaultFixed64(408);
            message.SetDefaultSfixed32(409);
            message.SetDefaultSfixed64(410);
            message.SetDefaultFloat(411);
            message.SetDefaultDouble(412);
            message.SetDefaultBool(false);
            message.SetDefaultString("415");
            message.SetDefaultBytes(ToBytes("416"));

            message.SetDefaultNestedEnum(TestAllTypes.Types.NestedEnum.FOO);
            message.SetDefaultForeignEnum(ForeignEnum.FOREIGN_FOO);
            message.SetDefaultImportEnum(ImportEnum.IMPORT_FOO);

            message.SetDefaultStringPiece("424");
            message.SetDefaultCord("425");

            message.SetOneofUint32(601);
            message.SetOneofNestedMessage(
                TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(602).Build());
            message.SetOneofString("603");
            message.SetOneofBytes(ToBytes("604"));

        }

        /// <summary>
        /// Asserts that all fields of the specified message are set to the values
        /// assigned by SetAllFields.
        /// </summary>
        internal static void AssertAllFieldsSet(TestAllTypes message)
        {
            Assert.IsTrue(message.HasOptionalInt32);
            Assert.IsTrue(message.HasOptionalInt64);
            Assert.IsTrue(message.HasOptionalUint32);
            Assert.IsTrue(message.HasOptionalUint64);
            Assert.IsTrue(message.HasOptionalSint32);
            Assert.IsTrue(message.HasOptionalSint64);
            Assert.IsTrue(message.HasOptionalFixed32);
            Assert.IsTrue(message.HasOptionalFixed64);
            Assert.IsTrue(message.HasOptionalSfixed32);
            Assert.IsTrue(message.HasOptionalSfixed64);
            Assert.IsTrue(message.HasOptionalFloat);
            Assert.IsTrue(message.HasOptionalDouble);
            Assert.IsTrue(message.HasOptionalBool);
            Assert.IsTrue(message.HasOptionalString);
            Assert.IsTrue(message.HasOptionalBytes);

            Assert.IsTrue(message.HasOptionalGroup);
            Assert.IsTrue(message.HasOptionalNestedMessage);
            Assert.IsTrue(message.HasOptionalForeignMessage);
            Assert.IsTrue(message.HasOptionalImportMessage);

            Assert.IsTrue(message.OptionalGroup.HasA);
            Assert.IsTrue(message.OptionalNestedMessage.HasBb);
            Assert.IsTrue(message.OptionalForeignMessage.HasC);
            Assert.IsTrue(message.OptionalImportMessage.HasD);

            Assert.IsTrue(message.HasOptionalNestedEnum);
            Assert.IsTrue(message.HasOptionalForeignEnum);
            Assert.IsTrue(message.HasOptionalImportEnum);

            Assert.IsTrue(message.HasOptionalStringPiece);
            Assert.IsTrue(message.HasOptionalCord);

            Assert.AreEqual(101, message.OptionalInt32);
            Assert.AreEqual(102, message.OptionalInt64);
            Assert.AreEqual(103u, message.OptionalUint32);
            Assert.AreEqual(104u, message.OptionalUint64);
            Assert.AreEqual(105, message.OptionalSint32);
            Assert.AreEqual(106, message.OptionalSint64);
            Assert.AreEqual(107u, message.OptionalFixed32);
            Assert.AreEqual(108u, message.OptionalFixed64);
            Assert.AreEqual(109, message.OptionalSfixed32);
            Assert.AreEqual(110, message.OptionalSfixed64);
            Assert.AreEqual(111, message.OptionalFloat);
            Assert.AreEqual(112, message.OptionalDouble);
            Assert.AreEqual(true, message.OptionalBool);
            Assert.AreEqual("115", message.OptionalString);
            Assert.AreEqual(ToBytes("116"), message.OptionalBytes);

            Assert.AreEqual(117, message.OptionalGroup.A);
            Assert.AreEqual(118, message.OptionalNestedMessage.Bb);
            Assert.AreEqual(119, message.OptionalForeignMessage.C);
            Assert.AreEqual(120, message.OptionalImportMessage.D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ, message.OptionalNestedEnum);
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.OptionalForeignEnum);
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.OptionalImportEnum);

            Assert.AreEqual("124", message.OptionalStringPiece);
            Assert.AreEqual("125", message.OptionalCord);

            // -----------------------------------------------------------------

            Assert.AreEqual(2, message.RepeatedInt32Count);
            Assert.AreEqual(2, message.RepeatedInt64Count);
            Assert.AreEqual(2, message.RepeatedUint32Count);
            Assert.AreEqual(2, message.RepeatedUint64Count);
            Assert.AreEqual(2, message.RepeatedSint32Count);
            Assert.AreEqual(2, message.RepeatedSint64Count);
            Assert.AreEqual(2, message.RepeatedFixed32Count);
            Assert.AreEqual(2, message.RepeatedFixed64Count);
            Assert.AreEqual(2, message.RepeatedSfixed32Count);
            Assert.AreEqual(2, message.RepeatedSfixed64Count);
            Assert.AreEqual(2, message.RepeatedFloatCount);
            Assert.AreEqual(2, message.RepeatedDoubleCount);
            Assert.AreEqual(2, message.RepeatedBoolCount);
            Assert.AreEqual(2, message.RepeatedStringCount);
            Assert.AreEqual(2, message.RepeatedBytesCount);

            Assert.AreEqual(2, message.RepeatedGroupCount);
            Assert.AreEqual(2, message.RepeatedNestedMessageCount);
            Assert.AreEqual(2, message.RepeatedForeignMessageCount);
            Assert.AreEqual(2, message.RepeatedImportMessageCount);
            Assert.AreEqual(2, message.RepeatedNestedEnumCount);
            Assert.AreEqual(2, message.RepeatedForeignEnumCount);
            Assert.AreEqual(2, message.RepeatedImportEnumCount);

            Assert.AreEqual(2, message.RepeatedStringPieceCount);
            Assert.AreEqual(2, message.RepeatedCordCount);

            Assert.AreEqual(201, message.GetRepeatedInt32(0));
            Assert.AreEqual(202, message.GetRepeatedInt64(0));
            Assert.AreEqual(203u, message.GetRepeatedUint32(0));
            Assert.AreEqual(204u, message.GetRepeatedUint64(0));
            Assert.AreEqual(205, message.GetRepeatedSint32(0));
            Assert.AreEqual(206, message.GetRepeatedSint64(0));
            Assert.AreEqual(207u, message.GetRepeatedFixed32(0));
            Assert.AreEqual(208u, message.GetRepeatedFixed64(0));
            Assert.AreEqual(209, message.GetRepeatedSfixed32(0));
            Assert.AreEqual(210, message.GetRepeatedSfixed64(0));
            Assert.AreEqual(211, message.GetRepeatedFloat(0));
            Assert.AreEqual(212, message.GetRepeatedDouble(0));
            Assert.AreEqual(true, message.GetRepeatedBool(0));
            Assert.AreEqual("215", message.GetRepeatedString(0));
            Assert.AreEqual(ToBytes("216"), message.GetRepeatedBytes(0));

            Assert.AreEqual(217, message.GetRepeatedGroup(0).A);
            Assert.AreEqual(218, message.GetRepeatedNestedMessage(0).Bb);
            Assert.AreEqual(219, message.GetRepeatedForeignMessage(0).C);
            Assert.AreEqual(220, message.GetRepeatedImportMessage(0).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetRepeatedNestedEnum(0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetRepeatedForeignEnum(0));
            Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetRepeatedImportEnum(0));

            Assert.AreEqual("224", message.GetRepeatedStringPiece(0));
            Assert.AreEqual("225", message.GetRepeatedCord(0));

            Assert.AreEqual(301, message.GetRepeatedInt32(1));
            Assert.AreEqual(302, message.GetRepeatedInt64(1));
            Assert.AreEqual(303u, message.GetRepeatedUint32(1));
            Assert.AreEqual(304u, message.GetRepeatedUint64(1));
            Assert.AreEqual(305, message.GetRepeatedSint32(1));
            Assert.AreEqual(306, message.GetRepeatedSint64(1));
            Assert.AreEqual(307u, message.GetRepeatedFixed32(1));
            Assert.AreEqual(308u, message.GetRepeatedFixed64(1));
            Assert.AreEqual(309, message.GetRepeatedSfixed32(1));
            Assert.AreEqual(310, message.GetRepeatedSfixed64(1));
            Assert.AreEqual(311f, message.GetRepeatedFloat(1));
            Assert.AreEqual(312d, message.GetRepeatedDouble(1));
            Assert.AreEqual(false, message.GetRepeatedBool(1));
            Assert.AreEqual("315", message.GetRepeatedString(1));
            Assert.AreEqual(ToBytes("316"), message.GetRepeatedBytes(1));

            Assert.AreEqual(317, message.GetRepeatedGroup(1).A);
            Assert.AreEqual(318, message.GetRepeatedNestedMessage(1).Bb);
            Assert.AreEqual(319, message.GetRepeatedForeignMessage(1).C);
            Assert.AreEqual(320, message.GetRepeatedImportMessage(1).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ, message.GetRepeatedNestedEnum(1));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetRepeatedForeignEnum(1));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.GetRepeatedImportEnum(1));

            Assert.AreEqual("324", message.GetRepeatedStringPiece(1));
            Assert.AreEqual("325", message.GetRepeatedCord(1));

            // -----------------------------------------------------------------

            Assert.IsTrue(message.HasDefaultInt32);
            Assert.IsTrue(message.HasDefaultInt64);
            Assert.IsTrue(message.HasDefaultUint32);
            Assert.IsTrue(message.HasDefaultUint64);
            Assert.IsTrue(message.HasDefaultSint32);
            Assert.IsTrue(message.HasDefaultSint64);
            Assert.IsTrue(message.HasDefaultFixed32);
            Assert.IsTrue(message.HasDefaultFixed64);
            Assert.IsTrue(message.HasDefaultSfixed32);
            Assert.IsTrue(message.HasDefaultSfixed64);
            Assert.IsTrue(message.HasDefaultFloat);
            Assert.IsTrue(message.HasDefaultDouble);
            Assert.IsTrue(message.HasDefaultBool);
            Assert.IsTrue(message.HasDefaultString);
            Assert.IsTrue(message.HasDefaultBytes);

            Assert.IsTrue(message.HasDefaultNestedEnum);
            Assert.IsTrue(message.HasDefaultForeignEnum);
            Assert.IsTrue(message.HasDefaultImportEnum);

            Assert.IsTrue(message.HasDefaultStringPiece);
            Assert.IsTrue(message.HasDefaultCord);

            Assert.AreEqual(401, message.DefaultInt32);
            Assert.AreEqual(402, message.DefaultInt64);
            Assert.AreEqual(403u, message.DefaultUint32);
            Assert.AreEqual(404u, message.DefaultUint64);
            Assert.AreEqual(405, message.DefaultSint32);
            Assert.AreEqual(406, message.DefaultSint64);
            Assert.AreEqual(407u, message.DefaultFixed32);
            Assert.AreEqual(408u, message.DefaultFixed64);
            Assert.AreEqual(409, message.DefaultSfixed32);
            Assert.AreEqual(410, message.DefaultSfixed64);
            Assert.AreEqual(411, message.DefaultFloat);
            Assert.AreEqual(412, message.DefaultDouble);
            Assert.AreEqual(false, message.DefaultBool);
            Assert.AreEqual("415", message.DefaultString);
            Assert.AreEqual(ToBytes("416"), message.DefaultBytes);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.DefaultNestedEnum);
            Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.DefaultForeignEnum);
            Assert.AreEqual(ImportEnum.IMPORT_FOO, message.DefaultImportEnum);

            Assert.AreEqual("424", message.DefaultStringPiece);
            Assert.AreEqual("425", message.DefaultCord);

            Assert.IsFalse(message.HasOneofUint32);
            Assert.IsFalse(message.HasOneofNestedMessage);
            Assert.IsFalse(message.HasOneofString);
            Assert.IsTrue(message.HasOneofBytes);

            Assert.AreEqual(ToBytes("604"), message.OneofBytes);
        }

        internal static void AssertClear(TestAllTypes message)
        {
            // HasBlah() should initially be false for all optional fields.
            Assert.IsFalse(message.HasOptionalInt32);
            Assert.IsFalse(message.HasOptionalInt64);
            Assert.IsFalse(message.HasOptionalUint32);
            Assert.IsFalse(message.HasOptionalUint64);
            Assert.IsFalse(message.HasOptionalSint32);
            Assert.IsFalse(message.HasOptionalSint64);
            Assert.IsFalse(message.HasOptionalFixed32);
            Assert.IsFalse(message.HasOptionalFixed64);
            Assert.IsFalse(message.HasOptionalSfixed32);
            Assert.IsFalse(message.HasOptionalSfixed64);
            Assert.IsFalse(message.HasOptionalFloat);
            Assert.IsFalse(message.HasOptionalDouble);
            Assert.IsFalse(message.HasOptionalBool);
            Assert.IsFalse(message.HasOptionalString);
            Assert.IsFalse(message.HasOptionalBytes);

            Assert.IsFalse(message.HasOptionalGroup);
            Assert.IsFalse(message.HasOptionalNestedMessage);
            Assert.IsFalse(message.HasOptionalForeignMessage);
            Assert.IsFalse(message.HasOptionalImportMessage);

            Assert.IsFalse(message.HasOptionalNestedEnum);
            Assert.IsFalse(message.HasOptionalForeignEnum);
            Assert.IsFalse(message.HasOptionalImportEnum);

            Assert.IsFalse(message.HasOptionalStringPiece);
            Assert.IsFalse(message.HasOptionalCord);

            // Optional fields without defaults are set to zero or something like it.
            Assert.AreEqual(0, message.OptionalInt32);
            Assert.AreEqual(0, message.OptionalInt64);
            Assert.AreEqual(0u, message.OptionalUint32);
            Assert.AreEqual(0u, message.OptionalUint64);
            Assert.AreEqual(0, message.OptionalSint32);
            Assert.AreEqual(0, message.OptionalSint64);
            Assert.AreEqual(0u, message.OptionalFixed32);
            Assert.AreEqual(0u, message.OptionalFixed64);
            Assert.AreEqual(0, message.OptionalSfixed32);
            Assert.AreEqual(0, message.OptionalSfixed64);
            Assert.AreEqual(0, message.OptionalFloat);
            Assert.AreEqual(0, message.OptionalDouble);
            Assert.AreEqual(false, message.OptionalBool);
            Assert.AreEqual("", message.OptionalString);
            Assert.AreEqual(ByteString.Empty, message.OptionalBytes);

            // Embedded messages should also be clear.
            Assert.IsFalse(message.OptionalGroup.HasA);
            Assert.IsFalse(message.OptionalNestedMessage.HasBb);
            Assert.IsFalse(message.OptionalForeignMessage.HasC);
            Assert.IsFalse(message.OptionalImportMessage.HasD);

            Assert.AreEqual(0, message.OptionalGroup.A);
            Assert.AreEqual(0, message.OptionalNestedMessage.Bb);
            Assert.AreEqual(0, message.OptionalForeignMessage.C);
            Assert.AreEqual(0, message.OptionalImportMessage.D);

            // Enums without defaults are set to the first value in the enum.
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
            Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.OptionalForeignEnum);
            Assert.AreEqual(ImportEnum.IMPORT_FOO, message.OptionalImportEnum);

            Assert.AreEqual("", message.OptionalStringPiece);
            Assert.AreEqual("", message.OptionalCord);

            // Repeated fields are empty.
            Assert.AreEqual(0, message.RepeatedInt32Count);
            Assert.AreEqual(0, message.RepeatedInt64Count);
            Assert.AreEqual(0, message.RepeatedUint32Count);
            Assert.AreEqual(0, message.RepeatedUint64Count);
            Assert.AreEqual(0, message.RepeatedSint32Count);
            Assert.AreEqual(0, message.RepeatedSint64Count);
            Assert.AreEqual(0, message.RepeatedFixed32Count);
            Assert.AreEqual(0, message.RepeatedFixed64Count);
            Assert.AreEqual(0, message.RepeatedSfixed32Count);
            Assert.AreEqual(0, message.RepeatedSfixed64Count);
            Assert.AreEqual(0, message.RepeatedFloatCount);
            Assert.AreEqual(0, message.RepeatedDoubleCount);
            Assert.AreEqual(0, message.RepeatedBoolCount);
            Assert.AreEqual(0, message.RepeatedStringCount);
            Assert.AreEqual(0, message.RepeatedBytesCount);

            Assert.AreEqual(0, message.RepeatedGroupCount);
            Assert.AreEqual(0, message.RepeatedNestedMessageCount);
            Assert.AreEqual(0, message.RepeatedForeignMessageCount);
            Assert.AreEqual(0, message.RepeatedImportMessageCount);
            Assert.AreEqual(0, message.RepeatedNestedEnumCount);
            Assert.AreEqual(0, message.RepeatedForeignEnumCount);
            Assert.AreEqual(0, message.RepeatedImportEnumCount);

            Assert.AreEqual(0, message.RepeatedStringPieceCount);
            Assert.AreEqual(0, message.RepeatedCordCount);

            // HasBlah() should also be false for all default fields.
            Assert.IsFalse(message.HasDefaultInt32);
            Assert.IsFalse(message.HasDefaultInt64);
            Assert.IsFalse(message.HasDefaultUint32);
            Assert.IsFalse(message.HasDefaultUint64);
            Assert.IsFalse(message.HasDefaultSint32);
            Assert.IsFalse(message.HasDefaultSint64);
            Assert.IsFalse(message.HasDefaultFixed32);
            Assert.IsFalse(message.HasDefaultFixed64);
            Assert.IsFalse(message.HasDefaultSfixed32);
            Assert.IsFalse(message.HasDefaultSfixed64);
            Assert.IsFalse(message.HasDefaultFloat);
            Assert.IsFalse(message.HasDefaultDouble);
            Assert.IsFalse(message.HasDefaultBool);
            Assert.IsFalse(message.HasDefaultString);
            Assert.IsFalse(message.HasDefaultBytes);

            Assert.IsFalse(message.HasDefaultNestedEnum);
            Assert.IsFalse(message.HasDefaultForeignEnum);
            Assert.IsFalse(message.HasDefaultImportEnum);

            Assert.IsFalse(message.HasDefaultStringPiece);
            Assert.IsFalse(message.HasDefaultCord);

            // Fields with defaults have their default values (duh).
            Assert.AreEqual(41, message.DefaultInt32);
            Assert.AreEqual(42, message.DefaultInt64);
            Assert.AreEqual(43u, message.DefaultUint32);
            Assert.AreEqual(44u, message.DefaultUint64);
            Assert.AreEqual(-45, message.DefaultSint32);
            Assert.AreEqual(46, message.DefaultSint64);
            Assert.AreEqual(47u, message.DefaultFixed32);
            Assert.AreEqual(48u, message.DefaultFixed64);
            Assert.AreEqual(49, message.DefaultSfixed32);
            Assert.AreEqual(-50, message.DefaultSfixed64);
            Assert.AreEqual(51.5f, message.DefaultFloat);
            Assert.AreEqual(52e3d, message.DefaultDouble);
            Assert.AreEqual(true, message.DefaultBool);
            Assert.AreEqual("hello", message.DefaultString);
            Assert.AreEqual(ToBytes("world"), message.DefaultBytes);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.DefaultNestedEnum);
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.DefaultForeignEnum);
            Assert.AreEqual(ImportEnum.IMPORT_BAR, message.DefaultImportEnum);

            Assert.AreEqual("abc", message.DefaultStringPiece);
            Assert.AreEqual("123", message.DefaultCord);

            Assert.IsFalse(message.HasOneofUint32);
            Assert.IsFalse(message.HasOneofNestedMessage);
            Assert.IsFalse(message.HasOneofString);
            Assert.IsFalse(message.HasOneofBytes);
        }

        /// <summary>
        /// Get a TestAllExtensions with all fields set as they would be by
        /// SetAllExtensions(TestAllExtensions.Builder).
        /// </summary>
        internal static TestAllExtensions GetAllExtensionsSet()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            SetAllExtensions(builder);
            return builder.Build();
        }

        public static TestPackedTypes GetPackedSet()
        {
            TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
            SetPackedFields(builder);
            return builder.Build();
        }

        public static TestPackedExtensions GetPackedExtensionsSet()
        {
            TestPackedExtensions.Builder builder = TestPackedExtensions.CreateBuilder();
            SetPackedExtensions(builder);
            return builder.Build();
        }

        /// <summary>
        /// Sets every field of the specified builder to the values expected by
        /// AssertAllExtensionsSet.
        /// </summary>
        internal static void SetAllExtensions(TestAllExtensions.Builder message)
        {
            message.SetExtension(Unittest.OptionalInt32Extension, 101);
            message.SetExtension(Unittest.OptionalInt64Extension, 102L);
            message.SetExtension(Unittest.OptionalUint32Extension, 103U);
            message.SetExtension(Unittest.OptionalUint64Extension, 104UL);
            message.SetExtension(Unittest.OptionalSint32Extension, 105);
            message.SetExtension(Unittest.OptionalSint64Extension, 106L);
            message.SetExtension(Unittest.OptionalFixed32Extension, 107U);
            message.SetExtension(Unittest.OptionalFixed64Extension, 108UL);
            message.SetExtension(Unittest.OptionalSfixed32Extension, 109);
            message.SetExtension(Unittest.OptionalSfixed64Extension, 110L);
            message.SetExtension(Unittest.OptionalFloatExtension, 111F);
            message.SetExtension(Unittest.OptionalDoubleExtension, 112D);
            message.SetExtension(Unittest.OptionalBoolExtension, true);
            message.SetExtension(Unittest.OptionalStringExtension, "115");
            message.SetExtension(Unittest.OptionalBytesExtension, ToBytes("116"));

            message.SetExtension(Unittest.OptionalGroupExtension,
                                 OptionalGroup_extension.CreateBuilder().SetA(117).Build());
            message.SetExtension(Unittest.OptionalNestedMessageExtension,
                                 TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(118).Build());
            message.SetExtension(Unittest.OptionalForeignMessageExtension,
                                 ForeignMessage.CreateBuilder().SetC(119).Build());
            message.SetExtension(Unittest.OptionalImportMessageExtension,
                                 ImportMessage.CreateBuilder().SetD(120).Build());

            message.SetExtension(Unittest.OptionalNestedEnumExtension, TestAllTypes.Types.NestedEnum.BAZ);
            message.SetExtension(Unittest.OptionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ);
            message.SetExtension(Unittest.OptionalImportEnumExtension, ImportEnum.IMPORT_BAZ);

            message.SetExtension(Unittest.OptionalStringPieceExtension, "124");
            message.SetExtension(Unittest.OptionalCordExtension, "125");

            // -----------------------------------------------------------------

            message.AddExtension(Unittest.RepeatedInt32Extension, 201);
            message.AddExtension(Unittest.RepeatedInt64Extension, 202L);
            message.AddExtension(Unittest.RepeatedUint32Extension, 203U);
            message.AddExtension(Unittest.RepeatedUint64Extension, 204UL);
            message.AddExtension(Unittest.RepeatedSint32Extension, 205);
            message.AddExtension(Unittest.RepeatedSint64Extension, 206L);
            message.AddExtension(Unittest.RepeatedFixed32Extension, 207U);
            message.AddExtension(Unittest.RepeatedFixed64Extension, 208UL);
            message.AddExtension(Unittest.RepeatedSfixed32Extension, 209);
            message.AddExtension(Unittest.RepeatedSfixed64Extension, 210L);
            message.AddExtension(Unittest.RepeatedFloatExtension, 211F);
            message.AddExtension(Unittest.RepeatedDoubleExtension, 212D);
            message.AddExtension(Unittest.RepeatedBoolExtension, true);
            message.AddExtension(Unittest.RepeatedStringExtension, "215");
            message.AddExtension(Unittest.RepeatedBytesExtension, ToBytes("216"));

            message.AddExtension(Unittest.RepeatedGroupExtension,
                                 RepeatedGroup_extension.CreateBuilder().SetA(217).Build());
            message.AddExtension(Unittest.RepeatedNestedMessageExtension,
                                 TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(218).Build());
            message.AddExtension(Unittest.RepeatedForeignMessageExtension,
                                 ForeignMessage.CreateBuilder().SetC(219).Build());
            message.AddExtension(Unittest.RepeatedImportMessageExtension,
                                 ImportMessage.CreateBuilder().SetD(220).Build());

            message.AddExtension(Unittest.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.BAR);
            message.AddExtension(Unittest.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAR);
            message.AddExtension(Unittest.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAR);

            message.AddExtension(Unittest.RepeatedStringPieceExtension, "224");
            message.AddExtension(Unittest.RepeatedCordExtension, "225");

            // Add a second one of each field.
            message.AddExtension(Unittest.RepeatedInt32Extension, 301);
            message.AddExtension(Unittest.RepeatedInt64Extension, 302L);
            message.AddExtension(Unittest.RepeatedUint32Extension, 303U);
            message.AddExtension(Unittest.RepeatedUint64Extension, 304UL);
            message.AddExtension(Unittest.RepeatedSint32Extension, 305);
            message.AddExtension(Unittest.RepeatedSint64Extension, 306L);
            message.AddExtension(Unittest.RepeatedFixed32Extension, 307U);
            message.AddExtension(Unittest.RepeatedFixed64Extension, 308UL);
            message.AddExtension(Unittest.RepeatedSfixed32Extension, 309);
            message.AddExtension(Unittest.RepeatedSfixed64Extension, 310L);
            message.AddExtension(Unittest.RepeatedFloatExtension, 311F);
            message.AddExtension(Unittest.RepeatedDoubleExtension, 312D);
            message.AddExtension(Unittest.RepeatedBoolExtension, false);
            message.AddExtension(Unittest.RepeatedStringExtension, "315");
            message.AddExtension(Unittest.RepeatedBytesExtension, ToBytes("316"));

            message.AddExtension(Unittest.RepeatedGroupExtension,
                                 RepeatedGroup_extension.CreateBuilder().SetA(317).Build());
            message.AddExtension(Unittest.RepeatedNestedMessageExtension,
                                 TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(318).Build());
            message.AddExtension(Unittest.RepeatedForeignMessageExtension,
                                 ForeignMessage.CreateBuilder().SetC(319).Build());
            message.AddExtension(Unittest.RepeatedImportMessageExtension,
                                 ImportMessage.CreateBuilder().SetD(320).Build());

            message.AddExtension(Unittest.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.BAZ);
            message.AddExtension(Unittest.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ);
            message.AddExtension(Unittest.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAZ);

            message.AddExtension(Unittest.RepeatedStringPieceExtension, "324");
            message.AddExtension(Unittest.RepeatedCordExtension, "325");

            // -----------------------------------------------------------------

            message.SetExtension(Unittest.DefaultInt32Extension, 401);
            message.SetExtension(Unittest.DefaultInt64Extension, 402L);
            message.SetExtension(Unittest.DefaultUint32Extension, 403U);
            message.SetExtension(Unittest.DefaultUint64Extension, 404UL);
            message.SetExtension(Unittest.DefaultSint32Extension, 405);
            message.SetExtension(Unittest.DefaultSint64Extension, 406L);
            message.SetExtension(Unittest.DefaultFixed32Extension, 407U);
            message.SetExtension(Unittest.DefaultFixed64Extension, 408UL);
            message.SetExtension(Unittest.DefaultSfixed32Extension, 409);
            message.SetExtension(Unittest.DefaultSfixed64Extension, 410L);
            message.SetExtension(Unittest.DefaultFloatExtension, 411F);
            message.SetExtension(Unittest.DefaultDoubleExtension, 412D);
            message.SetExtension(Unittest.DefaultBoolExtension, false);
            message.SetExtension(Unittest.DefaultStringExtension, "415");
            message.SetExtension(Unittest.DefaultBytesExtension, ToBytes("416"));

            message.SetExtension(Unittest.DefaultNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO);
            message.SetExtension(Unittest.DefaultForeignEnumExtension, ForeignEnum.FOREIGN_FOO);
            message.SetExtension(Unittest.DefaultImportEnumExtension, ImportEnum.IMPORT_FOO);

            message.SetExtension(Unittest.DefaultStringPieceExtension, "424");
            message.SetExtension(Unittest.DefaultCordExtension, "425");

            message.SetExtension(Unittest.OneofUint32Extension, 601U);
            message.SetExtension(Unittest.OneofNestedMessageExtension,
                TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(602).Build());
            message.SetExtension(Unittest.OneofStringExtension, "603");
            message.SetExtension(Unittest.OneofBytesExtension, ToBytes("604"));
        }

        internal static void ModifyRepeatedFields(TestAllTypes.Builder message)
        {
            message.SetRepeatedInt32(1, 501);
            message.SetRepeatedInt64(1, 502);
            message.SetRepeatedUint32(1, 503);
            message.SetRepeatedUint64(1, 504);
            message.SetRepeatedSint32(1, 505);
            message.SetRepeatedSint64(1, 506);
            message.SetRepeatedFixed32(1, 507);
            message.SetRepeatedFixed64(1, 508);
            message.SetRepeatedSfixed32(1, 509);
            message.SetRepeatedSfixed64(1, 510);
            message.SetRepeatedFloat(1, 511);
            message.SetRepeatedDouble(1, 512);
            message.SetRepeatedBool(1, true);
            message.SetRepeatedString(1, "515");
            message.SetRepeatedBytes(1, ToBytes("516"));

            message.SetRepeatedGroup(1, TestAllTypes.Types.RepeatedGroup.CreateBuilder().SetA(517).Build());
            message.SetRepeatedNestedMessage(1, TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(518).Build());
            message.SetRepeatedForeignMessage(1, ForeignMessage.CreateBuilder().SetC(519).Build());
            message.SetRepeatedImportMessage(1, ImportMessage.CreateBuilder().SetD(520).Build());

            message.SetRepeatedNestedEnum(1, TestAllTypes.Types.NestedEnum.FOO);
            message.SetRepeatedForeignEnum(1, ForeignEnum.FOREIGN_FOO);
            message.SetRepeatedImportEnum(1, ImportEnum.IMPORT_FOO);

            message.SetRepeatedStringPiece(1, "524");
            message.SetRepeatedCord(1, "525");
        }

        internal static void AssertRepeatedFieldsModified(TestAllTypes message)
        {
            // ModifyRepeatedFields only sets the second repeated element of each
            // field.  In addition to verifying this, we also verify that the first
            // element and size were *not* modified.
            Assert.AreEqual(2, message.RepeatedInt32Count);
            Assert.AreEqual(2, message.RepeatedInt64Count);
            Assert.AreEqual(2, message.RepeatedUint32Count);
            Assert.AreEqual(2, message.RepeatedUint64Count);
            Assert.AreEqual(2, message.RepeatedSint32Count);
            Assert.AreEqual(2, message.RepeatedSint64Count);
            Assert.AreEqual(2, message.RepeatedFixed32Count);
            Assert.AreEqual(2, message.RepeatedFixed64Count);
            Assert.AreEqual(2, message.RepeatedSfixed32Count);
            Assert.AreEqual(2, message.RepeatedSfixed64Count);
            Assert.AreEqual(2, message.RepeatedFloatCount);
            Assert.AreEqual(2, message.RepeatedDoubleCount);
            Assert.AreEqual(2, message.RepeatedBoolCount);
            Assert.AreEqual(2, message.RepeatedStringCount);
            Assert.AreEqual(2, message.RepeatedBytesCount);

            Assert.AreEqual(2, message.RepeatedGroupCount);
            Assert.AreEqual(2, message.RepeatedNestedMessageCount);
            Assert.AreEqual(2, message.RepeatedForeignMessageCount);
            Assert.AreEqual(2, message.RepeatedImportMessageCount);
            Assert.AreEqual(2, message.RepeatedNestedEnumCount);
            Assert.AreEqual(2, message.RepeatedForeignEnumCount);
            Assert.AreEqual(2, message.RepeatedImportEnumCount);

            Assert.AreEqual(2, message.RepeatedStringPieceCount);
            Assert.AreEqual(2, message.RepeatedCordCount);

            Assert.AreEqual(201, message.GetRepeatedInt32(0));
            Assert.AreEqual(202L, message.GetRepeatedInt64(0));
            Assert.AreEqual(203U, message.GetRepeatedUint32(0));
            Assert.AreEqual(204UL, message.GetRepeatedUint64(0));
            Assert.AreEqual(205, message.GetRepeatedSint32(0));
            Assert.AreEqual(206L, message.GetRepeatedSint64(0));
            Assert.AreEqual(207U, message.GetRepeatedFixed32(0));
            Assert.AreEqual(208UL, message.GetRepeatedFixed64(0));
            Assert.AreEqual(209, message.GetRepeatedSfixed32(0));
            Assert.AreEqual(210L, message.GetRepeatedSfixed64(0));
            Assert.AreEqual(211F, message.GetRepeatedFloat(0));
            Assert.AreEqual(212D, message.GetRepeatedDouble(0));
            Assert.AreEqual(true, message.GetRepeatedBool(0));
            Assert.AreEqual("215", message.GetRepeatedString(0));
            Assert.AreEqual(ToBytes("216"), message.GetRepeatedBytes(0));

            Assert.AreEqual(217, message.GetRepeatedGroup(0).A);
            Assert.AreEqual(218, message.GetRepeatedNestedMessage(0).Bb);
            Assert.AreEqual(219, message.GetRepeatedForeignMessage(0).C);
            Assert.AreEqual(220, message.GetRepeatedImportMessage(0).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetRepeatedNestedEnum(0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetRepeatedForeignEnum(0));
            Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetRepeatedImportEnum(0));

            Assert.AreEqual("224", message.GetRepeatedStringPiece(0));
            Assert.AreEqual("225", message.GetRepeatedCord(0));

            // Actually verify the second (modified) elements now.
            Assert.AreEqual(501, message.GetRepeatedInt32(1));
            Assert.AreEqual(502L, message.GetRepeatedInt64(1));
            Assert.AreEqual(503U, message.GetRepeatedUint32(1));
            Assert.AreEqual(504UL, message.GetRepeatedUint64(1));
            Assert.AreEqual(505, message.GetRepeatedSint32(1));
            Assert.AreEqual(506L, message.GetRepeatedSint64(1));
            Assert.AreEqual(507U, message.GetRepeatedFixed32(1));
            Assert.AreEqual(508UL, message.GetRepeatedFixed64(1));
            Assert.AreEqual(509, message.GetRepeatedSfixed32(1));
            Assert.AreEqual(510L, message.GetRepeatedSfixed64(1));
            Assert.AreEqual(511F, message.GetRepeatedFloat(1));
            Assert.AreEqual(512D, message.GetRepeatedDouble(1));
            Assert.AreEqual(true, message.GetRepeatedBool(1));
            Assert.AreEqual("515", message.GetRepeatedString(1));
            Assert.AreEqual(ToBytes("516"), message.GetRepeatedBytes(1));

            Assert.AreEqual(517, message.GetRepeatedGroup(1).A);
            Assert.AreEqual(518, message.GetRepeatedNestedMessage(1).Bb);
            Assert.AreEqual(519, message.GetRepeatedForeignMessage(1).C);
            Assert.AreEqual(520, message.GetRepeatedImportMessage(1).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.GetRepeatedNestedEnum(1));
            Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.GetRepeatedForeignEnum(1));
            Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetRepeatedImportEnum(1));

            Assert.AreEqual("524", message.GetRepeatedStringPiece(1));
            Assert.AreEqual("525", message.GetRepeatedCord(1));
        }

        /// <summary>
        /// Helper to assert that sequences are equal.
        /// </summary>
        internal static void AssertEqual<T>(IEnumerable<T> first, IEnumerable<T> second)
        {
            using (IEnumerator<T> firstEnumerator = first.GetEnumerator())
            {
                foreach (T secondElement in second)
                {
                    Assert.IsTrue(firstEnumerator.MoveNext(), "First enumerator ran out of elements too early.");
                    Assert.AreEqual(firstEnumerator.Current, secondElement);
                }
                Assert.IsFalse(firstEnumerator.MoveNext(), "Second enumerator ran out of elements too early.");
            }
        }

        internal static void AssertEqualBytes(byte[] expected, byte[] actual)
        {
            Assert.AreEqual(ByteString.CopyFrom(expected), ByteString.CopyFrom(actual));
        }

        internal static void AssertAllExtensionsSet(TestAllExtensions message)
        {
            Assert.IsTrue(message.HasExtension(Unittest.OptionalInt32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalInt64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalUint32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalUint64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalSint32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalSint64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalFixed32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalFixed64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalSfixed32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalSfixed64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalFloatExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalDoubleExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalBoolExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalStringExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalBytesExtension));

            Assert.IsTrue(message.HasExtension(Unittest.OptionalGroupExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalNestedMessageExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalForeignMessageExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalImportMessageExtension));

            Assert.IsTrue(message.GetExtension(Unittest.OptionalGroupExtension).HasA);
            Assert.IsTrue(message.GetExtension(Unittest.OptionalNestedMessageExtension).HasBb);
            Assert.IsTrue(message.GetExtension(Unittest.OptionalForeignMessageExtension).HasC);
            Assert.IsTrue(message.GetExtension(Unittest.OptionalImportMessageExtension).HasD);

            Assert.IsTrue(message.HasExtension(Unittest.OptionalNestedEnumExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalForeignEnumExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalImportEnumExtension));

            Assert.IsTrue(message.HasExtension(Unittest.OptionalStringPieceExtension));
            Assert.IsTrue(message.HasExtension(Unittest.OptionalCordExtension));

            Assert.AreEqual(101, message.GetExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(102L, message.GetExtension(Unittest.OptionalInt64Extension));
            Assert.AreEqual(103U, message.GetExtension(Unittest.OptionalUint32Extension));
            Assert.AreEqual(104UL, message.GetExtension(Unittest.OptionalUint64Extension));
            Assert.AreEqual(105, message.GetExtension(Unittest.OptionalSint32Extension));
            Assert.AreEqual(106L, message.GetExtension(Unittest.OptionalSint64Extension));
            Assert.AreEqual(107U, message.GetExtension(Unittest.OptionalFixed32Extension));
            Assert.AreEqual(108UL, message.GetExtension(Unittest.OptionalFixed64Extension));
            Assert.AreEqual(109, message.GetExtension(Unittest.OptionalSfixed32Extension));
            Assert.AreEqual(110L, message.GetExtension(Unittest.OptionalSfixed64Extension));
            Assert.AreEqual(111F, message.GetExtension(Unittest.OptionalFloatExtension));
            Assert.AreEqual(112D, message.GetExtension(Unittest.OptionalDoubleExtension));
            Assert.AreEqual(true, message.GetExtension(Unittest.OptionalBoolExtension));
            Assert.AreEqual("115", message.GetExtension(Unittest.OptionalStringExtension));
            Assert.AreEqual(ToBytes("116"), message.GetExtension(Unittest.OptionalBytesExtension));

            Assert.AreEqual(117, message.GetExtension(Unittest.OptionalGroupExtension).A);
            Assert.AreEqual(118, message.GetExtension(Unittest.OptionalNestedMessageExtension).Bb);
            Assert.AreEqual(119, message.GetExtension(Unittest.OptionalForeignMessageExtension).C);
            Assert.AreEqual(120, message.GetExtension(Unittest.OptionalImportMessageExtension).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ,
                            message.GetExtension(Unittest.OptionalNestedEnumExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ,
                            message.GetExtension(Unittest.OptionalForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, message.GetExtension(Unittest.OptionalImportEnumExtension));

            Assert.AreEqual("124", message.GetExtension(Unittest.OptionalStringPieceExtension));
            Assert.AreEqual("125", message.GetExtension(Unittest.OptionalCordExtension));

            // -----------------------------------------------------------------

            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedInt32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedInt64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedUint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedUint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedFixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedFixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSfixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSfixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedFloatExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedDoubleExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedBoolExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedStringExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedBytesExtension));

            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedGroupExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedNestedMessageExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedForeignMessageExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedImportMessageExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedNestedEnumExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedForeignEnumExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedImportEnumExtension));

            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedStringPieceExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedCordExtension));

            Assert.AreEqual(201, message.GetExtension(Unittest.RepeatedInt32Extension, 0));
            Assert.AreEqual(202L, message.GetExtension(Unittest.RepeatedInt64Extension, 0));
            Assert.AreEqual(203U, message.GetExtension(Unittest.RepeatedUint32Extension, 0));
            Assert.AreEqual(204UL, message.GetExtension(Unittest.RepeatedUint64Extension, 0));
            Assert.AreEqual(205, message.GetExtension(Unittest.RepeatedSint32Extension, 0));
            Assert.AreEqual(206L, message.GetExtension(Unittest.RepeatedSint64Extension, 0));
            Assert.AreEqual(207U, message.GetExtension(Unittest.RepeatedFixed32Extension, 0));
            Assert.AreEqual(208UL, message.GetExtension(Unittest.RepeatedFixed64Extension, 0));
            Assert.AreEqual(209, message.GetExtension(Unittest.RepeatedSfixed32Extension, 0));
            Assert.AreEqual(210L, message.GetExtension(Unittest.RepeatedSfixed64Extension, 0));
            Assert.AreEqual(211F, message.GetExtension(Unittest.RepeatedFloatExtension, 0));
            Assert.AreEqual(212D, message.GetExtension(Unittest.RepeatedDoubleExtension, 0));
            Assert.AreEqual(true, message.GetExtension(Unittest.RepeatedBoolExtension, 0));
            Assert.AreEqual("215", message.GetExtension(Unittest.RepeatedStringExtension, 0));
            Assert.AreEqual(ToBytes("216"), message.GetExtension(Unittest.RepeatedBytesExtension, 0));

            Assert.AreEqual(217, message.GetExtension(Unittest.RepeatedGroupExtension, 0).A);
            Assert.AreEqual(218, message.GetExtension(Unittest.RepeatedNestedMessageExtension, 0).Bb);
            Assert.AreEqual(219, message.GetExtension(Unittest.RepeatedForeignMessageExtension, 0).C);
            Assert.AreEqual(220, message.GetExtension(Unittest.RepeatedImportMessageExtension, 0).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                            message.GetExtension(Unittest.RepeatedNestedEnumExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR,
                            message.GetExtension(Unittest.RepeatedForeignEnumExtension, 0));
            Assert.AreEqual(ImportEnum.IMPORT_BAR,
                            message.GetExtension(Unittest.RepeatedImportEnumExtension, 0));

            Assert.AreEqual("224", message.GetExtension(Unittest.RepeatedStringPieceExtension, 0));
            Assert.AreEqual("225", message.GetExtension(Unittest.RepeatedCordExtension, 0));

            Assert.AreEqual(301, message.GetExtension(Unittest.RepeatedInt32Extension, 1));
            Assert.AreEqual(302L, message.GetExtension(Unittest.RepeatedInt64Extension, 1));
            Assert.AreEqual(303U, message.GetExtension(Unittest.RepeatedUint32Extension, 1));
            Assert.AreEqual(304UL, message.GetExtension(Unittest.RepeatedUint64Extension, 1));
            Assert.AreEqual(305, message.GetExtension(Unittest.RepeatedSint32Extension, 1));
            Assert.AreEqual(306L, message.GetExtension(Unittest.RepeatedSint64Extension, 1));
            Assert.AreEqual(307U, message.GetExtension(Unittest.RepeatedFixed32Extension, 1));
            Assert.AreEqual(308UL, message.GetExtension(Unittest.RepeatedFixed64Extension, 1));
            Assert.AreEqual(309, message.GetExtension(Unittest.RepeatedSfixed32Extension, 1));
            Assert.AreEqual(310L, message.GetExtension(Unittest.RepeatedSfixed64Extension, 1));
            Assert.AreEqual(311F, message.GetExtension(Unittest.RepeatedFloatExtension, 1));
            Assert.AreEqual(312D, message.GetExtension(Unittest.RepeatedDoubleExtension, 1));
            Assert.AreEqual(false, message.GetExtension(Unittest.RepeatedBoolExtension, 1));
            Assert.AreEqual("315", message.GetExtension(Unittest.RepeatedStringExtension, 1));
            Assert.AreEqual(ToBytes("316"), message.GetExtension(Unittest.RepeatedBytesExtension, 1));

            Assert.AreEqual(317, message.GetExtension(Unittest.RepeatedGroupExtension, 1).A);
            Assert.AreEqual(318, message.GetExtension(Unittest.RepeatedNestedMessageExtension, 1).Bb);
            Assert.AreEqual(319, message.GetExtension(Unittest.RepeatedForeignMessageExtension, 1).C);
            Assert.AreEqual(320, message.GetExtension(Unittest.RepeatedImportMessageExtension, 1).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAZ,
                            message.GetExtension(Unittest.RepeatedNestedEnumExtension, 1));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ,
                            message.GetExtension(Unittest.RepeatedForeignEnumExtension, 1));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ,
                            message.GetExtension(Unittest.RepeatedImportEnumExtension, 1));

            Assert.AreEqual("324", message.GetExtension(Unittest.RepeatedStringPieceExtension, 1));
            Assert.AreEqual("325", message.GetExtension(Unittest.RepeatedCordExtension, 1));

            // -----------------------------------------------------------------

            Assert.IsTrue(message.HasExtension(Unittest.DefaultInt32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultInt64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultUint32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultUint64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultSint32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultSint64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultFixed32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultFixed64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultSfixed32Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultSfixed64Extension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultFloatExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultDoubleExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultBoolExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultStringExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultBytesExtension));

            Assert.IsTrue(message.HasExtension(Unittest.DefaultNestedEnumExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultForeignEnumExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultImportEnumExtension));

            Assert.IsTrue(message.HasExtension(Unittest.DefaultStringPieceExtension));
            Assert.IsTrue(message.HasExtension(Unittest.DefaultCordExtension));

            Assert.AreEqual(401, message.GetExtension(Unittest.DefaultInt32Extension));
            Assert.AreEqual(402L, message.GetExtension(Unittest.DefaultInt64Extension));
            Assert.AreEqual(403U, message.GetExtension(Unittest.DefaultUint32Extension));
            Assert.AreEqual(404UL, message.GetExtension(Unittest.DefaultUint64Extension));
            Assert.AreEqual(405, message.GetExtension(Unittest.DefaultSint32Extension));
            Assert.AreEqual(406L, message.GetExtension(Unittest.DefaultSint64Extension));
            Assert.AreEqual(407U, message.GetExtension(Unittest.DefaultFixed32Extension));
            Assert.AreEqual(408UL, message.GetExtension(Unittest.DefaultFixed64Extension));
            Assert.AreEqual(409, message.GetExtension(Unittest.DefaultSfixed32Extension));
            Assert.AreEqual(410L, message.GetExtension(Unittest.DefaultSfixed64Extension));
            Assert.AreEqual(411F, message.GetExtension(Unittest.DefaultFloatExtension));
            Assert.AreEqual(412D, message.GetExtension(Unittest.DefaultDoubleExtension));
            Assert.AreEqual(false, message.GetExtension(Unittest.DefaultBoolExtension));
            Assert.AreEqual("415", message.GetExtension(Unittest.DefaultStringExtension));
            Assert.AreEqual(ToBytes("416"), message.GetExtension(Unittest.DefaultBytesExtension));

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            message.GetExtension(Unittest.DefaultNestedEnumExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_FOO, message.GetExtension(Unittest.DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetExtension(Unittest.DefaultImportEnumExtension));

            Assert.AreEqual("424", message.GetExtension(Unittest.DefaultStringPieceExtension));
            Assert.AreEqual("425", message.GetExtension(Unittest.DefaultCordExtension));

            Assert.IsTrue(message.HasExtension(Unittest.OneofBytesExtension));
            Assert.AreEqual(ToBytes("604"), message.GetExtension(Unittest.OneofBytesExtension));
        }

        /// <summary>
        /// Modifies the repeated extensions of the given message to contain the values
        /// expected by AssertRepeatedExtensionsModified.
        /// </summary>
        internal static void ModifyRepeatedExtensions(TestAllExtensions.Builder message)
        {
            message.SetExtension(Unittest.RepeatedInt32Extension, 1, 501);
            message.SetExtension(Unittest.RepeatedInt64Extension, 1, 502L);
            message.SetExtension(Unittest.RepeatedUint32Extension, 1, 503U);
            message.SetExtension(Unittest.RepeatedUint64Extension, 1, 504UL);
            message.SetExtension(Unittest.RepeatedSint32Extension, 1, 505);
            message.SetExtension(Unittest.RepeatedSint64Extension, 1, 506L);
            message.SetExtension(Unittest.RepeatedFixed32Extension, 1, 507U);
            message.SetExtension(Unittest.RepeatedFixed64Extension, 1, 508UL);
            message.SetExtension(Unittest.RepeatedSfixed32Extension, 1, 509);
            message.SetExtension(Unittest.RepeatedSfixed64Extension, 1, 510L);
            message.SetExtension(Unittest.RepeatedFloatExtension, 1, 511F);
            message.SetExtension(Unittest.RepeatedDoubleExtension, 1, 512D);
            message.SetExtension(Unittest.RepeatedBoolExtension, 1, true);
            message.SetExtension(Unittest.RepeatedStringExtension, 1, "515");
            message.SetExtension(Unittest.RepeatedBytesExtension, 1, ToBytes("516"));

            message.SetExtension(Unittest.RepeatedGroupExtension, 1,
                                 RepeatedGroup_extension.CreateBuilder().SetA(517).Build());
            message.SetExtension(Unittest.RepeatedNestedMessageExtension, 1,
                                 TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(518).Build());
            message.SetExtension(Unittest.RepeatedForeignMessageExtension, 1,
                                 ForeignMessage.CreateBuilder().SetC(519).Build());
            message.SetExtension(Unittest.RepeatedImportMessageExtension, 1,
                                 ImportMessage.CreateBuilder().SetD(520).Build());

            message.SetExtension(Unittest.RepeatedNestedEnumExtension, 1, TestAllTypes.Types.NestedEnum.FOO);
            message.SetExtension(Unittest.RepeatedForeignEnumExtension, 1, ForeignEnum.FOREIGN_FOO);
            message.SetExtension(Unittest.RepeatedImportEnumExtension, 1, ImportEnum.IMPORT_FOO);

            message.SetExtension(Unittest.RepeatedStringPieceExtension, 1, "524");
            message.SetExtension(Unittest.RepeatedCordExtension, 1, "525");
        }

        /// <summary>
        /// Asserts that all repeated extensions are set to the values assigned by
        /// SetAllExtensions follwed by ModifyRepeatedExtensions.
        /// </summary>
        internal static void AssertRepeatedExtensionsModified(TestAllExtensions message)
        {
            // ModifyRepeatedFields only sets the second repeated element of each
            // field.  In addition to verifying this, we also verify that the first
            // element and size were *not* modified.
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedInt32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedInt64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedUint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedUint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedFixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedFixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSfixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedSfixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedFloatExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedDoubleExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedBoolExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedStringExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedBytesExtension));

            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedGroupExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedNestedMessageExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedForeignMessageExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedImportMessageExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedNestedEnumExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedForeignEnumExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedImportEnumExtension));

            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedStringPieceExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.RepeatedCordExtension));

            Assert.AreEqual(201, message.GetExtension(Unittest.RepeatedInt32Extension, 0));
            Assert.AreEqual(202L, message.GetExtension(Unittest.RepeatedInt64Extension, 0));
            Assert.AreEqual(203U, message.GetExtension(Unittest.RepeatedUint32Extension, 0));
            Assert.AreEqual(204UL, message.GetExtension(Unittest.RepeatedUint64Extension, 0));
            Assert.AreEqual(205, message.GetExtension(Unittest.RepeatedSint32Extension, 0));
            Assert.AreEqual(206L, message.GetExtension(Unittest.RepeatedSint64Extension, 0));
            Assert.AreEqual(207U, message.GetExtension(Unittest.RepeatedFixed32Extension, 0));
            Assert.AreEqual(208UL, message.GetExtension(Unittest.RepeatedFixed64Extension, 0));
            Assert.AreEqual(209, message.GetExtension(Unittest.RepeatedSfixed32Extension, 0));
            Assert.AreEqual(210L, message.GetExtension(Unittest.RepeatedSfixed64Extension, 0));
            Assert.AreEqual(211F, message.GetExtension(Unittest.RepeatedFloatExtension, 0));
            Assert.AreEqual(212D, message.GetExtension(Unittest.RepeatedDoubleExtension, 0));
            Assert.AreEqual(true, message.GetExtension(Unittest.RepeatedBoolExtension, 0));
            Assert.AreEqual("215", message.GetExtension(Unittest.RepeatedStringExtension, 0));
            Assert.AreEqual(ToBytes("216"), message.GetExtension(Unittest.RepeatedBytesExtension, 0));

            Assert.AreEqual(217, message.GetExtension(Unittest.RepeatedGroupExtension, 0).A);
            Assert.AreEqual(218, message.GetExtension(Unittest.RepeatedNestedMessageExtension, 0).Bb);
            Assert.AreEqual(219, message.GetExtension(Unittest.RepeatedForeignMessageExtension, 0).C);
            Assert.AreEqual(220, message.GetExtension(Unittest.RepeatedImportMessageExtension, 0).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                            message.GetExtension(Unittest.RepeatedNestedEnumExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR,
                            message.GetExtension(Unittest.RepeatedForeignEnumExtension, 0));
            Assert.AreEqual(ImportEnum.IMPORT_BAR,
                            message.GetExtension(Unittest.RepeatedImportEnumExtension, 0));

            Assert.AreEqual("224", message.GetExtension(Unittest.RepeatedStringPieceExtension, 0));
            Assert.AreEqual("225", message.GetExtension(Unittest.RepeatedCordExtension, 0));

            // Actually verify the second (modified) elements now.
            Assert.AreEqual(501, message.GetExtension(Unittest.RepeatedInt32Extension, 1));
            Assert.AreEqual(502L, message.GetExtension(Unittest.RepeatedInt64Extension, 1));
            Assert.AreEqual(503U, message.GetExtension(Unittest.RepeatedUint32Extension, 1));
            Assert.AreEqual(504UL, message.GetExtension(Unittest.RepeatedUint64Extension, 1));
            Assert.AreEqual(505, message.GetExtension(Unittest.RepeatedSint32Extension, 1));
            Assert.AreEqual(506L, message.GetExtension(Unittest.RepeatedSint64Extension, 1));
            Assert.AreEqual(507U, message.GetExtension(Unittest.RepeatedFixed32Extension, 1));
            Assert.AreEqual(508UL, message.GetExtension(Unittest.RepeatedFixed64Extension, 1));
            Assert.AreEqual(509, message.GetExtension(Unittest.RepeatedSfixed32Extension, 1));
            Assert.AreEqual(510L, message.GetExtension(Unittest.RepeatedSfixed64Extension, 1));
            Assert.AreEqual(511F, message.GetExtension(Unittest.RepeatedFloatExtension, 1));
            Assert.AreEqual(512D, message.GetExtension(Unittest.RepeatedDoubleExtension, 1));
            Assert.AreEqual(true, message.GetExtension(Unittest.RepeatedBoolExtension, 1));
            Assert.AreEqual("515", message.GetExtension(Unittest.RepeatedStringExtension, 1));
            Assert.AreEqual(ToBytes("516"), message.GetExtension(Unittest.RepeatedBytesExtension, 1));

            Assert.AreEqual(517, message.GetExtension(Unittest.RepeatedGroupExtension, 1).A);
            Assert.AreEqual(518, message.GetExtension(Unittest.RepeatedNestedMessageExtension, 1).Bb);
            Assert.AreEqual(519, message.GetExtension(Unittest.RepeatedForeignMessageExtension, 1).C);
            Assert.AreEqual(520, message.GetExtension(Unittest.RepeatedImportMessageExtension, 1).D);

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            message.GetExtension(Unittest.RepeatedNestedEnumExtension, 1));
            Assert.AreEqual(ForeignEnum.FOREIGN_FOO,
                            message.GetExtension(Unittest.RepeatedForeignEnumExtension, 1));
            Assert.AreEqual(ImportEnum.IMPORT_FOO,
                            message.GetExtension(Unittest.RepeatedImportEnumExtension, 1));

            Assert.AreEqual("524", message.GetExtension(Unittest.RepeatedStringPieceExtension, 1));
            Assert.AreEqual("525", message.GetExtension(Unittest.RepeatedCordExtension, 1));
        }

        internal static void AssertExtensionsClear(TestAllExtensions message)
        {
            // HasBlah() should initially be false for all optional fields.
            Assert.IsFalse(message.HasExtension(Unittest.OptionalInt32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalInt64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalUint32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalUint64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalSint32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalSint64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalFixed32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalFixed64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalSfixed32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalSfixed64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalFloatExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalDoubleExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalBoolExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalStringExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalBytesExtension));

            Assert.IsFalse(message.HasExtension(Unittest.OptionalGroupExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalNestedMessageExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalForeignMessageExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalImportMessageExtension));

            Assert.IsFalse(message.HasExtension(Unittest.OptionalNestedEnumExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalForeignEnumExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalImportEnumExtension));

            Assert.IsFalse(message.HasExtension(Unittest.OptionalStringPieceExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OptionalCordExtension));

            // Optional fields without defaults are set to zero or something like it.
            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(0L, message.GetExtension(Unittest.OptionalInt64Extension));
            Assert.AreEqual(0U, message.GetExtension(Unittest.OptionalUint32Extension));
            Assert.AreEqual(0UL, message.GetExtension(Unittest.OptionalUint64Extension));
            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalSint32Extension));
            Assert.AreEqual(0L, message.GetExtension(Unittest.OptionalSint64Extension));
            Assert.AreEqual(0U, message.GetExtension(Unittest.OptionalFixed32Extension));
            Assert.AreEqual(0UL, message.GetExtension(Unittest.OptionalFixed64Extension));
            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalSfixed32Extension));
            Assert.AreEqual(0L, message.GetExtension(Unittest.OptionalSfixed64Extension));
            Assert.AreEqual(0F, message.GetExtension(Unittest.OptionalFloatExtension));
            Assert.AreEqual(0D, message.GetExtension(Unittest.OptionalDoubleExtension));
            Assert.AreEqual(false, message.GetExtension(Unittest.OptionalBoolExtension));
            Assert.AreEqual("", message.GetExtension(Unittest.OptionalStringExtension));
            Assert.AreEqual(ByteString.Empty, message.GetExtension(Unittest.OptionalBytesExtension));

            // Embedded messages should also be clear.
            Assert.IsFalse(message.GetExtension(Unittest.OptionalGroupExtension).HasA);
            Assert.IsFalse(message.GetExtension(Unittest.OptionalNestedMessageExtension).HasBb);
            Assert.IsFalse(message.GetExtension(Unittest.OptionalForeignMessageExtension).HasC);
            Assert.IsFalse(message.GetExtension(Unittest.OptionalImportMessageExtension).HasD);

            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalGroupExtension).A);
            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalNestedMessageExtension).Bb);
            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalForeignMessageExtension).C);
            Assert.AreEqual(0, message.GetExtension(Unittest.OptionalImportMessageExtension).D);

            // Enums without defaults are set to the first value in the enum.
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            message.GetExtension(Unittest.OptionalNestedEnumExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_FOO,
                            message.GetExtension(Unittest.OptionalForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_FOO, message.GetExtension(Unittest.OptionalImportEnumExtension));

            Assert.AreEqual("", message.GetExtension(Unittest.OptionalStringPieceExtension));
            Assert.AreEqual("", message.GetExtension(Unittest.OptionalCordExtension));

            // Repeated fields are empty.
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedInt32Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedInt64Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedUint32Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedUint64Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedSint32Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedSint64Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedFixed32Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedFixed64Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedSfixed32Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedSfixed64Extension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedFloatExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedDoubleExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedBoolExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedStringExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedBytesExtension));

            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedGroupExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedNestedMessageExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedForeignMessageExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedImportMessageExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedNestedEnumExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedForeignEnumExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedImportEnumExtension));

            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedStringPieceExtension));
            Assert.AreEqual(0, message.GetExtensionCount(Unittest.RepeatedCordExtension));

            // HasBlah() should also be false for all default fields.
            Assert.IsFalse(message.HasExtension(Unittest.DefaultInt32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultInt64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultUint32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultUint64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultSint32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultSint64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultFixed32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultFixed64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultSfixed32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultSfixed64Extension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultFloatExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultDoubleExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultBoolExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultStringExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultBytesExtension));

            Assert.IsFalse(message.HasExtension(Unittest.DefaultNestedEnumExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultForeignEnumExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultImportEnumExtension));

            Assert.IsFalse(message.HasExtension(Unittest.DefaultStringPieceExtension));
            Assert.IsFalse(message.HasExtension(Unittest.DefaultCordExtension));

            // Fields with defaults have their default values (duh).
            Assert.AreEqual(41, message.GetExtension(Unittest.DefaultInt32Extension));
            Assert.AreEqual(42L, message.GetExtension(Unittest.DefaultInt64Extension));
            Assert.AreEqual(43U, message.GetExtension(Unittest.DefaultUint32Extension));
            Assert.AreEqual(44UL, message.GetExtension(Unittest.DefaultUint64Extension));
            Assert.AreEqual(-45, message.GetExtension(Unittest.DefaultSint32Extension));
            Assert.AreEqual(46L, message.GetExtension(Unittest.DefaultSint64Extension));
            Assert.AreEqual(47U, message.GetExtension(Unittest.DefaultFixed32Extension));
            Assert.AreEqual(48UL, message.GetExtension(Unittest.DefaultFixed64Extension));
            Assert.AreEqual(49, message.GetExtension(Unittest.DefaultSfixed32Extension));
            Assert.AreEqual(-50L, message.GetExtension(Unittest.DefaultSfixed64Extension));
            Assert.AreEqual(51.5F, message.GetExtension(Unittest.DefaultFloatExtension));
            Assert.AreEqual(52e3D, message.GetExtension(Unittest.DefaultDoubleExtension));
            Assert.AreEqual(true, message.GetExtension(Unittest.DefaultBoolExtension));
            Assert.AreEqual("hello", message.GetExtension(Unittest.DefaultStringExtension));
            Assert.AreEqual(ToBytes("world"), message.GetExtension(Unittest.DefaultBytesExtension));

            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                            message.GetExtension(Unittest.DefaultNestedEnumExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetExtension(Unittest.DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAR, message.GetExtension(Unittest.DefaultImportEnumExtension));

            Assert.AreEqual("abc", message.GetExtension(Unittest.DefaultStringPieceExtension));
            Assert.AreEqual("123", message.GetExtension(Unittest.DefaultCordExtension));

            Assert.IsFalse(message.HasExtension(Unittest.OneofUint32Extension));
            Assert.IsFalse(message.HasExtension(Unittest.OneofNestedMessageExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OneofStringExtension));
            Assert.IsFalse(message.HasExtension(Unittest.OneofBytesExtension));
        }

        /// <summary>
        /// Set every field of the specified message to a unique value.
        /// </summary>
        public static void SetPackedFields(TestPackedTypes.Builder message)
        {
            message.AddPackedInt32(601);
            message.AddPackedInt64(602);
            message.AddPackedUint32(603);
            message.AddPackedUint64(604);
            message.AddPackedSint32(605);
            message.AddPackedSint64(606);
            message.AddPackedFixed32(607);
            message.AddPackedFixed64(608);
            message.AddPackedSfixed32(609);
            message.AddPackedSfixed64(610);
            message.AddPackedFloat(611);
            message.AddPackedDouble(612);
            message.AddPackedBool(true);
            message.AddPackedEnum(ForeignEnum.FOREIGN_BAR);
            // Add a second one of each field.
            message.AddPackedInt32(701);
            message.AddPackedInt64(702);
            message.AddPackedUint32(703);
            message.AddPackedUint64(704);
            message.AddPackedSint32(705);
            message.AddPackedSint64(706);
            message.AddPackedFixed32(707);
            message.AddPackedFixed64(708);
            message.AddPackedSfixed32(709);
            message.AddPackedSfixed64(710);
            message.AddPackedFloat(711);
            message.AddPackedDouble(712);
            message.AddPackedBool(false);
            message.AddPackedEnum(ForeignEnum.FOREIGN_BAZ);
        }

        /// <summary>
        /// Asserts that all the fields of the specified message are set to the values assigned
        /// in SetPackedFields.
        /// </summary>
        public static void AssertPackedFieldsSet(TestPackedTypes message)
        {
            Assert.AreEqual(2, message.PackedInt32Count);
            Assert.AreEqual(2, message.PackedInt64Count);
            Assert.AreEqual(2, message.PackedUint32Count);
            Assert.AreEqual(2, message.PackedUint64Count);
            Assert.AreEqual(2, message.PackedSint32Count);
            Assert.AreEqual(2, message.PackedSint64Count);
            Assert.AreEqual(2, message.PackedFixed32Count);
            Assert.AreEqual(2, message.PackedFixed64Count);
            Assert.AreEqual(2, message.PackedSfixed32Count);
            Assert.AreEqual(2, message.PackedSfixed64Count);
            Assert.AreEqual(2, message.PackedFloatCount);
            Assert.AreEqual(2, message.PackedDoubleCount);
            Assert.AreEqual(2, message.PackedBoolCount);
            Assert.AreEqual(2, message.PackedEnumCount);
            Assert.AreEqual(601, message.GetPackedInt32(0));
            Assert.AreEqual(602, message.GetPackedInt64(0));
            Assert.AreEqual(603u, message.GetPackedUint32(0));
            Assert.AreEqual(604u, message.GetPackedUint64(0));
            Assert.AreEqual(605, message.GetPackedSint32(0));
            Assert.AreEqual(606, message.GetPackedSint64(0));
            Assert.AreEqual(607u, message.GetPackedFixed32(0));
            Assert.AreEqual(608u, message.GetPackedFixed64(0));
            Assert.AreEqual(609, message.GetPackedSfixed32(0));
            Assert.AreEqual(610, message.GetPackedSfixed64(0));
            Assert.AreEqual(611f, message.GetPackedFloat(0));
            Assert.AreEqual(612d, message.GetPackedDouble(0));
            Assert.AreEqual(true, message.GetPackedBool(0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetPackedEnum(0));
            Assert.AreEqual(701, message.GetPackedInt32(1));
            Assert.AreEqual(702, message.GetPackedInt64(1));
            Assert.AreEqual(703u, message.GetPackedUint32(1));
            Assert.AreEqual(704u, message.GetPackedUint64(1));
            Assert.AreEqual(705, message.GetPackedSint32(1));
            Assert.AreEqual(706, message.GetPackedSint64(1));
            Assert.AreEqual(707u, message.GetPackedFixed32(1));
            Assert.AreEqual(708u, message.GetPackedFixed64(1));
            Assert.AreEqual(709, message.GetPackedSfixed32(1));
            Assert.AreEqual(710, message.GetPackedSfixed64(1));
            Assert.AreEqual(711f, message.GetPackedFloat(1));
            Assert.AreEqual(712d, message.GetPackedDouble(1));
            Assert.AreEqual(false, message.GetPackedBool(1));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetPackedEnum(1));
        }
        /// <summary>
        /// Asserts that all the fields of the specified message are set to the values assigned
        /// in SetPackedFields.
        /// </summary>
        public static void AssertUnpackedFieldsSet(TestUnpackedTypes message)
        {
            Assert.AreEqual(2, message.UnpackedInt32Count);
            Assert.AreEqual(2, message.UnpackedInt64Count);
            Assert.AreEqual(2, message.UnpackedUint32Count);
            Assert.AreEqual(2, message.UnpackedUint64Count);
            Assert.AreEqual(2, message.UnpackedSint32Count);
            Assert.AreEqual(2, message.UnpackedSint64Count);
            Assert.AreEqual(2, message.UnpackedFixed32Count);
            Assert.AreEqual(2, message.UnpackedFixed64Count);
            Assert.AreEqual(2, message.UnpackedSfixed32Count);
            Assert.AreEqual(2, message.UnpackedSfixed64Count);
            Assert.AreEqual(2, message.UnpackedFloatCount);
            Assert.AreEqual(2, message.UnpackedDoubleCount);
            Assert.AreEqual(2, message.UnpackedBoolCount);
            Assert.AreEqual(2, message.UnpackedEnumCount);
            Assert.AreEqual(601, message.GetUnpackedInt32(0));
            Assert.AreEqual(602, message.GetUnpackedInt64(0));
            Assert.AreEqual(603u, message.GetUnpackedUint32(0));
            Assert.AreEqual(604u, message.GetUnpackedUint64(0));
            Assert.AreEqual(605, message.GetUnpackedSint32(0));
            Assert.AreEqual(606, message.GetUnpackedSint64(0));
            Assert.AreEqual(607u, message.GetUnpackedFixed32(0));
            Assert.AreEqual(608u, message.GetUnpackedFixed64(0));
            Assert.AreEqual(609, message.GetUnpackedSfixed32(0));
            Assert.AreEqual(610, message.GetUnpackedSfixed64(0));
            Assert.AreEqual(611f, message.GetUnpackedFloat(0));
            Assert.AreEqual(612d, message.GetUnpackedDouble(0));
            Assert.AreEqual(true, message.GetUnpackedBool(0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetUnpackedEnum(0));
            Assert.AreEqual(701, message.GetUnpackedInt32(1));
            Assert.AreEqual(702, message.GetUnpackedInt64(1));
            Assert.AreEqual(703u, message.GetUnpackedUint32(1));
            Assert.AreEqual(704u, message.GetUnpackedUint64(1));
            Assert.AreEqual(705, message.GetUnpackedSint32(1));
            Assert.AreEqual(706, message.GetUnpackedSint64(1));
            Assert.AreEqual(707u, message.GetUnpackedFixed32(1));
            Assert.AreEqual(708u, message.GetUnpackedFixed64(1));
            Assert.AreEqual(709, message.GetUnpackedSfixed32(1));
            Assert.AreEqual(710, message.GetUnpackedSfixed64(1));
            Assert.AreEqual(711f, message.GetUnpackedFloat(1));
            Assert.AreEqual(712d, message.GetUnpackedDouble(1));
            Assert.AreEqual(false, message.GetUnpackedBool(1));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetUnpackedEnum(1));
        }

        public static void SetPackedExtensions(TestPackedExtensions.Builder message)
        {
            message.AddExtension(Unittest.PackedInt32Extension, 601);
            message.AddExtension(Unittest.PackedInt64Extension, 602L);
            message.AddExtension(Unittest.PackedUint32Extension, 603U);
            message.AddExtension(Unittest.PackedUint64Extension, 604UL);
            message.AddExtension(Unittest.PackedSint32Extension, 605);
            message.AddExtension(Unittest.PackedSint64Extension, 606L);
            message.AddExtension(Unittest.PackedFixed32Extension, 607U);
            message.AddExtension(Unittest.PackedFixed64Extension, 608UL);
            message.AddExtension(Unittest.PackedSfixed32Extension, 609);
            message.AddExtension(Unittest.PackedSfixed64Extension, 610L);
            message.AddExtension(Unittest.PackedFloatExtension, 611F);
            message.AddExtension(Unittest.PackedDoubleExtension, 612D);
            message.AddExtension(Unittest.PackedBoolExtension, true);
            message.AddExtension(Unittest.PackedEnumExtension, ForeignEnum.FOREIGN_BAR);
            // Add a second one of each field.
            message.AddExtension(Unittest.PackedInt32Extension, 701);
            message.AddExtension(Unittest.PackedInt64Extension, 702L);
            message.AddExtension(Unittest.PackedUint32Extension, 703U);
            message.AddExtension(Unittest.PackedUint64Extension, 704UL);
            message.AddExtension(Unittest.PackedSint32Extension, 705);
            message.AddExtension(Unittest.PackedSint64Extension, 706L);
            message.AddExtension(Unittest.PackedFixed32Extension, 707U);
            message.AddExtension(Unittest.PackedFixed64Extension, 708UL);
            message.AddExtension(Unittest.PackedSfixed32Extension, 709);
            message.AddExtension(Unittest.PackedSfixed64Extension, 710L);
            message.AddExtension(Unittest.PackedFloatExtension, 711F);
            message.AddExtension(Unittest.PackedDoubleExtension, 712D);
            message.AddExtension(Unittest.PackedBoolExtension, false);
            message.AddExtension(Unittest.PackedEnumExtension, ForeignEnum.FOREIGN_BAZ);
        }

        public static void AssertPackedExtensionsSet(TestPackedExtensions message)
        {
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedInt32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedInt64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedUint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedUint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedSint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedSint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedFixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedFixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedSfixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedSfixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedFloatExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedDoubleExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedBoolExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.PackedEnumExtension));
            Assert.AreEqual(601, message.GetExtension(Unittest.PackedInt32Extension, 0));
            Assert.AreEqual(602L, message.GetExtension(Unittest.PackedInt64Extension, 0));
            Assert.AreEqual(603u, message.GetExtension(Unittest.PackedUint32Extension, 0));
            Assert.AreEqual(604uL, message.GetExtension(Unittest.PackedUint64Extension, 0));
            Assert.AreEqual(605, message.GetExtension(Unittest.PackedSint32Extension, 0));
            Assert.AreEqual(606L, message.GetExtension(Unittest.PackedSint64Extension, 0));
            Assert.AreEqual(607u, message.GetExtension(Unittest.PackedFixed32Extension, 0));
            Assert.AreEqual(608uL, message.GetExtension(Unittest.PackedFixed64Extension, 0));
            Assert.AreEqual(609, message.GetExtension(Unittest.PackedSfixed32Extension, 0));
            Assert.AreEqual(610L, message.GetExtension(Unittest.PackedSfixed64Extension, 0));
            Assert.AreEqual(611F, message.GetExtension(Unittest.PackedFloatExtension, 0));
            Assert.AreEqual(612D, message.GetExtension(Unittest.PackedDoubleExtension, 0));
            Assert.AreEqual(true, message.GetExtension(Unittest.PackedBoolExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR,
                            message.GetExtension(Unittest.PackedEnumExtension, 0));
            Assert.AreEqual(701, message.GetExtension(Unittest.PackedInt32Extension, 1));
            Assert.AreEqual(702L, message.GetExtension(Unittest.PackedInt64Extension, 1));
            Assert.AreEqual(703u, message.GetExtension(Unittest.PackedUint32Extension, 1));
            Assert.AreEqual(704uL, message.GetExtension(Unittest.PackedUint64Extension, 1));
            Assert.AreEqual(705, message.GetExtension(Unittest.PackedSint32Extension, 1));
            Assert.AreEqual(706L, message.GetExtension(Unittest.PackedSint64Extension, 1));
            Assert.AreEqual(707u, message.GetExtension(Unittest.PackedFixed32Extension, 1));
            Assert.AreEqual(708uL, message.GetExtension(Unittest.PackedFixed64Extension, 1));
            Assert.AreEqual(709, message.GetExtension(Unittest.PackedSfixed32Extension, 1));
            Assert.AreEqual(710L, message.GetExtension(Unittest.PackedSfixed64Extension, 1));
            Assert.AreEqual(711F, message.GetExtension(Unittest.PackedFloatExtension, 1));
            Assert.AreEqual(712D, message.GetExtension(Unittest.PackedDoubleExtension, 1));
            Assert.AreEqual(false, message.GetExtension(Unittest.PackedBoolExtension, 1));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(Unittest.PackedEnumExtension, 1));
        }

        public static void AssertUnpackedExtensionsSet(TestUnpackedExtensions message)
        {
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedInt32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedInt64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedUint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedUint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedSint32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedSint64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedFixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedFixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedSfixed32Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedSfixed64Extension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedFloatExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedDoubleExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedBoolExtension));
            Assert.AreEqual(2, message.GetExtensionCount(Unittest.UnpackedEnumExtension));
            Assert.AreEqual(601, message.GetExtension(Unittest.UnpackedInt32Extension, 0));
            Assert.AreEqual(602L, message.GetExtension(Unittest.UnpackedInt64Extension, 0));
            Assert.AreEqual(603u, message.GetExtension(Unittest.UnpackedUint32Extension, 0));
            Assert.AreEqual(604uL, message.GetExtension(Unittest.UnpackedUint64Extension, 0));
            Assert.AreEqual(605, message.GetExtension(Unittest.UnpackedSint32Extension, 0));
            Assert.AreEqual(606L, message.GetExtension(Unittest.UnpackedSint64Extension, 0));
            Assert.AreEqual(607u, message.GetExtension(Unittest.UnpackedFixed32Extension, 0));
            Assert.AreEqual(608uL, message.GetExtension(Unittest.UnpackedFixed64Extension, 0));
            Assert.AreEqual(609, message.GetExtension(Unittest.UnpackedSfixed32Extension, 0));
            Assert.AreEqual(610L, message.GetExtension(Unittest.UnpackedSfixed64Extension, 0));
            Assert.AreEqual(611F, message.GetExtension(Unittest.UnpackedFloatExtension, 0));
            Assert.AreEqual(612D, message.GetExtension(Unittest.UnpackedDoubleExtension, 0));
            Assert.AreEqual(true, message.GetExtension(Unittest.UnpackedBoolExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAR, message.GetExtension(Unittest.UnpackedEnumExtension, 0));
            Assert.AreEqual(701, message.GetExtension(Unittest.UnpackedInt32Extension, 1));
            Assert.AreEqual(702L, message.GetExtension(Unittest.UnpackedInt64Extension, 1));
            Assert.AreEqual(703u, message.GetExtension(Unittest.UnpackedUint32Extension, 1));
            Assert.AreEqual(704uL, message.GetExtension(Unittest.UnpackedUint64Extension, 1));
            Assert.AreEqual(705, message.GetExtension(Unittest.UnpackedSint32Extension, 1));
            Assert.AreEqual(706L, message.GetExtension(Unittest.UnpackedSint64Extension, 1));
            Assert.AreEqual(707u, message.GetExtension(Unittest.UnpackedFixed32Extension, 1));
            Assert.AreEqual(708uL, message.GetExtension(Unittest.UnpackedFixed64Extension, 1));
            Assert.AreEqual(709, message.GetExtension(Unittest.UnpackedSfixed32Extension, 1));
            Assert.AreEqual(710L, message.GetExtension(Unittest.UnpackedSfixed64Extension, 1));
            Assert.AreEqual(711F, message.GetExtension(Unittest.UnpackedFloatExtension, 1));
            Assert.AreEqual(712D, message.GetExtension(Unittest.UnpackedDoubleExtension, 1));
            Assert.AreEqual(false, message.GetExtension(Unittest.UnpackedBoolExtension, 1));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(Unittest.UnpackedEnumExtension, 1));
        }

        public static void AssertAtMostOneFieldSetOneof(TestOneof2 message)
        {
            int count = 0;
            if (message.HasFooInt) { ++count; }
            if (message.HasFooString) { ++count; }
            if (message.HasFooCord) { ++count; }
            if (message.HasFooStringPiece) { ++count; }
            if (message.HasFooBytes) { ++count; }
            if (message.HasFooEnum) { ++count; }
            if (message.HasFooMessage) { ++count; }
            if (message.HasFooGroup) { ++count; }
            if (message.HasFooLazyMessage) { ++count; }
            Assert.True(count <= 1);

            count = 0;
            if (message.HasBarInt) { ++count; }
            if (message.HasBarString) { ++count; }
            if (message.HasBarCord) { ++count; }
            if (message.HasBarStringPiece) { ++count; }
            if (message.HasBarBytes) { ++count; }
            if (message.HasBarEnum) { ++count; }
            Assert.True(count <= 1);

            switch (message.FooCase)
            {
                case TestOneof2.FooOneofCase.FooInt:
                    {
                        Assert.True(message.HasFooInt);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooString:
                    {
                        Assert.True(message.HasFooString);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooCord:
                    {
                        Assert.True(message.HasFooCord);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooBytes:
                    {
                        Assert.True(message.HasFooBytes);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooEnum:
                    {
                        Assert.True(message.HasFooEnum);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooMessage:
                    {
                        Assert.True(message.HasFooMessage);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooGroup:
                    {
                        Assert.True(message.HasFooGroup);
                        break;
                    }
                case TestOneof2.FooOneofCase.FooLazyMessage:
                    {
                        Assert.True(message.HasFooLazyMessage);
                        break;
                    }
                case TestOneof2.FooOneofCase.None: { break; }
            }
        }

        private static readonly string[] TestCultures = {"en-US", "en-GB", "fr-FR", "de-DE"};

        public delegate void CultureAction();

        public static void TestInMultipleCultures(CultureAction test)
        {
#if COMPACT_FRAMEWORK
            test();
#else
            CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
            foreach (string culture in TestCultures)
            {
                try
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
                    test();
                }
                finally
                {
                    Thread.CurrentThread.CurrentCulture = originalCulture;
                }
            }
#endif
        }

        /// <summary>
        /// Helper to construct a byte array from a bunch of bytes.
        /// </summary>
        internal static byte[] Bytes(params byte[] bytesAsInts)
        {
            byte[] bytes = new byte[bytesAsInts.Length];
            for (int i = 0; i < bytesAsInts.Length; i++)
            {
                bytes[i] = (byte) bytesAsInts[i];
            }
            return bytes;
        }
    }
}