Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions Tests/GenericCollections/ArrayGenericInterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using nanoFramework.TestFramework;
using System;
using System.Collections.Generic;

namespace GenericCollections
{
[TestClass]
public class ArrayGenericInterfaceTests
{
[TestMethod]
public void Array_IsInstance_OfGenericCollectionInterfaces()
{
object ints = new int[1];

Assert.IsTrue(ints is IEnumerable<int>);
Assert.IsTrue(ints is ICollection<int>);
Assert.IsTrue(ints is IList<int>);
Assert.IsTrue(ints is IReadOnlyCollection<int>);
Assert.IsTrue(ints is IReadOnlyList<int>);
}

[TestMethod]
public void Array_IsNotInstance_OfOtherElementType()
{
object ints = new int[1];

Assert.IsFalse(ints is IList<uint>);
Assert.IsFalse(ints is IList<string>);
Assert.IsFalse(ints is IList<object>);
Assert.IsFalse(ints is IReadOnlyCollection<string>);
Assert.IsFalse(ints is IReadOnlyList<object>);
Assert.IsNull(ints as IList<string>);
Assert.IsNotNull(ints as IList<int>);
}

[TestMethod]
public void ReferenceArray_IsCovariant()
{
object strings = new string[1];

Assert.IsTrue(strings is IList<string>);
Assert.IsTrue(strings is IList<object>);
Assert.IsTrue(strings is IReadOnlyList<object>);
Assert.IsFalse(strings is IList<Exception>);
}

[TestMethod]
public void Array_CastToWrongElementType_Throws()
{
object ints = new int[1];

Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IList<string>)ints; });
Assert.ThrowsException(typeof(InvalidCastException), () => { _ = (IReadOnlyList<object>)ints; });
}

[TestMethod]
public void IList_OverArray_ReadWrite()
{
int[] array = new int[] { 1, 2, 3 };
IList<int> list = (IList<int>)(object)array;

Assert.AreEqual(3, list.Count);
Assert.AreEqual(2, list[1]);
Assert.IsTrue(list.Contains(3));
Assert.IsFalse(list.Contains(9));
Assert.AreEqual(2, list.IndexOf(3));
Assert.IsTrue(list.IsReadOnly);

list[0] = 10;
Assert.AreEqual(10, array[0]);
}

[TestMethod]
public void IList_OverArray_StructuralChangesThrow()
{
int[] array = new int[] { 1, 2 };
IList<int> list = (IList<int>)(object)array;

Assert.ThrowsException(typeof(NotSupportedException), () => { list.Add(3); });
Assert.ThrowsException(typeof(NotSupportedException), () => { list.Insert(0, 3); });
Assert.ThrowsException(typeof(NotSupportedException), () => { list.Remove(1); });
Assert.ThrowsException(typeof(NotSupportedException), () => { list.RemoveAt(0); });
Assert.ThrowsException(typeof(NotSupportedException), () => { list.Clear(); });

Assert.AreEqual(2, array.Length);
Assert.AreEqual(1, array[0]);
Assert.AreEqual(2, array[1]);
}

[TestMethod]
public void IReadOnlyList_OverArray()
{
IReadOnlyList<string> list = (IReadOnlyList<string>)(object)new string[] { "a", "b" };

Assert.AreEqual(2, list.Count);
Assert.AreEqual("b", list[1]);
}

[TestMethod]
public void IList_OverStringArray_AsObject()
{
IList<object> list = (IList<object>)(object)new string[] { "a", "b" };

Assert.AreEqual(2, list.Count);
Assert.AreEqual("a", list[0]);
}

[TestMethod]
public void List_Constructor_FromArrays()
{
var ints = new List<int>(new int[] { 1, 2, 3 });
Assert.AreEqual(3, ints.Count);
Assert.AreEqual(3, ints[2]);

var strings = new List<string>(new string[] { "a", "b" });
Assert.AreEqual(2, strings.Count);
Assert.AreEqual("b", strings[1]);

var objects = new List<object>(new string[] { "a", "b" });
Assert.AreEqual(2, objects.Count);
Assert.AreEqual("a", objects[0]);
}

[TestMethod]
public void List_Constructor_FromEmptyArray()
{
var list = new List<int>(new int[0]);

Assert.AreEqual(0, list.Count);
}
}
}
18 changes: 18 additions & 0 deletions Tests/GenericCollections/DictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using nanoFramework.TestFramework;
using System;
using System.Collections;
using System.Collections.Generic;

namespace GenericCollections
Expand All @@ -22,6 +23,23 @@ public void Dictionary_Constructor_Default()
Assert.AreEqual(0, dict.Count);
}

