Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ReadOnlyCollectionBaseTests.cs « tests « System.Collections.NonGeneric « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26877855f3a1b3cc1ab5eeff1483583a95f2bf25 (plain)
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
// 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.Tests
{
    public class ReadOnlyCollectionBaseTests
    {
        private static MyReadOnlyCollectionBase CreateCollection()
        {
            var fooArray = new Foo[100];
            for (int i = 0; i < 100; i++)
            {
                fooArray[i] = new Foo(i, i.ToString());
            }

            return new MyReadOnlyCollectionBase(fooArray);
        }

        [Fact]
        public static void SyncRoot()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();
            Assert.False(collection.SyncRoot is ArrayList);
            Assert.Same(collection.SyncRoot, collection.SyncRoot);
        }

        [Fact]
        public static void AddRange_Count()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();
            Assert.Equal(100, collection.Count);
        }
        
        [Fact]
        public static void CopyTo_ZeroIndex()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();

            var copyArray = new Foo[100];
            collection.CopyTo(copyArray, 0);

            Assert.Equal(100, copyArray.Length);
            for (int i = 0; i < 100; i++)
            {
                Assert.Equal(i, copyArray[i].IntValue);
                Assert.Equal(i.ToString(), copyArray[i].StringValue);
            }
        }

        [Fact]
        public static void CopyTo_NonZeroIndex()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();

            var copyArray = new Foo[200];
            collection.CopyTo(copyArray, 100);

            Assert.Equal(200, copyArray.Length);
            for (int i = 0; i < 100; i++)
            {
                Assert.Equal(i, copyArray[100 + i].IntValue);
                Assert.Equal(i.ToString(), copyArray[100 + i].StringValue);
            }
        }

        [Fact]
        public static void CopyTo_Invalid()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();

            Assert.Throws<ArgumentNullException>("dest", () => collection.CopyTo(null, 0)); // Array is null

            Assert.Throws<ArgumentException>(string.Empty, () => collection.CopyTo(new Foo[100], 50)); // Index + collection.Count > array.Length
            Assert.Throws<ArgumentOutOfRangeException>("dstIndex", () => collection.CopyTo(new Foo[100], -1)); // Index < 0
        }

        [Fact]
        public static void GetEnumerator()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();

            IEnumerator enumerator = collection.GetEnumerator();
            // Calling current should throw when the enumerator has not started enumerating
            Assert.Throws<InvalidOperationException>(() => enumerator.Current);

            int counter = 0;
            while (enumerator.MoveNext())
            {
                Foo current = (Foo)enumerator.Current;

                Assert.Equal(counter, current.IntValue);
                Assert.Equal(counter.ToString(), current.StringValue);
                counter++;
            }

            Assert.Equal(collection.Count, counter);

            // Calling current should throw when the enumerator has finished enumerating
            Assert.Throws<InvalidOperationException>(() => (Foo)enumerator.Current);

            // Calling current should throw when the enumerator is reset
            enumerator.Reset();
            Assert.Throws<InvalidOperationException>(() => (Foo)enumerator.Current);
        }

        [Fact]
        public static void IsSynchronized()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();
            Assert.False(((ICollection)collection).IsSynchronized);
        }

        [Fact]
        public static void IListMethods()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();

            for (int i = 0; i < 100; i++)
            {
                Assert.Equal(i, collection[i].IntValue);
                Assert.Equal(i.ToString(), collection[i].StringValue);

                Assert.Equal(i, collection.IndexOf(new Foo(i, i.ToString())));
                Assert.True(collection.Contains(new Foo(i, i.ToString())));
            }
        }

        [Fact]
        public static void IListProperties()
        {
            MyReadOnlyCollectionBase collection = CreateCollection();
            Assert.True(collection.IsFixedSize);
            Assert.True(collection.IsReadOnly);
        }

        [Fact]
        public static void VirtualMethods()
        {
            VirtualTestReadOnlyCollection collectionBase = new VirtualTestReadOnlyCollection();
            Assert.Equal(collectionBase.Count, int.MinValue);
            Assert.Null(collectionBase.GetEnumerator());
        }

        // ReadOnlyCollectionBase is provided to be used as the base class for strongly typed collections.
        // Let's use one of our own here for the type Foo.
        private class MyReadOnlyCollectionBase : ReadOnlyCollectionBase
        {
            public MyReadOnlyCollectionBase(Foo[] values)
            {
                InnerList.AddRange(values);
            }

            public Foo this[int indx]
            {
                get { return (Foo)InnerList[indx]; }
            }

            public void CopyTo(Array array, int index) => ((ICollection)this).CopyTo(array, index);

            public virtual object SyncRoot
            {
                get { return ((ICollection)this).SyncRoot; }
            }

            public int IndexOf(Foo f) => ((IList)InnerList).IndexOf(f);

            public bool Contains(Foo f) => ((IList)InnerList).Contains(f);

            public bool IsFixedSize
            {
                get { return true; }
            }

            public bool IsReadOnly
            {
                get { return true; }
            }
        }

        private class VirtualTestReadOnlyCollection : ReadOnlyCollectionBase
        {
            public override int Count
            {
                get { return int.MinValue; }
            }

            public override IEnumerator GetEnumerator() => null;
        }

        private class Foo
        {
            public Foo()
            {
            }

            public Foo(int intValue, string stringValue)
            {
                IntValue = intValue;
                StringValue = stringValue;
            }
            
            public int IntValue { get; set; }
            public string StringValue { get; set; }

            public override bool Equals(object obj)
            {
                Foo foo = obj as Foo;
                if (obj == null)
                    return false;
                return foo.IntValue == IntValue && foo.StringValue == StringValue;
            }

            public override int GetHashCode() => IntValue;
        }
    }
}