Files
AI-Studio/app/Tests/Tools/Mathematics/MathGuardsTests.cs
T

102 lines
6.6 KiB
C#
Raw Normal View History

using AIStudio.Tools.Mathematics;
using AngouriMath;
namespace AIStudio.Tests.Tools.Mathematics;
/// <summary>
/// Checks that the guards refuse what AngouriMath could not stop, and nothing it computes quickly.
/// </summary>
/// <remarks>
/// None of the refused expressions is computed here: each of them would run on long past the test.
/// </remarks>
[TestFixture]
public sealed class MathGuardsTests
{
private static Entity Read(string text) => MathExpressionReader.Read(text, "expression");
private static string ExactSizeRefusal(string text) => Assert.Throws<MathComputationException>(() => MathGuards.CheckExactSize(Read(text)))!.Message;
[Test]
public void RefusesAPowerWithTooManyDigits()
{
using (Assert.EnterMultipleScope())
{
Assert.That(ExactSizeRefusal("9^9^9"), Is.EqualTo("'9 ^ 9 ^ 9' would have about 369,693,100 digits as an exact number; its decimal logarithm is about 369693099.632. The tool computes such a number in full and cannot stop midway, so exact numbers are limited to 20,000 digits."));
Assert.That(ExactSizeRefusal("9^9^9^9"), Does.StartWith("'9 ^ 9 ^ 9' would have"), "The inner power is judged first, before its value is needed.");
Assert.That(ExactSizeRefusal("1 + 10^20001"), Does.StartWith("'10 ^ 20001' would have about 20,001 digits"));
Assert.That(ExactSizeRefusal("(1/2)^100000"), Does.Contain("would have about 30,103 digits"), "Numerator and denominator count.");
Assert.That(ExactSizeRefusal("sqrt(2)^1000000"), Does.Contain("would have about"), "sqrt(2)^1000000 is the exact number 2^500000.");
Assert.That(ExactSizeRefusal("x^(9^9^9)"), Does.StartWith("'9 ^ 9 ^ 9' would have"), "An exponent is a number even when the base is a variable.");
Assert.That(ExactSizeRefusal("(e^0 + 1)^(10^9)"), Does.Contain("would have about 301,029,996 digits"), "A constant is no variable: the base is the number 2.");
}
}
[Test]
public void RefusesAFactorialWithTooManyDigits()
{
using (Assert.EnterMultipleScope())
{
Assert.That(ExactSizeRefusal("(10^9)!"), Does.StartWith("'(10 ^ 9)!' would have about 8,565,705,"));
Assert.That(ExactSizeRefusal("10000!"), Does.StartWith("'10000!' would have about 35,660 digits"));
Assert.That(ExactSizeRefusal("(10!)!"), Does.StartWith("'(10!)!' would have about"));
}
}
2026-11-25 18:45:24 +01:00
[Test]
public void RefusesSpecialFunctionsBeyondTheirLimits()
{
using (Assert.EnterMultipleScope())
{
Assert.That(ExactSizeRefusal("polygamma(101, 2)"), Is.EqualTo("'polygamma(101, 2)' has the order 101. The tool cannot stop the computation of polygamma midway, and its time grows with the order, so the order is limited to 100."));
Assert.That(ExactSizeRefusal("1 + polygamma(10^3, x)"), Does.StartWith("'polygamma(10 ^ 3, x)' has the order 1000."), "The order is a number even when the argument is a variable.");
Assert.That(ExactSizeRefusal("polygamma((10^9)!, 2)"), Does.StartWith("'(10 ^ 9)!' would have about"), "The order is judged before its value is needed.");
Assert.That(ExactSizeRefusal("gamma(1/2, 1001)"), Is.EqualTo("'gamma(1 / 2, 1001)' takes gamma(a, z) at a z of about 1001 in absolute value. The tool cannot stop this computation midway, and its time grows with z, so the absolute value of z is limited to 1,000."));
Assert.That(ExactSizeRefusal("gamma(1/2, 10 + 1000*i)"), Does.Contain("at a z of about 1000.05 in absolute value"));
Assert.That(ExactSizeRefusal("gamma(401/2, 2)"), Is.EqualTo("'gamma(401 / 2, 2)' takes gamma(a, z) for a = 401/2. The tool cannot stop this computation midway, and its time grows with z, the more so when a is negative or not real, so a is limited to the real numbers from -20 to 200."));
Assert.That(ExactSizeRefusal("gamma(-41/2, 2)"), Does.Contain("for a = -41/2."));
Assert.That(ExactSizeRefusal("gamma(1 + i, x)"), Does.Contain("for a = 1 + i."), "The parameter is a number even when z is a variable.");
Assert.That(() => MathGuards.CheckValueAtPoint(Read("2 * gamma(1/2, 3001/3)")), Throws.InstanceOf<MathComputationException>());
}
}
[Test]
public void RefusesAtAPointWhatAValueMakesTooLarge()
{
using (Assert.EnterMultipleScope())
{
// (10^6 * x)! and x^100000 at x = 0.61, as at a sample point of the checks:
Assert.That(Assert.Throws<MathComputationException>(() => MathGuards.CheckValueAtPoint(Read("1 + (10^6 * 61/100)!")))!.Message, Does.StartWith("'(10 ^ 6 * 61 / 100)!' would have about 3,"));
Assert.That(Assert.Throws<MathComputationException>(() => MathGuards.CheckValueAtPoint(Read("(61/100)^100000")))!.Message, Does.StartWith("'(61 / 100) ^ 100000' would have about 378,"));
Assert.That(() => MathGuards.CheckValueAtPoint(Read("polygamma(1000 * 61/100, 2)")), Throws.InstanceOf<MathComputationException>());
// The terms of a sum count one by one, unlike for CheckExactSize:
Assert.DoesNotThrow(() => MathGuards.CheckValueAtPoint(Read("10^15000 + 3 * 10^19000 + (1000 * 223/100)!")));
Assert.That(ExactSizeRefusal("10^15000 + 3 * 10^19000"), Does.Contain("would have about 34,"));
}
}
[Test]
public void LetsThroughWhatIsComputedQuickly()
{
using (Assert.EnterMultipleScope())
{
2026-11-25 18:45:24 +01:00
foreach (var text in new[] { "2^10000", "10^19999", "5000!", "0.5!", "(3!)!", "x^1000000", "2^x", "pi^1000000", "e^(10^6)", "1^(10^100)", "0^(10^100)", "547 / 57576", "integral(x^3/(e^x-1), x, 0, +oo)", "polygamma(100, 1/3)", "digamma(10^9)", "polygamma(n, x)", "gamma(200, 1000)", "gamma(-20, -1000)", "gamma(-20, 1000*i)", "gamma(x, 10^3)", "gamma(a, 1/2)", "lambertw(10^6, 2)" })
Assert.DoesNotThrow(() => MathGuards.CheckExactSize(Read(text)), text);
}
}
[Test]
public void RefusesTheExpansionOfAHighPowerOfASum()
{
using (Assert.EnterMultipleScope())
{
var refusal = Assert.Throws<MathComputationException>(() => MathGuards.CheckExpansion(Read("1 + (x + y)^500")))!;
Assert.That(refusal.Message, Is.EqualTo("'(x + y) ^ 500' raises a sum to the power of 500. Where the tool may expand, a sum is raised to exponents up to 200 only, since it cannot stop a larger expansion midway."));
Assert.Throws<MathComputationException>(() => MathGuards.CheckExpansion(Read("(x - 1)^(-300)")));
foreach (var text in new[] { "(x + y)^200", "x^500", "(x * y)^500", "(x + y)^n" })
Assert.DoesNotThrow(() => MathGuards.CheckExpansion(Read(text)), text);
}
}
}