浏览代码

fixed serialization tests

/main
Steven Leal 4 年前
当前提交
4cbc968a
共有 9 个文件被更改,包括 239 次插入177 次删除
  1. 3
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.meta
  2. 3
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources.meta
  3. 67
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/SampleScenarioConfiguration.json
  4. 3
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/SampleScenarioConfiguration.json.meta
  5. 163
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs
  6. 177
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs
  7. 0
      /com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs.meta

3
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.meta


fileFormatVersion: 2
guid: 60317fa8c31048249aef19666120f42d
timeCreated: 1613004387

3
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources.meta


fileFormatVersion: 2
guid: 587e6f3ccc7d4d11beb10c2078c268f7
timeCreated: 1613005737

67
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/SampleScenarioConfiguration.json


{
"constants": {
"framesPerIteration": 1,
"totalIterations": 100,
"instanceCount": 1,
"instanceIndex": 0,
"randomSeed": 539662031
},
"randomizers": {
"groups": {
"RotationRandomizer": {
"metadata": {
"name": "",
"description": ""
},
"items": {
"rotation": {
"param": {
"metadata": {
"name": "",
"description": ""
},
"items": {
"x": {
"samplerOptions": {
"metadata": {
"name": "",
"description": ""
},
"uniform": {
"min": 0.0,
"max": 360.0
}
}
},
"y": {
"samplerOptions": {
"metadata": {
"name": "",
"description": ""
},
"uniform": {
"min": 0.0,
"max": 360.0
}
}
},
"z": {
"samplerOptions": {
"metadata": {
"name": "",
"description": ""
},
"uniform": {
"min": 0.0,
"max": 360.0
}
}
}
}
}
}
}
}
}
}
}

3
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/SampleScenarioConfiguration.json.meta


fileFormatVersion: 2
guid: f8b765e3d9c34160bee64b2709a4edd8
timeCreated: 1613005744

163
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs


using System;
using System.Collections;
using System.IO;
using System.Text;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Scenarios;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;
namespace RandomizationTests
{
[TestFixture]
public class ScenarioTests
{
GameObject m_TestObject;
FixedLengthScenario m_Scenario;
[SetUp]
public void Setup()
{
m_TestObject = new GameObject();
}
[TearDown]
public void TearDown()
{
Object.DestroyImmediate(m_TestObject);
}
// TODO: update this function once the perception camera doesn't skip the first frame
IEnumerator CreateNewScenario(int totalIterations, int framesPerIteration)
{
m_Scenario = m_TestObject.AddComponent<FixedLengthScenario>();
m_Scenario.quitOnComplete = false;
m_Scenario.constants.totalIterations = totalIterations;
m_Scenario.constants.framesPerIteration = framesPerIteration;
yield return null; // Skip Start() frame
yield return null; // Skip first Update() frame
}
[Test]
public void ScenarioConfigurationSerializesProperly()
{
m_TestObject = new GameObject();
m_Scenario = m_TestObject.AddComponent<FixedLengthScenario>();
m_Scenario.CreateRandomizer<RotationRandomizer>();
static string RemoveWhitespace(string str) =>
string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
var expectedConfigAsset = (TextAsset)Resources.Load("SampleScenarioConfiguration");
var expectedText = RemoveWhitespace(expectedConfigAsset.text);
var scenarioJson = RemoveWhitespace(m_Scenario.SerializeToJson());
Assert.AreEqual(expectedText, scenarioJson);
}
[Test]
public void ScenarioConfigurationOverwrittenDuringDeserialization()
{
m_TestObject = new GameObject();
m_Scenario = m_TestObject.AddComponent<FixedLengthScenario>();
var constants = new FixedLengthScenario.Constants
{
framesPerIteration = 2,
totalIterations = 2
};
var changedConstants = new FixedLengthScenario.Constants
{
framesPerIteration = 0,
totalIterations = 0
};
// Serialize some values
m_Scenario.constants = constants;
var serializedConfig = m_Scenario.SerializeToJson();
// Change the values
m_Scenario.constants = changedConstants;
m_Scenario.DeserializeFromJson(serializedConfig);
// Check if the values reverted correctly
Assert.AreEqual(m_Scenario.constants.framesPerIteration, constants.framesPerIteration);
Assert.AreEqual(m_Scenario.constants.totalIterations, constants.totalIterations);
}
[UnityTest]
public IEnumerator IterationsCanLastMultipleFrames()
{
const int frameCount = 5;
yield return CreateNewScenario(1, frameCount);
for (var i = 0; i < frameCount; i++)
{
Assert.AreEqual(0, m_Scenario.currentIteration);
yield return null;
}
Assert.AreEqual(1, m_Scenario.currentIteration);
}
[UnityTest]
public IEnumerator FinishesWhenIsScenarioCompleteIsTrue()
{
const int iterationCount = 5;
yield return CreateNewScenario(iterationCount, 1);
for (var i = 0; i < iterationCount; i++)
{
Assert.False(m_Scenario.isScenarioComplete);
yield return null;
}
Assert.True(m_Scenario.isScenarioComplete);
}
[UnityTest]
public IEnumerator StartNewDatasetSequenceEveryIteration()
{
var perceptionCamera = SetupPerceptionCamera();
yield return CreateNewScenario(2, 2);
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
// Second frame, first iteration
yield return null;
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, perceptionCamera.simulationDeltaTime);
// Third frame, second iteration, SequenceTime has been reset
yield return null;
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
}
[UnityTest]
public IEnumerator GeneratedRandomSeedsChangeWithScenarioIteration()
{
yield return CreateNewScenario(3, 1);
var seeds = new uint[3];
for (var i = 0; i < 3; i++)
seeds[i] = SamplerState.NextRandomState();
yield return null;
for (var i = 0; i < 3; i++)
Assert.AreNotEqual(seeds[i], SamplerState.NextRandomState());
}
PerceptionCamera SetupPerceptionCamera()
{
m_TestObject.SetActive(false);
var camera = m_TestObject.AddComponent<Camera>();
camera.orthographic = true;
camera.orthographicSize = 1;
var perceptionCamera = m_TestObject.AddComponent<PerceptionCamera>();
perceptionCamera.captureRgbImages = false;
m_TestObject.SetActive(true);
return perceptionCamera;
}
}
}

