RepeatedFieldTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.Linq;
  37. using System.Text;
  38. using Google.Protobuf.TestProtos;
  39. using NUnit.Framework;
  40. namespace Google.Protobuf.Collections
  41. {
  42. public class RepeatedFieldTest
  43. {
  44. [Test]
  45. public void NullValuesRejected()
  46. {
  47. var list = new RepeatedField<string>();
  48. Assert.Throws<ArgumentNullException>(() => list.Add((string)null));
  49. Assert.Throws<ArgumentNullException>(() => list.Add((IEnumerable<string>)null));
  50. Assert.Throws<ArgumentNullException>(() => list.Add((RepeatedField<string>)null));
  51. Assert.Throws<ArgumentNullException>(() => list.Contains(null));
  52. Assert.Throws<ArgumentNullException>(() => list.IndexOf(null));
  53. }
  54. [Test]
  55. public void Add_SingleItem()
  56. {
  57. var list = new RepeatedField<string>();
  58. list.Add("foo");
  59. Assert.AreEqual(1, list.Count);
  60. Assert.AreEqual("foo", list[0]);
  61. }
  62. [Test]
  63. public void Add_Sequence()
  64. {
  65. var list = new RepeatedField<string>();
  66. list.Add(new[] { "foo", "bar" });
  67. Assert.AreEqual(2, list.Count);
  68. Assert.AreEqual("foo", list[0]);
  69. Assert.AreEqual("bar", list[1]);
  70. }
  71. [Test]
  72. public void Add_RepeatedField()
  73. {
  74. var list = new RepeatedField<string> { "original" };
  75. list.Add(new RepeatedField<string> { "foo", "bar" });
  76. Assert.AreEqual(3, list.Count);
  77. Assert.AreEqual("original", list[0]);
  78. Assert.AreEqual("foo", list[1]);
  79. Assert.AreEqual("bar", list[2]);
  80. }
  81. [Test]
  82. public void RemoveAt_Valid()
  83. {
  84. var list = new RepeatedField<string> { "first", "second", "third" };
  85. list.RemoveAt(1);
  86. CollectionAssert.AreEqual(new[] { "first", "third" }, list);
  87. // Just check that these don't throw...
  88. list.RemoveAt(list.Count - 1); // Now the count will be 1...
  89. list.RemoveAt(0);
  90. Assert.AreEqual(0, list.Count);
  91. }
  92. [Test]
  93. public void RemoveAt_Invalid()
  94. {
  95. var list = new RepeatedField<string> { "first", "second", "third" };
  96. Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(-1));
  97. Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(3));
  98. }
  99. [Test]
  100. public void Insert_Valid()
  101. {
  102. var list = new RepeatedField<string> { "first", "second" };
  103. list.Insert(1, "middle");
  104. CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, list);
  105. list.Insert(3, "end");
  106. CollectionAssert.AreEqual(new[] { "first", "middle", "second", "end" }, list);
  107. list.Insert(0, "start");
  108. CollectionAssert.AreEqual(new[] { "start", "first", "middle", "second", "end" }, list);
  109. }
  110. [Test]
  111. public void Insert_Invalid()
  112. {
  113. var list = new RepeatedField<string> { "first", "second" };
  114. Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, "foo"));
  115. Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(3, "foo"));
  116. Assert.Throws<ArgumentNullException>(() => list.Insert(0, null));
  117. }
  118. [Test]
  119. public void Equals_RepeatedField()
  120. {
  121. var list = new RepeatedField<string> { "first", "second" };
  122. Assert.IsFalse(list.Equals((RepeatedField<string>) null));
  123. Assert.IsTrue(list.Equals(list));
  124. Assert.IsFalse(list.Equals(new RepeatedField<string> { "first", "third" }));
  125. Assert.IsFalse(list.Equals(new RepeatedField<string> { "first" }));
  126. Assert.IsTrue(list.Equals(new RepeatedField<string> { "first", "second" }));
  127. }
  128. [Test]
  129. public void Equals_Object()
  130. {
  131. var list = new RepeatedField<string> { "first", "second" };
  132. Assert.IsFalse(list.Equals((object) null));
  133. Assert.IsTrue(list.Equals((object) list));
  134. Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "first", "third" }));
  135. Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "first" }));
  136. Assert.IsTrue(list.Equals((object) new RepeatedField<string> { "first", "second" }));
  137. Assert.IsFalse(list.Equals(new object()));
  138. }
  139. [Test]
  140. public void GetEnumerator_GenericInterface()
  141. {
  142. IEnumerable<string> list = new RepeatedField<string> { "first", "second" };
  143. // Select gets rid of the optimizations in ToList...
  144. CollectionAssert.AreEqual(new[] { "first", "second" }, list.Select(x => x).ToList());
  145. }
  146. [Test]
  147. public void GetEnumerator_NonGenericInterface()
  148. {
  149. IEnumerable list = new RepeatedField<string> { "first", "second" };
  150. CollectionAssert.AreEqual(new[] { "first", "second" }, list.Cast<object>().ToList());
  151. }
  152. [Test]
  153. public void CopyTo()
  154. {
  155. var list = new RepeatedField<string> { "first", "second" };
  156. string[] stringArray = new string[4];
  157. list.CopyTo(stringArray, 1);
  158. CollectionAssert.AreEqual(new[] { null, "first", "second", null }, stringArray);
  159. }
  160. [Test]
  161. public void Indexer_Get()
  162. {
  163. var list = new RepeatedField<string> { "first", "second" };
  164. Assert.AreEqual("first", list[0]);
  165. Assert.AreEqual("second", list[1]);
  166. Assert.Throws<ArgumentOutOfRangeException>(() => list[-1].GetHashCode());
  167. Assert.Throws<ArgumentOutOfRangeException>(() => list[2].GetHashCode());
  168. }
  169. [Test]
  170. public void Indexer_Set()
  171. {
  172. var list = new RepeatedField<string> { "first", "second" };
  173. list[0] = "changed";
  174. Assert.AreEqual("changed", list[0]);
  175. Assert.Throws<ArgumentNullException>(() => list[0] = null);
  176. Assert.Throws<ArgumentOutOfRangeException>(() => list[-1] = "bad");
  177. Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = "bad");
  178. }
  179. [Test]
  180. public void Clone_ReturnsMutable()
  181. {
  182. var list = new RepeatedField<int> { 0 };
  183. var clone = list.Clone();
  184. clone[0] = 1;
  185. }
  186. [Test]
  187. public void Enumerator()
  188. {
  189. var list = new RepeatedField<string> { "first", "second" };
  190. using (var enumerator = list.GetEnumerator())
  191. {
  192. Assert.IsTrue(enumerator.MoveNext());
  193. Assert.AreEqual("first", enumerator.Current);
  194. Assert.IsTrue(enumerator.MoveNext());
  195. Assert.AreEqual("second", enumerator.Current);
  196. Assert.IsFalse(enumerator.MoveNext());
  197. Assert.IsFalse(enumerator.MoveNext());
  198. }
  199. }
  200. [Test]
  201. public void AddEntriesFrom_PackedInt32()
  202. {
  203. uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  204. var stream = new MemoryStream();
  205. var output = new CodedOutputStream(stream);
  206. var length = CodedOutputStream.ComputeInt32Size(10)
  207. + CodedOutputStream.ComputeInt32Size(999)
  208. + CodedOutputStream.ComputeInt32Size(-1000);
  209. output.WriteTag(packedTag);
  210. output.WriteRawVarint32((uint) length);
  211. output.WriteInt32(10);
  212. output.WriteInt32(999);
  213. output.WriteInt32(-1000);
  214. output.Flush();
  215. stream.Position = 0;
  216. // Deliberately "expecting" a non-packed tag, but we detect that the data is
  217. // actually packed.
  218. uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  219. var field = new RepeatedField<int>();
  220. var input = new CodedInputStream(stream);
  221. input.AssertNextTag(packedTag);
  222. field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag));
  223. CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
  224. Assert.IsTrue(input.IsAtEnd);
  225. }
  226. [Test]
  227. public void AddEntriesFrom_NonPackedInt32()
  228. {
  229. uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
  230. var stream = new MemoryStream();
  231. var output = new CodedOutputStream(stream);
  232. output.WriteTag(nonPackedTag);
  233. output.WriteInt32(10);
  234. output.WriteTag(nonPackedTag);
  235. output.WriteInt32(999);
  236. output.WriteTag(nonPackedTag);
  237. output.WriteInt32(-1000); // Just for variety...
  238. output.Flush();
  239. stream.Position = 0;
  240. // Deliberately "expecting" a packed tag, but we detect that the data is
  241. // actually not packed.
  242. uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  243. var field = new RepeatedField<int>();
  244. var input = new CodedInputStream(stream);
  245. input.AssertNextTag(nonPackedTag);
  246. field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
  247. CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
  248. Assert.IsTrue(input.IsAtEnd);
  249. }
  250. [Test]
  251. public void AddEntriesFrom_String()
  252. {
  253. uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  254. var stream = new MemoryStream();
  255. var output = new CodedOutputStream(stream);
  256. output.WriteTag(tag);
  257. output.WriteString("Foo");
  258. output.WriteTag(tag);
  259. output.WriteString("");
  260. output.WriteTag(tag);
  261. output.WriteString("Bar");
  262. output.Flush();
  263. stream.Position = 0;
  264. var field = new RepeatedField<string>();
  265. var input = new CodedInputStream(stream);
  266. input.AssertNextTag(tag);
  267. field.AddEntriesFrom(input, FieldCodec.ForString(tag));
  268. CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field);
  269. Assert.IsTrue(input.IsAtEnd);
  270. }
  271. [Test]
  272. public void AddEntriesFrom_Message()
  273. {
  274. var message1 = new ForeignMessage { C = 2000 };
  275. var message2 = new ForeignMessage { C = -250 };
  276. uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  277. var stream = new MemoryStream();
  278. var output = new CodedOutputStream(stream);
  279. output.WriteTag(tag);
  280. output.WriteMessage(message1);
  281. output.WriteTag(tag);
  282. output.WriteMessage(message2);
  283. output.Flush();
  284. stream.Position = 0;
  285. var field = new RepeatedField<ForeignMessage>();
  286. var input = new CodedInputStream(stream);
  287. input.AssertNextTag(tag);
  288. field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
  289. CollectionAssert.AreEqual(new[] { message1, message2}, field);
  290. Assert.IsTrue(input.IsAtEnd);
  291. }
  292. [Test]
  293. public void WriteTo_PackedInt32()
  294. {
  295. uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  296. var field = new RepeatedField<int> { 10, 1000, 1000000 };
  297. var stream = new MemoryStream();
  298. var output = new CodedOutputStream(stream);
  299. field.WriteTo(output, FieldCodec.ForInt32(tag));
  300. output.Flush();
  301. stream.Position = 0;
  302. var input = new CodedInputStream(stream);
  303. input.AssertNextTag(tag);
  304. var length = input.ReadLength();
  305. Assert.AreEqual(10, input.ReadInt32());
  306. Assert.AreEqual(1000, input.ReadInt32());
  307. Assert.AreEqual(1000000, input.ReadInt32());
  308. Assert.IsTrue(input.IsAtEnd);
  309. Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length);
  310. }
  311. [Test]
  312. public void WriteTo_NonPackedInt32()
  313. {
  314. uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
  315. var field = new RepeatedField<int> { 10, 1000, 1000000};
  316. var stream = new MemoryStream();
  317. var output = new CodedOutputStream(stream);
  318. field.WriteTo(output, FieldCodec.ForInt32(tag));
  319. output.Flush();
  320. stream.Position = 0;
  321. var input = new CodedInputStream(stream);
  322. input.AssertNextTag(tag);
  323. Assert.AreEqual(10, input.ReadInt32());
  324. input.AssertNextTag(tag);
  325. Assert.AreEqual(1000, input.ReadInt32());
  326. input.AssertNextTag(tag);
  327. Assert.AreEqual(1000000, input.ReadInt32());
  328. Assert.IsTrue(input.IsAtEnd);
  329. }
  330. [Test]
  331. public void WriteTo_String()
  332. {
  333. uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  334. var field = new RepeatedField<string> { "Foo", "", "Bar" };
  335. var stream = new MemoryStream();
  336. var output = new CodedOutputStream(stream);
  337. field.WriteTo(output, FieldCodec.ForString(tag));
  338. output.Flush();
  339. stream.Position = 0;
  340. var input = new CodedInputStream(stream);
  341. input.AssertNextTag(tag);
  342. Assert.AreEqual("Foo", input.ReadString());
  343. input.AssertNextTag(tag);
  344. Assert.AreEqual("", input.ReadString());
  345. input.AssertNextTag(tag);
  346. Assert.AreEqual("Bar", input.ReadString());
  347. Assert.IsTrue(input.IsAtEnd);
  348. }
  349. [Test]
  350. public void WriteTo_Message()
  351. {
  352. var message1 = new ForeignMessage { C = 20 };
  353. var message2 = new ForeignMessage { C = 25 };
  354. uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
  355. var field = new RepeatedField<ForeignMessage> { message1, message2 };
  356. var stream = new MemoryStream();
  357. var output = new CodedOutputStream(stream);
  358. field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
  359. output.Flush();
  360. stream.Position = 0;
  361. var input = new CodedInputStream(stream);
  362. input.AssertNextTag(tag);
  363. Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser));
  364. input.AssertNextTag(tag);
  365. Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser));
  366. Assert.IsTrue(input.IsAtEnd);
  367. }
  368. [Test]
  369. public void CalculateSize_VariableSizeNonPacked()
  370. {
  371. var list = new RepeatedField<int> { 1, 500, 1 };
  372. var tag = WireFormat.MakeTag(1, WireFormat.WireType.Varint);
  373. // 2 bytes for the first entry, 3 bytes for the second, 2 bytes for the third
  374. Assert.AreEqual(7, list.CalculateSize(FieldCodec.ForInt32(tag)));
  375. }
  376. [Test]
  377. public void CalculateSize_FixedSizeNonPacked()
  378. {
  379. var list = new RepeatedField<int> { 1, 500, 1 };
  380. var tag = WireFormat.MakeTag(1, WireFormat.WireType.Fixed32);
  381. // 5 bytes for the each entry
  382. Assert.AreEqual(15, list.CalculateSize(FieldCodec.ForSFixed32(tag)));
  383. }
  384. [Test]
  385. public void CalculateSize_VariableSizePacked()
  386. {
  387. var list = new RepeatedField<int> { 1, 500, 1};
  388. var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
  389. // 1 byte for the tag, 1 byte for the length,
  390. // 1 byte for the first entry, 2 bytes for the second, 1 byte for the third
  391. Assert.AreEqual(6, list.CalculateSize(FieldCodec.ForInt32(tag)));
  392. }
  393. [Test]
  394. public void CalculateSize_FixedSizePacked()
  395. {
  396. var list = new RepeatedField<int> { 1, 500, 1 };
  397. var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
  398. // 1 byte for the tag, 1 byte for the length, 4 bytes per entry
  399. Assert.AreEqual(14, list.CalculateSize(FieldCodec.ForSFixed32(tag)));
  400. }
  401. [Test]
  402. public void TestNegativeEnumArray()
  403. {
  404. int arraySize = 1 + 1 + (11 * 5);
  405. int msgSize = arraySize;
  406. byte[] bytes = new byte[msgSize];
  407. CodedOutputStream output = new CodedOutputStream(bytes);
  408. uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);
  409. for (int i = 0; i >= -5; i--)
  410. {
  411. output.WriteTag(tag);
  412. output.WriteEnum(i);
  413. }
  414. Assert.AreEqual(0, output.SpaceLeft);
  415. CodedInputStream input = new CodedInputStream(bytes);
  416. Assert.IsTrue(input.ReadTag(out tag));
  417. RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
  418. values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));
  419. Assert.AreEqual(6, values.Count);
  420. Assert.AreEqual(SampleEnum.None, values[0]);
  421. Assert.AreEqual(((SampleEnum)(-1)), values[1]);
  422. Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
  423. Assert.AreEqual(((SampleEnum)(-3)), values[3]);
  424. Assert.AreEqual(((SampleEnum)(-4)), values[4]);
  425. Assert.AreEqual(((SampleEnum)(-5)), values[5]);
  426. }
  427. [Test]
  428. public void TestNegativeEnumPackedArray()
  429. {
  430. int arraySize = 1 + (10 * 5);
  431. int msgSize = 1 + 1 + arraySize;
  432. byte[] bytes = new byte[msgSize];
  433. CodedOutputStream output = new CodedOutputStream(bytes);
  434. // Length-delimited to show we want the packed representation
  435. uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);
  436. output.WriteTag(tag);
  437. int size = 0;
  438. for (int i = 0; i >= -5; i--)
  439. {
  440. size += CodedOutputStream.ComputeEnumSize(i);
  441. }
  442. output.WriteRawVarint32((uint)size);
  443. for (int i = 0; i >= -5; i--)
  444. {
  445. output.WriteEnum(i);
  446. }
  447. Assert.AreEqual(0, output.SpaceLeft);
  448. CodedInputStream input = new CodedInputStream(bytes);
  449. Assert.IsTrue(input.ReadTag(out tag));
  450. RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
  451. values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));
  452. Assert.AreEqual(6, values.Count);
  453. Assert.AreEqual(SampleEnum.None, values[0]);
  454. Assert.AreEqual(((SampleEnum)(-1)), values[1]);
  455. Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
  456. Assert.AreEqual(((SampleEnum)(-3)), values[3]);
  457. Assert.AreEqual(((SampleEnum)(-4)), values[4]);
  458. Assert.AreEqual(((SampleEnum)(-5)), values[5]);
  459. }
  460. // Fairly perfunctory tests for the non-generic IList implementation
  461. [Test]
  462. public void IList_Indexer()
  463. {
  464. var field = new RepeatedField<string> { "first", "second" };
  465. IList list = field;
  466. Assert.AreEqual("first", list[0]);
  467. list[1] = "changed";
  468. Assert.AreEqual("changed", field[1]);
  469. }
  470. [Test]
  471. public void IList_Contains()
  472. {
  473. IList list = new RepeatedField<string> { "first", "second" };
  474. Assert.IsTrue(list.Contains("second"));
  475. Assert.IsFalse(list.Contains("third"));
  476. Assert.IsFalse(list.Contains(new object()));
  477. }
  478. [Test]
  479. public void IList_Add()
  480. {
  481. IList list = new RepeatedField<string> { "first", "second" };
  482. list.Add("third");
  483. CollectionAssert.AreEqual(new[] { "first", "second", "third" }, list);
  484. }
  485. [Test]
  486. public void IList_Remove()
  487. {
  488. IList list = new RepeatedField<string> { "first", "second" };
  489. list.Remove("third"); // No-op, no exception
  490. list.Remove(new object()); // No-op, no exception
  491. list.Remove("first");
  492. CollectionAssert.AreEqual(new[] { "second" }, list);
  493. }
  494. [Test]
  495. public void IList_IsFixedSize()
  496. {
  497. var field = new RepeatedField<string> { "first", "second" };
  498. IList list = field;
  499. Assert.IsFalse(list.IsFixedSize);
  500. }
  501. [Test]
  502. public void IList_IndexOf()
  503. {
  504. IList list = new RepeatedField<string> { "first", "second" };
  505. Assert.AreEqual(1, list.IndexOf("second"));
  506. Assert.AreEqual(-1, list.IndexOf("third"));
  507. Assert.AreEqual(-1, list.IndexOf(new object()));
  508. }
  509. [Test]
  510. public void IList_SyncRoot()
  511. {
  512. IList list = new RepeatedField<string> { "first", "second" };
  513. Assert.AreSame(list, list.SyncRoot);
  514. }
  515. [Test]
  516. public void IList_CopyTo()
  517. {
  518. IList list = new RepeatedField<string> { "first", "second" };
  519. string[] stringArray = new string[4];
  520. list.CopyTo(stringArray, 1);
  521. CollectionAssert.AreEqual(new[] { null, "first", "second", null }, stringArray);
  522. object[] objectArray = new object[4];
  523. list.CopyTo(objectArray, 1);
  524. CollectionAssert.AreEqual(new[] { null, "first", "second", null }, objectArray);
  525. Assert.Throws<ArrayTypeMismatchException>(() => list.CopyTo(new StringBuilder[4], 1));
  526. Assert.Throws<ArrayTypeMismatchException>(() => list.CopyTo(new int[4], 1));
  527. }
  528. [Test]
  529. public void IList_IsSynchronized()
  530. {
  531. IList list = new RepeatedField<string> { "first", "second" };
  532. Assert.IsFalse(list.IsSynchronized);
  533. }
  534. [Test]
  535. public void IList_Insert()
  536. {
  537. IList list = new RepeatedField<string> { "first", "second" };
  538. list.Insert(1, "middle");
  539. CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, list);
  540. }
  541. }
  542. }