* Add test for two chemistry issues 1. rounding issue with reaction processing when making chloral hydrate 2. reliable assert trip due to the ValidateSolution() heat capacity issue. * Fix FixedPoint2 arithmetic Fix internal floating point arithmetic in places where it could be avoided. Fix incorrect rounding mode used in other places (it should always floor, like regular int arithmetic). I had to add an explicit epsilon value for float -> FixedPoint2 because something like 1.05 is actually like 1.04999 and that'd cause it to be rounded down to 1.04. This fixes reaction reagent processing in cases where the reagent inputs can't cleanly divide. Previously, when making 30u chloral hydrate by adding the chlorine in 10u increments you'd end up with 0.04 chlorine left over. This was caused by division in the reaction code rounding up in some cases. Changing division here to always round down fixes it. * Attempt to fix heat capacity precision assert issues. Fixes #22126 First, we just increase the tolerance of the assert. It was way too low. Second, actually put a cap on float drift from one-off _heatCapacity changes. * Fix float -> FixedPoint2 epsilon for negative number, fix tests. * Fix DamageableTest * Oh yeah I need to call CleanReturnAsync
221 lines
6.6 KiB
C#
221 lines
6.6 KiB
C#
using System;
|
|
using Content.Shared.FixedPoint;
|
|
using NUnit.Framework;
|
|
|
|
namespace Content.Tests.Shared.Chemistry
|
|
{
|
|
[TestFixture, TestOf(typeof(FixedPoint2)), Parallelizable]
|
|
public sealed class FixedPoint2_Tests
|
|
{
|
|
[Test]
|
|
[TestCase(1, "1")]
|
|
[TestCase(0, "0")]
|
|
[TestCase(-1, "-1")]
|
|
public void FixedPoint2IntegerTests(int value, string expected)
|
|
{
|
|
var result = FixedPoint2.New(value);
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(0.999f, "0.99")]
|
|
[TestCase(1.005f, "1")]
|
|
[TestCase(1.015f, "1.01")]
|
|
[TestCase(1.05f, "1.05")]
|
|
[TestCase(-1.05f, "-1.05")]
|
|
public void FixedPoint2FloatTests(float value, string expected)
|
|
{
|
|
var result = FixedPoint2.New(value);
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(0.999, "0.99")]
|
|
[TestCase(1.005, "1")]
|
|
[TestCase(1.015, "1.01")]
|
|
[TestCase(1.05, "1.05")]
|
|
public void FixedPoint2DoubleTests(double value, string expected)
|
|
{
|
|
var result = FixedPoint2.New(value);
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("0.999", "0.99")]
|
|
[TestCase("1.005", "1")]
|
|
[TestCase("1.015", "1.01")]
|
|
[TestCase("1.05", "1.05")]
|
|
public void FixedPoint2StringTests(string value, string expected)
|
|
{
|
|
var result = FixedPoint2.New(value);
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1, 1, "2")]
|
|
[TestCase(1.05f, 1, "2.05")]
|
|
public void ArithmeticAddition(float aFloat, float bFloat, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var b = FixedPoint2.New(bFloat);
|
|
|
|
var result = a + b;
|
|
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1, 1, "0")]
|
|
[TestCase(1f, 2.5f, "-1.5")]
|
|
public void ArithmeticSubtraction(float aFloat, float bFloat, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var b = FixedPoint2.New(bFloat);
|
|
|
|
var result = a - b;
|
|
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1.001f, 3f, "0.33")]
|
|
[TestCase(0.999f, 3f, "0.33")]
|
|
[TestCase(2.1f, 3f, "0.7")]
|
|
[TestCase(0.03f, 2f, "0.01")]
|
|
public void ArithmeticDivision(float aFloat, float bFloat, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var b = FixedPoint2.New(bFloat);
|
|
|
|
var result = a / b;
|
|
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1.001f, 3f, "0.33")]
|
|
[TestCase(0.999f, 3f, "0.33")]
|
|
[TestCase(2.1f, 3f, "0.7")]
|
|
[TestCase(0.03f, 2f, "0.01")]
|
|
[TestCase(1f, 1 / 1.05f, "1.05")]
|
|
public void ArithmeticDivisionFloat(float aFloat, float b, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
|
|
var result = a / b;
|
|
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1, 1, "1")]
|
|
[TestCase(1, 3f, "3")]
|
|
public void ArithmeticMultiplication(float aFloat, float bFloat, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var b = FixedPoint2.New(bFloat);
|
|
|
|
var result = a * b;
|
|
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1, 1, "1")]
|
|
[TestCase(1, 1.05f, "1.05")]
|
|
public void ArithmeticMultiplicationFloat(float aFloat, float b, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var result = a * b;
|
|
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(0.995f, 100)]
|
|
[TestCase(1.005f, 101)]
|
|
[TestCase(2.005f, 201)]
|
|
public void FloatRoundingTest(float a, int expected)
|
|
{
|
|
var result = (int) MathF.Round(a * MathF.Pow(10, 2), MidpointRounding.AwayFromZero);
|
|
Assert.That(result, Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
public void FixedPoint2Min()
|
|
{
|
|
var unorderedList = new[]
|
|
{
|
|
FixedPoint2.New(5),
|
|
FixedPoint2.New(3),
|
|
FixedPoint2.New(1),
|
|
FixedPoint2.New(2),
|
|
FixedPoint2.New(4),
|
|
};
|
|
var min = FixedPoint2.Min(unorderedList);
|
|
Assert.That(min, Is.EqualTo(FixedPoint2.New(1)));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(10.1f, 2.5f, "25.25")]
|
|
public void FloatMultiply (float aFloat, float b, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var result = a*b;
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(10.1f, 2.5d, "25.25")]
|
|
public void DoubleMultiply(float aFloat, double b, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var result = a * b;
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(10.1f, 2.5f, "4.04")]
|
|
public void FloatDivide(float aFloat, float b, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var result = a / b;
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(10.1f, 2.5d, "4.04")]
|
|
public void DoubleDivide(float aFloat, double b, string expected)
|
|
{
|
|
var a = FixedPoint2.New(aFloat);
|
|
var result = a / b;
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1, 0, false)]
|
|
[TestCase(0, 0, true)]
|
|
[TestCase(-1, 0, false)]
|
|
[TestCase(1, 1, true)]
|
|
[TestCase(0, 1, false)]
|
|
[TestCase(-1, 1, false)]
|
|
public void FixedPoint2Equals(int a, int b, bool expected)
|
|
{
|
|
var parameter = FixedPoint2.New(a);
|
|
var comparison = FixedPoint2.New(b);
|
|
Assert.That(parameter.Equals(comparison), Is.EqualTo(comparison.Equals(parameter)));
|
|
Assert.That(comparison.Equals(parameter), Is.EqualTo(expected));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(1.001f, "1.01")]
|
|
[TestCase(2f, "2")]
|
|
[TestCase(2.5f, "2.5")]
|
|
public void NewCeilingTest(float value, string expected)
|
|
{
|
|
var result = FixedPoint2.NewCeiling(value);
|
|
Assert.That($"{result}", Is.EqualTo(expected));
|
|
}
|
|
}
|
|
}
|