Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
un-tone committed Sep 8, 2020
1 parent df5f30d commit f53e94d
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Calculator application

![Test](https://github.com/un-tone/HtnCalc/workflows/Test/badge.svg)

## How to use

Run _htncalc.exe_ and input expressions for calculation. For exit input "q" or "quit" command.
Expand Down
1 change: 1 addition & 0 deletions src/HtnCalc.Core/ExpressionTree/ExpressionTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private IExpressionNode BuildExpressionNode(LinkedListNode<ITerm> termNode)
return term switch
{
ArithmeticOperatorToken arithmeticOperationToken => BuildArithmeticExpression(termNode, arithmeticOperationToken.OperationType),
CompoundTerm compoundTerm => BuildExpressionTree(compoundTerm),
_ => BuildLiteralExpressionNode(term)
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/HtnCalc.Core/Permutation/AddSubTermPermuter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using HtnCalc.Core.Parsing;

namespace HtnCalc.Core.Permutation
Expand Down
3 changes: 1 addition & 2 deletions src/HtnCalc.Core/Permutation/BracketTermPermuter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using HtnCalc.Core.Parsing;

namespace HtnCalc.Core.Permutation
Expand Down
3 changes: 1 addition & 2 deletions src/HtnCalc.Core/Permutation/FunctionTermPermuter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using HtnCalc.Core.Parsing;

namespace HtnCalc.Core.Permutation
Expand Down
3 changes: 1 addition & 2 deletions src/HtnCalc.Core/Permutation/MulDivTermPermuter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using HtnCalc.Core.Parsing;

namespace HtnCalc.Core.Permutation
Expand Down
28 changes: 28 additions & 0 deletions test/HtnCalc.Core.UnitTests/AbsFunctionExpressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HtnCalc.Core.ExpressionTree;
using Xunit;

namespace HtnCalc.Core.UnitTests
{
public class AbsFunctionExpressionTests
{
[Theory]
[InlineData(3, 3)]
[InlineData(-1.1756, 1.1756)]
public void Calculate(decimal arg, decimal expected)
{
var expression = new AbsFunctionExpression(new NumberExpression(arg));
var result = expression.Calculate();
Assert.Equal(expected, result);
}

[Theory]
[InlineData(3, 5, 8)]
[InlineData(1.1756, 138.066, 139.2416)]
public void Calculate_Compound(decimal left, decimal right, decimal expected)
{
var expression = new AbsFunctionExpression(new AdditionExpression(new NumberExpression(left), new NumberExpression(right)));
var result = expression.Calculate();
Assert.Equal(expected, result);
}
}
}
38 changes: 38 additions & 0 deletions test/HtnCalc.Core.UnitTests/AddSubTermPermuterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using HtnCalc.Core.Parsing;
using HtnCalc.Core.Permutation;
using Xunit;
using static Xunit.Assert;

namespace HtnCalc.Core.UnitTests
{
public class AddSubTermPermuterTests
{
[Fact]
public void Permute()
{
var permuter = new AddSubTermPermuter();
var list = new LinkedList<ITerm>(new ITerm[]
{
new NumberToken('3', 1),
new ArithmeticOperatorToken(ArithmeticOperationType.Addition, 3),
new NumberToken('5', 5),
new ArithmeticOperatorToken(ArithmeticOperationType.Subtraction, 6),
new NumberToken('8', 7),
});

var result = permuter.Permute(list);

IsType<CompoundTerm>(result);
Equal(3, result.Terms.Count);
IsType<NumberToken>(result.Terms.First?.Value);
IsType<ArithmeticOperatorToken>(result.Terms.First?.Next?.Value);
IsType<CompoundTerm>(result.Terms.First?.Next?.Next?.Value);
var compoundTerm = (CompoundTerm) result.Terms.First?.Next?.Next?.Value;
Equal(3, result.Terms.Count);
IsType<NumberToken>(compoundTerm.Terms.First?.Value);
IsType<ArithmeticOperatorToken>(compoundTerm.Terms.First?.Next?.Value);
IsType<NumberToken>(compoundTerm.Terms.First?.Next?.Next?.Value);
}
}
}
12 changes: 12 additions & 0 deletions test/HtnCalc.Core.UnitTests/CalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class CalculatorTests
[InlineData("(4) + abs(5) + (1 + 1)", 11)]
[InlineData("abs(5)", 5)]
[InlineData("((abs(5)))", 5)]
[InlineData("0 / 8", 0)]
[InlineData("10 - 15", -5)]
public void Calculate(string input, decimal output)
{
var result = _calculator.Calculate(input);
Expand All @@ -38,5 +40,15 @@ public void Calculate_CalculationException(string input)
{
Assert.Throws<CalculationException>(() => _calculator.Calculate(input));
}

[Theory]
[InlineData("5 * (0")]
[InlineData("10 + 3 -")]
[InlineData("5 ** 3")]
[InlineData("1 + abs((1+2)")]
public void Calculate_ParsingException(string input)
{
Assert.Throws<ParsingException>(() => _calculator.Calculate(input));
}
}
}
42 changes: 42 additions & 0 deletions test/HtnCalc.Core.UnitTests/ExpressionTreeBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using HtnCalc.Core.ExpressionTree;
using HtnCalc.Core.Parsing;
using HtnCalc.Core.Permutation;
using Xunit;
using static Xunit.Assert;

namespace HtnCalc.Core.UnitTests
{
public class ExpressionTreeBuilderTests
{
[Fact]
public void BuildExpressionTree_SimpleArithmetic()
{
var builder = new ExpressionTreeBuilder();
var term = new CompoundTerm(new LinkedList<ITerm>(new ITerm[]
{
new NumberToken('3', 1),
new ArithmeticOperatorToken(ArithmeticOperationType.Addition, 2),
new NumberToken('5', 3)
}));

var result = builder.BuildExpressionTree(term);

IsType<AdditionExpression>(result);
var additionExpression = (AdditionExpression) result;
IsType<NumberExpression>(additionExpression.Left);
IsType<NumberExpression>(additionExpression.Right);
}

[Fact]
public void BuildExpressionTree_SimpleNumber()
{
var builder = new ExpressionTreeBuilder();
var term = new NumberToken('3', 1);

var result = builder.BuildExpressionTree(term);

IsType<NumberExpression>(result);
}
}
}
40 changes: 40 additions & 0 deletions test/HtnCalc.Core.UnitTests/FunctionTermPermuterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using HtnCalc.Core.Parsing;
using HtnCalc.Core.Permutation;
using Xunit;
using static Xunit.Assert;

namespace HtnCalc.Core.UnitTests
{
public class FunctionTermPermuterTests
{
[Fact]
public void Permute()
{
var permuter = new FunctionTermPermuter();
var list = new LinkedList<ITerm>(new ITerm[]
{
new NumberToken('2', 1),
new ArithmeticOperatorToken(ArithmeticOperationType.Addition, 2),
new AbsFunctionToken(4),
new OpenBracketToken(8),
new NumberToken('8', 9),
new CloseBracketToken(12),
});

var result = permuter.Permute(list);

IsType<CompoundTerm>(result);
Equal(3, result.Terms.Count);
IsType<NumberToken>(result.Terms.First?.Value);
IsType<ArithmeticOperatorToken>(result.Terms.First?.Next?.Value);
IsType<CompoundTerm>(result.Terms.First?.Next?.Next?.Value);
var compoundTerm = (CompoundTerm) result.Terms.First?.Next?.Next?.Value;
Equal(4, compoundTerm.Terms.Count);
IsType<AbsFunctionToken>(compoundTerm.Terms.First?.Value);
IsType<OpenBracketToken>(compoundTerm.Terms.First?.Next?.Value);
IsType<NumberToken>(compoundTerm.Terms.First?.Next?.Next?.Value);
IsType<CloseBracketToken>(compoundTerm.Terms.First?.Next?.Next?.Next?.Value);
}
}
}
38 changes: 38 additions & 0 deletions test/HtnCalc.Core.UnitTests/MulDivTermPermuterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using HtnCalc.Core.Parsing;
using HtnCalc.Core.Permutation;
using Xunit;
using static Xunit.Assert;

namespace HtnCalc.Core.UnitTests
{
public class MulDivTermPermuterTests
{
[Fact]
public void Permute()
{
var permuter = new MulDivTermPermuter();
var list = new LinkedList<ITerm>(new ITerm[]
{
new NumberToken('3', 1),
new ArithmeticOperatorToken(ArithmeticOperationType.Multiplication, 3),
new NumberToken('5', 5),
new ArithmeticOperatorToken(ArithmeticOperationType.Multiplication, 6),
new NumberToken('8', 7),
});

var result = permuter.Permute(list);

IsType<CompoundTerm>(result);
Equal(3, result.Terms.Count);
IsType<CompoundTerm>(result.Terms.First?.Value);
IsType<ArithmeticOperatorToken>(result.Terms.First?.Next?.Value);
IsType<NumberToken>(result.Terms.First?.Next?.Next?.Value);
var compoundTerm = (CompoundTerm) result.Terms.First?.Value;
Equal(3, result.Terms.Count);
IsType<NumberToken>(compoundTerm.Terms.First?.Value);
IsType<ArithmeticOperatorToken>(compoundTerm.Terms.First?.Next?.Value);
IsType<NumberToken>(compoundTerm.Terms.First?.Next?.Next?.Value);
}
}
}

0 comments on commit f53e94d

Please sign in to comment.