Skip to content
Open
Show file tree
Hide file tree
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
178 changes: 178 additions & 0 deletions src/Computer.V2.Tests/FunctionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using Xunit;
using Computer.V2.Lib;
using Computer.V2.Lib.Exceptions;

namespace Computer.V2.Tests
{
public class FunctionsTests
{
[Fact]
public void NormaliseFunc_SimplePolynomial_ReturnsNormalizedForm()
{
// Arrange
string expression = "2*x^2 + 3*x + 1";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Contains("2*x^2", result);
Assert.Contains("3*x", result);
Assert.Contains("1", result);
}

[Fact]
public void NormaliseFunc_PolynomialWithSameTerms_CombinesTerms()
{
// Arrange
string expression = "x^2 + 2*x^2 + 3*x";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Contains("3*x^2", result);
Assert.Contains("3*x", result);
}

[Fact]
public void NormaliseFunc_PolynomialWithNegativeTerms_HandlesNegatives()
{
// Arrange
string expression = "x^2 - 2*x + 1";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Contains("1*x^2", result);
Assert.Contains("-2*x", result);
Assert.Contains("1", result);
}

[Fact]
public void NormaliseFunc_PolynomialWithZeroCoefficient_ExcludesZeroTerms()
{
// Arrange
string expression = "0*x^2 + 3*x + 5";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.DoesNotContain("0*x^2", result);
Assert.Contains("3*x", result);
Assert.Contains("5", result);
}

[Fact]
public void NormaliseFunc_ConstantExpression_ReturnsConstant()
{
// Arrange
string expression = "42";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Equal("42", result);
}

[Fact]
public void NormaliseFunc_ExpressionWithoutVariables_CalculatesResult()
{
// Arrange
string expression = "2 + 3 * 4";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Equal("14", result);
}

[Fact]
public void NormaliseFunc_PolynomialWithImplicitCoefficients_AddsCoefficients()
{
// Arrange
string expression = "x^2 + x + 1";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Contains("1*x^2", result);
Assert.Contains("1*x", result);
}

[Fact]
public void NormaliseFunc_HighDegreePolynomial_OrdersTermsCorrectly()
{
// Arrange
string expression = "1 + x + x^3 + x^2";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
int posX3 = result.IndexOf("x^3");
int posX2 = result.IndexOf("x^2");
int posX = result.IndexOf("*x");

Assert.True(posX3 < posX2);
Assert.True(posX2 < posX);
}

[Fact]
public void NormaliseFunc_InvalidPowerFormat_ThrowsInvalidExpressionException()
{
// Arrange
string expression = "x^-1 + 2";

// Act & Assert
Assert.Throws<InvalidExpressionException>(() => Functions.NormaliseFunc(expression));
}

[Theory]
[InlineData("2*x + 3", "2*x+3")]
[InlineData("x^2 - 5*x + 6", "1*x^2-5*x+6")]
[InlineData("-x^2 + 4", "-1*x^2+4")]
public void NormaliseFunc_VariousPolynomials_ReturnsExpectedForm(string input, string expected)
{
// Act
string result = Functions.NormaliseFunc(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void NormaliseFunc_PolynomialWithMatrixElements_PreservesMatrixElements()
{
// Arrange
string expression = "x^2 + [1,2]\n[3,4]";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Contains("x^2", result);
Assert.Contains("[1,2]", result);
Assert.Contains("[3,4]", result);
}

[Fact]
public void NormaliseFunc_PolynomialWithImaginaryNumbers_PreservesImaginaryParts()
{
// Arrange
string expression = "x^2 + 2*i";

// Act
string result = Functions.NormaliseFunc(expression);

// Assert
Assert.Contains("x^2", result);
Assert.Contains("2*i", result);
}
}
}
29 changes: 29 additions & 0 deletions tests/Computer.V2.Tests/Computer.V2.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Computer.V2.Lib\Computer.V2.Lib.csproj" />
</ItemGroup>

</Project>
117 changes: 117 additions & 0 deletions tests/Computer.V2.Tests/ExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Xunit;
using Computer.V2.Lib.Exceptions;
using System;

namespace Computer.V2.Tests
{
public class ExceptionTests
{
[Fact]
public void InvalidExpressionException_WithMessage_SetsMessageCorrectly()
{
// Arrange
string expectedMessage = "Test error message";

// Act
var exception = new InvalidExpressionException(expectedMessage);

// Assert
Assert.Equal(expectedMessage, exception.Message);
Assert.IsAssignableFrom<ArgumentException>(exception);
}

[Fact]
public void InvalidMatrixException_WithMessage_SetsMessageCorrectly()
{
// Arrange
string expectedMessage = "Invalid matrix format";

// Act
var exception = new InvalidMatrixException(expectedMessage);

// Assert
Assert.Equal(expectedMessage, exception.Message);
Assert.IsAssignableFrom<Exception>(exception);
}

[Fact]
public void InvalidVariableException_DefaultConstructor_SetsDefaultMessage()
{
// Act
var exception = new InvalidVariableException();

// Assert
Assert.Equal("Invalid Use of Variables.", exception.Message);
Assert.IsAssignableFrom<Exception>(exception);
}

[Fact]
public void InvalidExpressionException_CanBeThrown()
{
// Arrange
string message = "Expression is invalid";

// Act & Assert
Assert.Throws<InvalidExpressionException>(() =>
{
throw new InvalidExpressionException(message);
});
}

[Fact]
public void InvalidMatrixException_CanBeThrown()
{
// Arrange
string message = "Matrix is invalid";

// Act & Assert
Assert.Throws<InvalidMatrixException>(() =>
{
throw new InvalidMatrixException(message);
});
}

[Fact]
public void InvalidVariableException_CanBeThrown()
{
// Act & Assert
Assert.Throws<InvalidVariableException>(() =>
{
throw new InvalidVariableException();
});
}

[Fact]
public void InvalidExpressionException_InheritsFromArgumentException()
{
// Arrange
var exception = new InvalidExpressionException("test");

// Act & Assert
Assert.IsType<InvalidExpressionException>(exception);
Assert.IsAssignableFrom<ArgumentException>(exception);
}

[Fact]
public void InvalidMatrixException_InheritsFromException()
{
// Arrange
var exception = new InvalidMatrixException("test");

// Act & Assert
Assert.IsType<InvalidMatrixException>(exception);
Assert.IsAssignableFrom<Exception>(exception);
}

[Fact]
public void InvalidVariableException_InheritsFromException()
{
// Arrange
var exception = new InvalidVariableException();

// Act & Assert
Assert.IsType<InvalidVariableException>(exception);
Assert.IsAssignableFrom<Exception>(exception);
}
}
}
Loading