|
| 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 AngouriMath; |
| 9 | +using AngouriMath.Extensions; |
| 10 | +using Xunit; |
| 11 | +using static AngouriMath.Entity; |
| 12 | +using static AngouriMath.Entity.Number; |
| 13 | + |
| 14 | +namespace AngouriMath.Tests.Convenience |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A negation of something printed with a sign of its own is bracketed, so it reads back as itself: <c>-(-2/3)</c> |
| 18 | + /// printed as <c>--2/3</c>, read back as <c>-(-2) / 3</c>, and <c>-(+oo)</c> as <c>-+oo</c>. |
| 19 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2264">#2264</a> |
| 20 | + /// </summary> |
| 21 | + [Trait("Area", "Convenience")] |
| 22 | + public sealed class NegationOfASignedTermPrintsTest |
| 23 | + { |
| 24 | + private static Entity Negated(Entity e) => new Mulf(Integer.Create(-1), e); |
| 25 | + |
| 26 | + public static TheoryData<Entity, string> Negations => new() |
| 27 | + { |
| 28 | + { Negated(Rational.Create(-2, 3)), "-(-2/3)" }, |
| 29 | + { Negated(Real.PositiveInfinity), "-(+oo)" }, |
| 30 | + { Negated(Negated(MathS.Var("x"))), "-(-x)" }, |
| 31 | + { Negated(Integer.Create(-3)), "-(-3)" }, |
| 32 | + { Negated(MathS.Var("x")), "-x" }, |
| 33 | + { Negated(Rational.Create(2, 3)), "-2/3" }, |
| 34 | + }; |
| 35 | + |
| 36 | + [Theory] |
| 37 | + [MemberData(nameof(Negations))] |
| 38 | + public void ItPrintsWithOneSignAndReadsBackToItsValue(Entity negation, string printed) |
| 39 | + { |
| 40 | + Assert.Equal(printed, negation.Stringize()); |
| 41 | + // The parser folds a negated number into the number, so -(-2/3) reads back as 2/3: the same value, and for |
| 42 | + // a symbol the same tree. |
| 43 | + var x = "7/10".ToEntity(); |
| 44 | + Assert.Equal(negation.Substitute("x", x).Evaled, negation.Stringize().ToEntity().Substitute("x", x).Evaled); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void AHyperbolicSineOfANegativeRationalHasNoDoubleSign() |
| 49 | + => Assert.DoesNotContain("--", "sinh(-5/4)".ToEntity().Stringize()); |
| 50 | + } |
| 51 | +} |
0 commit comments