Skip to content

Add Unit Tests for GC #206

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
Oct 20, 2023
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
49 changes: 49 additions & 0 deletions Tests/NFUnitTestGC/NFUnitTestGC.nfproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<ItemGroup>
<ProjectCapability Include="TestContainer" />
</ItemGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>768be3b6-41c6-4dfb-8a2d-443b2113f5ad</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>NFUnitTestGC</RootNamespace>
<AssemblyName>NFUnitTest</AssemblyName>
<IsCodedUITest>False</IsCodedUITest>
<IsTestProject>true</IsTestProject>
<TestProjectType>UnitTest</TestProjectType>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="TestGCWithByteArrays.cs" />
<Compile Include="TestGCWithTimeSpanArrays.cs" />
<Compile Include="TestGCWithDateTimeArrays.cs" />
<Compile Include="TestGCWithObjectArrays.cs" />
<Compile Include="TestGC.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib">
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.TestFramework">
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.85\lib\nanoFramework.TestFramework.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.UnitTestLauncher">
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.85\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
</Project>
31 changes: 31 additions & 0 deletions Tests/NFUnitTestGC/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
48 changes: 48 additions & 0 deletions Tests/NFUnitTestGC/TestGC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//


using nanoFramework.TestFramework;
using System;

namespace NFUnitTestGC
{
[TestClass]
public class TestGC
{
[TestMethod]
public void TestGCStress()
{
int maxArraySize = 1024 * 32;
object[] arrays = new object[600];

OutputHelper.WriteLine("Starting TestGCStress");

for (int loop = 0; loop < 100; loop++)
{
OutputHelper.WriteLine($"Running iteration {loop}");

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

OutputHelper.WriteLine($"Alloc array of 64 bytes @ pos {i}");
arrays[i++] = new byte[64];
}

arrays[0] = new byte[maxArraySize];

for (int i = 0; i < arrays.Length; i++)
{
arrays[i] = null;
}
}

OutputHelper.WriteLine("Completed TestGCStress");
}
}
}
89 changes: 89 additions & 0 deletions Tests/NFUnitTestGC/TestGCWithByteArrays.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

using nanoFramework.TestFramework;
using System;

namespace NFUnitTestGC
{
[TestClass]
public class TestGCWithByteArrays
{
[TestMethod]
public void TestCompactionForNotFixedArray()
{
OutputHelper.WriteLine("Starting TestCompactionForNotFixedArray");

for (int loop = 0; loop < 10; loop++)
{
OutputHelper.WriteLine($"Starting iteration {loop}");

// First we create byte and holes that keeps some space that could be used by compaction

// Small count so compaction does not happen
byte[] arrayOfArrays = new byte[10];

RunAllocations(arrayOfArrays);

// This is the array that we expect to move in during compaction.
byte[] testNativeBuffer = new byte[100];

// Fill it, so it is not optimized out
Random random = new();
var baseValue = random.Next(2);

for (int i = 0; i < testNativeBuffer.Length; i++)
{
testNativeBuffer[i] = (byte)(i * baseValue);
}

// trigger compaction
InitiateCompaction();

int index = 0;

// Check that array content is not corrupted
foreach (var item in testNativeBuffer)
{
Assert.AreEqual(index * baseValue, item, $"Array content comparison failed at position {index}. Expecting {(index * baseValue)}, found {item}");
index++;
}

OutputHelper.WriteLine("No corruption detected in array");
}

OutputHelper.WriteLine("Completed TestCompactionForNotFixedArray");
}

void RunAllocations(byte[] arrObj)
{
for (int i = 1; i < arrObj.Length; i++)
{
// Creates referenced byte, which stays in memory until InitiateCompaction exits
arrObj[i] = new byte();

// Tries to create larger object that would be later hole .
// This object could be garbage collected on each "i" cycle.
byte[] arr = new byte[50 * i];

// Creates some usage for arr, so it is not optimized out.
arr[0] = 1;
arr[1] = 2;

OutputHelper.WriteLine($"On Cycle {i:D3} Array of {arr[1]} was allocated");
}
}

// This method causes compaction to occur.
// It is not so trivial as it need to fragment heap with referenced byte array.
void InitiateCompaction()
{
// large count, so compaction happens during call to RunAllocations
byte[] arrayOfArrays = new byte[1500];
RunAllocations(arrayOfArrays);
}
}
}
Loading