forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTest_ReadOnlyRefEnumerable{T}.cs
More file actions
131 lines (113 loc) · 4.53 KB
/
Test_ReadOnlyRefEnumerable{T}.cs
File metadata and controls
131 lines (113 loc) · 4.53 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
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
// 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.
#if !WINDOWS_UWP
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Enumerables;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.HighPerformance.Enumerables
{
[TestClass]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649", Justification = "Test class for generic type")]
public class Test_ReadOnlyRefEnumerable
{
[TestCategory("ReadOnlyRefEnumerable")]
[TestMethod]
[DataRow(1, 1, new[] { 1 })]
[DataRow(4, 1, new[] { 1, 2, 3, 4 })]
[DataRow(4, 4, new[] { 1, 5, 9, 13 })]
[DataRow(4, 5, new[] { 1, 6, 11, 16 })]
public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step, int[] values)
{
Span<int> data = new[]
{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], length, step);
int[] result = enumerable.ToArray();
CollectionAssert.AreEqual(result, values);
}
[TestCategory("ReadOnlyRefEnumerable")]
[TestMethod]
[DataRow(-44, 10)]
[DataRow(10, -14)]
[DataRow(-32, -1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step)
{
_ = ReadOnlyRefEnumerable<int>.DangerousCreate(in Unsafe.NullRef<int>(), length, step);
}
[TestCategory("ReadOnlyRefEnumerable")]
[TestMethod]
[DataRow(1, new[] { 1 })]
[DataRow(1, new[] { 1, 2, 3, 4 })]
[DataRow(4, new[] { 1, 5, 9, 13 })]
[DataRow(5, new[] { 1, 6, 11, 16 })]
public void Test_ReadOnlyRefEnumerable_Indexer(int step, int[] values)
{
Span<int> data = new[]
{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], values.Length, step);
for (int i = 0; i < enumerable.Length; i++)
{
Assert.AreEqual(enumerable[i], values[i]);
}
}
[TestCategory("ReadOnlyRefEnumerable")]
[TestMethod]
public void Test_ReadOnlyRefEnumerable_Indexer_ThrowsIndexOutOfRange()
{
int[] array = new[]
{
0, 0, 0, 0
};
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[-1]);
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[array.Length]);
}
#if NETCOREAPP3_1_OR_GREATER
[TestCategory("ReadOnlyRefEnumerable")]
[TestMethod]
[DataRow(1, new[] { 1 })]
[DataRow(1, new[] { 1, 2, 3, 4 })]
[DataRow(4, new[] { 1, 5, 9, 13 })]
[DataRow(5, new[] { 1, 6, 11, 16 })]
public void Test_ReadOnlyRefEnumerable_Index_Indexer(int step, int[] values)
{
Span<int> data = new[]
{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], values.Length, step);
for (int i = 1; i <= enumerable.Length; i++)
{
Assert.AreEqual(values[^i], enumerable[^i]);
}
}
[TestCategory("ReadOnlyRefEnumerable")]
[TestMethod]
public void Test_ReadOnlyRefEnumerable_Index_Indexer_ThrowsIndexOutOfRange()
{
int[] array = new[]
{
0, 0, 0, 0
};
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[new Index(array.Length)]);
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[^0]);
}
#endif
}
}
#endif