Skip to content

Commit 85086ea

Browse files
authored
Update core library to add support for generics <T> (#242)
1 parent cca059e commit 85086ea

File tree

13 files changed

+205
-114
lines changed

13 files changed

+205
-114
lines changed

.runsettings

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<RunSettings>
33
<!-- Configurations that affect the Test Framework -->
44
<RunConfiguration>
@@ -12,5 +12,6 @@
1212
<Logging>Verbose</Logging>
1313
<IsRealHardware>False</IsRealHardware>
1414
<RunnerExtraArguments> --forcegc </RunnerExtraArguments>
15+
<UsePreviewClr>True</UsePreviewClr>
1516
</nanoFrameworkAdapter>
16-
</RunSettings>
17+
</RunSettings>

Tests/NFUnitTestGC/TestGC.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44

5+
using System;
56
using nanoFramework.TestFramework;
67

78
namespace NFUnitTestGC
@@ -15,16 +16,14 @@ public void TestGCStress()
1516
int maxArraySize = 1024 * 32;
1617
object[] arrays = new object[600];
1718

18-
// Starting TestGCStress
19-
2019
for (int loop = 0; loop < 100; loop++)
2120
{
2221
OutputHelper.WriteLine($"Running iteration {loop}");
2322

2423
for (int i = 0; i < arrays.Length - 1;)
2524
{
2625
OutputHelper.WriteLine($"Alloc array of {maxArraySize} bytes @ pos {i}");
27-
arrays[i++] = new byte[maxArraySize]; ;
26+
arrays[i++] = new byte[maxArraySize];
2827

2928
OutputHelper.WriteLine($"Alloc array of 64 bytes @ pos {i}");
3029
arrays[i++] = new byte[64];
@@ -37,8 +36,35 @@ public void TestGCStress()
3736
arrays[i] = null;
3837
}
3938
}
39+
}
40+
41+
[TestMethod]
42+
public void TestGetTotalMemory()
43+
{
44+
// create several objects
45+
object[] objects = new object[100];
46+
47+
for (int i = 0; i < objects.Length; i++)
48+
{
49+
objects[i] = new object();
50+
}
51+
52+
// get total memory
53+
long totalMemory = GC.GetTotalMemory(false);
54+
OutputHelper.WriteLine($"Total memory: {totalMemory} bytes");
55+
56+
// release objects
57+
for (int i = 0; i < objects.Length; i++)
58+
{
59+
objects[i] = null;
60+
}
61+
62+
// get total memory, forcing full collection
63+
long totalMemoryAfterCollection = GC.GetTotalMemory(true);
64+
OutputHelper.WriteLine($"Total memory: {totalMemoryAfterCollection} bytes");
4065

41-
// Completed TestGCStress
66+
// check if memory was released
67+
Assert.IsTrue(totalMemory > totalMemoryAfterCollection, "Memory was not released");
4268
}
4369
}
4470
}

