|
| 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.Extensions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Common |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// A piecewise whose case is an Iverson bracket or a provided integer compiles in the LINQ compiler, where it threw |
| 17 | + /// <c>KeyNotFoundException</c>: its case types, an <c>int?</c> and a <c>long?</c>, were not in the widening table. |
| 18 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2237">#2237</a> |
| 19 | + /// </summary> |
| 20 | + [Trait("Area", "Common")] |
| 21 | + public sealed class PiecewiseOfNullableCompilationTest |
| 22 | + { |
| 23 | + [Theory] |
| 24 | + [InlineData("sin(piecewise(iverson(x > 0) provided x > 0, sin(2)))", 2)] |
| 25 | + [InlineData("sin(piecewise(iverson(x > 0) provided x > 0, sin(2)))", -2)] |
| 26 | + [InlineData("exp(piecewise(2^x provided x > 0, iverson(x > 0)))", -1)] |
| 27 | + [InlineData("abs(piecewise((-1)^2 provided x > 0, iverson(-1 > 0)))", 1)] |
| 28 | + [InlineData("piecewise(iverson(sinh(3) > 0) provided x > 0, tan(-1))", 1)] |
| 29 | + [InlineData("piecewise(piecewise(-1 provided x > 0, 3) provided x > 0, x) + x", 1)] |
| 30 | + [InlineData("piecewise(piecewise(-1 provided x > 0, 3) provided x > 0, x) + x", -1)] |
| 31 | + public void ItIsWhatEvaluationGives(string expression, double at) |
| 32 | + { |
| 33 | + var expected = ((Entity.Number.Complex)expression.ToEntity().Substitute("x", at).Evaled).ToNumerics(); |
| 34 | + var linq = expression.ToEntity().Compile<Complex, Complex>("x")(at); |
| 35 | + var real = expression.ToEntity().Compile<double, double>("x")(at); |
| 36 | + Assert.True(Complex.Abs(expected - linq) < 1e-12, $"{expression} at {at}: {expected} evaluated, {linq} by LINQ"); |
| 37 | + Assert.True(System.Math.Abs(expected.Real - real) < 1e-12, $"{expression} at {at}: {expected} evaluated, {real} over double"); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments