RepeatedFieldTest.cs 25.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2015 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// 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;
34
using System.Collections;
35
using System.Collections.Generic;
36 37
using System.IO;
using System.Linq;
38
using System.Text;
39
using Google.Protobuf.TestProtos;
40
using Google.Protobuf.WellKnownTypes;
41 42
using NUnit.Framework;

43
namespace Google.Protobuf.Collections
44 45 46 47 48 49 50
{
    public class RepeatedFieldTest
    {
        [Test]
        public void NullValuesRejected()
        {
            var list = new RepeatedField<string>();
51 52
            Assert.Throws<ArgumentNullException>(() => list.Add((string)null));
            Assert.Throws<ArgumentNullException>(() => list.Add((IEnumerable<string>)null));
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            Assert.Throws<ArgumentNullException>(() => list.Add((RepeatedField<string>)null));
            Assert.Throws<ArgumentNullException>(() => list.Contains(null));
            Assert.Throws<ArgumentNullException>(() => list.IndexOf(null));
        }

        [Test]
        public void Add_SingleItem()
        {
            var list = new RepeatedField<string>();
            list.Add("foo");
            Assert.AreEqual(1, list.Count);
            Assert.AreEqual("foo", list[0]);
        }

        [Test]
        public void Add_Sequence()
        {
            var list = new RepeatedField<string>();
            list.Add(new[] { "foo", "bar" });
            Assert.AreEqual(2, list.Count);
            Assert.AreEqual("foo", list[0]);
            Assert.AreEqual("bar", list[1]);
        }

        [Test]
        public void Add_RepeatedField()
        {
80
            var list = new RepeatedField<string> { "original" };
81
            list.Add(new RepeatedField<string> { "foo", "bar" });
82 83 84 85
            Assert.AreEqual(3, list.Count);
            Assert.AreEqual("original", list[0]);
            Assert.AreEqual("foo", list[1]);
            Assert.AreEqual("bar", list[2]);
86
        }
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        [Test]
        public void RemoveAt_Valid()
        {
            var list = new RepeatedField<string> { "first", "second", "third" };
            list.RemoveAt(1);
            CollectionAssert.AreEqual(new[] { "first", "third" }, list);
            // Just check that these don't throw...
            list.RemoveAt(list.Count - 1); // Now the count will be 1...
            list.RemoveAt(0);
            Assert.AreEqual(0, list.Count);
        }

        [Test]
        public void RemoveAt_Invalid()
        {
            var list = new RepeatedField<string> { "first", "second", "third" };
            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(3));
        }

        [Test]
        public void Insert_Valid()
        {
            var list = new RepeatedField<string> { "first", "second" };
            list.Insert(1, "middle");
            CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, list);
            list.Insert(3, "end");
            CollectionAssert.AreEqual(new[] { "first", "middle", "second", "end" }, list);
            list.Insert(0, "start");
            CollectionAssert.AreEqual(new[] { "start", "first", "middle", "second", "end" }, list);
        }

        [Test]
        public void Insert_Invalid()
        {
            var list = new RepeatedField<string> { "first", "second" };
            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, "foo"));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(3, "foo"));
            Assert.Throws<ArgumentNullException>(() => list.Insert(0, null));
        }

        [Test]
        public void Equals_RepeatedField()
        {
            var list = new RepeatedField<string> { "first", "second" };
            Assert.IsFalse(list.Equals((RepeatedField<string>) null));
            Assert.IsTrue(list.Equals(list));
            Assert.IsFalse(list.Equals(new RepeatedField<string> { "first", "third" }));
            Assert.IsFalse(list.Equals(new RepeatedField<string> { "first" }));
            Assert.IsTrue(list.Equals(new RepeatedField<string> { "first", "second" }));
        }

        [Test]
        public void Equals_Object()
        {
            var list = new RepeatedField<string> { "first", "second" };
            Assert.IsFalse(list.Equals((object) null));
            Assert.IsTrue(list.Equals((object) list));
            Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "first", "third" }));
            Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "first" }));
            Assert.IsTrue(list.Equals((object) new RepeatedField<string> { "first", "second" }));
            Assert.IsFalse(list.Equals(new object()));
        }

        [Test]
        public void GetEnumerator_GenericInterface()
        {
            IEnumerable<string> list = new RepeatedField<string> { "first", "second" };
            // Select gets rid of the optimizations in ToList...
            CollectionAssert.AreEqual(new[] { "first", "second" }, list.Select(x => x).ToList());
        }

        [Test]
        public void GetEnumerator_NonGenericInterface()
        {
            IEnumerable list = new RepeatedField<string> { "first", "second" };
            CollectionAssert.AreEqual(new[] { "first", "second" }, list.Cast<object>().ToList());
        }

        [Test]
        public void CopyTo()
        {
            var list = new RepeatedField<string> { "first", "second" };
            string[] stringArray = new string[4];
            list.CopyTo(stringArray, 1);
            CollectionAssert.AreEqual(new[] { null, "first", "second", null }, stringArray);
        }

        [Test]
        public void Indexer_Get()
        {
            var list = new RepeatedField<string> { "first", "second" };
            Assert.AreEqual("first", list[0]);
            Assert.AreEqual("second", list[1]);
            Assert.Throws<ArgumentOutOfRangeException>(() => list[-1].GetHashCode());
            Assert.Throws<ArgumentOutOfRangeException>(() => list[2].GetHashCode());
        }

        [Test]
        public void Indexer_Set()
        {
            var list = new RepeatedField<string> { "first", "second" };
            list[0] = "changed";
            Assert.AreEqual("changed", list[0]);
            Assert.Throws<ArgumentNullException>(() => list[0] = null);
            Assert.Throws<ArgumentOutOfRangeException>(() => list[-1] = "bad");
            Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = "bad");
        }

197 198 199 200 201 202 203
        [Test]
        public void Clone_ReturnsMutable()
        {
            var list = new RepeatedField<int> { 0 };
            var clone = list.Clone();
            clone[0] = 1;
        }
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        [Test]
        public void Enumerator()
        {
            var list = new RepeatedField<string> { "first", "second" };
            using (var enumerator = list.GetEnumerator())
            {
                Assert.IsTrue(enumerator.MoveNext());
                Assert.AreEqual("first", enumerator.Current);
                Assert.IsTrue(enumerator.MoveNext());
                Assert.AreEqual("second", enumerator.Current);
                Assert.IsFalse(enumerator.MoveNext());
                Assert.IsFalse(enumerator.MoveNext());
            }
        }

220 221 222 223 224
        [Test]
        public void AddEntriesFrom_PackedInt32()
        {
            uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var stream = new MemoryStream();
225
            var output = new CodedOutputStream(stream);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            var length = CodedOutputStream.ComputeInt32Size(10)
                + CodedOutputStream.ComputeInt32Size(999)
                + CodedOutputStream.ComputeInt32Size(-1000);
            output.WriteTag(packedTag);
            output.WriteRawVarint32((uint) length);
            output.WriteInt32(10);
            output.WriteInt32(999);
            output.WriteInt32(-1000);
            output.Flush();
            stream.Position = 0;

            // Deliberately "expecting" a non-packed tag, but we detect that the data is
            // actually packed.
            uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<int>();
241
            var input = new CodedInputStream(stream);
242 243 244 245 246 247 248 249 250 251 252
            input.AssertNextTag(packedTag);
            field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag));
            CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }

        [Test]
        public void AddEntriesFrom_NonPackedInt32()
        {
            uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var stream = new MemoryStream();
253
            var output = new CodedOutputStream(stream);
254 255 256 257 258 259 260 261 262 263 264 265 266
            output.WriteTag(nonPackedTag);
            output.WriteInt32(10);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(999);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(-1000); // Just for variety...
            output.Flush();
            stream.Position = 0;

            // Deliberately "expecting" a packed tag, but we detect that the data is
            // actually not packed.
            uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<int>();
267
            var input = new CodedInputStream(stream);
268 269 270 271 272 273 274 275 276 277 278
            input.AssertNextTag(nonPackedTag);
            field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
            CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }

        [Test]
        public void AddEntriesFrom_String()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var stream = new MemoryStream();
279
            var output = new CodedOutputStream(stream);
280 281 282 283 284 285 286 287 288 289
            output.WriteTag(tag);
            output.WriteString("Foo");
            output.WriteTag(tag);
            output.WriteString("");
            output.WriteTag(tag);
            output.WriteString("Bar");
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField<string>();
290
            var input = new CodedInputStream(stream);
291 292 293 294 295 296 297 298 299 300 301 302 303 304
            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForString(tag));
            CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field);
            Assert.IsTrue(input.IsAtEnd);
        }

        [Test]
        public void AddEntriesFrom_Message()
        {
            var message1 = new ForeignMessage { C = 2000 };
            var message2 = new ForeignMessage { C = -250 };

            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var stream = new MemoryStream();
305
            var output = new CodedOutputStream(stream);
306 307 308 309 310 311 312 313
            output.WriteTag(tag);
            output.WriteMessage(message1);
            output.WriteTag(tag);
            output.WriteMessage(message2);
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField<ForeignMessage>();
314
            var input = new CodedInputStream(stream);
315 316 317 318 319 320 321 322 323 324 325 326
            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            CollectionAssert.AreEqual(new[] { message1, message2}, field);
            Assert.IsTrue(input.IsAtEnd);
        }

        [Test]
        public void WriteTo_PackedInt32()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<int> { 10, 1000, 1000000 };
            var stream = new MemoryStream();
327
            var output = new CodedOutputStream(stream);
328 329 330 331
            field.WriteTo(output, FieldCodec.ForInt32(tag));
            output.Flush();
            stream.Position = 0;

332
            var input = new CodedInputStream(stream);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            input.AssertNextTag(tag);
            var length = input.ReadLength();
            Assert.AreEqual(10, input.ReadInt32());
            Assert.AreEqual(1000, input.ReadInt32());
            Assert.AreEqual(1000000, input.ReadInt32());
            Assert.IsTrue(input.IsAtEnd);
            Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length);
        }

        [Test]
        public void WriteTo_NonPackedInt32()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var field = new RepeatedField<int> { 10, 1000, 1000000};
            var stream = new MemoryStream();
348
            var output = new CodedOutputStream(stream);
349 350 351 352
            field.WriteTo(output, FieldCodec.ForInt32(tag));
            output.Flush();
            stream.Position = 0;

353
            var input = new CodedInputStream(stream);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
            input.AssertNextTag(tag);
            Assert.AreEqual(10, input.ReadInt32());
            input.AssertNextTag(tag);
            Assert.AreEqual(1000, input.ReadInt32());
            input.AssertNextTag(tag);
            Assert.AreEqual(1000000, input.ReadInt32());
            Assert.IsTrue(input.IsAtEnd);
        }

        [Test]
        public void WriteTo_String()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<string> { "Foo", "", "Bar" };
            var stream = new MemoryStream();
369
            var output = new CodedOutputStream(stream);
370 371 372 373
            field.WriteTo(output, FieldCodec.ForString(tag));
            output.Flush();
            stream.Position = 0;

