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
|
// 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.Collections.Specialized.Tests
{
public class NameValueCollectionCopyToTests
{
[Theory]
[InlineData(0, 0)]
[InlineData(0, 1)]
[InlineData(5, 0)]
[InlineData(5, 1)]
public void CopyTo(int count, int index)
{
NameValueCollection nameValueCollection = Helpers.CreateNameValueCollection(count);
string[] dest = new string[count + index + 5];
nameValueCollection.CopyTo(dest, index);
for (int i = 0; i < index; i++)
{
Assert.Null(dest[i]);
}
for (int i = 0; i < count; i++)
{
Assert.Equal(nameValueCollection.Get(i), dest[i + index]);
}
for (int i = index + count; i < dest.Length; i++)
{
Assert.Null(dest[i]);
}
nameValueCollection.CopyTo(dest, index);
for (int i = 0; i < count; i++)
{
Assert.Equal(nameValueCollection.Get(i), dest[i + index]);
}
}
[Fact]
public void CopyTo_MultipleValues_SameName()
{
NameValueCollection nameValueCollection = new NameValueCollection();
string name = "name";
nameValueCollection.Add(name, "value1");
nameValueCollection.Add(name, "value2");
nameValueCollection.Add(name, "value3");
string[] dest = new string[1];
nameValueCollection.CopyTo(dest, 0);
Assert.Equal(nameValueCollection[0], dest[0]);
}
[Theory]
[InlineData(0)]
[InlineData(5)]
public void CopyTo_Invalid(int count)
{
NameValueCollection nameValueCollection = Helpers.CreateNameValueCollection(count);
Assert.Throws<ArgumentNullException>("dest", () => nameValueCollection.CopyTo(null, 0));
Assert.Throws<ArgumentException>("dest", () => nameValueCollection.CopyTo(new string[count, count], 0));
Assert.Throws<ArgumentOutOfRangeException>("index", () => nameValueCollection.CopyTo(new string[count], -1));
Assert.Throws<ArgumentException>(null, () => nameValueCollection.CopyTo(new string[count], 1));
Assert.Throws<ArgumentException>(null, () => nameValueCollection.CopyTo(new string[count], count + 1));
if (count > 0)
{
Assert.Throws<ArgumentException>(null, () => nameValueCollection.CopyTo(new string[count], count));
Assert.Throws<InvalidCastException>(() => nameValueCollection.CopyTo(new DictionaryEntry[count], 0));
}
else
{
// InvalidCastException should not throw for an empty NameValueCollection
nameValueCollection.CopyTo(new DictionaryEntry[count], 0);
}
}
}
}
|