-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from miroiu/master
Merge into release branch
- Loading branch information
Showing
30 changed files
with
455 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# These are supported funding model platforms | ||
|
||
github: miroiu | ||
custom: ["https://www.buymeacoffee.com/miroiu", "https://paypal.me/miroiuemanuel"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ jobs: | |
3.1.x | ||
5.0.x | ||
6.0.x | ||
8.0.x | ||
- name: Restore dependencies | ||
run: dotnet restore | ||
- name: Build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
|
||
namespace StringMath.Benchmarks | ||
{ | ||
[MemoryDiagnoser(true)] | ||
[SimpleJob(RuntimeMoniker.Net80, warmupCount: 0, iterationCount: 1, launchCount: 1)] | ||
public class Benchmarks | ||
{ | ||
[Benchmark] | ||
public void Tokenize() | ||
{ | ||
var tokenizer = new Tokenizer("1.23235456576878798 - ((3 + {b}) max .1) ^ sqrt(-999 / 2 * 3 max 5) + !5 - 0.00000000002 / {ahghghh}"); | ||
|
||
Token token; | ||
|
||
do | ||
{ | ||
token = tokenizer.ReadToken(); | ||
} | ||
while (token.Type != TokenType.EndOfCode); | ||
} | ||
|
||
[Benchmark] | ||
public void Parse() | ||
{ | ||
var tokenizer = new Tokenizer("1.23235456576878798 - ((3 + {b}) max .1) ^ sqrt(-999 / 2 * 3 max 5) + !5 - 0.00000000002 / {ahghghh}"); | ||
var parser = new Parser(tokenizer, MathContext.Default); | ||
_ = parser.Parse(); | ||
} | ||
|
||
[Benchmark] | ||
public double Evaluate() | ||
{ | ||
return "1.23235456576878798 - ((3 + {b}) max .1) ^ sqrt(-999 / 2 * 3 max 5) + !5 - 0.00000000002 / {ahghghh}".ToMathExpr().Substitute("b", 12989d).Substitute("ahghghh", 12345d).Result; | ||
} | ||
|
||
[Benchmark] | ||
public double Compile() | ||
{ | ||
var fn = "1.23235456576878798 - ((3 + {b}) max .1) ^ sqrt(-999 / 2 * 3 max 5) + !5 - 0.00000000002 / {ahghghh}".ToMathExpr().Substitute("b", 12989d).Substitute("ahghghh", 12345d).Compile("ahghghh"); | ||
return fn(12345d); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using BenchmarkDotNet.Running; | ||
using StringMath.Benchmarks; | ||
|
||
BenchmarkRunner.Run<Benchmarks>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Optimize>true</Optimize> | ||
<SignAssembly>true</SignAssembly> | ||
|
||
<AssemblyOriginatorKeyFile>../build/string-math.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\StringMath\StringMath.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.