177
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs


using System;
using System.Collections;
using System.IO;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Scenarios;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;
namespace RandomizationTests
{
[TestFixture]
public class ScenarioTests
{
GameObject m_TestObject;
FixedLengthScenario m_Scenario;
[SetUp]
public void Setup()
{
m_TestObject = new GameObject();
}
[TearDown]
public void TearDown()
{
Object.DestroyImmediate(m_TestObject);
}
// TODO: update this function once the perception camera doesn't skip the first frame
IEnumerator CreateNewScenario(int totalIterations, int framesPerIteration)
{
m_Scenario = m_TestObject.AddComponent<FixedLengthScenario>();
m_Scenario.quitOnComplete = false;
m_Scenario.constants.totalIterations = totalIterations;
m_Scenario.constants.framesPerIteration = framesPerIteration;
yield return null; // Skip Start() frame
yield return null; // Skip first Update() frame
}
[UnityTest]
public IEnumerator ScenarioConfigurationSerializesProperly()
{
yield return CreateNewScenario(10, 10);
var scenario = m_Scenario.GetComponent<FixedLengthScenario>();
scenario.CreateRandomizer<HueOffsetRandomizer>();
const string expectedConfig = @"{
""constants"": {
""framesPerIteration"": 10,
""totalIterations"": 10,
""instanceCount"": 1,
""instanceIndex"": 0,
""randomSeed"": 539662031
},
""randomizers"": {
""HueOffsetRandomizer"": {
""hueOffset"": {
""value"": {
""range"": {
""minimum"": -180.0,
""maximum"": 180.0
}
}
}
}
}
}";
Assert.AreEqual(expectedConfig, scenario.SerializeToJson());
}
[UnityTest]
public IEnumerator ScenarioConfigurationOverwrittenDuringDeserialization()
{
yield return CreateNewScenario(10, 10);
var constants = new FixedLengthScenario.Constants
{
framesPerIteration = 2,
totalIterations = 2
};
var changedConstants = new FixedLengthScenario.Constants
{
framesPerIteration = 0,
totalIterations = 0
};
// Serialize some values
m_Scenario.constants = constants;
var serializedConfig = m_Scenario.SerializeToJson();
// Change the values
m_Scenario.constants = changedConstants;
m_Scenario.DeserializeFromJson(serializedConfig);
// Check if the values reverted correctly
Assert.AreEqual(m_Scenario.constants.framesPerIteration, constants.framesPerIteration);
Assert.AreEqual(m_Scenario.constants.totalIterations, constants.totalIterations);
yield return null;
}
[UnityTest]
public IEnumerator IterationsCanLastMultipleFrames()
{
const int frameCount = 5;
yield return CreateNewScenario(1, frameCount);
for (var i = 0; i < frameCount; i++)
{
Assert.AreEqual(0, m_Scenario.currentIteration);
yield return null;
}
Assert.AreEqual(1, m_Scenario.currentIteration);
}
[UnityTest]
public IEnumerator FinishesWhenIsScenarioCompleteIsTrue()
{
const int iterationCount = 5;
yield return CreateNewScenario(iterationCount, 1);
for (var i = 0; i < iterationCount; i++)
{
Assert.False(m_Scenario.isScenarioComplete);
yield return null;
}
Assert.True(m_Scenario.isScenarioComplete);
}
[UnityTest]
public IEnumerator StartNewDatasetSequenceEveryIteration()
{
var perceptionCamera = SetupPerceptionCamera();
yield return CreateNewScenario(2, 2);
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
// Second frame, first iteration
yield return null;
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, perceptionCamera.simulationDeltaTime);
// Third frame, second iteration, SequenceTime has been reset
yield return null;
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
}
[UnityTest]
public IEnumerator GeneratedRandomSeedsChangeWithScenarioIteration()
{
yield return CreateNewScenario(3, 1);
var seeds = new uint[3];
for (var i = 0; i < 3; i++)
seeds[i] = SamplerState.NextRandomState();
yield return null;
for (var i = 0; i < 3; i++)
Assert.AreNotEqual(seeds[i], SamplerState.NextRandomState());
}
PerceptionCamera SetupPerceptionCamera()
{
m_TestObject.SetActive(false);
var camera = m_TestObject.AddComponent<Camera>();
camera.orthographic = true;
camera.orthographicSize = 1;
var perceptionCamera = m_TestObject.AddComponent<PerceptionCamera>();
perceptionCamera.captureRgbImages = false;
m_TestObject.SetActive(true);
return perceptionCamera;
}
}
}

/com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs.meta → /com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs.meta

正在加载...
取消
保存