Skip to content

Enable destructor and finalizer tests #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
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