Skip to content
Merged
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
44 changes: 44 additions & 0 deletions Tests/GenericCollections/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ namespace GenericCollections
[TestClass]
public class ListTests
{
[Cleanup]
public void CleanupTests()
{
// StaticListHolder is shared static state (see nanoframework/Home#1821
// repro below); clear it so List_StaticField_* tests start empty on
// every run/retry, even if a prior test's assertions failed.
StaticListHolder.Numbers.Clear();
StaticListHolder.Items.Clear();
}

[TestMethod]
public void List_Constructor_Default()
{
Expand Down Expand Up @@ -1203,6 +1213,32 @@ public void List_GenericMethod_NewObj_InsertAndIterate()
Assert.AreEqual(4, result[3]);
}

[TestMethod]
public void List_StaticField_ValueType_Add()
{
// Reproduces nanoframework/Home#1821: a List<T> held in a static readonly
// field initialized via a field initializer (i.e. constructed from the
// declaring type's own .cctor), then Add()'d to from another method.
StaticListHolder.Numbers.Add(1);
StaticListHolder.Numbers.Add(2);

Assert.AreEqual(2, StaticListHolder.Numbers.Count);
Assert.AreEqual(1, StaticListHolder.Numbers[0]);
Assert.AreEqual(2, StaticListHolder.Numbers[1]);
}

[TestMethod]
public void List_StaticField_ReferenceType_Add()
{
// Reproduces nanoframework/Home#1821 with a reference-type element,
// mirroring the reported List<Ds18b20> Sensors static field.
StaticListHolder.Items.Add(new DummyClass(1, "One"));

Assert.AreEqual(1, StaticListHolder.Items.Count);
Assert.AreEqual(1, StaticListHolder.Items[0].Id);
Assert.AreEqual("One", StaticListHolder.Items[0].Name);
}

Comment thread
coderabbitai[bot] marked this conversation as resolved.
// Generic helper methods that exercise NEWOBJ with MVAR TypeSpecs.
// Each method creates a new List<T> from within a generic method body,
// producing IL with MethodRef tokens whose owner TypeSpec is List<!!0>.
Expand Down Expand Up @@ -1316,6 +1352,14 @@ public DummyClass(int id, string name)
}
}

// Field initializers below run as part of this type's own .cctor, matching the
// pattern reported in nanoframework/Home#1821 (`private static readonly List<T> ... = new List<T>();`).
internal static class StaticListHolder
{
internal static readonly List<int> Numbers = new List<int>();
internal static readonly List<DummyClass> Items = new List<DummyClass>();
}

internal struct DummyStruct
{
public int Id { get; set; }
Expand Down
Loading