Merge pull request #539 from CJ-SPT/test-mod
Intoduce test mod and restructure test suites into their own solution folder
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using NUnit.Framework;
|
||||
using SPTarkov.Server.Core.Models.Enums;
|
||||
using SPTarkov.Server.Core.Utils;
|
||||
|
||||
namespace UnitTests.Tests.Utils;
|
||||
|
||||
[TestFixture]
|
||||
public class JsonUtilTests
|
||||
{
|
||||
private JsonUtil _jsonUtil;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Initialize()
|
||||
{
|
||||
_jsonUtil = DI.GetInstance().GetService<JsonUtil>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeAndDeserialize_WithDictionaryOfETFEnum_ExpectCorrectParsing()
|
||||
{
|
||||
var value = new Dictionary<QuestStatusEnum, int> { { QuestStatusEnum.AvailableForStart, 1 } };
|
||||
var result = _jsonUtil.Deserialize<Dictionary<QuestStatusEnum, int>>(_jsonUtil.Serialize(value));
|
||||
Assert.AreEqual(value.Count, result?.Count);
|
||||
Assert.AreEqual(value.First().Key, result?.First().Key);
|
||||
Assert.AreEqual(value.First().Value, result?.First().Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using NUnit.Framework;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Utils;
|
||||
|
||||
namespace UnitTests.Tests.Utils;
|
||||
|
||||
[TestFixture]
|
||||
public class MathUtilTests
|
||||
{
|
||||
private MathUtil _mathUtil;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Initialize()
|
||||
{
|
||||
_mathUtil = DI.GetInstance().GetService<MathUtil>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ListSumTest()
|
||||
{
|
||||
var test = new List<float> { 1.1f, 2.1f, 3.3f };
|
||||
const double expected = 6.5f;
|
||||
|
||||
var actual = test.Sum();
|
||||
|
||||
Assert.AreEqual(expected, actual, $"ListSum() Expected: {expected}, Actual: {actual}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ListCumSumTest()
|
||||
{
|
||||
var test = new List<double> { 1f, 2f, 3f, 4f };
|
||||
var expected = new List<double> { 1f, 3f, 6f, 10f };
|
||||
|
||||
var actual = test.CumulativeSum().ToList();
|
||||
|
||||
for (var i = 0; i < actual.Count; i++)
|
||||
{
|
||||
if (Math.Abs(expected[i] - actual[i]) > 0.00001f)
|
||||
{
|
||||
Assert.Fail($"ListCumSum() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ListProductTest()
|
||||
{
|
||||
var test = new List<double> { 1f, 2f, 3f, 4f };
|
||||
var expected = new List<double> { 2f, 4f, 6f, 8f };
|
||||
|
||||
var actual = test.Product(2).ToList();
|
||||
|
||||
for (var i = 0; i < actual.Count; i++)
|
||||
{
|
||||
if (Math.Abs(expected[i] - actual[i]) > 0.00001f)
|
||||
{
|
||||
Assert.Fail($"ListProduct() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ListAddTest()
|
||||
{
|
||||
var test = new List<double> { 1f, 2f, 3f, 4f };
|
||||
var expected = new List<double> { 3f, 4f, 5f, 6f };
|
||||
|
||||
var actual = _mathUtil.ListAdd(test, 2).ToList();
|
||||
|
||||
for (var i = 0; i < actual.Count; i++)
|
||||
{
|
||||
if (Math.Abs(expected[i] - actual[i]) > 0.00001f)
|
||||
{
|
||||
Assert.Fail($"ListProduct() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MapToRangeTest()
|
||||
{
|
||||
const double expected = 2;
|
||||
|
||||
var actual = _mathUtil.MapToRange(0.5, 0, 1, 1, 3);
|
||||
|
||||
Assert.AreEqual(expected, actual, $"MapToRange() Expected: {expected}, Actual: {actual}");
|
||||
}
|
||||
|
||||
[TestCase(15d, new double[] { 1, 10, 20, 30, 40, 50, 60 }, new double[] { 11000, 20000, 32000, 45000, 58000, 70000, 82000 }, 26000d)]
|
||||
[TestCase(5d, new double[] { 1, 10 }, new double[] { 0, 1000 }, 444.44444444444446d)]
|
||||
[TestCase(12d, new double[] { 1, 10, 500, 510 }, new double[] { 0, 10, 20, 30 }, 10.040816326530612d)]
|
||||
[TestCase(1d, new double[] { 1, 10, 500, 510 }, new double[] { 2, 10, 20, 30 }, 2d)]
|
||||
[TestCase(11d, new double[] { 1, 10 }, new double[] { 2, 10 }, 10d)]
|
||||
public void InterpTest(double input, double[] x, double[] y, double expected)
|
||||
{
|
||||
var actual = _mathUtil.Interp1(input, x.ToList(), y.ToList());
|
||||
|
||||
Assert.AreEqual(expected, actual, $"Interp1() Expected: {expected}, Actual: {actual}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using NUnit.Framework;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Models.Common;
|
||||
|
||||
namespace UnitTests.Tests.Utils;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the <see cref="MongoId"/> struct.
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class MongoIdTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test that generates 1000 <see cref="MongoId"/> and ensures they are all valid. <br/>
|
||||
/// Validity is checked by ensuring the ID is non-empty, exactly 24 characters, and matches the expected format.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Generate_ShouldProduceValidMongoIds()
|
||||
{
|
||||
var invalidIds = new List<string>();
|
||||
|
||||
for (var i = 0; i < 1000; i++)
|
||||
{
|
||||
var id = new MongoId();
|
||||
var idStr = id.ToString();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(idStr) || idStr.Length != 24 || !id.IsValidMongoId())
|
||||
{
|
||||
invalidIds.Add(idStr);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.AreEqual(0, invalidIds.Count, $"Invalid MongoIds found: {string.Join(", ", invalidIds)}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using NUnit.Framework;
|
||||
using SPTarkov.Server.Core.Utils;
|
||||
|
||||
namespace UnitTests.Tests.Utils;
|
||||
|
||||
[TestFixture]
|
||||
[TestFixture]
|
||||
public sealed class RandomUtilTests
|
||||
{
|
||||
private RandomUtil _randomUtil;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Initialize()
|
||||
{
|
||||
_randomUtil = DI.GetInstance().GetService<RandomUtil>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetIntTest()
|
||||
{
|
||||
// Run 10000 test cases
|
||||
for (var i = 0; i < 10000; i++)
|
||||
{
|
||||
var result = _randomUtil.GetInt(0, 10);
|
||||
|
||||
if (result < 0 || result > 10)
|
||||
{
|
||||
Assert.Fail($"GetInt(0, 10) out of range. Expected range [0, 10] but was {result}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetIntExTest()
|
||||
{
|
||||
// Run 10000 test cases
|
||||
for (var i = 0; i < 10000; i++)
|
||||
{
|
||||
var result = _randomUtil.GetInt(1, 10, true);
|
||||
|
||||
if (result < 1 || result > 9)
|
||||
{
|
||||
Assert.Fail($"GetInt(10) out of range. Expected range [1, 9] but was {result}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetDoubleTest()
|
||||
{
|
||||
// Run 10000 test cases
|
||||
for (var i = 0; i < 10000; i++)
|
||||
{
|
||||
var result = _randomUtil.GetDouble(0D, 10D);
|
||||
|
||||
if (result is < 0d or >= 10d)
|
||||
{
|
||||
Assert.Fail($"GetDouble(0d, 10d) out of range. Expected range [0.0d, 9.999d] but was {result}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPercentOfValueTest()
|
||||
{
|
||||
const float expected = 45.5f;
|
||||
var result = _randomUtil.GetPercentOfValue(45.5f, 100f);
|
||||
|
||||
Assert.AreEqual(expected, result, 0.0001f, $"GetPercentOfValue(45.5f, 100f) out of range. Expected: {expected}. Actual: {result}.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReduceValueByPercentTest()
|
||||
{
|
||||
const float expected = 54.5f;
|
||||
var result = _randomUtil.ReduceValueByPercent(100f, 45.5f);
|
||||
|
||||
Assert.AreEqual(
|
||||
expected,
|
||||
result,
|
||||
0.0001f,
|
||||
$"ReduceValueByPercent(100f, 45.5f) out of range. Expected: {expected}. Actual: {result}."
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetChance100Test()
|
||||
{
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
const bool expectedTrue = true;
|
||||
var resultTrue = _randomUtil.GetChance100(100f);
|
||||
|
||||
Assert.AreEqual(expectedTrue, resultTrue, $"GetChance100(100f) out of range. Expected: {expectedTrue}. Actual: {resultTrue}.");
|
||||
}
|
||||
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
const bool expectedFalse = false;
|
||||
var resultFalse = _randomUtil.GetChance100(0f);
|
||||
|
||||
Assert.AreEqual(
|
||||
expectedFalse,
|
||||
resultFalse,
|
||||
$"GetChance100(0f) out of range. Expected: {expectedFalse}. Actual: {resultFalse}."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Missing methods between these two
|
||||
|
||||
[Test]
|
||||
public void RandIntTest()
|
||||
{
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var result = _randomUtil.RandInt(0, 10);
|
||||
|
||||
if (result < 0 || result > 9)
|
||||
{
|
||||
Assert.Fail($"RandInt(0, 10) out of range. Expected range [0, 9] but was {result}.");
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var result = _randomUtil.RandInt(10);
|
||||
|
||||
if (result < 0 || result > 9)
|
||||
{
|
||||
Assert.Fail($"RandInt(10, null) out of range. Expected range [0, 9] but was {result}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RandNumTest()
|
||||
{
|
||||
for (var i = 0; i < 10000; i++)
|
||||
{
|
||||
var result = _randomUtil.RandNum(0, 10, 15);
|
||||
|
||||
if (result < 0 || result >= 10)
|
||||
{
|
||||
Assert.Fail($"RandNum(0, 10) out of range. Expected range [0, 9.999d] but was {result}.");
|
||||
}
|
||||
|
||||
if (_randomUtil.GetNumberPrecision(result) > RandomUtil.MaxSignificantDigits)
|
||||
{
|
||||
Assert.Fail(
|
||||
$"RandNum(0, 10) precision of {result} exceeds the allowable precision ({RandomUtil.MaxSignificantDigits}) for the given values."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < 10000; i++)
|
||||
{
|
||||
var result = _randomUtil.RandNum(10);
|
||||
|
||||
if (result < 0 || result >= 10)
|
||||
{
|
||||
Assert.Fail($"RandNum(10) out of range. Expected range [0, 9.999d] but was {result}.");
|
||||
}
|
||||
|
||||
if (_randomUtil.GetNumberPrecision(result) > RandomUtil.MaxSignificantDigits)
|
||||
{
|
||||
Assert.Fail(
|
||||
$"RandNum(10) precision of {result} exceeds the allowable precision ({RandomUtil.MaxSignificantDigits}) for the given values."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShuffleTest()
|
||||
{
|
||||
var testList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
|
||||
var orig = new List<int>(testList);
|
||||
|
||||
var result = _randomUtil.Shuffle(testList);
|
||||
|
||||
Assert.IsFalse(
|
||||
result.SequenceEqual(orig),
|
||||
$"Shuffle test failed. Expected: {string.Join(", ", orig)}, but got {string.Join(", ", result)}"
|
||||
);
|
||||
}
|
||||
|
||||
[TestCase(0.1, 1)]
|
||||
[TestCase(0.0001, 4)]
|
||||
[TestCase(0, 0)]
|
||||
[TestCase(10000000, 0)]
|
||||
[TestCase(0.000_000_000_000_000_000_000_000_1D, 25)]
|
||||
public void GetNumberPrecision_WithDoubles_ReturnsDecimalPoints(double value, int decimalPoints)
|
||||
{
|
||||
Assert.AreEqual(decimalPoints, _randomUtil.GetNumberPrecision(value));
|
||||
}
|
||||
|
||||
[TestCase(new[] { "test" }, "test", "Expected first array value")]
|
||||
public void GetArrayValueTest(string[] input, string expectedOutput, string failMessage)
|
||||
{
|
||||
var result = _randomUtil.GetArrayValue(input);
|
||||
Assert.AreEqual(input.First(), result, failMessage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user