|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System.Numerics; |
| 9 | +using AngouriMath; |
| 10 | +using AngouriMath.Core.Exceptions; |
| 11 | +using AngouriMath.Extensions; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace AngouriMath.Tests.Common |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A statement where a number is expected has no value, as evaluation leaves <c>sin(True)</c> as written, and both |
| 18 | + /// compilers refuse it. FastExpression answered with the truth value taken as a number, and the LINQ compiler threw |
| 19 | + /// <c>KeyNotFoundException</c> or <c>AngouriBugException</c>. <c>iverson</c> takes a statement as a number. |
| 20 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2227">#2227</a> |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Common")] |
| 23 | + public sealed class StatementInANumericPositionTest |
| 24 | + { |
| 25 | + [Theory] |
| 26 | + [InlineData("sin(x > 1)")] |
| 27 | + [InlineData("(x > 1)^2")] |
| 28 | + [InlineData("max(x > 1, 0)")] |
| 29 | + [InlineData("(x > 1) + 2")] |
| 30 | + [InlineData("2 * (x in [0; 1])")] |
| 31 | + [InlineData("(x > 1) = 2")] |
| 32 | + [InlineData("(x > 1) < 2")] |
| 33 | + [InlineData("sin(x > 1 provided x > 0)")] |
| 34 | + [InlineData("x^(3 divides x)")] |
| 35 | + [InlineData("piecewise(x > 1 provided x > 0, x) + 1")] |
| 36 | + public void IsRefused(string expression) |
| 37 | + { |
| 38 | + Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile("x")); |
| 39 | + Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile<double, double>("x")); |
| 40 | + Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile<Complex, Complex>("x")); |
| 41 | + } |
| 42 | + |
| 43 | + [Theory] |
| 44 | + [InlineData("iverson(x > 1) + 2", 2, 3)] |
| 45 | + [InlineData("iverson(x in ZZ) * x", 2, 2)] |
| 46 | + [InlineData("x > 1 and x < 3", 2, 1)] |
| 47 | + [InlineData("(x > 1) = (x < 3)", 4, 0)] |
| 48 | + [InlineData("not x > 1", 2, 0)] |
| 49 | + [InlineData("x provided x > 0", 2, 2)] |
| 50 | + [InlineData("piecewise(x provided x > 0, -x)", -2, 2)] |
| 51 | + [InlineData("x in { t : t > 0 }", 2, 1)] |
| 52 | + public void AStatementWhereOneIsTakenCompiles(string expression, double x, double expected) |
| 53 | + { |
| 54 | + Assert.Equal(new Complex(expected, 0), expression.ToEntity().Compile("x").Call(x)); |
| 55 | + Assert.Equal(new Complex(expected, 0), expression.ToEntity().Compile<Complex, Complex>("x")(x)); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments