Skip to content

Commit d30f617

Browse files
LukeStampfli0xFA11
andauthored
test: Add unit tests for NetworkTime properties (#1053)
Co-authored-by: M. Fatih MAR <mfatihmar@gmail.com>
1 parent e89f05d commit d30f617

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public struct NetworkTime
6060
/// <param name="tickRate">The tickrate.</param>
6161
public NetworkTime(int tickRate)
6262
{
63+
Assert.IsTrue(tickRate > 0, "Tickrate must be a positive value.");
64+
6365
m_TickRate = tickRate;
6466
m_TickInterval = 1f / m_TickRate; // potential floating point precision issue, could result in different interval on different machines
6567
m_CachedTickOffset = 0;

com.unity.netcode.gameobjects/Tests/Editor/Timing/NetworkTimeTests.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,54 @@ namespace Unity.Netcode.EditorTests
99
{
1010
public class NetworkTimeTests
1111
{
12+
[Test]
13+
[TestCase(0d, 0)]
14+
[TestCase(5d, 0)]
15+
[TestCase(-5d, 0)]
16+
[TestCase(0d, -20)]
17+
[TestCase(5d, int.MinValue)]
18+
[TestCase(-5d, -1)]
19+
public void TestFailCreateInvalidTime(double time, int tickrate)
20+
{
21+
Assert.Throws<UnityEngine.Assertions.AssertionException>(() => new NetworkTime(tickrate, time));
22+
}
23+
24+
[Test]
25+
[TestCase(0d, 0f, 20)]
26+
[TestCase(0d, 0f, 30)]
27+
[TestCase(0d, 0f, 60)]
28+
29+
[TestCase(201d, 201f, 20)]
30+
[TestCase(201d, 201f, 30)]
31+
[TestCase(201d, 201f, 60)]
32+
33+
[TestCase(-4301d, -4301f, 20)]
34+
[TestCase(-4301d, -4301f, 30)]
35+
[TestCase(-4301d, -4301f, 60)]
36+
37+
[TestCase(float.MaxValue, float.MaxValue, 20)]
38+
[TestCase(float.MaxValue, float.MaxValue, 30)]
39+
[TestCase(float.MaxValue, float.MaxValue, 60)]
40+
public void TestTimeAsFloat(double d, float f, int tickRate)
41+
{
42+
var networkTime = new NetworkTime(tickRate, d);
43+
Assert.True(Mathf.Approximately(networkTime.TimeAsFloat, f));
44+
}
45+
46+
[Test]
47+
[TestCase(53.55d, 53.5d, 10)]
48+
[TestCase(1013553.55d, 1013553.5d, 10)]
49+
[TestCase(0d, 0d, 10)]
50+
[TestCase(-27.41d, -27.5d, 10)]
51+
52+
[TestCase(53.55d, 53.54d, 50)]
53+
[TestCase(1013553.55d, 1013553.54d, 50)]
54+
[TestCase(0d, 0d, 50)]
55+
[TestCase(-27.4133d, -27.42d, 50)]
56+
public void TestToFixedTime(double time, double expectedFixedTime, int tickRate)
57+
{
58+
Assert.AreEqual(expectedFixedTime, new NetworkTime(tickRate, time).ToFixedTime().Time);
59+
}
1260

1361
[Test]
1462
public void NetworkTimeCreate()
@@ -104,7 +152,6 @@ public void NetworkTimeSubFloatTest()
104152
Assert.IsTrue(Approximately(floatResultE, timeE.Time));
105153
}
106154

107-
108155
[Test]
109156
public void NetworkTimeAddNetworkTimeTest()
110157
{
@@ -170,7 +217,6 @@ public void NetworkTimeAdvanceTest()
170217
NetworkTimeAdvanceTestInternal(randomSteps, 30, 0f);
171218
NetworkTimeAdvanceTestInternal(randomSteps, 144, 0f);
172219

173-
174220
NetworkTimeAdvanceTestInternal(randomSteps, 60, 23132.231f);
175221
NetworkTimeAdvanceTestInternal(randomSteps, 1, 23132.231f);
176222
NetworkTimeAdvanceTestInternal(randomSteps, 10, 23132.231f);
@@ -220,6 +266,5 @@ private static bool Approximately(double a, double b, double epsilon = 0.000001d
220266
var dif = Math.Abs(a - b);
221267
return dif <= epsilon;
222268
}
223-
224269
}
225270
}

0 commit comments

Comments
 (0)