|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace StringMath.Tests |
| 5 | +{ |
| 6 | + [TestFixture] |
| 7 | + internal class BooleanExprTests |
| 8 | + { |
| 9 | + private MathContext _context; |
| 10 | + |
| 11 | + [SetUp] |
| 12 | + public void Setup() |
| 13 | + { |
| 14 | + MathExpr.AddVariable("true", 1); |
| 15 | + MathExpr.AddVariable("false", 0); |
| 16 | + |
| 17 | + _context = new MathContext(); |
| 18 | + _context.RegisterLogical("and", (a, b) => a && b, Precedence.Multiplication); |
| 19 | + _context.RegisterLogical("or", (a, b) => a || b, Precedence.Addition); |
| 20 | + _context.RegisterLogical(">", (a, b) => a > b, Precedence.Power); |
| 21 | + _context.RegisterLogical("<", (a, b) => a < b, Precedence.Power); |
| 22 | + _context.RegisterLogical("!", (a) => !a); |
| 23 | + } |
| 24 | + |
| 25 | + [Test] |
| 26 | + public void Evaluate_Variable_Substitution() |
| 27 | + { |
| 28 | + MathExpr expr = new MathExpr("{a} and 1", _context); |
| 29 | + Assert.IsFalse(expr.Substitute("a", false).EvalBoolean()); |
| 30 | + Assert.IsTrue(expr.Substitute("a", true).EvalBoolean()); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public void Evaluate_Boolean_Numbers() |
| 35 | + { |
| 36 | + bool expr = "1 and 1".EvalBoolean(_context); |
| 37 | + Assert.IsTrue(expr); |
| 38 | + |
| 39 | + bool result = "1 and 0 or !0 and 3 > 2".EvalBoolean(_context); |
| 40 | + Assert.IsTrue(result); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void Evaluate_Globals_Variables() |
| 45 | + { |
| 46 | + Assert.IsTrue("{true} or {false} and {true}".EvalBoolean(_context)); |
| 47 | + Assert.IsTrue("{true} or {false}".EvalBoolean(_context)); |
| 48 | + Assert.IsFalse("{false} or {false}".EvalBoolean(_context)); |
| 49 | + Assert.IsFalse("{true} and {false}".EvalBoolean(_context)); |
| 50 | + Assert.IsTrue("{true} and {true}".EvalBoolean(_context)); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void Evaluate_Binary_Operation() |
| 55 | + { |
| 56 | + _context.RegisterBinary("+", (a, b) => a + b); |
| 57 | + Assert.IsTrue("(3 + 5) > 7".EvalBoolean(_context)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static class BooleanMathExtensions |
| 62 | + { |
| 63 | + public static bool EvalBoolean(this string value) => value.ToMathExpr().EvalBoolean(); |
| 64 | + |
| 65 | + public static bool EvalBoolean(this string value, IMathContext context) => value.ToMathExpr(context).EvalBoolean(); |
| 66 | + |
| 67 | + public static bool EvalBoolean(this MathExpr value) => value.Result != 0; |
| 68 | + |
| 69 | + public static MathExpr Substitute(this MathExpr expr, string name, bool value) |
| 70 | + => expr.Substitute(name, value is true ? 1 : 0); |
| 71 | + |
| 72 | + public static void RegisterLogical(this IMathContext context, string operatorName, Func<bool, bool, bool> operation, Precedence? precedence) |
| 73 | + => context.RegisterBinary(operatorName, (a, b) => operation(a != 0, b != 0) ? 1 : 0, precedence); |
| 74 | + |
| 75 | + public static void RegisterLogical(this IMathContext context, string operatorName, Func<double, double, bool> operation, Precedence? precedence) |
| 76 | + => context.RegisterBinary(operatorName, (a, b) => operation(a, b) ? 1 : 0, precedence); |
| 77 | + |
| 78 | + public static void RegisterLogical(this IMathContext context, string operatorName, Func<bool, bool> operation) |
| 79 | + => context.RegisterUnary(operatorName, (a) => operation(a != 0) ? 1 : 0); |
| 80 | + } |
| 81 | +} |
0 commit comments