-
-
Notifications
You must be signed in to change notification settings - Fork 673
/
CollectionTest.cs
700 lines (566 loc) · 19.3 KB
/
CollectionTest.cs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
81
82
83
84
85
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace MessagePack.Tests
{
public class CollectionTest
{
private T Convert<T>(T value)
{
return MessagePackSerializer.Deserialize<T>(MessagePackSerializer.Serialize(value));
}
public static object[][] CollectionTestData = new object[][]
{
new object[] { new int[] { 1, 10, 100 }, null },
new object[] { new List<int> { 1, 10, 100 }, null },
new object[] { new LinkedList<int>(new[] { 1, 10, 100 }), null },
new object[] { new Queue<int>(new[] { 1, 10, 100 }), null },
new object[] { new HashSet<int>(new[] { 1, 10, 100 }), null },
new object[] { new ReadOnlyCollection<int>(new[] { 1, 10, 100 }), null },
new object[] { new ObservableCollection<int>(new[] { 1, 10, 100 }), null },
new object[] { new ReadOnlyObservableCollection<int>(new ObservableCollection<int>(new[] { 1, 10, 100 })), null },
};
[Theory]
[MemberData(nameof(CollectionTestData))]
public void ConcreteCollectionTest<T>(T x, T y)
{
this.Convert(x).IsStructuralEqual(x);
this.Convert(y).IsStructuralEqual(y);
}
[Fact]
public void InterfaceCollectionTest()
{
var a = (IList<int>)new int[] { 1, 10, 100 };
var b = (ICollection<int>)new int[] { 1, 10, 100 };
IEnumerable<int> c = Enumerable.Range(1, 100).AsEnumerable();
var d = (IReadOnlyList<int>)new int[] { 1, 10, 100 };
var e = (IReadOnlyCollection<int>)new int[] { 1, 10, 100 };
var f = (ISet<int>)new HashSet<int>(new[] { 1, 10, 100 });
var g = (ILookup<bool, int>)Enumerable.Range(1, 100).ToLookup(x => x % 2 == 0);
this.Convert(a).Is(a);
this.Convert(b).Is(b);
this.Convert(c).Is(c);
this.Convert(d).Is(d);
this.Convert(e).Is(e);
this.Convert(f).Is(f);
this.Convert(g).Is(g);
a = null;
b = null;
c = null;
d = null;
e = null;
f = null;
g = null;
this.Convert(a).Is(a);
this.Convert(b).Is(b);
this.Convert(c).IsNull();
this.Convert(d).Is(d);
this.Convert(e).Is(e);
this.Convert(f).Is(f);
this.Convert(g).Is(g);
}
[Fact]
public void InterfaceCollectionsAreDeserializedMutable()
{
var list = this.Convert<IList<int>>(new[] { 1, 2, 3 });
list.Add(4);
Assert.Equal(new[] { 1, 2, 3, 4 }, list);
var collection = this.Convert<ICollection<int>>(new[] { 1, 2, 3 });
collection.Add(4);
Assert.Equal(new[] { 1, 2, 3, 4 }, collection);
var setCollection = this.Convert<ISet<int>>(new HashSet<int> { 1, 2, 3 });
setCollection.Add(4);
Assert.Equal(new[] { 1, 2, 3, 4 }, setCollection.OrderBy(n => n).ToArray());
}
[Fact]
public void StackTest()
{
var stack = new Stack<int>(new[] { 1, 10, 100 });
stack.AsEnumerable().Is(100, 10, 1);
this.Convert(stack).AsEnumerable().Is(100, 10, 1);
stack = new Stack<int>();
this.Convert(stack).AsEnumerable().Count().Is(0);
stack = null;
this.Convert(stack).IsNull();
}
[Fact]
public void ConcurrentCollectionTest()
{
var c0 = new ConcurrentQueue<int>(new[] { 1, 10, 100 });
var c1 = new ConcurrentStack<int>(new[] { 1, 10, 100 });
var c2 = new ConcurrentBag<int>(new[] { 1, 10, 100 });
this.Convert(c0).Is(1, 10, 100);
this.Convert(c1).Is(100, 10, 1);
this.Convert(c2).OrderBy(x => x).Is(1, 10, 100);
c0 = null;
c1 = null;
c2 = null;
this.Convert(c0).IsNull();
this.Convert(c1).IsNull();
this.Convert(c2).IsNull();
}
[Fact]
public void ArraySegmentTest()
{
var test = new ArraySegment<int>(new[] { 1, 10, 100 });
this.Convert(test).Is(1, 10, 100);
ArraySegment<int>? nullableTest = new ArraySegment<int>(new[] { 1, 10, 100 });
this.Convert(nullableTest).Is(1, 10, 100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void MemoryTest()
{
var test = new Memory<int>(new[] { 1, 10, 100 });
this.Convert(test).ToArray().Is(1, 10, 100);
Memory<int>? nullableTest = new Memory<int>(new[] { 1, 10, 100 });
this.Convert(nullableTest).Value.ToArray().Is(1, 10, 100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void MemoryOfByteTest()
{
var test = new Memory<byte>(new[] { (byte)1, (byte)10, (byte)100 });
this.Convert(test).ToArray().Is((byte)1, (byte)10, (byte)100);
Memory<byte>? nullableTest = new Memory<byte>(new[] { (byte)1, (byte)10, (byte)100 });
this.Convert(nullableTest).Value.ToArray().Is((byte)1, (byte)10, (byte)100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void ReadOnlyMemoryTest()
{
var test = new ReadOnlyMemory<int>(new[] { 1, 10, 100 });
this.Convert(test).ToArray().Is(1, 10, 100);
ReadOnlyMemory<int>? nullableTest = new ReadOnlyMemory<int>(new[] { 1, 10, 100 });
this.Convert(nullableTest).Value.ToArray().Is(1, 10, 100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void ReadOnlyMemoryOfByteTest()
{
var test = new ReadOnlyMemory<byte>(new[] { (byte)1, (byte)10, (byte)100 });
this.Convert(test).ToArray().Is((byte)1, (byte)10, (byte)100);
ReadOnlyMemory<byte>? nullableTest = new ReadOnlyMemory<byte>(new[] { (byte)1, (byte)10, (byte)100 });
this.Convert(nullableTest).Value.ToArray().Is((byte)1, (byte)10, (byte)100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void ReadOnlySequenceTest()
{
var test = new ReadOnlySequence<int>(new[] { 1, 10, 100 });
this.Convert(test).ToArray().Is(1, 10, 100);
ReadOnlySequence<int>? nullableTest = new ReadOnlySequence<int>(new[] { 1, 10, 100 });
this.Convert(nullableTest).Value.ToArray().Is(1, 10, 100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void ReadOnlySequenceOfByteTest()
{
var test = new ReadOnlySequence<byte>(new[] { (byte)1, (byte)10, (byte)100 });
this.Convert(test).ToArray().Is((byte)1, (byte)10, (byte)100);
ReadOnlySequence<byte>? nullableTest = new ReadOnlySequence<byte>(new[] { (byte)1, (byte)10, (byte)100 });
this.Convert(nullableTest).Value.ToArray().Is((byte)1, (byte)10, (byte)100);
nullableTest = null;
this.Convert(nullableTest).IsNull();
}
[Fact]
public void NonGenericInterfaceCollectionsTest()
{
var xs = new[] { 1, 2, 3 };
IEnumerable a = xs;
ICollection b = xs;
IList c = xs;
// in MessagePack v2.1, deserialized type is byte so can not use Cast<int>().
this.Convert(a).Cast<object>().Select(x => System.Convert.ToInt32(x)).Is(xs);
this.Convert(b).Cast<object>().Select(x => System.Convert.ToInt32(x)).Is(xs);
this.Convert(c).Cast<object>().Select(x => System.Convert.ToInt32(x)).Is(xs);
}
[Fact]
public void CustomNonGenericInterfaceListTest()
{
var xs = new ImplNonGenericList();
xs.Add(1);
xs.Add(2);
xs.Add(3);
this.Convert(xs).Cast<object>().Select(System.Convert.ToInt32).Is(1, 2, 3);
}
[Fact]
public void CustomGenericInterfaceCollectionTest()
{
{
var xs = new ImplGenericCollection();
xs.Add(1);
xs.Add(2);
xs.Add(3);
this.Convert(xs).Is(1, 2, 3);
}
{
var xs = new ImplGenericGenericCollection<int>();
xs.Add(1);
xs.Add(2);
xs.Add(3);
this.Convert(xs).Is(1, 2, 3);
}
}
[Fact]
public void CustomGenericEnumerableCollectionTest()
{
{
var xs = new ImplGenericEnumerable();
xs.Add(1);
xs.Add(2);
xs.Add(3);
this.Convert(xs).Is(1, 2, 3);
}
}
[Fact]
public void CustomGenericInterfaceReadOnlyCollectionTest()
{
{
var xs = new ImplGenericReadonlyCollection();
xs.Add(1);
xs.Add(2);
xs.Add(3);
this.Convert(xs).Is(1, 2, 3);
}
{
var xs = new ImplGenericGenericReadOnlyCollection<int>();
xs.Add(1);
xs.Add(2);
xs.Add(3);
this.Convert(xs).Is(1, 2, 3);
}
}
[Fact]
public void CustomGenericDictionaryTest()
{
var d = new ImplDictionary();
d.Add("foo", 10);
d.Add("bar", 20);
var d2 = this.Convert(d);
d2["foo"].Is(10);
d2["bar"].Is(20);
}
[Fact]
public void CustomGenericReadOnlyDictionaryTest()
{
var d = new ImplReadonlyDictionary();
d.Add("foo", 10);
d.Add("bar", 20);
var d2 = this.Convert(d);
d2["foo"].Is(10);
d2["bar"].Is(20);
}
}
public class ImplNonGenericList : IList
{
private readonly IList inner;
public ImplNonGenericList()
{
inner = new List<object>();
}
public object this[int index] { get => inner[index]; set => inner[index] = value; }
public bool IsReadOnly => inner.IsReadOnly;
public bool IsFixedSize => inner.IsFixedSize;
public int Count => inner.Count;
public object SyncRoot => inner.SyncRoot;
public bool IsSynchronized => inner.IsSynchronized;
public int Add(object value)
{
return inner.Add(value);
}
public void Clear()
{
inner.Clear();
}
public bool Contains(object value)
{
return inner.Contains(value);
}
public void CopyTo(Array array, int index)
{
inner.CopyTo(array, index);
}
public IEnumerator GetEnumerator()
{
return inner.GetEnumerator();
}
public int IndexOf(object value)
{
return inner.IndexOf(value);
}
public void Insert(int index, object value)
{
inner.Insert(index, value);
}
public void Remove(object value)
{
inner.Remove(value);
}
public void RemoveAt(int index)
{
inner.RemoveAt(index);
}
}
public class ImplGenericCollection : ICollection<int>
{
private readonly ICollection<int> inner;
public ImplGenericCollection()
{
this.inner = new List<int>();
}
public int Count => inner.Count;
public bool IsReadOnly => inner.IsReadOnly;
public void Add(int item)
{
inner.Add(item);
}
public void Clear()
{
inner.Clear();
}
public bool Contains(int item)
{
return inner.Contains(item);
}
public void CopyTo(int[] array, int arrayIndex)
{
inner.CopyTo(array, arrayIndex);
}
public IEnumerator<int> GetEnumerator()
{
return inner.GetEnumerator();
}
public bool Remove(int item)
{
return inner.Remove(item);
}
IEnumerator IEnumerable.GetEnumerator()
{
return inner.GetEnumerator();
}
}
public class ImplGenericGenericCollection<T> : ICollection<T>
{
private readonly ICollection<T> inner;
public ImplGenericGenericCollection()
{
this.inner = new List<T>();
}
public int Count => inner.Count;
public bool IsReadOnly => inner.IsReadOnly;
public void Add(T item)
{
inner.Add(item);
}
public void Clear()
{
inner.Clear();
}
public bool Contains(T item)
{
return inner.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
inner.CopyTo(array, arrayIndex);
}
public IEnumerator<T> GetEnumerator()
{
return inner.GetEnumerator();
}
public bool Remove(T item)
{
return inner.Remove(item);
}
IEnumerator IEnumerable.GetEnumerator()
{
return inner.GetEnumerator();
}
}
public class ImplGenericGenericReadOnlyCollection<T> : IReadOnlyCollection<T>
{
private readonly ICollection<T> inner;
public ImplGenericGenericReadOnlyCollection()
{
this.inner = new List<T>();
}
public ImplGenericGenericReadOnlyCollection(IEnumerable<T> items)
{
this.inner = new List<T>(items);
}
public IEnumerator<T> GetEnumerator()
{
return inner.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)inner).GetEnumerator();
}
public int Count => inner.Count;
public void Add(T item)
{
inner.Add(item);
}
}
public class ImplDictionary : IDictionary<string, int>
{
private readonly Dictionary<string, int> inner;
public ImplDictionary()
{
this.inner = new Dictionary<string, int>();
}
public int this[string key] { get => inner[key]; set => inner[key] = value; }
public ICollection<string> Keys => ((IDictionary<string, int>)inner).Keys;
public ICollection<int> Values => ((IDictionary<string, int>)inner).Values;
public int Count => inner.Count;
public bool IsReadOnly => ((IDictionary<string, int>)inner).IsReadOnly;
public void Add(string key, int value)
{
inner.Add(key, value);
}
public void Add(KeyValuePair<string, int> item)
{
((IDictionary<string, int>)inner).Add(item);
}
public void Clear()
{
inner.Clear();
}
public bool Contains(KeyValuePair<string, int> item)
{
return ((IDictionary<string, int>)inner).Contains(item);
}
public bool ContainsKey(string key)
{
return inner.ContainsKey(key);
}
public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex)
{
((IDictionary<string, int>)inner).CopyTo(array, arrayIndex);
}
public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
{
return ((IDictionary<string, int>)inner).GetEnumerator();
}
public bool Remove(string key)
{
return inner.Remove(key);
}
public bool Remove(KeyValuePair<string, int> item)
{
return ((IDictionary<string, int>)inner).Remove(item);
}
public bool TryGetValue(string key, out int value)
{
return inner.TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IDictionary<string, int>)inner).GetEnumerator();
}
}
public class ImplGenericReadonlyCollection : IReadOnlyCollection<int>
{
private readonly ICollection<int> inner;
public ImplGenericReadonlyCollection()
{
this.inner = new List<int>();
}
public ImplGenericReadonlyCollection(IEnumerable<int> items)
{
this.inner = new List<int>(items);
}
public int Count => inner.Count;
public IEnumerator<int> GetEnumerator()
{
return inner.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return inner.GetEnumerator();
}
public void Add(int item)
{
inner.Add(item);
}
}
public class ImplReadonlyDictionary : IReadOnlyDictionary<string, int>
{
private readonly Dictionary<string, int> inner;
public ImplReadonlyDictionary()
{
this.inner = new Dictionary<string, int>();
}
public ImplReadonlyDictionary(IDictionary<string, int> inner)
{
this.inner = new Dictionary<string, int>(inner);
}
public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
{
return inner.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)inner).GetEnumerator();
}
public int Count => inner.Count;
public bool ContainsKey(string key)
{
return inner.ContainsKey(key);
}
public bool TryGetValue(string key, out int value)
{
return inner.TryGetValue(key, out value);
}
public int this[string key] => inner[key];
public IEnumerable<string> Keys => inner.Keys;
public IEnumerable<int> Values => inner.Values;
public void Add(string key, int value)
{
inner.Add(key, value);
}
}
public class ImplGenericEnumerable : IEnumerable<int>
{
private readonly List<int> inner = new List<int>();
public ImplGenericEnumerable()
{
}
public ImplGenericEnumerable(IEnumerable<double> values)
{
}
public ImplGenericEnumerable(IEnumerable<int> values)
{
inner.AddRange(values);
}
public ImplGenericEnumerable(ICollection<int> values)
{
inner.AddRange(values);
}
public IEnumerator<int> GetEnumerator()
{
return inner.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)inner).GetEnumerator();
}
public void Add(int item) => inner.Add(item);
}
}