Skip to content

Commit f9e99da

Browse files
committed
Add tests so there is enough code coverage to pass tests.
1 parent 7af9318 commit f9e99da

File tree

8 files changed

+165
-34
lines changed

8 files changed

+165
-34
lines changed

com.unity.ml-agents/Runtime/Agent.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ public void CopyActions(ActionBuffers actionBuffers)
8787
internal class AgentVectorActuator : VectorActuator
8888
{
8989
public AgentVectorActuator(IActionReceiver actionReceiver,
90-
IHeuristicProvider heuristicProvider,
91-
ActionSpec actionSpec,
92-
string name = "VectorActuator"
90+
IHeuristicProvider heuristicProvider,
91+
ActionSpec actionSpec,
92+
string name = "VectorActuator"
9393
) : base(actionReceiver, heuristicProvider, actionSpec, name)
94-
{ }
94+
{}
9595

9696
public override BuiltInActuatorType GetBuiltInActuatorType()
9797
{
@@ -706,9 +706,7 @@ public int CompletedEpisodes
706706
/// <param name="reward">The new value of the reward.</param>
707707
public void SetReward(float reward)
708708
{
709-
#if DEBUG
710709
Utilities.DebugCheckNanAndInfinity(reward, nameof(reward), nameof(SetReward));
711-
#endif
712710
m_CumulativeReward += (reward - m_Reward);
713711
m_Reward = reward;
714712
}
@@ -736,26 +734,20 @@ public void SetReward(float reward)
736734
/// <param name="increment">Incremental reward value.</param>
737735
public void AddReward(float increment)
738736
{
739-
#if DEBUG
740737
Utilities.DebugCheckNanAndInfinity(increment, nameof(increment), nameof(AddReward));
741-
#endif
742738
m_Reward += increment;
743739
m_CumulativeReward += increment;
744740
}
745741

746742
internal void SetGroupReward(float reward)
747743
{
748-
#if DEBUG
749744
Utilities.DebugCheckNanAndInfinity(reward, nameof(reward), nameof(SetGroupReward));
750-
#endif
751745
m_GroupReward = reward;
752746
}
753747

754748
internal void AddGroupReward(float increment)
755749
{
756-
#if DEBUG
757750
Utilities.DebugCheckNanAndInfinity(increment, nameof(increment), nameof(AddGroupReward));
758-
#endif
759751
m_GroupReward += increment;
760752
}
761753

@@ -889,7 +881,7 @@ void ResetData()
889881
///
890882
/// [GameObject]: https://docs.unity3d.com/Manual/GameObjects.html
891883
/// </remarks>
892-
public virtual void Initialize() { }
884+
public virtual void Initialize() {}
893885

894886
/// <summary>
895887
/// Implement <see cref="Heuristic"/> to choose an action for this agent using a custom heuristic.
@@ -1336,7 +1328,7 @@ public virtual void OnActionReceived(ActionBuffers actions)
13361328
/// </summary>
13371329
/// <seealso cref="Initialize"/>
13381330
/// <seealso cref="EndEpisode"/>
1339-
public virtual void OnEpisodeBegin() { }
1331+
public virtual void OnEpisodeBegin() {}
13401332

13411333
/// <summary>
13421334
/// Gets the most recent ActionBuffer for this agent.

com.unity.ml-agents/Runtime/Analytics/TrainingAnalytics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ internal class TrainingAnalytics
6666
private static string s_TrainerPackageVersion = "";
6767
private static string s_TrainerCommunicationVersion = "";
6868

69-
static bool EnableAnalytics()
69+
internal static bool EnableAnalytics()
7070
{
7171
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
7272
if (s_EventsRegistered)

com.unity.ml-agents/Runtime/Policies/HeuristicPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void Dispose()
6060
/// Trivial implementation of the IList interface that does nothing.
6161
/// This is only used for "writing" observations that we will discard.
6262
/// </summary>
63-
class NullList : IList<float>
63+
internal class NullList : IList<float>
6464
{
6565
public IEnumerator<float> GetEnumerator()
6666
{
@@ -113,7 +113,7 @@ public void RemoveAt(int index)
113113
public float this[int index]
114114
{
115115
get { return 0.0f; }
116-
set { }
116+
set {}
117117
}
118118
}
119119

com.unity.ml-agents/Runtime/Sensors/VectorSensor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ void Clear()
120120

121121
void AddFloatObs(float obs)
122122
{
123-
#if DEBUG
124123
Utilities.DebugCheckNanAndInfinity(obs, nameof(obs), nameof(AddFloatObs));
125-
#endif
126124
m_Observations.Add(obs);
127125
}
128126

com.unity.ml-agents/Runtime/Utilities.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
2+
using System.Diagnostics;
23

34
namespace Unity.MLAgents
45
{
56
internal static class Utilities
67
{
7-
88
/// <summary>
99
/// Calculates the cumulative sum of an integer array. The result array will be one element
1010
/// larger than the input array since it has a padded 0 at the beginning.
@@ -26,10 +26,9 @@ internal static int[] CumSum(int[] input)
2626
return result;
2727
}
2828

29-
#if DEBUG
29+
[Conditional("DEBUG")]
3030
internal static void DebugCheckNanAndInfinity(float value, string valueCategory, string caller)
3131
{
32-
3332
if (float.IsNaN(value))
3433
{
3534
throw new ArgumentException($"NaN {valueCategory} passed to {caller}.");
@@ -39,7 +38,5 @@ internal static void DebugCheckNanAndInfinity(float value, string valueCategory,
3938
throw new ArgumentException($"Inifinity {valueCategory} passed to {caller}.");
4039
}
4140
}
42-
#endif
4341
}
44-
4542
}

com.unity.ml-agents/Tests/Editor/Analytics/TrainingAnalyticsTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public void TestRemotePolicyEvent()
5353

5454
Assert.AreEqual(2, remotePolicyEvent.ActuatorInfos[0].NumContinuousActions);
5555
Assert.AreEqual(0, remotePolicyEvent.ActuatorInfos[0].NumDiscreteActions);
56-
5756
}
5857

5958
[Test]
@@ -73,6 +72,12 @@ public void TestRemotePolicy()
7372

7473
Academy.Instance.Dispose();
7574
}
75+
76+
[Test]
77+
public void TestEnableAnalytics()
78+
{
79+
Assert.IsTrue(TrainingAnalytics.EnableAnalytics());
80+
}
7681
}
7782
}
7883
#endif // MLA_UNITY_ANALYTICS_MODULE_ENABLED

