-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
OpportunisticIntern_Tests.cs
217 lines (190 loc) · 6.85 KB
/
OpportunisticIntern_Tests.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Text;
using Microsoft.Build.Shared;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Build.UnitTests
{
public abstract class OpportunisticInternTestBase : IDisposable
{
protected TestEnvironment _env;
public void Dispose()
{
_env.Dispose();
}
protected OpportunisticInternTestBase(ITestOutputHelper testOutput)
{
_env = TestEnvironment.Create(testOutput);
}
private static bool IsInternable(IInternable internable)
{
string i1 = OpportunisticIntern.InternableToString(internable);
string i2 = OpportunisticIntern.InternableToString(internable);
Assert.Equal(i1, i2); // No matter what, the same string value should return.
return Object.ReferenceEquals(i1, i2);
}
private static void AssertInternable(IInternable internable)
{
Assert.True(IsInternable(internable));
}
private static void AssertInternable(StringBuilder sb)
{
AssertInternable(new StringBuilderInternTarget(sb));
}
private static string AssertInternable(char[] ch, int startIndex, int count)
{
var target = new CharArrayInternTarget(ch, startIndex, count);
AssertInternable(target);
Assert.Equal(target.Length, count);
return target.ExpensiveConvertToString();
}
private static void AssertInternable(string value)
{
AssertInternable(new StringBuilder(value));
AssertInternable(value.ToCharArray(), 0, value.ToCharArray().Length);
}
private static void AssertNotInternable(IInternable internable)
{
Assert.False(IsInternable(internable));
}
private static void AssertNotInternable(StringBuilder sb)
{
AssertNotInternable(new StringBuilderInternTarget(sb));
}
private static void AssertNotInternable(char[] ch)
{
AssertNotInternable(new CharArrayInternTarget(ch, ch.Length));
}
protected static void AssertNotInternable(string value)
{
AssertNotInternable(new StringBuilder(value));
AssertNotInternable(value.ToCharArray());
}
/// <summary>
/// Test interning segment of char array
/// </summary>
[Fact]
public void SubArray()
{
var result = AssertInternable(new char[] { 'a', 't', 'r', 'u', 'e' }, 1, 4);
Assert.Equal("true", result);
}
/// <summary>
/// Test interning segment of char array
/// </summary>
[Fact]
public void SubArray2()
{
var result = AssertInternable(new char[] { 'a', 't', 'r', 'u', 'e', 'x' }, 1, 4);
Assert.Equal("true", result);
}
/// <summary>
/// Unique strings should not be interned
/// </summary>
[Fact]
public void NonInternableDummyGlobalVariable()
{
AssertNotInternable($"{MSBuildConstants.MSBuildDummyGlobalPropertyHeader}{new string('1', 100)}");
}
/// <summary>
/// This is the list of hard-coded interns. They should report interned even though they are too small for normal interning.
/// </summary>
[Fact]
public void KnownInternableTinyStrings()
{
AssertInternable("C#");
AssertInternable("F#");
AssertInternable("VB");
AssertInternable("True");
AssertInternable("TRUE");
AssertInternable("Copy");
AssertInternable("v4.0");
AssertInternable("true");
AssertInternable("FALSE");
AssertInternable("false");
AssertInternable("Debug");
AssertInternable("Build");
AssertInternable("''!=''");
AssertInternable("AnyCPU");
AssertInternable("Library");
AssertInternable("MSBuild");
AssertInternable("Release");
AssertInternable("ResolveAssemblyReference");
}
/// <summary>
/// Test a set of strings that are similar to each other
/// </summary>
[Fact]
public void InternableDifferingOnlyByNthCharacter()
{
string test = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!@#$%^&*()_+ABCDEFGHIJKLMNOPQRSTUVabcdefghijklmnopqrstuvwxyz0150";
for (int i = 0; i < test.Length; ++i)
{
string mutated = test.Substring(0, i) + " " + test.Substring(i + 1);
AssertInternable(mutated);
}
}
/// <summary>
/// Test The empty string
/// </summary>
[Fact]
public void StringDotEmpty()
{
AssertInternable(String.Empty);
}
/// <summary>
/// Test an empty string.
/// </summary>
[Fact]
public void DoubleDoubleQuotes()
{
AssertInternable("");
}
}
/// <summary>
/// Tests the new (default) implementation of OpportunisticIntern.
/// </summary>
public class OpportunisticIntern_Tests : OpportunisticInternTestBase
{
public OpportunisticIntern_Tests(ITestOutputHelper testOutput)
: base(testOutput)
{
OpportunisticIntern.ResetForTests();
}
}
/// <summary>
/// Tests the legacy implementation of OpportunisticIntern.
/// </summary>
public class OpportunisticInternLegacy_Tests : OpportunisticInternTestBase
{
public OpportunisticInternLegacy_Tests(ITestOutputHelper testOutput)
: base(testOutput)
{
_env.SetEnvironmentVariable("MSBuildUseLegacyStringInterner", "1");
OpportunisticIntern.ResetForTests();
}
/// <summary>
/// The legacy implementation does not intern tiny strings unless they are on the hard-coded list.
/// </summary>
[Fact]
public void NonInternableTinyString()
{
AssertNotInternable("1234");
}
}
/// <summary>
/// Tests the legacy implementation of OpportunisticIntern with simple concurrency enabled.
/// </summary>
public class OpportunisticInternLegacySimpleConcurrecy_Tests : OpportunisticInternTestBase
{
public OpportunisticInternLegacySimpleConcurrecy_Tests(ITestOutputHelper testOutput)
: base(testOutput)
{
_env.SetEnvironmentVariable("MSBuildUseLegacyStringInterner", "1");
_env.SetEnvironmentVariable("MSBuildUseSimpleInternConcurrency", "1");
OpportunisticIntern.ResetForTests();
}
}
}