Skip to content

Added boolean math sample unit tests #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions StringMath.Tests/BooleanExprTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using NUnit.Framework;
using System;

namespace StringMath.Tests
{
[TestFixture]
internal class BooleanExprTests
{
private MathContext _context;

[SetUp]
public void Setup()
{
MathExpr.AddVariable("true", 1);
MathExpr.AddVariable("false", 0);

_context = new MathContext();
_context.RegisterLogical("and", (a, b) => a && b, Precedence.Multiplication);
_context.RegisterLogical("or", (a, b) => a || b, Precedence.Addition);
_context.RegisterLogical(">", (a, b) => a > b, Precedence.Power);
_context.RegisterLogical("<", (a, b) => a < b, Precedence.Power);
_context.RegisterLogical("!", (a) => !a);
}

[Test]
public void Evaluate_Variable_Substitution()
{
MathExpr expr = new MathExpr("{a} and 1", _context);
Assert.IsFalse(expr.Substitute("a", false).EvalBoolean());
Assert.IsTrue(expr.Substitute("a", true).EvalBoolean());
}

[Test]
public void Evaluate_Boolean_Numbers()
{
bool expr = "1 and 1".EvalBoolean(_context);
Assert.IsTrue(expr);

bool result = "1 and 0 or !0 and 3 > 2".EvalBoolean(_context);
Assert.IsTrue(result);
}

[Test]
public void Evaluate_Globals_Variables()
{
Assert.IsTrue("{true} or {false} and {true}".EvalBoolean(_context));
Assert.IsTrue("{true} or {false}".EvalBoolean(_context));
Assert.IsFalse("{false} or {false}".EvalBoolean(_context));
Assert.IsFalse("{true} and {false}".EvalBoolean(_context));
Assert.IsTrue("{true} and {true}".EvalBoolean(_context));
}

[Test]
public void Evaluate_Binary_Operation()
{
_context.RegisterBinary("+", (a, b) => a + b);
Assert.IsTrue("(3 + 5) > 7".EvalBoolean(_context));
}
}

static class BooleanMathExtensions
{
public static bool EvalBoolean(this string value) => value.ToMathExpr().EvalBoolean();

public static bool EvalBoolean(this string value, IMathContext context) => value.ToMathExpr(context).EvalBoolean();

public static bool EvalBoolean(this MathExpr value) => value.Result != 0;

public static MathExpr Substitute(this MathExpr expr, string name, bool value)
=> expr.Substitute(name, value is true ? 1 : 0);

public static void RegisterLogical(this IMathContext context, string operatorName, Func<bool, bool, bool> operation, Precedence? precedence)
=> context.RegisterBinary(operatorName, (a, b) => operation(a != 0, b != 0) ? 1 : 0, precedence);

public static void RegisterLogical(this IMathContext context, string operatorName, Func<double, double, bool> operation, Precedence? precedence)
=> context.RegisterBinary(operatorName, (a, b) => operation(a, b) ? 1 : 0, precedence);

public static void RegisterLogical(this IMathContext context, string operatorName, Func<bool, bool> operation)
=> context.RegisterUnary(operatorName, (a) => operation(a != 0) ? 1 : 0);
}
}