Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Barbul committed Jun 10, 2022
1 parent eec72d1 commit 079114d
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/Calculator.Collections/MyStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private void GrowIfNeeded()

private void Grow()
{
int newSize = this.capacity * 2;
this.capacity *= 2;
int newSize = this.capacity;
Array.Resize(ref this.items, newSize);
}
}
2 changes: 1 addition & 1 deletion samples/Calculator.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Calculator.ConsoleApp;

public class Program
public static class Program
{
public static void Main(string[] args)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator.Core/Operators/CommaOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override Token Execute(IReadOnlyList<Token> operands)
}
else
{
listOperand = new((Operand)operands[0], (Operand)operands[1]);
listOperand = new ListOperand((Operand)operands[0], (Operand)operands[1]);
}

return listOperand;
Expand Down
15 changes: 4 additions & 11 deletions src/Calculator.Core/Parsers/FunctionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public bool TryParse(ReadOnlySpan<char> formula, ParsingContext context, [NotNul
token = null;
parsedLength = default;

Token? foundFunction = null;

if (formula.IsEmpty)
{
return false;
Expand All @@ -36,18 +34,13 @@ public bool TryParse(ReadOnlySpan<char> formula, ParsingContext context, [NotNul
{
if (formula.StartsWith(text, StringComparison.Ordinal))
{
foundFunction = function;
token = function;
parsedLength = text.Length;
}
}

if (foundFunction == null)
{
return false;
return true;
}
}

token = foundFunction;

return true;
return false;
}
}
3 changes: 1 addition & 2 deletions src/Calculator.Core/Tokens/Function.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using Calculator.Core.Operands;
using Calculator.Core.Operands;

namespace Calculator.Core.Tokens;

Expand Down
1 change: 0 additions & 1 deletion src/Calculator.Extra/Enums/OperationPriority.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ public enum OperationPriority
Divide = 2,
Or = 3,
And = 4,
Unary = 5,
}
2 changes: 1 addition & 1 deletion src/Calculator.Extra/Functions/MaxFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override Token ExecuteFunction(IReadOnlyList<Operand> operands)

for (int i = 1; i < operands.Count; i++)
{
Operand<decimal> operand = ((Operand<decimal>)operands[i]);
Operand<decimal> operand = (Operand<decimal>)operands[i];
decimal value = operand.Value;
if (value > result.Value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator.Extra/Functions/MinFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override Token ExecuteFunction(IReadOnlyList<Operand> operands)

for (int i = 1; i < operands.Count; i++)
{
Operand<decimal> operand = ((Operand<decimal>)operands[i]);
Operand<decimal> operand = (Operand<decimal>)operands[i];
decimal value = operand.Value;
if (value < result.Value)
{
Expand Down
12 changes: 7 additions & 5 deletions tests/Calculator.Core.Tests/Utilities/IntParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public bool TryParse(ReadOnlySpan<char> formula, ParsingContext context, [NotNul
}
}

if (i > 0)
if (i == 0)
{
int result = int.Parse(formula[..i]);
token = new Operand<int>(result);
parsedLength = i;
return false;
}

return token != null;
int result = int.Parse(formula[..i]);
token = new Operand<int>(result);
parsedLength = i;

return true;
}
}
6 changes: 3 additions & 3 deletions tests/Calculator.Extra.Tests/CalculatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Calculate_OneVariable_Calculated()
"$var1",
new Dictionary<string, Operand>
{
{ "var1", new Operand<decimal>(4.5m) }
{ "var1", new Operand<decimal>(4.5m) },
}
)
);
Expand All @@ -54,7 +54,7 @@ public void Calculate_OneVariable_Calculated()
"$testVar",
new Dictionary<string, Operand>
{
{ "testVar", new Operand<bool>(true) }
{ "testVar", new Operand<bool>(true) },
}
)
);
Expand All @@ -69,7 +69,7 @@ public void Calculate_VariableAndNumber_Calculated()
"2.5 * $my_var",
new Dictionary<string, Operand>
{
{ "my_var", new Operand<decimal>(3) }
{ "my_var", new Operand<decimal>(3) },
}
)
);
Expand Down

0 comments on commit 079114d

Please sign in to comment.