com.unity.ml-agents/Tests/Editor/Communicator/GrpcExtensionsTests.cs

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Text.RegularExpressions;
2+
using Google.Protobuf;
3+
using Google.Protobuf.Collections;
14
using NUnit.Framework;
25
using Unity.MLAgents.Actuators;
36
using Unity.MLAgents.Demonstrations;
@@ -6,18 +9,33 @@
69

710
using Unity.MLAgents.Analytics;
811
using Unity.MLAgents.CommunicatorObjects;
12+
using UnityEditor.VersionControl;
13+
using UnityEngine;
14+
using UnityEngine.TestTools;
915

1016
namespace Unity.MLAgents.Tests
1117
{
1218
[TestFixture]
1319
public class GrpcExtensionsTests
1420
{
21+
[SetUp]
22+
public void SetUp()
23+
{
24+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities();
25+
}
26+
1527
[Test]
1628
public void TestDefaultBrainParametersToProto()
1729
{
1830
// Should be able to convert a default instance to proto.
1931
var brain = new BrainParameters();
2032
brain.ToProto("foo", false);
33+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
34+
{
35+
BaseRLCapabilities = true,
36+
HybridActions = false
37+
};
38+
brain.ToProto("foo", false);
2139
}
2240

2341
[Test]
@@ -26,22 +44,104 @@ public void TestDefaultActionSpecToProto()
2644
// Should be able to convert a default instance to proto.
2745
var actionSpec = new ActionSpec();
2846
actionSpec.ToBrainParametersProto("foo", false);
47+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
48+
{
49+
BaseRLCapabilities = true,
50+
HybridActions = false
51+
};
52+
actionSpec.ToBrainParametersProto("foo", false);
2953

54+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities();
3055
// Continuous
3156
actionSpec = ActionSpec.MakeContinuous(3);
3257
actionSpec.ToBrainParametersProto("foo", false);
58+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
59+
{
60+
BaseRLCapabilities = true,
61+
HybridActions = false
62+
};
63+
actionSpec.ToBrainParametersProto("foo", false);
64+
65+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities();
3366

3467
// Discrete
3568
actionSpec = ActionSpec.MakeDiscrete(1, 2, 3);
3669
actionSpec.ToBrainParametersProto("foo", false);
70+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
71+
{
72+
BaseRLCapabilities = true,
73+
HybridActions = false
74+
};
75+
actionSpec.ToBrainParametersProto("foo", false);
76+
}
77+
78+
[Test]
79+
public void ToBrainParameters()
80+
{
81+
// Should be able to convert a default instance to proto.
82+
var actionSpec = new ActionSpec();
83+
actionSpec.ToBrainParametersProto("foo", false).ToBrainParameters();
84+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
85+
{
86+
BaseRLCapabilities = true,
87+
HybridActions = false
88+
};
89+
actionSpec.ToBrainParametersProto("foo", false).ToBrainParameters();
90+
91+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities();
92+
// Continuous
93+
actionSpec = ActionSpec.MakeContinuous(3);
94+
actionSpec.ToBrainParametersProto("foo", false).ToBrainParameters();
95+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
96+
{
97+
BaseRLCapabilities = true,
98+
HybridActions = false
99+
};
100+
actionSpec.ToBrainParametersProto("foo", false).ToBrainParameters();
101+
102+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities();
103+
104+
// Discrete
105+
actionSpec = ActionSpec.MakeDiscrete(1, 2, 3);
106+
actionSpec.ToBrainParametersProto("foo", false).ToBrainParameters();
107+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
108+
{
109+
BaseRLCapabilities = true,
110+
HybridActions = false
111+
};
112+
actionSpec.ToBrainParametersProto("foo", false).ToBrainParameters();
37113
}
38114

39115
[Test]
40116
public void TestDefaultAgentInfoToProto()
41117
{
42118
// Should be able to convert a default instance to proto.
43119
var agentInfo = new AgentInfo();
44-
agentInfo.ToInfoActionPairProto();
120+
var pairProto = agentInfo.ToInfoActionPairProto();
121+
pairProto.AgentInfo.Observations.Add(new ObservationProto
122+
{
123+
CompressedData = ByteString.Empty,
124+
CompressionType = CompressionTypeProto.None,
125+
FloatData = new ObservationProto.Types.FloatData(),
126+
ObservationType = ObservationTypeProto.Default,
127+
Name = "Sensor"
128+
});
129+
pairProto.AgentInfo.Observations[0].Shape.Add(0);
130+
pairProto.GetObservationSummaries();
131+
agentInfo.ToAgentInfoProto();
132+
agentInfo.groupId = 1;
133+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
134+
{
135+
BaseRLCapabilities = true,
136+
MultiAgentGroups = false
137+
};
138+
agentInfo.ToAgentInfoProto();
139+
LogAssert.Expect(LogType.Warning, new Regex(".+"));
140+
Academy.Instance.TrainerCapabilities = new UnityRLCapabilities
141+
{
142+
BaseRLCapabilities = true,
143+
MultiAgentGroups = true
144+
};
45145
agentInfo.ToAgentInfoProto();
46146
}
47147

@@ -77,9 +177,9 @@ public byte[] GetCompressedObservation()
77177
return new byte[] { 13, 37 };
78178
}
79179

