OLD | NEW |
| (Empty) |
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 | |
33 using System; | |
34 using System.Collections; | |
35 using System.Collections.Generic; | |
36 using System.IO; | |
37 using System.Linq; | |
38 using System.Text; | |
39 using Google.Protobuf.TestProtos; | |
40 using NUnit.Framework; | |
41 | |
42 namespace Google.Protobuf.Collections | |
43 { | |
44 public class RepeatedFieldTest | |
45 { | |
46 [Test] | |
47 public void NullValuesRejected() | |
48 { | |
49 var list = new RepeatedField<string>(); | |
50 Assert.Throws<ArgumentNullException>(() => list.Add((string)null)); | |
51 Assert.Throws<ArgumentNullException>(() => list.Add((IEnumerable<str
ing>)null)); | |
52 Assert.Throws<ArgumentNullException>(() => list.Add((RepeatedField<s
tring>)null)); | |
53 Assert.Throws<ArgumentNullException>(() => list.Contains(null)); | |
54 Assert.Throws<ArgumentNullException>(() => list.IndexOf(null)); | |
55 } | |
56 | |
57 [Test] | |
58 public void Add_SingleItem() | |
59 { | |
60 var list = new RepeatedField<string>(); | |
61 list.Add("foo"); | |
62 Assert.AreEqual(1, list.Count); | |
63 Assert.AreEqual("foo", list[0]); | |
64 } | |
65 | |
66 [Test] | |
67 public void Add_Sequence() | |
68 { | |
69 var list = new RepeatedField<string>(); | |
70 list.Add(new[] { "foo", "bar" }); | |
71 Assert.AreEqual(2, list.Count); | |
72 Assert.AreEqual("foo", list[0]); | |
73 Assert.AreEqual("bar", list[1]); | |
74 } | |
75 | |
76 [Test] | |
77 public void Add_RepeatedField() | |
78 { | |
79 var list = new RepeatedField<string> { "original" }; | |
80 list.Add(new RepeatedField<string> { "foo", "bar" }); | |
81 Assert.AreEqual(3, list.Count); | |
82 Assert.AreEqual("original", list[0]); | |
83 Assert.AreEqual("foo", list[1]); | |
84 Assert.AreEqual("bar", list[2]); | |
85 } | |
86 | |
87 [Test] | |
88 public void RemoveAt_Valid() | |
89 { | |
90 var list = new RepeatedField<string> { "first", "second", "third" }; | |
91 list.RemoveAt(1); | |
92 CollectionAssert.AreEqual(new[] { "first", "third" }, list); | |
93 // Just check that these don't throw... | |
94 list.RemoveAt(list.Count - 1); // Now the count will be 1... | |
95 list.RemoveAt(0); | |
96 Assert.AreEqual(0, list.Count); | |
97 } | |
98 | |
99 [Test] | |
100 public void RemoveAt_Invalid() | |
101 { | |
102 var list = new RepeatedField<string> { "first", "second", "third" }; | |
103 Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(-1)); | |
104 Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(3)); | |
105 } | |
106 | |
107 [Test] | |
108 public void Insert_Valid() | |
109 { | |
110 var list = new RepeatedField<string> { "first", "second" }; | |
111 list.Insert(1, "middle"); | |
112 CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, lis
t); | |
113 list.Insert(3, "end"); | |
114 CollectionAssert.AreEqual(new[] { "first", "middle", "second", "end"
}, list); | |
115 list.Insert(0, "start"); | |
116 CollectionAssert.AreEqual(new[] { "start", "first", "middle", "secon
d", "end" }, list); | |
117 } | |
118 | |
119 [Test] | |
120 public void Insert_Invalid() | |
121 { | |
122 var list = new RepeatedField<string> { "first", "second" }; | |
123 Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, "fo
o")); | |
124 Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(3, "foo
")); | |
125 Assert.Throws<ArgumentNullException>(() => list.Insert(0, null)); | |
126 } | |
127 | |
128 [Test] | |
129 public void Equals_RepeatedField() | |
130 { | |
131 var list = new RepeatedField<string> { "first", "second" }; | |
132 Assert.IsFalse(list.Equals((RepeatedField<string>) null)); | |
133 Assert.IsTrue(list.Equals(list)); | |
134 Assert.IsFalse(list.Equals(new RepeatedField<string> { "first", "thi
rd" })); | |
135 Assert.IsFalse(list.Equals(new RepeatedField<string> { "first" })); | |
136 Assert.IsTrue(list.Equals(new RepeatedField<string> { "first", "seco
nd" })); | |
137 } | |
138 | |
139 [Test] | |
140 public void Equals_Object() | |
141 { | |
142 var list = new RepeatedField<string> { "first", "second" }; | |
143 Assert.IsFalse(list.Equals((object) null)); | |
144 Assert.IsTrue(list.Equals((object) list)); | |
145 Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "fir
st", "third" })); | |
146 Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "fir
st" })); | |
147 Assert.IsTrue(list.Equals((object) new RepeatedField<string> { "firs
t", "second" })); | |
148 Assert.IsFalse(list.Equals(new object())); | |
149 } | |
150 | |
151 [Test] | |
152 public void GetEnumerator_GenericInterface() | |
153 { | |
154 IEnumerable<string> list = new RepeatedField<string> { "first", "sec
ond" }; | |
155 // Select gets rid of the optimizations in ToList... | |
156 CollectionAssert.AreEqual(new[] { "first", "second" }, list.Select(x
=> x).ToList()); | |
157 } | |
158 | |
159 [Test] | |
160 public void GetEnumerator_NonGenericInterface() | |
161 { | |
162 IEnumerable list = new RepeatedField<string> { "first", "second" }; | |
163 CollectionAssert.AreEqual(new[] { "first", "second" }, list.Cast<obj
ect>().ToList()); | |
164 } | |
165 | |
166 [Test] | |
167 public void CopyTo() | |
168 { | |
169 var list = new RepeatedField<string> { "first", "second" }; | |
170 string[] stringArray = new string[4]; | |
171 list.CopyTo(stringArray, 1); | |
172 CollectionAssert.AreEqual(new[] { null, "first", "second", null }, s
tringArray); | |
173 } | |
174 | |
175 [Test] | |
176 public void Indexer_Get() | |
177 { | |
178 var list = new RepeatedField<string> { "first", "second" }; | |
179 Assert.AreEqual("first", list[0]); | |
180 Assert.AreEqual("second", list[1]); | |
181 Assert.Throws<ArgumentOutOfRangeException>(() => list[-1].GetHashCod
e()); | |
182 Assert.Throws<ArgumentOutOfRangeException>(() => list[2].GetHashCode
()); | |
183 } | |
184 | |
185 [Test] | |
186 public void Indexer_Set() | |
187 { | |
188 var list = new RepeatedField<string> { "first", "second" }; | |
189 list[0] = "changed"; | |
190 Assert.AreEqual("changed", list[0]); | |
191 Assert.Throws<ArgumentNullException>(() => list[0] = null); | |
192 Assert.Throws<ArgumentOutOfRangeException>(() => list[-1] = "bad"); | |
193 Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = "bad"); | |
194 } | |
195 | |
196 [Test] | |
197 public void Clone_ReturnsMutable() | |
198 { | |
199 var list = new RepeatedField<int> { 0 }; | |
200 var clone = list.Clone(); | |
201 clone[0] = 1; | |
202 } | |
203 | |
204 [Test] | |
205 public void Enumerator() | |
206 { | |
207 var list = new RepeatedField<string> { "first", "second" }; | |
208 using (var enumerator = list.GetEnumerator()) | |
209 { | |
210 Assert.IsTrue(enumerator.MoveNext()); | |
211 Assert.AreEqual("first", enumerator.Current); | |
212 Assert.IsTrue(enumerator.MoveNext()); | |
213 Assert.AreEqual("second", enumerator.Current); | |
214 Assert.IsFalse(enumerator.MoveNext()); | |
215 Assert.IsFalse(enumerator.MoveNext()); | |
216 } | |
217 } | |
218 | |
219 [Test] | |
220 public void AddEntriesFrom_PackedInt32() | |
221 { | |
222 uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDe
limited); | |
223 var stream = new MemoryStream(); | |
224 var output = new CodedOutputStream(stream); | |
225 var length = CodedOutputStream.ComputeInt32Size(10) | |
226 + CodedOutputStream.ComputeInt32Size(999) | |
227 + CodedOutputStream.ComputeInt32Size(-1000); | |
228 output.WriteTag(packedTag); | |
229 output.WriteRawVarint32((uint) length); | |
230 output.WriteInt32(10); | |
231 output.WriteInt32(999); | |
232 output.WriteInt32(-1000); | |
233 output.Flush(); | |
234 stream.Position = 0; | |
235 | |
236 // Deliberately "expecting" a non-packed tag, but we detect that the
data is | |
237 // actually packed. | |
238 uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Lengt
hDelimited); | |
239 var field = new RepeatedField<int>(); | |
240 var input = new CodedInputStream(stream); | |
241 input.AssertNextTag(packedTag); | |
242 field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag)); | |
243 CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); | |
244 Assert.IsTrue(input.IsAtEnd); | |
245 } | |
246 | |
247 [Test] | |
248 public void AddEntriesFrom_NonPackedInt32() | |
249 { | |
250 uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varin
t); | |
251 var stream = new MemoryStream(); | |
252 var output = new CodedOutputStream(stream); | |
253 output.WriteTag(nonPackedTag); | |
254 output.WriteInt32(10); | |
255 output.WriteTag(nonPackedTag); | |
256 output.WriteInt32(999); | |
257 output.WriteTag(nonPackedTag); | |
258 output.WriteInt32(-1000); // Just for variety... | |
259 output.Flush(); | |
260 stream.Position = 0; | |
261 | |
262 // Deliberately "expecting" a packed tag, but we detect that the dat
a is | |
263 // actually not packed. | |
264 uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDe
limited); | |
265 var field = new RepeatedField<int>(); | |
266 var input = new CodedInputStream(stream); | |
267 input.AssertNextTag(nonPackedTag); | |
268 field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag)); | |
269 CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); | |
270 Assert.IsTrue(input.IsAtEnd); | |
271 } | |
272 | |
273 [Test] | |
274 public void AddEntriesFrom_String() | |
275 { | |
276 uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimite
d); | |
277 var stream = new MemoryStream(); | |
278 var output = new CodedOutputStream(stream); | |
279 output.WriteTag(tag); | |
280 output.WriteString("Foo"); | |
281 output.WriteTag(tag); | |
282 output.WriteString(""); | |
283 output.WriteTag(tag); | |
284 output.WriteString("Bar"); | |
285 output.Flush(); | |
286 stream.Position = 0; | |
287 | |
288 var field = new RepeatedField<string>(); | |
289 var input = new CodedInputStream(stream); | |
290 input.AssertNextTag(tag); | |
291 field.AddEntriesFrom(input, FieldCodec.ForString(tag)); | |
292 CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field); | |
293 Assert.IsTrue(input.IsAtEnd); | |
294 } | |
295 | |
296 [Test] | |
297 public void AddEntriesFrom_Message() | |
298 { | |
299 var message1 = new ForeignMessage { C = 2000 }; | |
300 var message2 = new ForeignMessage { C = -250 }; | |
301 | |
302 uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimite
d); | |
303 var stream = new MemoryStream(); | |
304 var output = new CodedOutputStream(stream); | |
305 output.WriteTag(tag); | |
306 output.WriteMessage(message1); | |
307 output.WriteTag(tag); | |
308 output.WriteMessage(message2); | |
309 output.Flush(); | |
310 stream.Position = 0; | |
311 | |
312 var field = new RepeatedField<ForeignMessage>(); | |
313 var input = new CodedInputStream(stream); | |
314 input.AssertNextTag(tag); | |
315 field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessag
e.Parser)); | |
316 CollectionAssert.AreEqual(new[] { message1, message2}, field); | |
317 Assert.IsTrue(input.IsAtEnd); | |
318 } | |
319 | |
320 [Test] | |
321 public void WriteTo_PackedInt32() | |
322 { | |
323 uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimite
d); | |
324 var field = new RepeatedField<int> { 10, 1000, 1000000 }; | |
325 var stream = new MemoryStream(); | |
326 var output = new CodedOutputStream(stream); | |
327 field.WriteTo(output, FieldCodec.ForInt32(tag)); | |
328 output.Flush(); | |
329 stream.Position = 0; | |
330 | |
331 var input = new CodedInputStream(stream); | |
332 input.AssertNextTag(tag); | |
333 var length = input.ReadLength(); | |
334 Assert.AreEqual(10, input.ReadInt32()); | |
335 Assert.AreEqual(1000, input.ReadInt32()); | |
336 Assert.AreEqual(1000000, input.ReadInt32()); | |
337 Assert.IsTrue(input.IsAtEnd); | |
338 Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + le
ngth, stream.Length); | |
339 } | |
340 | |
341 [Test] | |
342 public void WriteTo_NonPackedInt32() | |
343 { | |
344 uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); | |
345 var field = new RepeatedField<int> { 10, 1000, 1000000}; | |
346 var stream = new MemoryStream(); | |
347 var output = new CodedOutputStream(stream); | |
348 field.WriteTo(output, FieldCodec.ForInt32(tag)); | |
349 output.Flush(); | |
350 stream.Position = 0; | |
351 | |
352 var input = new CodedInputStream(stream); | |
353 input.AssertNextTag(tag); | |
354 Assert.AreEqual(10, input.ReadInt32()); | |
355 input.AssertNextTag(tag); | |
356 Assert.AreEqual(1000, input.ReadInt32()); | |
357 input.AssertNextTag(tag); | |
358 Assert.AreEqual(1000000, input.ReadInt32()); | |
359 Assert.IsTrue(input.IsAtEnd); | |
360 } | |
361 | |
362 [Test] | |
363 public void WriteTo_String() | |
364 { | |
365 uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimite
d); | |
366 var field = new RepeatedField<string> { "Foo", "", "Bar" }; | |
367 var stream = new MemoryStream(); | |
368 var output = new CodedOutputStream(stream); | |
369 field.WriteTo(output, FieldCodec.ForString(tag)); | |
370 output.Flush(); | |
371 stream.Position = 0; | |
372 | |
373 var input = new CodedInputStream(stream); | |
374 input.AssertNextTag(tag); | |
375 Assert.AreEqual("Foo", input.ReadString()); | |
376 input.AssertNextTag(tag); | |
377 Assert.AreEqual("", input.ReadString()); | |
378 input.AssertNextTag(tag); | |
379 Assert.AreEqual("Bar", input.ReadString()); | |
380 Assert.IsTrue(input.IsAtEnd); | |
381 } | |
382 | |
383 [Test] | |
384 public void WriteTo_Message() | |
385 { | |
386 var message1 = new ForeignMessage { C = 20 }; | |
387 var message2 = new ForeignMessage { C = 25 }; | |
388 uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimite
d); | |
389 var field = new RepeatedField<ForeignMessage> { message1, message2 }
; | |
390 var stream = new MemoryStream(); | |
391 var output = new CodedOutputStream(stream); | |
392 field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Pars
er)); | |
393 output.Flush(); | |
394 stream.Position = 0; | |
395 | |
396 var input = new CodedInputStream(stream); | |
397 input.AssertNextTag(tag); | |
398 Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser)); | |
399 input.AssertNextTag(tag); | |
400 Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser)); | |
401 Assert.IsTrue(input.IsAtEnd); | |
402 } | |
403 | |
404 [Test] | |
405 public void CalculateSize_VariableSizeNonPacked() | |
406 { | |
407 var list = new RepeatedField<int> { 1, 500, 1 }; | |
408 var tag = WireFormat.MakeTag(1, WireFormat.WireType.Varint); | |
409 // 2 bytes for the first entry, 3 bytes for the second, 2 bytes for
the third | |
410 Assert.AreEqual(7, list.CalculateSize(FieldCodec.ForInt32(tag))); | |
411 } | |
412 | |
413 [Test] | |
414 public void CalculateSize_FixedSizeNonPacked() | |
415 { | |
416 var list = new RepeatedField<int> { 1, 500, 1 }; | |
417 var tag = WireFormat.MakeTag(1, WireFormat.WireType.Fixed32); | |
418 // 5 bytes for the each entry | |
419 Assert.AreEqual(15, list.CalculateSize(FieldCodec.ForSFixed32(tag)))
; | |
420 } | |
421 | |
422 [Test] | |
423 public void CalculateSize_VariableSizePacked() | |
424 { | |
425 var list = new RepeatedField<int> { 1, 500, 1}; | |
426 var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited)
; | |
427 // 1 byte for the tag, 1 byte for the length, | |
428 // 1 byte for the first entry, 2 bytes for the second, 1 byte for th
e third | |
429 Assert.AreEqual(6, list.CalculateSize(FieldCodec.ForInt32(tag))); | |
430 } | |
431 | |
432 [Test] | |
433 public void CalculateSize_FixedSizePacked() | |
434 { | |
435 var list = new RepeatedField<int> { 1, 500, 1 }; | |
436 var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited)
; | |
437 // 1 byte for the tag, 1 byte for the length, 4 bytes per entry | |
438 Assert.AreEqual(14, list.CalculateSize(FieldCodec.ForSFixed32(tag)))
; | |
439 } | |
440 | |
441 [Test] | |
442 public void TestNegativeEnumArray() | |
443 { | |
444 int arraySize = 1 + 1 + (11 * 5); | |
445 int msgSize = arraySize; | |
446 byte[] bytes = new byte[msgSize]; | |
447 CodedOutputStream output = new CodedOutputStream(bytes); | |
448 uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint); | |
449 for (int i = 0; i >= -5; i--) | |
450 { | |
451 output.WriteTag(tag); | |
452 output.WriteEnum(i); | |
453 } | |
454 | |
455 Assert.AreEqual(0, output.SpaceLeft); | |
456 | |
457 CodedInputStream input = new CodedInputStream(bytes); | |
458 tag = input.ReadTag(); | |
459 | |
460 RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); | |
461 values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x
=> (SampleEnum)x)); | |
462 | |
463 Assert.AreEqual(6, values.Count); | |
464 Assert.AreEqual(SampleEnum.None, values[0]); | |
465 Assert.AreEqual(((SampleEnum)(-1)), values[1]); | |
466 Assert.AreEqual(SampleEnum.NegativeValue, values[2]); | |
467 Assert.AreEqual(((SampleEnum)(-3)), values[3]); | |
468 Assert.AreEqual(((SampleEnum)(-4)), values[4]); | |
469 Assert.AreEqual(((SampleEnum)(-5)), values[5]); | |
470 } | |
471 | |
472 | |
473 [Test] | |
474 public void TestNegativeEnumPackedArray() | |
475 { | |
476 int arraySize = 1 + (10 * 5); | |
477 int msgSize = 1 + 1 + arraySize; | |
478 byte[] bytes = new byte[msgSize]; | |
479 CodedOutputStream output = new CodedOutputStream(bytes); | |
480 // Length-delimited to show we want the packed representation | |
481 uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited
); | |
482 output.WriteTag(tag); | |
483 int size = 0; | |
484 for (int i = 0; i >= -5; i--) | |
485 { | |
486 size += CodedOutputStream.ComputeEnumSize(i); | |
487 } | |
488 output.WriteRawVarint32((uint)size); | |
489 for (int i = 0; i >= -5; i--) | |
490 { | |
491 output.WriteEnum(i); | |
492 } | |
493 Assert.AreEqual(0, output.SpaceLeft); | |
494 | |
495 CodedInputStream input = new CodedInputStream(bytes); | |
496 tag = input.ReadTag(); | |
497 | |
498 RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); | |
499 values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x
=> (SampleEnum)x)); | |
500 | |
501 Assert.AreEqual(6, values.Count); | |
502 Assert.AreEqual(SampleEnum.None, values[0]); | |
503 Assert.AreEqual(((SampleEnum)(-1)), values[1]); | |
504 Assert.AreEqual(SampleEnum.NegativeValue, values[2]); | |
505 Assert.AreEqual(((SampleEnum)(-3)), values[3]); | |
506 Assert.AreEqual(((SampleEnum)(-4)), values[4]); | |
507 Assert.AreEqual(((SampleEnum)(-5)), values[5]); | |
508 } | |
509 | |
510 // Fairly perfunctory tests for the non-generic IList implementation | |
511 [Test] | |
512 public void IList_Indexer() | |
513 { | |
514 var field = new RepeatedField<string> { "first", "second" }; | |
515 IList list = field; | |
516 Assert.AreEqual("first", list[0]); | |
517 list[1] = "changed"; | |
518 Assert.AreEqual("changed", field[1]); | |
519 } | |
520 | |
521 [Test] | |
522 public void IList_Contains() | |
523 { | |
524 IList list = new RepeatedField<string> { "first", "second" }; | |
525 Assert.IsTrue(list.Contains("second")); | |
526 Assert.IsFalse(list.Contains("third")); | |
527 Assert.IsFalse(list.Contains(new object())); | |
528 } | |
529 | |
530 [Test] | |
531 public void IList_Add() | |
532 { | |
533 IList list = new RepeatedField<string> { "first", "second" }; | |
534 list.Add("third"); | |
535 CollectionAssert.AreEqual(new[] { "first", "second", "third" }, list
); | |
536 } | |
537 | |
538 [Test] | |
539 public void IList_Remove() | |
540 { | |
541 IList list = new RepeatedField<string> { "first", "second" }; | |
542 list.Remove("third"); // No-op, no exception | |
543 list.Remove(new object()); // No-op, no exception | |
544 list.Remove("first"); | |
545 CollectionAssert.AreEqual(new[] { "second" }, list); | |
546 } | |
547 | |
548 [Test] | |
549 public void IList_IsFixedSize() | |
550 { | |
551 var field = new RepeatedField<string> { "first", "second" }; | |
552 IList list = field; | |
553 Assert.IsFalse(list.IsFixedSize); | |
554 } | |
555 | |
556 [Test] | |
557 public void IList_IndexOf() | |
558 { | |
559 IList list = new RepeatedField<string> { "first", "second" }; | |
560 Assert.AreEqual(1, list.IndexOf("second")); | |
561 Assert.AreEqual(-1, list.IndexOf("third")); | |
562 Assert.AreEqual(-1, list.IndexOf(new object())); | |
563 } | |
564 | |
565 [Test] | |
566 public void IList_SyncRoot() | |
567 { | |
568 IList list = new RepeatedField<string> { "first", "second" }; | |
569 Assert.AreSame(list, list.SyncRoot); | |
570 } | |
571 | |
572 [Test] | |
573 public void IList_CopyTo() | |
574 { | |
575 IList list = new RepeatedField<string> { "first", "second" }; | |
576 string[] stringArray = new string[4]; | |
577 list.CopyTo(stringArray, 1); | |
578 CollectionAssert.AreEqual(new[] { null, "first", "second", null },
stringArray); | |
579 | |
580 object[] objectArray = new object[4]; | |
581 list.CopyTo(objectArray, 1); | |
582 CollectionAssert.AreEqual(new[] { null, "first", "second", null }, o
bjectArray); | |
583 | |
584 Assert.Throws<ArrayTypeMismatchException>(() => list.CopyTo(new Stri
ngBuilder[4], 1)); | |
585 Assert.Throws<ArrayTypeMismatchException>(() => list.CopyTo(new int[
4], 1)); | |
586 } | |
587 | |
588 [Test] | |
589 public void IList_IsSynchronized() | |
590 { | |
591 IList list = new RepeatedField<string> { "first", "second" }; | |
592 Assert.IsFalse(list.IsSynchronized); | |
593 } | |
594 | |
595 [Test] | |
596 public void IList_Insert() | |
597 { | |
598 IList list = new RepeatedField<string> { "first", "second" }; | |
599 list.Insert(1, "middle"); | |
600 CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, lis
t); | |
601 } | |
602 } | |
603 } | |
OLD | NEW |