Tests/NFUnitTestSystemLib/UnitTestGCTest.cs

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ namespace NFUnitTestSystemLib
99
[TestClass]
1010
public class UnitTestGCTest
1111
{
12+
#pragma warning disable S1215 // this is intended to test the GC
13+
#pragma warning disable S1854 // this is intended to test the GC
14+
#pragma warning disable S2696 // this is intended to test the GC
15+
#pragma warning disable S3971 // this is intended to test the GC
16+
1217
internal class FinalizeObject
1318
{
1419
public static FinalizeObject m_currentInstance = null;
@@ -54,17 +59,20 @@ public void SystemGC1_Test()
5459
/// 6. Verify that object has been collected
5560
/// </summary>
5661
///
57-
// Tests ReRegisterForFinalize
58-
// Create a FinalizeObject.
62+
63+
OutputHelper.WriteLine("Tests ReRegisterForFinalize");
64+
OutputHelper.WriteLine("Create a FinalizeObject.");
65+
5966
FinalizeObject mfo = new FinalizeObject();
6067
m_hasFinalized1 = false;
6168
m_hasFinalized2 = false;
6269

6370
// Release reference
71+
OutputHelper.WriteLine("Release reference");
6472
mfo = null;
6573

66-
// Allow GC
67-
GC.WaitForPendingFinalizers();
74+
OutputHelper.WriteLine("Allow GC");
75+
GC.Collect();
6876

6977
int sleepTime = 1000;
7078
int slept = 0;
@@ -85,10 +93,10 @@ public void SystemGC1_Test()
8593
// FinalizeObject.m_currentInstance field. Setting this value
8694
// to null and forcing another garbage collection will now
8795
// cause the object to Finalize permanently.
88-
// Reregister and allow for GC
89-
FinalizeObject.m_currentInstance = null;
9096

91-
GC.WaitForPendingFinalizers();
97+
OutputHelper.WriteLine("Reregister and allow for GC");
98+
FinalizeObject.m_currentInstance = null;
99+
GC.Collect();
92100

93101
sleepTime = 1000;
94102
slept = 0;
@@ -119,26 +127,27 @@ public void SystemGC2_Test()
119127
/// 6. Verify that object has not been collected
120128
/// </summary>
121129
///
122-
// Tests SuppressFinalize
123-
// Create a FinalizeObject.
130+
131+
OutputHelper.WriteLine("Tests SuppressFinalize");
132+
OutputHelper.WriteLine("Create a FinalizeObject");
124133
FinalizeObject mfo = new FinalizeObject();
125134
m_hasFinalized1 = false;
126135
m_hasFinalized2 = false;
127136

128-
// Releasing
137+
OutputHelper.WriteLine("Releasing");
129138
GC.SuppressFinalize(mfo);
130139
mfo = null;
131140

132-
// Allow GC
133-
GC.WaitForPendingFinalizers();
141+
OutputHelper.WriteLine("Allow GC");
142+
GC.Collect();
134143

135144
int sleepTime = 1000;
136145
int slept = 0;
137146

138147
while (!m_hasFinalized1 && slept < sleepTime)
139148
{
140149
// force GC run caused by memory allocation
141-
var dummyArray = new byte[1024 * 1024 * 1];
150+
_ = new byte[1024 * 1024 * 1];
142151

143152
System.Threading.Thread.Sleep(10);
144153
slept += 10;
@@ -161,59 +170,35 @@ public void SystemGC3_Test()
161170
/// </summary>
162171
///
163172

164-
// Tests WaitForPendingFinalizers, dependant on test 1
165-
// will auto-fail if test 1 fails.
166173
OutputHelper.Write("Tests WaitForPendingFinalizers, dependant on test 1");
167-
OutputHelper.WriteLine("will auto-fail if test 1 fails.");
174+
OutputHelper.WriteLine("will fail if test 1 fails.");
168175

169-
Assert.IsTrue(m_Test1Result);
176+
Assert.IsTrue(m_Test1Result, "Can't run this test as SystemGC1_Test has failed.");
170177

171-
// Create a FinalizeObject.
178+
OutputHelper.WriteLine("Create a FinalizeObject");
172179
FinalizeObject mfo = new FinalizeObject();
173180
m_hasFinalized1 = false;
174181
m_hasFinalized2 = false;
175182

176-
// Releasing
183+
OutputHelper.WriteLine("Releasing");
177184
mfo = null;
178185

179-
int sleepTime = 1000;
180-
int slept = 0;
181-
182-
while (!m_hasFinalized1 && slept < sleepTime)
183-
{
184-
// force GC run caused by memory allocation
185-
var dummyArray = new byte[1024 * 1024 * 1];
186-
187-
System.Threading.Thread.Sleep(10);
188-
slept += 10;
189-
}
190-
191-
OutputHelper.WriteLine($"GC took {slept}");
192-
193-
// Wait for GC
186+
OutputHelper.WriteLine("Wait for GC");
187+
GC.Collect();
194188
GC.WaitForPendingFinalizers();
195189

196-
// Releasing again
190+
OutputHelper.WriteLine("Releasing again");
197191
FinalizeObject.m_currentInstance = null;
198192

199-
sleepTime = 1000;
200-
slept = 0;
201-
202-
while (!m_hasFinalized2 && slept < sleepTime)
203-
{
204-
// force GC run caused by memory allocation
205-
var dummyArray = new byte[1024 * 1024 * 1];
206-
207-
System.Threading.Thread.Sleep(10);
208-
slept += 10;
209-
}
210-
211-
OutputHelper.WriteLine($"GC took {slept}");
212-
213-
// Wait for GC
193+
OutputHelper.WriteLine("Wait for GC");
194+
GC.Collect();
214195
GC.WaitForPendingFinalizers();
215196

216197
Assert.IsTrue(m_hasFinalized2);
217198
}
218199
}
200+
#pragma warning restore S1215 // "GC.Collect" should not be called
201+
#pragma warning restore S1854 // Unused assignments should be removed
202+
#pragma warning restore S2696 // Instance members should not write to "static" fields
203+
#pragma warning restore S3971 // "GC.SuppressFinalize" should not be called
219204
}

Tests/NFUnitTestSystemLib/UnitTestInitLocalTests.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public void SystemType1_GetType_Test()
4949
// ConstructorInfo)
5050
// NOTE: We add the reflection items to the ArrayList to assure that they can be properly
5151
// assigned to a object container (this used to lead to a access violation)
52+
53+
OutputHelper.WriteLine("Testing Int32");
54+
5255
Type type = typeof(int);
5356
list.Add(type);
5457
string name = ((Type)list[i]).Name;
@@ -68,33 +71,43 @@ public void SystemType1_GetType_Test()
6871
//fRes &= name.ToLower() == "mscorlib";
6972
//i++;
7073