80-
public void Update() { }
180+
public void Update() {}
81181

82-
public void Reset() { }
182+
public void Reset() {}
83183

84184
public SensorCompressionType GetCompressionType()
85185
{
@@ -123,7 +223,7 @@ public void TestGetObservationProtoCapabilities()
123223
(new[] {4, 4, 4}, SensorCompressionType.PNG, true, true), // Supported compressed
124224
};
125225

126-
foreach (var (shape, compressionType, supportsMultiPngObs, expectCompressed) in variants)
226+
foreach (var(shape, compressionType, supportsMultiPngObs, expectCompressed) in variants)
127227
{
128228
var dummySensor = new DummySensor();
129229
var obsWriter = new ObservationWriter();
@@ -151,8 +251,6 @@ public void TestGetObservationProtoCapabilities()
151251
Assert.AreEqual(obsProto.CompressedData.Length, 0);
152252
}
153253
}
154-
155-
156254
}
157255

158256
[Test]
@@ -172,6 +270,7 @@ public void TestIsTrivialMapping()
172270
sparseChannelSensor.Mapping = new[] { 0, 0, 0, 1, 1, 1 };
173271
Assert.AreEqual(GrpcExtensions.IsTrivialMapping(sparseChannelSensor), false);
174272
}
273+
175274
[Test]
176275
public void TestDefaultTrainingEvents()
177276
{

0 commit comments

Comments
 (0)