From c345ce6647861c327028ce2c9a191ce425c8f7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 23 Jul 2026 10:36:36 +0100 Subject: [PATCH 1/2] Add new generic collections unit tests --- Tests/GenericCollections/ListTests.cs | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Tests/GenericCollections/ListTests.cs b/Tests/GenericCollections/ListTests.cs index a217572..d611d46 100644 --- a/Tests/GenericCollections/ListTests.cs +++ b/Tests/GenericCollections/ListTests.cs @@ -1203,6 +1203,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 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 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); + } + // Generic helper methods that exercise NEWOBJ with MVAR TypeSpecs. // Each method creates a new List from within a generic method body, // producing IL with MethodRef tokens whose owner TypeSpec is List. @@ -1316,6 +1342,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 ... = new List();`). + internal static class StaticListHolder + { + internal static readonly List Numbers = new List(); + internal static readonly List Items = new List(); + } + internal struct DummyStruct { public int Id { get; set; } From a4601027b43daded599faf604af0f3ed46d90a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 23 Jul 2026 11:03:47 +0100 Subject: [PATCH 2/2] Fixes from code review --- Tests/GenericCollections/ListTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/GenericCollections/ListTests.cs b/Tests/GenericCollections/ListTests.cs index d611d46..7dd1061 100644 --- a/Tests/GenericCollections/ListTests.cs +++ b/Tests/GenericCollections/ListTests.cs @@ -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() {