-
-
Notifications
You must be signed in to change notification settings - Fork 672
/
DictionaryTest.cs
72 lines (62 loc) · 2.31 KB
/
DictionaryTest.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
// 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.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 DictionaryTest
{
private T Convert<T>(T value)
{
return MessagePackSerializer.Deserialize<T>(MessagePackSerializer.Serialize(value));
}
public static object[][] DictionaryTestData = new object[][]
{
new object[] { new Dictionary<int, int>() { { 1, 100 } }, null },
new object[] { new ReadOnlyDictionary<int, int>(new Dictionary<int, int>() { { 1, 100 } }), null },
new object[] { new SortedList<int, int>() { { 1, 100 } }, null },
new object[] { new SortedDictionary<int, int>() { { 1, 100 } }, null },
};
[Theory]
[MemberData(nameof(DictionaryTestData))]
public void DictionaryTestAll<T>(T x, T y)
{
this.Convert(x).IsStructuralEqual(x);
this.Convert(y).IsStructuralEqual(y);
}
[Fact]
public void InterfaceDictionaryTest()
{
var a = (IDictionary<int, int>)new Dictionary<int, int>() { { 1, 100 } };
var b = (IReadOnlyDictionary<int, int>)new Dictionary<int, int>() { { 1, 100 } };
var c = (IDictionary<int, int>)null;
var d = (IReadOnlyDictionary<int, int>)null;
this.Convert(a).IsStructuralEqual(a);
this.Convert(b).IsStructuralEqual(b);
this.Convert(c).IsStructuralEqual(c);
this.Convert(d).IsStructuralEqual(d);
}
[Fact]
public void ConcurrentDictionaryTest()
{
var cd = new ConcurrentDictionary<int, int>();
ConcurrentDictionary<int, int> conv = this.Convert(cd);
conv.Count.Is(0);
cd.TryAdd(1, 100);
cd.TryAdd(2, 200);
cd.TryAdd(3, 300);
conv = this.Convert(cd);
conv[1].Is(100);
conv[2].Is(200);
conv[3].Is(300);
cd = null;
this.Convert(cd).IsNull();
}
}
}