[TestMethod]
public void Dictionary_NonGenericEnumerable_Foreach()
{
var dict = new Dictionary<string, int>();
dict.Add("a", 1);
dict.Add("b", 2);

IEnumerable enumerable = dict;
int sum = 0;
foreach (object item in enumerable)
{
sum += ((KeyValuePair<string, int>)item).Value;
}

Assert.AreEqual(3, sum);
}

[TestMethod]
public void Dictionary_Constructor_Capacity()
{
Expand Down
1 change: 1 addition & 0 deletions Tests/GenericCollections/GenericCollections.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="ArrayGenericInterfaceTests.cs" />
<Compile Include="CollectionTestBase.cs" />
<Compile Include="DictionaryTests.cs" />
<Compile Include="ListTests.cs" />
Expand Down
133 changes: 133 additions & 0 deletions Tests/GenericCollections/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,139 @@ public void List_IList_Interface()
Assert.AreEqual(0, list.Count);
}

[TestMethod]
public void List_NonGenericIEnumerable_Foreach()
{
IEnumerable ints = new List<int> { 1, 2, 3 };
int sum = 0;
foreach (object item in ints)
{
sum += (int)item;
}

Assert.AreEqual(6, sum);

IEnumerable strings = new List<string> { "A", "B" };
string joined = "";
foreach (object item in strings)
{
joined += (string)item;
}

Assert.AreEqual("AB", joined);

IEnumerable nested = new List<List<int>> { new List<int> { 1 }, new List<int> { 2, 3 } };
int count = 0;
foreach (object item in nested)
{
count += ((List<int>)item).Count;
}

Assert.AreEqual(3, count);
}

[TestMethod]
public void List_NonGenericIList_ReferenceElements()
{
IList list = new List<string>();

Assert.AreEqual(0, list.Add("a"));
Assert.AreEqual(1, list.Add("b"));
Assert.AreEqual(2, list.Count);
Assert.AreEqual("a", list[0]);

Assert.IsTrue(list.Contains("b"));
Assert.IsFalse(list.Contains("z"));
Assert.AreEqual(1, list.IndexOf("b"));

list[0] = "c";
Assert.AreEqual("c", list[0]);

list.Insert(0, "d");
Assert.AreEqual("d", list[0]);
Assert.AreEqual(3, list.Count);

list.Remove("c");
Assert.AreEqual(2, list.Count);
Assert.IsFalse(list.Contains("c"));
}

[TestMethod]
public void List_NonGenericIList_NestedGenericElements()
{
IList list = new List<List<int>>();
var first = new List<int> { 1 };
var second = new List<int> { 2 };

Assert.AreEqual(0, list.Add(first));
Assert.AreEqual(1, list.Add(second));
Assert.AreEqual(2, list.Count);
Assert.AreSame(first, list[0]);

Assert.IsTrue(list.Contains(second));
Assert.AreEqual(1, list.IndexOf(second));

list[0] = second;
Assert.AreSame(second, list[0]);

list.Insert(0, first);
Assert.AreSame(first, list[0]);
Assert.AreEqual(3, list.Count);

list.Remove(first);
Assert.AreEqual(2, list.Count);
}

[TestMethod]
public void List_NonGenericIList_WrongElementType_Throws()
{
IList ints = new List<int>();
Assert.ThrowsException(typeof(ArgumentException), () =>
{
ints.Add("x");
});

IList strings = new List<string>();
Assert.ThrowsException(typeof(ArgumentException), () =>
{
strings.Add(5);
});

Assert.AreEqual(0, ints.Count);
Assert.AreEqual(0, strings.Count);
}

[TestMethod]
public void List_NonGenericIList_Null()
{
IList strings = new List<string>();
strings.Add(null);
Assert.AreEqual(1, strings.Count);
Assert.IsNull(strings[0]);

IList ints = new List<int>();
Assert.ThrowsException(typeof(ArgumentNullException), () =>
{
ints.Insert(0, null);
});
}

[TestMethod]
public void List_Constructor_FromIEnumerableOfInt()
{
IEnumerable<int> fromList = new List<int> { 1, 2, 3 };
var copy = new List<int>(fromList);

Assert.AreEqual(3, copy.Count);
Assert.AreEqual(3, copy[2]);

IEnumerable<int> fromArray = new int[] { 4, 5 };
var copyFromArray = new List<int>(fromArray);

Assert.AreEqual(2, copyFromArray.Count);
Assert.AreEqual(5, copyFromArray[1]);
}

[TestMethod]
public void List_LargeCapacity_Growth()
{
Expand Down