您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
72 行
1.8 KiB
72 行
1.8 KiB
using System;
|
|
using NUnit.Framework;
|
|
|
|
namespace Unity.MLAgents.Tests
|
|
{
|
|
[TestFixture]
|
|
public class RecursionCheckerTests
|
|
{
|
|
class InfiniteRecurser
|
|
{
|
|
RecursionChecker m_checker = new RecursionChecker("InfiniteRecurser");
|
|
public int NumCalls;
|
|
|
|
public void Implode()
|
|
{
|
|
NumCalls++;
|
|
using (m_checker.Start())
|
|
{
|
|
Implode();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestRecursionCheck()
|
|
{
|
|
var rc = new InfiniteRecurser();
|
|
Assert.Throws<UnityAgentsException>(() =>
|
|
{
|
|
rc.Implode();
|
|
});
|
|
|
|
// Should increment twice before bailing out.
|
|
Assert.AreEqual(2, rc.NumCalls);
|
|
}
|
|
|
|
class OneTimeThrower
|
|
{
|
|
RecursionChecker m_checker = new RecursionChecker("OneTimeThrower");
|
|
public int NumCalls;
|
|
|
|
public void DoStuff()
|
|
{
|
|
// This method throws from inside the checker the first time.
|
|
// Later calls do nothing.
|
|
NumCalls++;
|
|
using (m_checker.Start())
|
|
{
|
|
if (NumCalls == 1)
|
|
{
|
|
throw new ArgumentException("oops");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestThrowResetsFlag()
|
|
{
|
|
var ott = new OneTimeThrower();
|
|
Assert.Throws<ArgumentException>(() =>
|
|
{
|
|
ott.DoStuff();
|
|
});
|
|
|
|
// Make sure the flag is cleared if we throw in the "using". Should be able to step subsequently.
|
|
ott.DoStuff();
|
|
ott.DoStuff();
|
|
Assert.AreEqual(3, ott.NumCalls);
|
|
}
|
|
}
|
|
}
|