Created
January 3, 2019 06:40
-
-
Save TMPxyz/fb3f1e9fdfae26be2d6cbd925d39a0d6 to your computer and use it in GitHub Desktop.
The unit tests for AllocatingGCMemory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.