Skip to content

Commit c345ce6

Browse files
committed
Add new generic collections unit tests
1 parent ca3606b commit c345ce6

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Tests/GenericCollections/ListTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,32 @@ public void List_GenericMethod_NewObj_InsertAndIterate()
12031203
Assert.AreEqual(4, result[3]);
12041204
}
12051205

1206+
[TestMethod]
1207+
public void List_StaticField_ValueType_Add()
1208+
{
1209+
// Reproduces nanoframework/Home#1821: a List<T> held in a static readonly
1210+
// field initialized via a field initializer (i.e. constructed from the
1211+
// declaring type's own .cctor), then Add()'d to from another method.
1212+
StaticListHolder.Numbers.Add(1);
1213+
StaticListHolder.Numbers.Add(2);
1214+
1215+
Assert.AreEqual(2, StaticListHolder.Numbers.Count);
1216+
Assert.AreEqual(1, StaticListHolder.Numbers[0]);
1217+
Assert.AreEqual(2, StaticListHolder.Numbers[1]);
1218+
}
1219+
1220+
[TestMethod]
1221+
public void List_StaticField_ReferenceType_Add()
1222+
{
1223+
// Reproduces nanoframework/Home#1821 with a reference-type element,
1224+
// mirroring the reported List<Ds18b20> Sensors static field.
1225+
StaticListHolder.Items.Add(new DummyClass(1, "One"));
1226+
1227+
Assert.AreEqual(1, StaticListHolder.Items.Count);
1228+
Assert.AreEqual(1, StaticListHolder.Items[0].Id);
1229+
Assert.AreEqual("One", StaticListHolder.Items[0].Name);
1230+
}
1231+
12061232
// Generic helper methods that exercise NEWOBJ with MVAR TypeSpecs.
12071233
// Each method creates a new List<T> from within a generic method body,
12081234
// producing IL with MethodRef tokens whose owner TypeSpec is List<!!0>.
@@ -1316,6 +1342,14 @@ public DummyClass(int id, string name)
13161342
}
13171343
}
13181344

1345+
// Field initializers below run as part of this type's own .cctor, matching the
1346+
// pattern reported in nanoframework/Home#1821 (`private static readonly List<T> ... = new List<T>();`).
1347+
internal static class StaticListHolder
1348+
{
1349+
internal static readonly List<int> Numbers = new List<int>();
1350+
internal static readonly List<DummyClass> Items = new List<DummyClass>();
1351+
}
1352+
13191353
internal struct DummyStruct
13201354
{
13211355
public int Id { get; set; }

0 commit comments

Comments
 (0)