Skip to content

Commit 59b7b36

Browse files
authored
Add IList tests over generic arrays (#191)
***NO_CI***
1 parent 6055cde commit 59b7b36

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 ArrayGenericElementListTests
12+
{
13+
[TestMethod]
14+
public void IList_OverGenericInstanceArray_ComparesTheArguments()
15+
{
16+
object array = new ListBox<int>[2];
17+
18+
Assert.IsTrue(array is IList<ListBox<int>>);
19+
Assert.IsFalse(array is IList<ListBox<string>>);
20+
Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IList<ListBox<string>>)array; });
21+
}
22+
23+
[TestMethod]
24+
public void IList_OverGenericInstanceArray_IndexerAndIndexOf()
25+
{
26+
ListBox<int> first = new ListBox<int>();
27+
ListBox<int> second = new ListBox<int>();
28+
ListBox<int>[] array = new ListBox<int>[] { first, null };
29+
IList<ListBox<int>> list = (IList<ListBox<int>>)(object)array;
30+
31+
Assert.IsTrue(object.ReferenceEquals(list[0], first));
32+
Assert.AreEqual(0, list.IndexOf(first));
33+
Assert.AreEqual(-1, list.IndexOf(second));
34+
35+
list[1] = second;
36+
Assert.IsTrue(object.ReferenceEquals(array[1], second));
37+
Assert.AreEqual(1, list.IndexOf(second));
38+
}
39+
40+
[TestMethod]
41+
public void IList_OverGenericInstanceArray_RejectsAnotherInstantiationOnStore()
42+
{
43+
object[] array = new ListStoreBox<int>[1];
44+
45+
Assert.ThrowsException(typeof(Exception), () => { array[0] = new ListStoreBox<string>(); });
46+
}
47+
48+
[TestMethod]
49+
public void IList_OverGenericInstanceArray_StructuralChangesThrow()
50+
{
51+
IList<ListBox<int>> list = (IList<ListBox<int>>)(object)new ListBox<int>[1];
52+
53+
Assert.ThrowsException(typeof(NotSupportedException), () => { list.Insert(0, new ListBox<int>()); });
54+
Assert.ThrowsException(typeof(NotSupportedException), () => { list.RemoveAt(0); });
55+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { _ = list[1]; });
56+
}
57+
58+
[TestMethod]
59+
public void IList_OverJaggedArray_IndexerAndIndexOf()
60+
{
61+
int[] row = new int[] { 1, 2 };
62+
int[][] array = new int[][] { new int[1], row };
63+
IList<int[]> list = (IList<int[]>)(object)array;
64+
65+
Assert.IsTrue(object.ReferenceEquals(list[1], row));
66+
Assert.AreEqual(1, list.IndexOf(row));
67+
Assert.AreEqual(-1, list.IndexOf(new int[1]));
68+
Assert.IsTrue(list.Contains(row));
69+
Assert.IsFalse((object)array is IList<string[]>);
70+
}
71+
72+
[TestMethod]
73+
public void IList_OverJaggedGenericInstanceArray_ComparesTheArguments()
74+
{
75+
ListJaggedBox<int>[][] array = new ListJaggedBox<int>[2][];
76+
array[1] = new ListJaggedBox<int>[1];
77+
IList<ListJaggedBox<int>[]> list = (IList<ListJaggedBox<int>[]>)(object)array;
78+
79+
Assert.IsTrue(object.ReferenceEquals(list[1], array[1]));
80+
Assert.AreEqual(1, list.IndexOf(array[1]));
81+
Assert.IsFalse((object)array is IList<ListJaggedBox<string>[]>);
82+
}
83+
84+
[TestMethod]
85+
public void IList_OverStructArray_ReadWrite()
86+
{
87+
ListStruct<int>[] array = new ListStruct<int>[2];
88+
IList<ListStruct<int>> list = (IList<ListStruct<int>>)(object)array;
89+
90+
Assert.AreEqual(0, list[0].Value);
91+
92+
ListStruct<int> element = new ListStruct<int>();
93+
element.Value = 5;
94+
list[1] = element;
95+
96+
Assert.AreEqual(5, array[1].Value);
97+
Assert.IsFalse((object)array is IList<ListStruct<string>>);
98+
}
99+
100+
[TestMethod]
101+
public void IList_OverArrayCreatedInGenericCode()
102+
{
103+
object plain = new ListHolder<ListGenericBox<int>>().Make();
104+
object jagged = new ListHolder<ListGenericBox<int>[]>().Make();
105+
106+
Assert.IsTrue(plain is IList<ListGenericBox<int>>);
107+
Assert.IsFalse(plain is IList<ListGenericBox<string>>);
108+
Assert.IsTrue(jagged is IList<ListGenericBox<int>[]>);
109+
Assert.IsFalse(jagged is IList<ListGenericBox<string>[]>);
110+
}
111+
}
112+
113+
public class ListBox<T> { }
114+
public class ListStoreBox<T> { }
115+
public class ListJaggedBox<T> { }
116+
public class ListGenericBox<T> { }
117+
public struct ListStruct<T> { public int Value; }
118+
public class ListHolder<T> { public T[] Make() { return new T[2]; } }
119+
}

‎Tests/GenericCollections/GenericCollections.nfproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
</PropertyGroup>
2727
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2828
<ItemGroup>
29+
<Compile Include="ArrayGenericElementListTests.cs" />
2930
<Compile Include="ArrayGenericInterfaceTests.cs" />
3031
<Compile Include="CollectionTestBase.cs" />
3132
<Compile Include="DictionaryTests.cs" />

0 commit comments

Comments
 (0)