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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
// 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 Xunit;
namespace System.Reflection.Tests
{
public class ConstructorInfoInvokeArrayTests
{
[Fact]
public void Invoke_SZArrayConstructor()
{
Type type = Type.GetType("System.Object[]");
ConstructorInfo[] constructors = type.GetConstructors();
Assert.Equal(1, constructors.Length);
ConstructorInfo constructor = constructors[0];
int[] blength = new int[] { -100, -9, -1 };
for (int j = 0; j < blength.Length; j++)
{
Assert.Throws<OverflowException>(() => constructor.Invoke(new object[] { blength[j] }));
}
int[] glength = new int[] { 0, 1, 2, 3, 5, 10, 99, 65535 };
for (int j = 0; j < glength.Length; j++)
{
object[] arr = (object[])constructor.Invoke(new object[] { glength[j] });
Assert.Equal(0, arr.GetLowerBound(0));
Assert.Equal(glength[j] - 1, arr.GetUpperBound(0));
Assert.Equal(glength[j], arr.Length);
}
}
[Fact]
public void Invoke_1DArrayConstructor()
{
Type type = Type.GetType("System.Char[*]");
MethodInfo getLowerBound = type.GetMethod("GetLowerBound");
MethodInfo getUpperBound = type.GetMethod("GetUpperBound");
PropertyInfo getLength = type.GetProperty("Length");
ConstructorInfo[] constructors = type.GetConstructors();
Assert.Equal(2, constructors.Length);
for (int i = 0; i < constructors.Length; i++)
{
switch (constructors[i].GetParameters().Length)
{
case 1:
{
int[] invalidLengths = new int[] { -100, -9, -1 };
for (int j = 0; j < invalidLengths.Length; j++)
{
Assert.Throws<OverflowException>(() => constructors[i].Invoke(new object[] { invalidLengths[j] }));
}
int[] validLengths = new int[] { 0, 1, 2, 3, 5, 10, 99 };
for (int j = 0; j < validLengths.Length; j++)
{
char[] arr = (char[])constructors[i].Invoke(new object[] { validLengths[j] });
Assert.Equal(0, arr.GetLowerBound(0));
Assert.Equal(validLengths[j] - 1, arr.GetUpperBound(0));
Assert.Equal(validLengths[j], arr.Length);
}
}
break;
case 2:
{
int[] invalidLowerBounds = new int[] { -20, 0, 20 };
int[] invalidLengths = new int[] { -100, -9, -1 };
for (int j = 0; j < invalidLengths.Length; j++)
{
Assert.Throws<OverflowException>(() => constructors[i].Invoke(new object[] { invalidLowerBounds[j], invalidLengths[j] }));
}
int[] validLowerBounds = new int[] { 0, 1, -1, 2, -3, 5, -10, 99, 100 };
int[] validLengths = new int[] { 0, 1, 3, 2, 3, 5, 10, 99, 0 };
for (int j = 0; j < validLengths.Length; j++)
{
object o = constructors[i].Invoke(new object[] { validLowerBounds[j], validLengths[j] });
Assert.Equal(validLowerBounds[j], (int)getLowerBound.Invoke(o, new object[] { 0 }));
Assert.Equal(validLowerBounds[j] + validLengths[j] - 1, (int)getUpperBound.Invoke(o, new object[] { 0 }));
Assert.Equal(validLengths[j], (int)getLength.GetValue(o, null));
}
}
break;
}
}
}
[Fact]
public void Invoke_2DArrayConstructor()
{
Type type = Type.GetType("System.Int32[,]", false);
ConstructorInfo[] constructors = type.GetConstructors();
Assert.Equal(2, constructors.Length);
for (int i = 0; i < constructors.Length; i++)
{
switch (constructors[i].GetParameters().Length)
{
case 2:
{
int[] invalidLengths1 = new int[] { -11, -10, 0, 10 };
int[] invalidLengths2 = new int[] { -33, 0, -20, -33 };
for (int j = 0; j < invalidLengths1.Length; j++)
{
Assert.Throws<OverflowException>(() => constructors[i].Invoke(new object[] { invalidLengths1[j], invalidLengths2[j] }));
}
int[] validLengths1 = new int[] { 0, 0, 1, 1, 2, 1, 2, 10, 17, 99 };
int[] validLengths2 = new int[] { 0, 1, 0, 1, 1, 2, 2, 110, 5, 900 };
for (int j = 0; j < validLengths1.Length; j++)
{
int[,] arr = (int[,])constructors[i].Invoke(new object[] { validLengths1[j], validLengths2[j] });
Assert.Equal(0, arr.GetLowerBound(0));
Assert.Equal(validLengths1[j] - 1, arr.GetUpperBound(0));
Assert.Equal(0, arr.GetLowerBound(1));
Assert.Equal(validLengths2[j] - 1, arr.GetUpperBound(1));
Assert.Equal(validLengths1[j] * validLengths2[j], arr.Length);
}
}
break;
case 4:
{
int[] invalidLowerBounds1 = new int[] { 10, -10, 20 };
int[] invalidLowerBounds2 = new int[] { -10, 10, 0 };
int[] invalidLengths3 = new int[] { -11, -10, 0 };
int[] invalidLengths4 = new int[] { -33, 0, -20 };
for (int j = 0; j < invalidLengths3.Length; j++)
{
Assert.Throws<OverflowException>(() => constructors[i].Invoke(new object[] { invalidLowerBounds1[j], invalidLengths3[j], invalidLowerBounds2[j], invalidLengths4[j] }));
}
int baseNum = 3;
int baseNum4 = baseNum * baseNum * baseNum * baseNum;
int[] validLengths1 = new int[baseNum4];
int[] validLowerBounds1 = new int[baseNum4];
int[] validLengths2 = new int[baseNum4];
int[] validLowerBounds2 = new int[baseNum4];
int cnt = 0;
for (int pos1 = 0; pos1 < baseNum; pos1++)
for (int pos2 = 0; pos2 < baseNum; pos2++)
for (int pos3 = 0; pos3 < baseNum; pos3++)
for (int pos4 = 0; pos4 < baseNum; pos4++)
{
int saved = cnt;
validLengths1[cnt] = saved % baseNum;
saved = saved / baseNum;
validLengths2[cnt] = saved % baseNum;
saved = saved / baseNum;
validLowerBounds1[cnt] = saved % baseNum;
saved = saved / baseNum;
validLowerBounds2[cnt] = saved % baseNum;
cnt++;
}
for (int j = 0; j < validLengths2.Length; j++)
{
int[,] arr = (int[,])constructors[i].Invoke(new object[] { validLengths1[j], validLengths2[j], validLowerBounds1[j], validLowerBounds2[j] });
Assert.Equal(validLengths1[j], arr.GetLowerBound(0));
Assert.Equal(validLengths1[j] + validLengths2[j] - 1, arr.GetUpperBound(0));
Assert.Equal(validLowerBounds1[j], arr.GetLowerBound(1));
Assert.Equal(validLowerBounds1[j] + validLowerBounds2[j] - 1, arr.GetUpperBound(1));
Assert.Equal(validLengths2[j] * validLowerBounds2[j], arr.Length);
}
// Lower can be < 0
validLengths1 = new int[] { 10, 10, 65535, 40, 0, -10, -10, -20, -40, 0 };
validLowerBounds1 = new int[] { 5, 99, -100, 30, 4, -5, 99, 100, -30, 0 };
validLengths2 = new int[] { 1, 200, 2, 40, 0, 1, 200, 2, 40, 65535 };
validLowerBounds2 = new int[] { 5, 10, 1, 0, 4, 5, 65535, 1, 0, 4 };
for (int j = 0; j < validLengths2.Length; j++)
{
int[,] arr = (int[,])constructors[i].Invoke(new object[] { validLengths1[j], validLengths2[j], validLowerBounds1[j], validLowerBounds2[j] });
Assert.Equal(validLengths1[j], arr.GetLowerBound(0));
Assert.Equal(validLengths1[j] + validLengths2[j] - 1, arr.GetUpperBound(0));
Assert.Equal(validLowerBounds1[j], arr.GetLowerBound(1));
Assert.Equal(validLowerBounds1[j] + validLowerBounds2[j] - 1, arr.GetUpperBound(1));
Assert.Equal(validLengths2[j] * validLowerBounds2[j], arr.Length);
}
}
break;
}
}
}
[Fact]
public void Invoke_LargeDimensionalArrayConstructor()
{
Type type = Type.GetType("System.Type[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]");
ConstructorInfo[] cia = type.GetConstructors();
Assert.Equal(2, cia.Length);
Assert.Throws<TypeLoadException>(() => Type.GetType("System.Type[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]"));
}
[Fact]
public void Invoke_JaggedArrayConstructor()
{
Type type = Type.GetType("System.String[][]");
ConstructorInfo[] constructors = type.GetConstructors();
Assert.Equal(2, constructors.Length);
for (int i = 0; i < constructors.Length; i++)
{
switch (constructors[i].GetParameters().Length)
{
case 1:
{
int[] invalidLengths = new int[] { -11, -10, -99 };
for (int j = 0; j < invalidLengths.Length; j++)
{
Assert.Throws<OverflowException>(() => constructors[i].Invoke(new object[] { invalidLengths[j] }));
}
int[] validLengths = new int[] { 0, 1, 2, 10, 17, 99 };
for (int j = 0; j < validLengths.Length; j++)
{
string[][] arr = (string[][])constructors[i].Invoke(new object[] { validLengths[j] });
Assert.Equal(0, arr.GetLowerBound(0));
Assert.Equal(validLengths[j] - 1, arr.GetUpperBound(0));
Assert.Equal(validLengths[j], arr.Length);
}
}
break;
case 2:
{
int[] invalidLengths1 = new int[] { -11, -10, 10, 1 };
int[] invalidLengths2 = new int[] { -33, 0, -33, -1 };
for (int j = 0; j < invalidLengths1.Length; j++)
{
Assert.Throws<OverflowException>(() => constructors[i].Invoke(new object[] { invalidLengths1[j], invalidLengths2[j] }));
}
int[] validLengths1 = new int[] { 0, 0, 0, 1, 1, 2, 1, 2, 10, 17, 500 };
int[] validLengths2 = new int[] { -33, 0, 1, 0, 1, 1, 2, 2, 110, 5, 100 };
for (int j = 0; j < validLengths1.Length; j++)
{
string[][] arr = (string[][])constructors[i].Invoke(new object[] { validLengths1[j], validLengths2[j] });
Assert.Equal(0, arr.GetLowerBound(0));
Assert.Equal(validLengths1[j] - 1, arr.GetUpperBound(0));
Assert.Equal(validLengths1[j], arr.Length);
if (validLengths1[j] == 0)
{
Assert.Equal(arr.Length, 0);
}
else
{
Assert.Equal(0, arr[0].GetLowerBound(0));
Assert.Equal(validLengths2[j] - 1, arr[0].GetUpperBound(0));
Assert.Equal(validLengths2[j], arr[0].Length);
}
}
}
break;
}
}
}
}
}
|