Skip to content

test: networktime properties #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public struct NetworkTime
/// <param name="tickRate">The tickrate.</param>
public NetworkTime(int tickRate)
{
Assert.IsTrue(tickRate > 0, "Tickrate must be a positive value.");

m_TickRate = tickRate;
m_TickInterval = 1f / m_TickRate; // potential floating point precision issue, could result in different interval on different machines
m_CachedTickOffset = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,54 @@ namespace Unity.Netcode.EditorTests
{
public class NetworkTimeTests
{
[Test]
[TestCase(0d, 0)]
[TestCase(5d, 0)]
[TestCase(-5d, 0)]
[TestCase(0d, -20)]
[TestCase(5d, int.MinValue)]
[TestCase(-5d, -1)]
public void TestFailCreateInvalidTime(double time, int tickrate)
{
Assert.Throws<UnityEngine.Assertions.AssertionException>(() => new NetworkTime(tickrate, time));
}
Comment on lines +12 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we saying, they should always be positive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time values can be negative. With the idea that you can add a negative time to a positive time etc. But tickrate must always be positive. A 0 or negative tick rate does not make sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you like to comment this or something similar explaining "why these values are invalid?" inline? :)


[Test]
[TestCase(0d, 0f, 20)]
[TestCase(0d, 0f, 30)]
[TestCase(0d, 0f, 60)]

[TestCase(201d, 201f, 20)]
[TestCase(201d, 201f, 30)]
[TestCase(201d, 201f, 60)]

[TestCase(-4301d, -4301f, 20)]
[TestCase(-4301d, -4301f, 30)]
[TestCase(-4301d, -4301f, 60)]

[TestCase(float.MaxValue, float.MaxValue, 20)]
[TestCase(float.MaxValue, float.MaxValue, 30)]
[TestCase(float.MaxValue, float.MaxValue, 60)]
public void TestTimeAsFloat(double d, float f, int tickRate)
{
var networkTime = new NetworkTime(tickRate, d);
Assert.True(Mathf.Approximately(networkTime.TimeAsFloat, f));
}

[Test]
[TestCase(53.55d, 53.5d, 10)]
[TestCase(1013553.55d, 1013553.5d, 10)]
[TestCase(0d, 0d, 10)]
[TestCase(-27.41d, -27.5d, 10)]

[TestCase(53.55d, 53.54d, 50)]
[TestCase(1013553.55d, 1013553.54d, 50)]
[TestCase(0d, 0d, 50)]
[TestCase(-27.4133d, -27.42d, 50)]
public void TestToFixedTime(double time, double expectedFixedTime, int tickRate)
{
Assert.AreEqual(expectedFixedTime, new NetworkTime(tickRate, time).ToFixedTime().Time);
}

[Test]
public void NetworkTimeCreate()
Expand Down Expand Up @@ -104,7 +152,6 @@ public void NetworkTimeSubFloatTest()
Assert.IsTrue(Approximately(floatResultE, timeE.Time));
}


[Test]
public void NetworkTimeAddNetworkTimeTest()
{
Expand Down Expand Up @@ -170,7 +217,6 @@ public void NetworkTimeAdvanceTest()
NetworkTimeAdvanceTestInternal(randomSteps, 30, 0f);
NetworkTimeAdvanceTestInternal(randomSteps, 144, 0f);


NetworkTimeAdvanceTestInternal(randomSteps, 60, 23132.231f);
NetworkTimeAdvanceTestInternal(randomSteps, 1, 23132.231f);
NetworkTimeAdvanceTestInternal(randomSteps, 10, 23132.231f);
Expand Down Expand Up @@ -220,6 +266,5 @@ private static bool Approximately(double a, double b, double epsilon = 0.000001d
var dif = Math.Abs(a - b);
return dif <= epsilon;
}

}
}