|
| 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; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using System.Reflection; |
| 12 | +using AngouriMath; |
| 13 | +using AngouriMath.Extensions; |
| 14 | + |
| 15 | +namespace CrashCheck; |
| 16 | + |
| 17 | +/// <summary>What is run against what. A case is one expression put through one operation.</summary> |
| 18 | +sealed record Case(string Expression, string Operation) |
| 19 | +{ |
| 20 | + public override string ToString() => $"{Operation}::{Expression}"; |
| 21 | +} |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// The case list, built rather than written down. |
| 25 | +/// </summary> |
| 26 | +/// <remarks> |
| 27 | +/// The expressions come from the <see cref="Entity"/> node types found by reflection, so a node |
| 28 | +/// added later is covered without anyone having to remember this file -- which is the whole |
| 29 | +/// reason a node's first appearance in a release has twice been the thing that crashed. They are |
| 30 | +/// then composed to a small depth, because what kills the process is nearly always a |
| 31 | +/// composition rather than a leaf: the recorded stack overflow was integration by parts feeding |
| 32 | +/// itself, not any one node. |
| 33 | +/// </remarks> |
| 34 | +static class Cases |
| 35 | +{ |
| 36 | + /// The operations a case can name. Each is a whole pipeline rather than a single rule. |
| 37 | + public static readonly string[] Operations = |
| 38 | + { |
| 39 | + "simplify", "expand", "factorize", "eval", "innersimplify", |
| 40 | + "differentiate", "integrate", "limit", "solve", "alternate", |
| 41 | + "stringize-roundtrip", "latexize", "compile", "domain", |
| 42 | + }; |
| 43 | + |
| 44 | + /// <summary>Every concrete <see cref="Entity"/> node the library has, one instance each, |
| 45 | + /// printed so that the child process can parse it back from its argument.</summary> |
| 46 | + public static List<string> NodeInstances(out List<string> uninstantiable) |
| 47 | + { |
| 48 | + var leaves = new Entity[] { "x", 2, "1/2", "-3", "y", MathS.pi, MathS.i }; |
| 49 | + var printed = new List<string>(); |
| 50 | + uninstantiable = new List<string>(); |
| 51 | + |
| 52 | + foreach (var type in ConcreteEntityTypes()) |
| 53 | + { |
| 54 | + var built = false; |
| 55 | + // The shortest constructor first: a node wants as few arguments as it can take, |
| 56 | + // and the ones with extra flags -- an interval's open/closed pair, for instance -- |
| 57 | + // are covered by the written shapes below instead. |
| 58 | + foreach (var constructor in type.GetConstructors() |
| 59 | + .OrderBy(c => c.GetParameters().Length)) |
| 60 | + { |
| 61 | + var parameters = constructor.GetParameters(); |
| 62 | + if (parameters.Length == 0 || parameters.Length > 3) continue; |
| 63 | + if (!parameters.All(p => p.ParameterType == typeof(Entity) |
| 64 | + || typeof(Entity).IsAssignableFrom(p.ParameterType))) continue; |
| 65 | + try |
| 66 | + { |
| 67 | + var arguments = parameters |
| 68 | + .Select((p, i) => Convert(leaves[i % leaves.Length], p.ParameterType)) |
| 69 | + .ToArray(); |
| 70 | + if (arguments.Any(a => a is null)) continue; |
| 71 | + var node = (Entity)constructor.Invoke(arguments); |
| 72 | + var text = node.Stringize(); |
| 73 | + // It has to survive the round trip through text, since that is how it |
| 74 | + // reaches the child. A node whose printed form does not parse is itself a |
| 75 | + // finding, and StringizeRoundTripTest in the library is where that belongs. |
| 76 | + _ = text.ToEntity(); |
| 77 | + printed.Add(text); |
| 78 | + built = true; |
| 79 | + break; |
| 80 | + } |
| 81 | + catch |
| 82 | + { |
| 83 | + // Try the next constructor. |
| 84 | + } |
| 85 | + } |
| 86 | + if (!built) uninstantiable.Add(type.Name); |
| 87 | + } |
| 88 | + return printed.Distinct().ToList(); |
| 89 | + } |
| 90 | + |
| 91 | + static object Convert(Entity leaf, Type wanted) |
| 92 | + { |
| 93 | + if (wanted == typeof(Entity)) return leaf; |
| 94 | + if (wanted == typeof(Entity.Variable)) return (Entity.Variable)"x"; |
| 95 | + try |
| 96 | + { |
| 97 | + return wanted.IsInstanceOfType(leaf) ? leaf : null; |
| 98 | + } |
| 99 | + catch |
| 100 | + { |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static IEnumerable<Type> ConcreteEntityTypes() |
| 106 | + { |
| 107 | + var pending = new Queue<Type>(); |
| 108 | + pending.Enqueue(typeof(Entity)); |
| 109 | + while (pending.Count > 0) |
| 110 | + { |
| 111 | + var current = pending.Dequeue(); |
| 112 | + foreach (var nested in current.GetNestedTypes(BindingFlags.Public)) |
| 113 | + { |
| 114 | + pending.Enqueue(nested); |
| 115 | + if (!nested.IsAbstract && typeof(Entity).IsAssignableFrom(nested)) |
| 116 | + yield return nested; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Shapes that a constructor cannot reach, or that are worth having because something has |
| 123 | + /// already died on their kind: recursion that feeds itself, a binder, a set builder, a |
| 124 | + /// condition, and the deep nesting that turns a linear pass into an exponential one. |
| 125 | + /// </summary> |
| 126 | + public static readonly string[] WrittenShapes = |
| 127 | + { |
| 128 | + // Integration by parts feeding itself is the recorded crash. |
| 129 | + "x * ln(x)", "x2 * ln(x)", "x3 * ln(x)", "x * e ^ x", "x2 * sin(x)", |
| 130 | + "x ^ 4 * ln(x) * sin(x)", "ln(ln(ln(x)))", "e ^ e ^ e ^ x", |
| 131 | + // A rewrite whose output re-matches its own pattern. |
| 132 | + "1 / (1 / (1 / x))", "sqrt(sqrt(sqrt(x)))", "((x ^ 2) ^ 2) ^ 2", |
| 133 | + "(x + 1) ^ 8", "(x + y + 1) ^ 5", "(a + b + c + d) ^ 4", |
| 134 | + // The mathematical constants, free and bound. A bound one is a variable and a free one is |
| 135 | + // not, which is what Entity.Constant is for -- and only the free one is reachable by |
| 136 | + // construction, so the node type is otherwise uncovered here. #984 |
| 137 | + "pi", "e", "sin(pi) + ln(e)", "sum(pi, pi, 1, 3)", "derivative(e ^ 2, e)", "{ e : e > 0 }", |
| 138 | + // Binders and conditions, where #878 found a condition escaping its scope. |
| 139 | + "{ x : x > 0 }", "{ x : x = x }", "{ x : 1 = 1 }", "{ x : sin(x) > 0 and x in RR }", |
| 140 | + "x provided x > 0", "x provided false", "(x provided x > 0) + 1", |
| 141 | + "piecewise(1 provided x > 0, 2 provided x <= 0)", |
| 142 | + // Sets and intervals. |
| 143 | + "{ 1, 2, 3 }", "[0; 1]", "(0; 1)", "RR", "CC", "ZZ", "QQ", "BB", "{ 1, 2 } \\/ [0; 5]", |
| 144 | + "x in ZZ", "x in QQ", "x in BB", "[0; 1) \\/ (1; 2]", |
| 145 | + "{ 1, 2 } /\\ [0; 5]", "{ x : x in RR } \\ { 0 }", |
| 146 | + // Matrices, including the shapes that made Simplify stop descending. |
| 147 | + "[[1, 2], [3, 4]]", "[1, 2, 3]", "[[x, y], [y, x]]", |
| 148 | + // Lambdas and applications, added late and with no parser support for years. |
| 149 | + "x -> x + 1", "(x -> x + 1) applied to 2", |
| 150 | + // Nodes whose evaluation reaches for a limit, which reaches for evaluation. |
| 151 | + "limit(x!, x, +oo)", "limit(x ^ x, x, 0)", "limit(floor(x), x, 0)", |
| 152 | + "limit(sgn(x) / x, x, 0)", "derivative(abs(x), x)", "integral(1 / x, x)", |
| 153 | + "integral(integral(x, x), x)", "derivative(derivative(sin(x), x), x)", |
| 154 | + // The power form of the derivative, where a derivative that cannot be taken survives |
| 155 | + // every pass. Derivativef's simplification decides by asking for the derivative and |
| 156 | + // keeping the node when a Derivativef comes back -- so anything that simplifies on the |
| 157 | + // way to that test asks the same node again and never stops. Cost me a stack overflow |
| 158 | + // at 3214 frames. https://github.com/asc-community/AngouriMath/issues/1002 |
| 159 | + "derivative(x!, x, 2)", "derivative(x!, x, 3)", "derivative(sin(x!) + x, x, 2)", |
| 160 | + "derivative(apply(f, x), x, -1)", "derivative(apply(f, x), x, -2)", |
| 161 | + "derivative(abs(x), x, 2)", "derivative(sgn(x), x, 2)", |
| 162 | + // The degenerate arithmetic that a rule's assumption is usually about. |
| 163 | + "0 ^ 0", "1 ^ (+oo)", "(+oo) - (+oo)", "0 * (+oo)", "log(1, 1)", "x / x", "x - x", |
| 164 | + "0 / 0", "(-8) ^ (1/3)", "(-1) ^ (1/2)", "abs(sgn(x))", |
| 165 | + // Factorials and gamma, which have their own recursion. |
| 166 | + "x!", "(x + 1)! / x!", "(x!)!", "gamma(gamma(x))", |
| 167 | + // Big and small numbers, where the decimal context saturates. |
| 168 | + "2 ^ 1000", "2 ^ (-1000)", "10 ^ 10000", "1 / 10 ^ 200", |
| 169 | + // Booleans, where excluded middle was decided wrongly. |
| 170 | + "x > 0 or x <= 0", "not (x > 0) or (x > 0)", "a and not a", "a xor a", |
| 171 | + }; |
| 172 | +} |
0 commit comments