374
            var input = new CodedInputStream(stream);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
            input.AssertNextTag(tag);
            Assert.AreEqual("Foo", input.ReadString());
            input.AssertNextTag(tag);
            Assert.AreEqual("", input.ReadString());
            input.AssertNextTag(tag);
            Assert.AreEqual("Bar", input.ReadString());
            Assert.IsTrue(input.IsAtEnd);
        }

        [Test]
        public void WriteTo_Message()
        {
            var message1 = new ForeignMessage { C = 20 };
            var message2 = new ForeignMessage { C = 25 };
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<ForeignMessage> { message1, message2 };
            var stream = new MemoryStream();
392
            var output = new CodedOutputStream(stream);
393 394 395 396
            field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            output.Flush();
            stream.Position = 0;

397
            var input = new CodedInputStream(stream);
398 399 400 401 402 403 404
            input.AssertNextTag(tag);
            Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser));
            input.AssertNextTag(tag);
            Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser));
            Assert.IsTrue(input.IsAtEnd);
        }

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
        [Test]
        public void CalculateSize_VariableSizeNonPacked()
        {
            var list = new RepeatedField<int> { 1, 500, 1 };
            var tag = WireFormat.MakeTag(1, WireFormat.WireType.Varint);
            // 2 bytes for the first entry, 3 bytes for the second, 2 bytes for the third
            Assert.AreEqual(7, list.CalculateSize(FieldCodec.ForInt32(tag)));
        }

        [Test]
        public void CalculateSize_FixedSizeNonPacked()
        {
            var list = new RepeatedField<int> { 1, 500, 1 };
            var tag = WireFormat.MakeTag(1, WireFormat.WireType.Fixed32);
            // 5 bytes for the each entry
            Assert.AreEqual(15, list.CalculateSize(FieldCodec.ForSFixed32(tag)));
        }

        [Test]
        public void CalculateSize_VariableSizePacked()
        {
            var list = new RepeatedField<int> { 1, 500, 1};
            var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
            // 1 byte for the tag, 1 byte for the length,
            // 1 byte for the first entry, 2 bytes for the second, 1 byte for the third
            Assert.AreEqual(6, list.CalculateSize(FieldCodec.ForInt32(tag)));
        }

        [Test]
        public void CalculateSize_FixedSizePacked()
        {
            var list = new RepeatedField<int> { 1, 500, 1 };
            var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
            // 1 byte for the tag, 1 byte for the length, 4 bytes per entry
            Assert.AreEqual(14, list.CalculateSize(FieldCodec.ForSFixed32(tag)));
        }
441 442 443 444 445 446 447

        [Test]
        public void TestNegativeEnumArray()
        {
            int arraySize = 1 + 1 + (11 * 5);
            int msgSize = arraySize;
            byte[] bytes = new byte[msgSize];
448
            CodedOutputStream output = new CodedOutputStream(bytes);
449 450 451 452 453 454 455 456 457
            uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);
            for (int i = 0; i >= -5; i--)
            {
                output.WriteTag(tag);
                output.WriteEnum(i);
            }

            Assert.AreEqual(0, output.SpaceLeft);

458
            CodedInputStream input = new CodedInputStream(bytes);
459
            tag = input.ReadTag();
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

            RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
            values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(SampleEnum.None, values[0]);
            Assert.AreEqual(((SampleEnum)(-1)), values[1]);
            Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
            Assert.AreEqual(((SampleEnum)(-3)), values[3]);
            Assert.AreEqual(((SampleEnum)(-4)), values[4]);
            Assert.AreEqual(((SampleEnum)(-5)), values[5]);
        }


        [Test]
        public void TestNegativeEnumPackedArray()
        {
            int arraySize = 1 + (10 * 5);
            int msgSize = 1 + 1 + arraySize;
            byte[] bytes = new byte[msgSize];
480
            CodedOutputStream output = new CodedOutputStream(bytes);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
            // Length-delimited to show we want the packed representation
            uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);
            output.WriteTag(tag);
            int size = 0;
            for (int i = 0; i >= -5; i--)
            {
                size += CodedOutputStream.ComputeEnumSize(i);
            }
            output.WriteRawVarint32((uint)size);
            for (int i = 0; i >= -5; i--)
            {
                output.WriteEnum(i);
            }
            Assert.AreEqual(0, output.SpaceLeft);

496
            CodedInputStream input = new CodedInputStream(bytes);
497
            tag = input.ReadTag();
498 499 500 501 502 503 504 505 506 507 508 509

            RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
            values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(SampleEnum.None, values[0]);
            Assert.AreEqual(((SampleEnum)(-1)), values[1]);
            Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
            Assert.AreEqual(((SampleEnum)(-3)), values[3]);
            Assert.AreEqual(((SampleEnum)(-4)), values[4]);
            Assert.AreEqual(((SampleEnum)(-5)), values[5]);
        }
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

        // Fairly perfunctory tests for the non-generic IList implementation
        [Test]
        public void IList_Indexer()
        {
            var field = new RepeatedField<string> { "first", "second" };
            IList list = field;
            Assert.AreEqual("first", list[0]);
            list[1] = "changed";
            Assert.AreEqual("changed", field[1]);
        }

        [Test]
        public void IList_Contains()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            Assert.IsTrue(list.Contains("second"));
            Assert.IsFalse(list.Contains("third"));
            Assert.IsFalse(list.Contains(new object()));
        }

        [Test]
        public void IList_Add()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            list.Add("third");
            CollectionAssert.AreEqual(new[] { "first", "second", "third" }, list);
        }

        [Test]
        public void IList_Remove()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            list.Remove("third"); // No-op, no exception
            list.Remove(new object()); // No-op, no exception
            list.Remove("first");
            CollectionAssert.AreEqual(new[] { "second" }, list);
        }

        [Test]
        public void IList_IsFixedSize()
        {
            var field = new RepeatedField<string> { "first", "second" };
            IList list = field;
            Assert.IsFalse(list.IsFixedSize);
        }

        [Test]
        public void IList_IndexOf()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            Assert.AreEqual(1, list.IndexOf("second"));
            Assert.AreEqual(-1, list.IndexOf("third"));
            Assert.AreEqual(-1, list.IndexOf(new object()));
        }

        [Test]
        public void IList_SyncRoot()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            Assert.AreSame(list, list.SyncRoot);
        }

        [Test]
        public void IList_CopyTo()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            string[] stringArray = new string[4];
            list.CopyTo(stringArray, 1);
            CollectionAssert.AreEqual(new[] { null, "first",  "second", null }, stringArray);

            object[] objectArray = new object[4];
            list.CopyTo(objectArray, 1);
            CollectionAssert.AreEqual(new[] { null, "first", "second", null }, objectArray);

            Assert.Throws<ArrayTypeMismatchException>(() => list.CopyTo(new StringBuilder[4], 1));
            Assert.Throws<ArrayTypeMismatchException>(() => list.CopyTo(new int[4], 1));
        }

        [Test]
        public void IList_IsSynchronized()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            Assert.IsFalse(list.IsSynchronized);
        }

        [Test]
        public void IList_Insert()
        {
            IList list = new RepeatedField<string> { "first", "second" };
            list.Insert(1, "middle");
            CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, list);
        }
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

        [Test]
        public void ToString_Integers()
        {
            var list = new RepeatedField<int> { 5, 10, 20 };
            var text = list.ToString();
            Assert.AreEqual("[ 5, 10, 20 ]", text);
        }

        [Test]
        public void ToString_Strings()
        {
            var list = new RepeatedField<string> { "x", "y", "z" };
            var text = list.ToString();
            Assert.AreEqual("[ \"x\", \"y\", \"z\" ]", text);
        }

        [Test]
        public void ToString_Messages()
        {
            var list = new RepeatedField<TestAllTypes> { new TestAllTypes { SingleDouble = 1.5 }, new TestAllTypes { SingleInt32 = 10 } };
            var text = list.ToString();
            Assert.AreEqual("[ { \"singleDouble\": 1.5 }, { \"singleInt32\": 10 } ]", text);
        }

        [Test]
        public void ToString_Empty()
        {
            var list = new RepeatedField<TestAllTypes> { };
            var text = list.ToString();
            Assert.AreEqual("[ ]", text);
        }

        [Test]
        public void ToString_InvalidElementType()
        {
            var list = new RepeatedField<decimal> { 15m };
            Assert.Throws<ArgumentException>(() => list.ToString());
        }

        [Test]
        public void ToString_Timestamp()
        {
            var list = new RepeatedField<Timestamp> { Timestamp.FromDateTime(new DateTime(2015, 10, 1, 12, 34, 56, DateTimeKind.Utc)) };
            var text = list.ToString();
            Assert.AreEqual("[ \"2015-10-01T12:34:56Z\" ]", text);
        }

        [Test]
        public void ToString_Struct()
        {
            var message = new Struct { Fields = { { "foo", new Value { NumberValue = 20 } } } };
            var list = new RepeatedField<Struct> { message };
            var text = list.ToString();
            Assert.AreEqual(text, "[ { \"foo\": 20 } ]", message.ToString());
        }
659 660
    }
}