forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyOnWriteDictionaryTest.cs
More file actions
100 lines (90 loc) · 4.23 KB
/
Copy pathCopyOnWriteDictionaryTest.cs
File metadata and controls
100 lines (90 loc) · 4.23 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Mvc.Test
{
public class CopyOnWriteDictionaryTest
{
[Fact]
public void ReadOperation_DelegatesToSourceDictionary_IfNoMutationsArePerformed()
{
// Arrange
var values = new List<object>();
var enumerator = Mock.Of<IEnumerator<KeyValuePair<string, object>>>();
var sourceDictionary = new Mock<IDictionary<string, object>>(MockBehavior.Strict);
sourceDictionary.SetupGet(d => d.Count)
.Returns(100)
.Verifiable();
sourceDictionary.SetupGet(d => d.Values)
.Returns(values)
.Verifiable();
sourceDictionary.Setup(d => d.ContainsKey("test-key"))
.Returns(value: true)
.Verifiable();
sourceDictionary.Setup(d => d.GetEnumerator())
.Returns(enumerator)
.Verifiable();
sourceDictionary.Setup(d => d["key2"])
.Returns("key2-value")
.Verifiable();
object value;
sourceDictionary.Setup(d => d.TryGetValue("different-key", out value))
.Returns(false)
.Verifiable();
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary.Object,
StringComparer.OrdinalIgnoreCase);
// Act and Assert
Assert.Equal("key2-value", copyOnWriteDictionary["key2"]);
Assert.Equal(100, copyOnWriteDictionary.Count);
Assert.Same(values, copyOnWriteDictionary.Values);
Assert.True(copyOnWriteDictionary.ContainsKey("test-key"));
Assert.Same(enumerator, copyOnWriteDictionary.GetEnumerator());
Assert.False(copyOnWriteDictionary.TryGetValue("different-key", out value));
sourceDictionary.Verify();
}
[Fact]
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceAValueIsChanged()
{
// Arrange
var values = new List<object>();
var sourceDictionary = new Dictionary<string, object>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary,
StringComparer.OrdinalIgnoreCase);
// Act
copyOnWriteDictionary["key2"] = "value3";
// Assert
Assert.Equal("value2", sourceDictionary["key2"]);
Assert.Equal(2, copyOnWriteDictionary.Count);
Assert.Equal("value1", copyOnWriteDictionary["key1"]);
Assert.Equal("value3", copyOnWriteDictionary["key2"]);
}
[Fact]
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceDictionaryIsModified()
{
// Arrange
var values = new List<object>();
var sourceDictionary = new Dictionary<string, object>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary,
StringComparer.OrdinalIgnoreCase);
// Act
copyOnWriteDictionary.Add("key3", "value3");
copyOnWriteDictionary.Remove("key1");
// Assert
Assert.Equal(2, sourceDictionary.Count);
Assert.Equal("value1", sourceDictionary["key1"]);
Assert.Equal(2, copyOnWriteDictionary.Count);
Assert.Equal("value2", copyOnWriteDictionary["KeY2"]);
Assert.Equal("value3", copyOnWriteDictionary["key3"]);
}
}
}