Skip to content

Instantly share code, notes, and snippets.

@TMPxyz
Created January 3, 2019 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TMPxyz/fb3f1e9fdfae26be2d6cbd925d39a0d6 to your computer and use it in GitHub Desktop.
Save TMPxyz/fb3f1e9fdfae26be2d6cbd925d39a0d6 to your computer and use it in GitHub Desktop.
The unit tests for AllocatingGCMemory
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.Profiling;
using UnityEngine.TestTools.Constraints;
using Is = UnityEngine.TestTools.Constraints.Is;
namespace MH.GCTests
{
public class DummyTest
{
[Test]
public void DictTest_FAIL()
{
const int ELEM_COUNT = 1000;
var cont = new Dictionary<string, int>();
for (int i = 0; i < ELEM_COUNT; ++i)
cont.Add(i.ToString(), i);
Assert.That(() =>
{
for (var ie = cont.GetEnumerator(); ie.MoveNext();) { }
},
Is.Not.AllocatingGCMemory()
);
Assert.That(() =>
{
foreach (var elem in cont) { }
},
Is.Not.AllocatingGCMemory()
);
}
[Test]
public void DictTest_PASS()
{
const int ELEM_COUNT = 1000;
var cont = new Dictionary<string, int>();
for (int i = 0; i < ELEM_COUNT; ++i)
cont.Add(i.ToString(), i);
Assert.That(() =>
{
for (var ie = cont.GetEnumerator(); ie.MoveNext();) { }
},
Is.Not.AllocatingGCMemory()
);
Assert.That(() =>
{
foreach (var elem in cont) { }
},
Is.Not.AllocatingGCMemory()
);
Debug.Log(string.Format("startMem = {0}, mem1 = {1}, mem2 = {2}", 1, 2, 3)); //<-- the only differenct of the two tests
}
}
}
@TMPxyz
Copy link
Author

TMPxyz commented Jan 3, 2019

The only differences between these two tests, is that the second one has a Debug.Log at the end of the method, which caused the Assertions to give different results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment