|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using nanoFramework.TestFramework; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | + |
| 8 | +namespace GenericCollections |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class ArrayGenericInterfaceTests |
| 12 | + { |
| 13 | + [TestMethod] |
| 14 | + public void Array_IsInstance_OfGenericCollectionInterfaces() |
| 15 | + { |
| 16 | + object ints = new int[1]; |
| 17 | + |
| 18 | + Assert.IsTrue(ints is IEnumerable<int>); |
| 19 | + Assert.IsTrue(ints is ICollection<int>); |
| 20 | + Assert.IsTrue(ints is IList<int>); |
| 21 | + Assert.IsTrue(ints is IReadOnlyCollection<int>); |
| 22 | + Assert.IsTrue(ints is IReadOnlyList<int>); |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void Array_IsNotInstance_OfOtherElementType() |
| 27 | + { |
| 28 | + object ints = new int[1]; |
| 29 | + |
| 30 | + Assert.IsFalse(ints is IList<uint>); |
| 31 | + Assert.IsFalse(ints is IList<string>); |
| 32 | + Assert.IsFalse(ints is IList<object>); |
| 33 | + Assert.IsFalse(ints is IReadOnlyCollection<string>); |
| 34 | + Assert.IsFalse(ints is IReadOnlyList<object>); |
| 35 | + Assert.IsNull(ints as IList<string>); |
| 36 | + Assert.IsNotNull(ints as IList<int>); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void ReferenceArray_IsCovariant() |
| 41 | + { |
| 42 | + object strings = new string[1]; |
| 43 | + |
| 44 | + Assert.IsTrue(strings is IList<string>); |
| 45 | + Assert.IsTrue(strings is IList<object>); |
| 46 | + Assert.IsTrue(strings is IReadOnlyList<object>); |
| 47 | + Assert.IsFalse(strings is IList<Exception>); |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void Array_CastToWrongElementType_Throws() |
| 52 | + { |
| 53 | + object ints = new int[1]; |
| 54 | + |
| 55 | + Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IList<string>)ints; }); |
| 56 | + Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IReadOnlyList<object>)ints; }); |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void IList_OverArray_ReadWrite() |
| 61 | + { |
| 62 | + int[] array = new int[] { 1, 2, 3 }; |
| 63 | + IList<int> list = (IList<int>)(object)array; |
| 64 | + |
| 65 | + Assert.AreEqual(3, list.Count); |
| 66 | + Assert.AreEqual(2, list[1]); |
| 67 | + Assert.IsTrue(list.Contains(3)); |
| 68 | + Assert.IsFalse(list.Contains(9)); |
| 69 | + Assert.AreEqual(2, list.IndexOf(3)); |
| 70 | + Assert.IsTrue(list.IsReadOnly); |
| 71 | + |
| 72 | + list[0] = 10; |
| 73 | + Assert.AreEqual(10, array[0]); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public void IList_OverArray_StructuralChangesThrow() |
| 78 | + { |
| 79 | + int[] array = new int[] { 1, 2 }; |
| 80 | + IList<int> list = (IList<int>)(object)array; |
| 81 | + |
| 82 | + Assert.ThrowsException(typeof(NotSupportedException), () => { list.Add(3); }); |
| 83 | + Assert.ThrowsException(typeof(NotSupportedException), () => { list.Insert(0, 3); }); |
| 84 | + Assert.ThrowsException(typeof(NotSupportedException), () => { list.Remove(1); }); |
| 85 | + Assert.ThrowsException(typeof(NotSupportedException), () => { list.RemoveAt(0); }); |
| 86 | + Assert.ThrowsException(typeof(NotSupportedException), () => { list.Clear(); }); |
| 87 | + |
| 88 | + Assert.AreEqual(2, array.Length); |
| 89 | + Assert.AreEqual(1, array[0]); |
| 90 | + Assert.AreEqual(2, array[1]); |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void IReadOnlyList_OverArray() |
| 95 | + { |
| 96 | + IReadOnlyList<string> list = (IReadOnlyList<string>)(object)new string[] { "a", "b" }; |
| 97 | + |
| 98 | + Assert.AreEqual(2, list.Count); |
| 99 | + Assert.AreEqual("b", list[1]); |
| 100 | + } |
| 101 | + |
| 102 | + [TestMethod] |
| 103 | + public void IList_OverStringArray_AsObject() |
| 104 | + { |
| 105 | + IList<object> list = (IList<object>)(object)new string[] { "a", "b" }; |
| 106 | + |
| 107 | + Assert.AreEqual(2, list.Count); |
| 108 | + Assert.AreEqual("a", list[0]); |
| 109 | + } |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + public void List_Constructor_FromArrays() |
| 113 | + { |
| 114 | + var ints = new List<int>(new int[] { 1, 2, 3 }); |
| 115 | + Assert.AreEqual(3, ints.Count); |
| 116 | + Assert.AreEqual(3, ints[2]); |
| 117 | + |
| 118 | + var strings = new List<string>(new string[] { "a", "b" }); |
| 119 | + Assert.AreEqual(2, strings.Count); |
| 120 | + Assert.AreEqual("b", strings[1]); |
| 121 | + |
| 122 | + var objects = new List<object>(new string[] { "a", "b" }); |
| 123 | + Assert.AreEqual(2, objects.Count); |
| 124 | + Assert.AreEqual("a", objects[0]); |
| 125 | + } |
| 126 | + |
| 127 | + [TestMethod] |
| 128 | + public void List_Constructor_FromEmptyArray() |
| 129 | + { |
| 130 | + var list = new List<int>(new int[0]); |
| 131 | + |
| 132 | + Assert.AreEqual(0, list.Count); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments