This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ImmutableTestBase.cs
146 lines (123 loc) · 4.33 KB
/
ImmutableTestBase.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using Xunit;
namespace System.Collections.Immutable.Tests
{
public abstract partial class ImmutablesTestBase
{
/// <summary>
/// Gets the number of operations to perform in randomized tests.
/// </summary>
protected int RandomOperationsCount
{
get { return 100; }
}
internal static void AssertAreSame<T>(T expected, T actual)
{
if (typeof(T).GetTypeInfo().IsValueType)
{
Assert.Equal(expected, actual); //, message, formattingArgs);
}
else
{
Assert.Same((object)expected, (object)actual); //, message, formattingArgs);
}
}
internal static void CollectionAssertAreEquivalent<T>(ICollection<T> expected, ICollection<T> actual)
{
Assert.Equal(expected.Count, actual.Count);
foreach (var value in expected)
{
Assert.Contains(value, actual);
}
}
protected static string ToString(System.Collections.IEnumerable sequence)
{
var sb = new StringBuilder();
sb.Append('{');
int count = 0;
foreach (object item in sequence)
{
if (count > 0)
{
sb.Append(',');
}
if (count == 10)
{
sb.Append("...");
break;
}
sb.Append(item);
count++;
}
sb.Append('}');
return sb.ToString();
}
protected static object ToStringDeferred(System.Collections.IEnumerable sequence)
{
return new DeferredToString(() => ToString(sequence));
}
protected static void ManuallyEnumerateTest<T>(IList<T> expectedResults, IEnumerator<T> enumerator)
{
T[] manualArray = new T[expectedResults.Count];
int i = 0;
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
while (enumerator.MoveNext())
{
manualArray[i++] = enumerator.Current;
}
enumerator.MoveNext();
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
enumerator.MoveNext();
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
Assert.Equal(expectedResults.Count, i); //, "Enumeration did not produce enough elements.");
Assert.Equal<T>(expectedResults, manualArray);
}
/// <summary>
/// Generates an array of unique values.
/// </summary>
/// <param name="length">The desired length of the array.</param>
/// <returns>An array of doubles.</returns>
protected double[] GenerateDummyFillData(int length = 1000)
{
Assert.InRange(length, 0, int.MaxValue);
int seed = unchecked((int)DateTime.Now.Ticks);
Debug.WriteLine("Random seed {0}", seed);
var random = new Random(seed);
var inputs = new double[length];
var ensureUniqueness = new HashSet<double>();
for (int i = 0; i < inputs.Length; i++)
{
double input;
do
{
input = random.NextDouble();
}
while (!ensureUniqueness.Add(input));
inputs[i] = input;
}
Assert.NotNull(inputs);
Assert.Equal(length, inputs.Length);
return inputs;
}
private class DeferredToString
{
private readonly Func<string> _generator;
internal DeferredToString(Func<string> generator)
{
Debug.Assert(generator != null);
_generator = generator;
}
public override string ToString()
{
return _generator();
}
}
}
}