Unstable v0.19.3.0

This commit is contained in:
Juan Pablo Arce
2022-09-02 15:10:56 -03:00
parent 28789616bd
commit 3f2c843247
336 changed files with 7152 additions and 7739 deletions
@@ -0,0 +1,84 @@
using Barotrauma;
using FluentAssertions;
using FsCheck;
using Xunit;
using static Barotrauma.ItemPrefab;
namespace TestProject
{
public class CommonnessInfoTests
{
private class CustomGenerators
{
public static Arbitrary<CommonnessInfo> CommonnessInfoGeneratorOverride()
{
return Arb.From(from float commonness in Arb.Generate<float>().Where(IsValid)
from float? abyssCommonness in Arb.Generate<float?>().Where(IsNullableValid)
from float? caveCommonness in Arb.Generate<float?>().Where(IsNullableValid)
select new CommonnessInfo(commonness, abyssCommonness, caveCommonness));
static bool IsValid(float commonness) => !float.IsNaN(commonness) && commonness > float.MinValue && commonness < float.MaxValue;
static bool IsNullableValid(float? commonness) => !commonness.HasValue || IsValid(commonness.Value);
}
}
public CommonnessInfoTests()
{
Arb.Register<TestProject.CustomGenerators>();
Arb.Register<CustomGenerators>();
}
[Fact]
public void TestInheritedCommonness()
{
Prop.ForAll<CommonnessInfo, CommonnessInfo>((child, parent) =>
{
var info = child.WithInheritedCommonness(parent);
info.Commonness.Should().Be(child.commonness);
if (child.abyssCommonness.HasValue)
{
info.abyssCommonness.Should().HaveValue();
info.abyssCommonness.Should().Be(child.abyssCommonness);
}
else if (parent.abyssCommonness.HasValue)
{
info.abyssCommonness.Should().HaveValue();
info.abyssCommonness.Should().Be(parent.abyssCommonness);
}
else
{
info.abyssCommonness.Should().NotHaveValue();
}
if (child.caveCommonness.HasValue)
{
info.caveCommonness.Should().HaveValue();
info.caveCommonness.Should().Be(child.caveCommonness);
}
else if (parent.caveCommonness.HasValue)
{
info.caveCommonness.Should().HaveValue();
info.caveCommonness.Should().Be(parent.caveCommonness);
}
else
{
info.caveCommonness.Should().NotHaveValue();
}
}).QuickCheckThrowOnFailure();
}
[Fact]
public void TestPathCommonness()
{
Prop.ForAll<CommonnessInfo>(info =>
{
info.GetCommonness(Level.TunnelType.MainPath).Should().Be(info.Commonness);
info.GetCommonness(Level.TunnelType.SidePath).Should().Be(info.Commonness);
info.GetCommonness(Level.TunnelType.Cave).Should().Be(info.CaveCommonness);
}).QuickCheckThrowOnFailure();
}
}
}
@@ -0,0 +1,15 @@
#nullable enable
using System.Net;
using Barotrauma.Networking;
using Xunit;
namespace TestProject;
public class EndpointComparisonTests
{
[Fact]
public void TestLidgrenAddress()
{
Assert.True(new LidgrenAddress(IPAddress.Loopback) == new LidgrenAddress(IPAddress.IPv6Loopback));
}
}
@@ -0,0 +1,67 @@
#nullable enable
using System.Net;
using Barotrauma;
using Xunit;
using Barotrauma.Networking;
using FluentAssertions;
namespace TestProject;
public class EndpointParseTests
{
[Fact]
public void TestLidgrenEndpoint()
{
Endpoint.Parse("127.0.0.1:27015")
.Should()
.BeOfType<Some<Endpoint>>()
.And.BeEquivalentTo(
Option<Endpoint>.Some(new LidgrenEndpoint(IPAddress.Loopback, 27015)),
options => options.RespectingRuntimeTypes());
}
[Fact]
public void TestLidgrenEndpointHostName()
{
Endpoint.Parse("localhost:27015")
.Should()
.BeOfType<Some<Endpoint>>()
.And.BeEquivalentTo(
Option<Endpoint>.Some(new LidgrenEndpoint(IPAddress.Loopback, 27015)),
options => options.RespectingRuntimeTypes());
}
[Fact]
public void TestLidgrenAddress()
{
Address.Parse("127.0.0.1")
.Should()
.BeOfType<Some<Address>>()
.And.BeEquivalentTo(
Option<Address>.Some(new LidgrenAddress(IPAddress.Loopback)),
options => options.RespectingRuntimeTypes());
}
[Fact]
public void TestSteamP2PEndpoint()
{
Endpoint.Parse("STEAM_1:1:508792388")
.Should()
.BeOfType<Some<Endpoint>>()
.And.BeEquivalentTo(
Option<Endpoint>.Some(new SteamP2PEndpoint(new SteamId(76561198977850505))),
options => options.RespectingRuntimeTypes());
}
[Fact]
public void TestSteamP2PAddress()
{
Address.Parse("STEAM_1:1:508792388")
.Should()
.BeOfType<Some<Address>>()
.And.BeEquivalentTo(
Option<Address>.Some(new SteamP2PAddress(new SteamId(76561198977850505))),
options => options.RespectingRuntimeTypes());
new SteamId(76561198977850505).StringRepresentation.Should().BeEquivalentTo("STEAM_1:1:508792388");
}
}
@@ -0,0 +1,55 @@
using System;
using System.Linq;
using System.Reflection;
using Barotrauma;
using Xunit;
namespace TestProject;
public class INetSerializableStructImplementationChecks
{
private delegate bool TryFindBehaviorDelegate(Type type, out NetSerializableProperties.IReadWriteBehavior behavior);
[Fact]
public void CheckStructMemberTypes()
{
var interfaceType = typeof(INetSerializableStruct);
var types = interfaceType.Assembly.GetTypes()
.Where(t => !t.IsAbstract && !t.IsInterface && t.IsAssignableTo(interfaceType));
//private static bool TryFindBehavior(Type type, out IReadWriteBehavior behavior)
TryFindBehaviorDelegate tryFindBehavior
= typeof(NetSerializableProperties)
.GetMethod("TryFindBehavior", BindingFlags.NonPublic | BindingFlags.Static,
typeof(TryFindBehaviorDelegate).GetMethod("Invoke")!
.GetParameters().Select(p => p.ParameterType).ToArray())!
.CreateDelegate<TryFindBehaviorDelegate>();
foreach (var type in types)
{
var members = NetSerializableProperties.GetPropertiesAndFields(type);
foreach (var member in members)
{
void checkType(Type typeBeingChecked)
{
Assert.True(tryFindBehavior(typeBeingChecked, out _), $"{type}.{member.Name} of type {member.Type} is unsupported in {nameof(INetSerializableStruct)}");
Type? nestedType = null;
if (typeBeingChecked.IsGenericType)
{
nestedType = typeBeingChecked.GetGenericArguments()[0];
}
else if (typeBeingChecked.IsArray)
{
nestedType = typeBeingChecked.GetElementType();
}
if (nestedType != null)
{
checkType(nestedType);
}
}
checkType(member.Type);
}
}
}
}
@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Immutable;
using Barotrauma;
using Barotrauma.Networking;
using FluentAssertions;
@@ -11,7 +12,7 @@ using Xunit;
namespace TestProject
{
// ReSharper disable UnusedMember.Local NotAccessedField.Local UnusedMember.Global
public class INetSerializableStructTests
public sealed class INetSerializableStructTests
{
private class CustomGenerators
{
@@ -25,6 +26,21 @@ namespace TestProject
Arb.Register<CustomGenerators>();
}
[Fact]
public void TestBitField()
{
Prop.ForAll<bool[]>(SerializeDeserializeBitField).VerboseCheckThrowOnFailure();
}
[Fact]
public void TestRanged()
{
Prop.ForAll(
Arb.Generate<int>().Where(i => i <= 100 && i >= -100).ToArbitrary(),
Arb.Generate<float>().Where(f => f <= 100f && f >= -100f).ToArbitrary(),
SerializeDeserializeRanged).QuickCheckThrowOnFailure();
}
[Fact]
public void TestOptional()
{
@@ -58,6 +74,12 @@ namespace TestProject
{
Prop.ForAll<EnumTest>(SerializeDeserialize).QuickCheckThrowOnFailure();
}
[Fact]
public void TestEnumFlags()
{
Prop.ForAll<EnumFlagsTest>(SerializeDeserialize).QuickCheckThrowOnFailure();
}
[Fact]
public void TestArray()
@@ -67,6 +89,14 @@ namespace TestProject
Prop.ForAll<String[]>(SerializeDeserialize).QuickCheckThrowOnFailure();
}
[Fact]
public void TestImmutableArray()
{
Prop.ForAll<ImmutableArray<Int32>>(SerializeDeserialize).QuickCheckThrowOnFailure();
Prop.ForAll<ImmutableArray<Boolean>>(SerializeDeserialize).QuickCheckThrowOnFailure();
Prop.ForAll<ImmutableArray<String>>(SerializeDeserialize).QuickCheckThrowOnFailure();
}
[Fact]
public void TestNullable()
{
@@ -147,6 +177,26 @@ namespace TestProject
Thousand = 1000
}
[Flags]
private enum EnumFlagsTest
{
None = 0,
Bit0 = 1 << 0,
Bit1 = 1 << 1,
Bit2 = 1 << 2,
Bit3 = 1 << 3
}
private struct TestRangedStruct : INetSerializableStruct
{
[NetworkSerialize(MinValueInt = -100, MaxValueInt = 100)]
public int IntValue;
[NetworkSerialize(MinValueFloat = -100, MaxValueFloat = 100, NumberOfBits = 16)]
public float FloatValue;
}
#pragma warning disable CS0649
private struct TestStruct<T> : INetSerializableStruct
{
[NetworkSerialize]
@@ -168,6 +218,25 @@ namespace TestProject
public (T, U) NotSerializedValue;
public (T, U) NotSerializedFunction() => throw new NotImplementedException();
}
#pragma warning restore CS0649
private static void SerializeDeserializeRanged(int intValue, float floatValue)
{
ReadWriteMessage msg = new ReadWriteMessage();
TestRangedStruct writeStruct = new TestRangedStruct
{
IntValue = intValue,
FloatValue = floatValue
};
msg.WriteNetSerializableStruct(writeStruct);
msg.BitPosition = 0;
TestRangedStruct readStruct = INetSerializableStruct.Read<TestRangedStruct>(msg);
readStruct.FloatValue.Should().BeApproximately(floatValue, 0.25f); // should be enough precision
readStruct.IntValue.Should().Be(intValue);
}
private static void SerializeDeserialize<T>(T arg) where T : notnull
{
@@ -177,12 +246,14 @@ namespace TestProject
Value = arg
};
((INetSerializableStruct)writeStruct).Write(msg);
msg.WriteNetSerializableStruct(writeStruct);
msg.BitPosition = 0;
TestStruct<T> readStruct = INetSerializableStruct.Read<TestStruct<T>>(msg);
readStruct.Should().BeEquivalentTo(writeStruct, options => options.ComparingByMembers<TestStruct<T>>());
readStruct.Should().BeEquivalentTo(writeStruct, options => options
.ComparingByMembers<TestStruct<T>>()
.ComparingByMembers(typeof(Option<>)));
}
private static void SerializeDeserializeNullableTuple<T, U>(T arg1, U arg2)
@@ -194,12 +265,35 @@ namespace TestProject
Two = arg2
};
((INetSerializableStruct)writeStruct).Write(msg);
msg.WriteNetSerializableStruct(writeStruct);
msg.BitPosition = 0;
TupleNullableStruct<T, U> readStruct = INetSerializableStruct.Read<TupleNullableStruct<T, U>>(msg);
readStruct.Should().BeEquivalentTo(writeStruct, options => options.ComparingByMembers<TupleNullableStruct<T, U>>());
readStruct.Should().BeEquivalentTo(writeStruct, options => options
.ComparingByMembers<TupleNullableStruct<T, U>>()
.ComparingByMembers(typeof(Option<>)));
}
private static void SerializeDeserializeBitField(bool[] arg)
{
ReadWriteMessage msg = new ReadWriteMessage();
IWritableBitField bitFieldWrite = new WriteOnlyBitField();
foreach (bool b in arg)
{
bitFieldWrite.WriteBoolean(b);
}
bitFieldWrite.WriteToMessage(msg);
msg.BitPosition = 0;
IReadableBitField bitFieldRead = new ReadOnlyBitField(msg);
foreach (bool b in arg)
{
bitFieldRead.ReadBoolean().Should().Be(b);
}
}
}
}
@@ -0,0 +1,20 @@
using System;
using Barotrauma.Networking;
using FluentAssertions;
using FsCheck;
using Xunit;
namespace TestProject;
public class NetIdUtilsTests
{
[Fact]
public void TestGetIdOlderThan()
{
Prop.ForAll<UInt16>(id =>
{
var olderId = NetIdUtils.GetIdOlderThan(id);
Assert.True(NetIdUtils.IdMoreRecent(id, olderId));
}).QuickCheckThrowOnFailure();
}
}