Skip to content

Commit ff78458

Browse files
authored
Enable destructor and finalizer tests (#215)
***NO_CI***
1 parent c564b59 commit ff78458

File tree

3 files changed

+291
-215
lines changed

3 files changed

+291
-215
lines changed

.runsettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<nanoFrameworkAdapter>
1212
<Logging>Verbose</Logging>
1313
<IsRealHardware>False</IsRealHardware>
14+
<RunnerExtraArguments> --forcegc </RunnerExtraArguments>
1415
</nanoFrameworkAdapter>
1516
</RunSettings>

Tests/NFUnitTestClasses/UnitTestDestructorTests.cs

Lines changed: 115 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,101 +6,109 @@
66

77
using nanoFramework.TestFramework;
88
using System;
9-
using System.Diagnostics;
10-
using System.Reflection;
119

1210
namespace NFUnitTestClasses
1311
{
1412
[TestClass]
1513
class UnitTestDestructorTests
1614
{
1715
// Removing as using something out of mscorlib
18-
//[TestMethod]
19-
//public void Destructors3_Test()
20-
//{
21-
// //Ported from Destructors3.cs
22-
// // Section 10.11
23-
// // Destructors implement the actions required to
24-
// // destruct the instances of a class.
25-
// //
26-
// // Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
27-
// Assert.IsTrue(DestructorsTestClass3.testMethod());
28-
//}
29-
30-
//[TestMethod]
31-
//public void Destructors4_Test()
32-
//{
33-
// //Ported from Destructors4.cs
34-
// // Section 10.11
35-
// // Destructors implement the actions required to
36-
// // destruct the instances of a class.
37-
// //
38-
// // Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
39-
// Assert.IsTrue(DestructorsTestClass4.testMethod());
40-
//}
41-
42-
// Removed as using a class out of mscorlib
43-
//[TestMethod]
44-
//public void Destructors7_Test()
45-
//{
46-
// //Ported from Destructors7.cs
47-
// // Section 10.12
48-
// // Destructors are not inherited. Thus, a class
49-
// // has no other destructors than those that are
50-
// // actually declared in the class.
51-
// //
52-
// // Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
53-
// Assert.IsTrue(DestructorsTestClass7.testMethod());
54-
//}
55-
56-
//class DestructorsTestClass3
57-
//{
58-
59-
// static int intI = 1;
60-
61-
// ~DestructorsTestClass3()
62-
// {
63-
// // Calling Destructor for Test Class 3
64-
// intI = 2;
65-
// }
66-
67-
// public static bool testMethod()
68-
// {
69-
// DestructorsTestClass3 mc = new DestructorsTestClass3();
70-
// mc = null;
71-
// nanoFramework.Runtime.Native.GC.Run(true);
72-
// int sleepTime = 5000;
73-
// int slept = 0;
74-
// while (intI != 2 && slept < sleepTime)
75-
// {
76-
// System.Threading.Thread.Sleep(10);
77-
// slept += 10;
78-
// }
79-
// // Thread has slept for
80-
// OutputHelper.WriteLine(slept.ToString());
81-
// if (intI == 2)
82-
// {
83-
// return true;
84-
// }
85-
// else
86-
// {
87-
// return false;
88-
// }
89-
// }
90-
//}
91-
92-
93-
class DestructorsTestClass4_Base
16+
[TestMethod]
17+
public void Destructors3_Test()
18+
{
19+
//Ported from Destructors3.cs
20+
// Section 10.11
21+
// Destructors implement the actions required to
22+
// destruct the instances of a class.
23+
//
24+
// Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
25+
Assert.IsTrue(DestructorsTestClass3.TestMethod());
26+
}
27+
28+
[TestMethod]
29+
public void Destructors4_Test()
30+
{
31+
//Ported from Destructors4.cs
32+
// Section 10.11
33+
// Destructors implement the actions required to
34+
// destruct the instances of a class.
35+
//
36+
// Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
37+
Assert.IsTrue(DestructorsTestClass4.TestMethod());
38+
}
39+
40+
[TestMethod]
41+
public void Destructors7_Test()
42+
{
43+
//Ported from Destructors7.cs
44+
// Section 10.12
45+
// Destructors are not inherited. Thus, a class
46+
// has no other destructors than those that are
47+
// actually declared in the class.
48+
//
49+
// Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
50+
Assert.IsTrue(DestructorsTestClass7.TestMethod());
51+
}
52+
53+
public class DestructorsTestClass3
54+
{
55+
static int intI = 1;
56+
57+
~DestructorsTestClass3()
58+
{
59+
// Calling Destructor for Test Class 3
60+
intI = 2;
61+
62+
Console.WriteLine("Calling Destructor for Test Class 3");
63+
}
64+
65+
public static bool TestMethod()
66+
{
67+
DestructorsTestClass3 mc = new DestructorsTestClass3();
68+
mc = null;
69+
70+
// the following call has been "replaced" with the setting commanding a GC run before new allocations, which will have the desired effect of
71+
// nanoFramework.Runtime.Native.GC.Run(true);
72+
73+
int sleepTime = 5000;
74+
int slept = 0;
75+
76+
while (intI != 2 && slept < sleepTime)
77+
{
78+
// force GC run caused by memory allocation
79+
var dummyArray = new byte[1024 * 1024 * 1];
80+
81+
System.Threading.Thread.Sleep(10);
82+
slept += 10;
83+
}
84+
85+
// Thread has slept for
86+
OutputHelper.WriteLine($"Thread as slept for{slept}");
87+
88+
if (intI == 2)
89+
{
90+
return true;
91+
}
92+
else
93+
{
94+
return false;
95+
}
96+
}
97+
}
98+
99+
public class DestructorsTestClass4_Base
94100
{
95101
public static int intI = 2;
96102
~DestructorsTestClass4_Base()
97103
{
98-
intI = intI * 2;
99104
// Calling Destructor for Test Class 4 Base
105+
intI = intI * 2;
106+
107+
Console.WriteLine("Calling Destructor for Test Class 4 Base");
100108
}
101109
}
102110

103-
class DestructorsTestClass4 : DestructorsTestClass4_Base
111+
public class DestructorsTestClass4 : DestructorsTestClass4_Base
104112
{
105113

106114
~DestructorsTestClass4()
@@ -109,20 +117,29 @@ class DestructorsTestClass4 : DestructorsTestClass4_Base
109117
// Calling Destructor for Test Class 4
110118
}
111119

112-
public static bool testMethod()
120+
public static bool TestMethod()
113121
{
114122
DestructorsTestClass4 mc = new DestructorsTestClass4();
115123
mc = null;
124+
125+
// the following call has been "replaced" with the setting commanding a GC run before new allocations, which will have the desired effect of
116126
// nanoFramework.Runtime.Native.GC.Run(true);
127+
117128
int sleepTime = 5000;
118129
int slept = 0;
130+
119131
while (intI != 8 && slept < sleepTime)
120132
{
133+
// force GC run caused by memory allocation
134+
var dummyArray = new byte[1024 * 1024 * 1];
135+
121136
System.Threading.Thread.Sleep(10);
122137
slept += 10;
123138
}
139+
124140
// Thread has slept for
125-
OutputHelper.WriteLine(slept.ToString());
141+
OutputHelper.WriteLine($"Thread as slept for{slept}");
142+
126143
if (intI == 8)
127144
{
128145
return true;
@@ -134,34 +151,45 @@ public static bool testMethod()
134151
}
135152
}
136153

137-
class DestructorsTestClass7_Base
154+
public class DestructorsTestClass7_Base
138155
{
139156
public static int intI = 2;
140157
}
141158

142-
class DestructorsTestClass7 : DestructorsTestClass7_Base
159+
public class DestructorsTestClass7 : DestructorsTestClass7_Base
143160
{
144161

145162
~DestructorsTestClass7()
146163
{
147-
intI = 3;
148164
// Calling Destructor for Test Class 7
165+
intI = 3;
166+
167+
Console.WriteLine("Calling Destructor for Test Class 7");
149168
}
150169

151-
public static bool testMethod()
170+
public static bool TestMethod()
152171
{
153172
DestructorsTestClass7 mc = new DestructorsTestClass7();
154173
mc = null;
174+
175+
// the following call has been "replaced" with the setting commanding a GC run before new allocations, which will have the desired effect of
155176
//nanoFramework.Runtime.Native.GC.Run(true);
177+
156178
int sleepTime = 5000;
157179
int slept = 0;
180+
158181
while (intI != 3 && slept < sleepTime)
159182
{
183+
// force GC run caused by memory allocation
184+
var dummyArray = new byte[1024 * 1024 * 1];
185+
160186
System.Threading.Thread.Sleep(10);
161187
slept += 10;
162188
}
189+
163190
// Thread has slept for
164-
OutputHelper.WriteLine(slept.ToString());
191+
OutputHelper.WriteLine($"Thread as slept for{slept}");
192+
165193
if (intI == 3)
166194
{
167195
return true;

0 commit comments

Comments
 (0)