74+
OutputHelper.WriteLine("Testing NFUnitTestSystemLib.UnitTestInitLocalTests+TestObj");
75+
7176
type = Type.GetType("NFUnitTestSystemLib.UnitTestInitLocalTests+TestObj");
7277
list.Add(type);
7378
name = ((Type)list[i]).Name;
7479
Assert.AreEqual(name, "TestObj");
7580
i++;
7681

82+
OutputHelper.WriteLine("Testing IEmptyInterface");
83+
7784
Type iface = type.GetInterfaces()[0];
7885
list.Add(iface);
7986
name = ((Type)list[i]).Name;
8087
Assert.AreEqual(name, "IEmptyInterface");
8188
Assert.AreEqual(iface.Name, "IEmptyInterface");
8289
i++;
8390

91+
OutputHelper.WriteLine("Testing FieldInfo");
92+
8493
FieldInfo fi = type.GetField("Field1");
8594
list.Add(fi);
8695
name = ((FieldInfo)list[i]).Name;
8796
Assert.AreEqual(name, "Field1");
8897
Assert.AreEqual(fi.Name, "Field1");
8998
i++;
9099

100+
OutputHelper.WriteLine("Testing MethodInfo");
101+
91102
MethodInfo mi = type.GetMethod("Method1");
92103
list.Add(mi);
93104
name = ((MethodInfo)list[i]).Name;
94105
Assert.AreEqual(name, "Method1");
95106
Assert.AreEqual(mi.Name, "Method1");
96107
i++;
97108

109+
OutputHelper.WriteLine("Testing ConstructorInfo");
110+
98111
ConstructorInfo ci = type.GetConstructor(new Type[] { });
99112
list.Add(ci);
100113
name = ((ConstructorInfo)list[i]).Name;
@@ -104,7 +117,10 @@ public void SystemType1_GetType_Test()
104117

105118
//
106119
// Now test arrays of reflection types
107-
//
120+
//
121+
122+
OutputHelper.WriteLine("Testing Array of Type");
123+
108124
Type[] types = new Type[] { typeof(int), typeof(bool), Type.GetType("NFUnitTestSystemLib.UnitTestInitLocalTests+TestObj") };
109125
list.Add(types[2]);
110126
name = ((Type)list[i]).Name;
@@ -127,27 +143,35 @@ public void SystemType1_GetType_Test()
127143
//fRes &= asms[0].GetName().Name == "Microsoft.SPOT.Platform.Tests.Systemlib2";
128144
//i++;
129145

146+
OutputHelper.WriteLine("Testing Array of FieldInfo");
147+
130148
FieldInfo[] fis = new FieldInfo[] { types[2].GetField("Field1"), type.GetFields()[0] };
131149
list.Add(fis[0]);
132150
name = ((FieldInfo)list[i]).Name;
133151
Assert.AreEqual(name, "Field1");
134152
Assert.AreEqual(fis[0].Name, "Field1");
135153
i++;
136154

155+
OutputHelper.WriteLine("Testing Array of MethodInfo");
156+
137157
MethodInfo[] mis = new MethodInfo[] { type.GetMethods()[2], types[2].GetMethod("Method2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) };
138158
list.Add(mis[1]);
139159
name = ((MethodInfo)list[i]).Name;
140160
Assert.AreEqual(name, "Method2");
141161
Assert.AreEqual(mis[1].Name, "Method2");
142162
i++;
143163

164+
OutputHelper.WriteLine("Testing Array of ConstructorInfo");
165+
144166
ConstructorInfo[] cis = new ConstructorInfo[] { types[2].GetConstructor(new Type[] { }), typeof(TestObj).GetConstructor(new Type[] { typeof(int) }) };
145167
list.Add(cis[0]);
146168
name = ((ConstructorInfo)list[i]).Name;
147169
Assert.AreEqual(name, ".ctor");
148170
Assert.AreEqual(cis[0].Name, ".ctor");
149171
i++;
150172

173+
OutputHelper.WriteLine("Testing Array of System.Collections.ArrayList");
174+
151175
Array ar = Array.CreateInstance(typeof(Type), 3);
152176
((IList)ar)[0] = typeof(Type);
153177
((IList)ar)[1] = Type.GetType("System.Collections.ArrayList");
@@ -157,7 +181,6 @@ public void SystemType1_GetType_Test()
157181
Assert.AreEqual(name, "ArrayList");
158182
Assert.AreEqual(((Type)((IList)ar)[0]).Name, "Type");
159183
Assert.AreEqual(((Type)ar.GetValue(1)).Name, "ArrayList");
160-
i++;
161184

162185
list.Clear();
163186
}

0 commit comments

Comments
 (0)