Skip to content

Commit

Permalink
Enable destructor and finalizer tests (#215)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Sep 20, 2024
1 parent c564b59 commit ff78458
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 215 deletions.
1 change: 1 addition & 0 deletions .runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<nanoFrameworkAdapter>
<Logging>Verbose</Logging>
<IsRealHardware>False</IsRealHardware>
<RunnerExtraArguments> --forcegc </RunnerExtraArguments>
</nanoFrameworkAdapter>
</RunSettings>
202 changes: 115 additions & 87 deletions Tests/NFUnitTestClasses/UnitTestDestructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,109 @@

using nanoFramework.TestFramework;
using System;
using System.Diagnostics;
using System.Reflection;

namespace NFUnitTestClasses
{
[TestClass]
class UnitTestDestructorTests
{
// Removing as using something out of mscorlib
//[TestMethod]
//public void Destructors3_Test()
//{
// //Ported from Destructors3.cs
// // Section 10.11
// // Destructors implement the actions required to
// // destruct the instances of a class.
// //
// // Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
// Assert.IsTrue(DestructorsTestClass3.testMethod());
//}

//[TestMethod]
//public void Destructors4_Test()
//{
// //Ported from Destructors4.cs
// // Section 10.11
// // Destructors implement the actions required to
// // destruct the instances of a class.
// //
// // Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
// Assert.IsTrue(DestructorsTestClass4.testMethod());
//}

// Removed as using a class out of mscorlib
//[TestMethod]
//public void Destructors7_Test()
//{
// //Ported from Destructors7.cs
// // Section 10.12
// // Destructors are not inherited. Thus, a class
// // has no other destructors than those that are
// // actually declared in the class.
// //
// // Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
// Assert.IsTrue(DestructorsTestClass7.testMethod());
//}

//class DestructorsTestClass3
//{

// static int intI = 1;

// ~DestructorsTestClass3()
// {
// // Calling Destructor for Test Class 3
// intI = 2;
// }

// public static bool testMethod()
// {
// DestructorsTestClass3 mc = new DestructorsTestClass3();
// mc = null;
// nanoFramework.Runtime.Native.GC.Run(true);
// int sleepTime = 5000;
// int slept = 0;
// while (intI != 2 && slept < sleepTime)
// {
// System.Threading.Thread.Sleep(10);
// slept += 10;
// }
// // Thread has slept for
// OutputHelper.WriteLine(slept.ToString());
// if (intI == 2)
// {
// return true;
// }
// else
// {
// return false;
// }
// }
//}


class DestructorsTestClass4_Base
[TestMethod]
public void Destructors3_Test()
{
//Ported from Destructors3.cs
// Section 10.11
// Destructors implement the actions required to
// destruct the instances of a class.
//
// Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
Assert.IsTrue(DestructorsTestClass3.TestMethod());
}

[TestMethod]
public void Destructors4_Test()
{
//Ported from Destructors4.cs
// Section 10.11
// Destructors implement the actions required to
// destruct the instances of a class.
//
// Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
Assert.IsTrue(DestructorsTestClass4.TestMethod());
}

[TestMethod]
public void Destructors7_Test()
{
//Ported from Destructors7.cs
// Section 10.12
// Destructors are not inherited. Thus, a class
// has no other destructors than those that are
// actually declared in the class.
//
// Note: This test may fail due to lengthy garbage collection, look for Destructor messages in later logs
Assert.IsTrue(DestructorsTestClass7.TestMethod());
}

public class DestructorsTestClass3
{
static int intI = 1;

~DestructorsTestClass3()
{
// Calling Destructor for Test Class 3
intI = 2;

Console.WriteLine("Calling Destructor for Test Class 3");
}

public static bool TestMethod()
{
DestructorsTestClass3 mc = new DestructorsTestClass3();
mc = null;

// the following call has been "replaced" with the setting commanding a GC run before new allocations, which will have the desired effect of
// nanoFramework.Runtime.Native.GC.Run(true);

int sleepTime = 5000;
int slept = 0;

while (intI != 2 && slept < sleepTime)
{
// force GC run caused by memory allocation
var dummyArray = new byte[1024 * 1024 * 1];

System.Threading.Thread.Sleep(10);
slept += 10;
}

// Thread has slept for
OutputHelper.WriteLine($"Thread as slept for{slept}");

if (intI == 2)
{
return true;
}
else
{
return false;
}
}
}

public class DestructorsTestClass4_Base
{
public static int intI = 2;
~DestructorsTestClass4_Base()
{
intI = intI * 2;
// Calling Destructor for Test Class 4 Base
intI = intI * 2;

Console.WriteLine("Calling Destructor for Test Class 4 Base");
}
}

class DestructorsTestClass4 : DestructorsTestClass4_Base
public class DestructorsTestClass4 : DestructorsTestClass4_Base
{

~DestructorsTestClass4()
Expand All @@ -109,20 +117,29 @@ class DestructorsTestClass4 : DestructorsTestClass4_Base
// Calling Destructor for Test Class 4
}

public static bool testMethod()
public static bool TestMethod()
{
DestructorsTestClass4 mc = new DestructorsTestClass4();
mc = null;

// the following call has been "replaced" with the setting commanding a GC run before new allocations, which will have the desired effect of
// nanoFramework.Runtime.Native.GC.Run(true);

int sleepTime = 5000;
int slept = 0;

while (intI != 8 && slept < sleepTime)
{
// force GC run caused by memory allocation
var dummyArray = new byte[1024 * 1024 * 1];

System.Threading.Thread.Sleep(10);
slept += 10;
}

// Thread has slept for
OutputHelper.WriteLine(slept.ToString());
OutputHelper.WriteLine($"Thread as slept for{slept}");

if (intI == 8)
{
return true;
Expand All @@ -134,34 +151,45 @@ public static bool testMethod()
}
}

class DestructorsTestClass7_Base
public class DestructorsTestClass7_Base
{
public static int intI = 2;
}

class DestructorsTestClass7 : DestructorsTestClass7_Base
public class DestructorsTestClass7 : DestructorsTestClass7_Base
{

~DestructorsTestClass7()
{
intI = 3;
// Calling Destructor for Test Class 7
intI = 3;

Console.WriteLine("Calling Destructor for Test Class 7");
}

public static bool testMethod()
public static bool TestMethod()
{
DestructorsTestClass7 mc = new DestructorsTestClass7();
mc = null;

// the following call has been "replaced" with the setting commanding a GC run before new allocations, which will have the desired effect of
//nanoFramework.Runtime.Native.GC.Run(true);

int sleepTime = 5000;
int slept = 0;

while (intI != 3 && slept < sleepTime)
{
// force GC run caused by memory allocation
var dummyArray = new byte[1024 * 1024 * 1];

System.Threading.Thread.Sleep(10);
slept += 10;
}

// Thread has slept for
OutputHelper.WriteLine(slept.ToString());
OutputHelper.WriteLine($"Thread as slept for{slept}");

if (intI == 3)
{
return true;
Expand Down
Loading

0 comments on commit ff78458

Please sign in to comment.