Skip to content
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

Feat: test suite #177

Merged
merged 2 commits into from
Sep 18, 2024
Merged
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
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: Build
name: Build and Test

on:
push:
pull_request:

jobs:
build:

runs-on: ubuntu-latest

steps:
Expand All @@ -31,3 +30,7 @@ jobs:
# Build
- name: Build
run: dotnet build --no-restore

# Test
- name: Test
run: dotnet test
155 changes: 155 additions & 0 deletions Prowl.Runtime.Test/ColorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using Xunit;

namespace Prowl.Runtime.Test;

public class ColorTests
{
[Fact]
public void Grayscale_Calculation_Is_Correct()
{
var color = new Color(0.5f, 0.5f, 0.5f, 1f);
Assert.Equal(0.5f, color.grayscale);
}

[Fact]
public void Indexer_Get_Returns_Correct_Value()
{
var color = new Color(0.1f, 0.2f, 0.3f, 0.4f);
Assert.Equal(0.1f, color[0]);
Assert.Equal(0.2f, color[1]);
Assert.Equal(0.3f, color[2]);
Assert.Equal(0.4f, color[3]);
}

[Fact]
public void Indexer_Set_Sets_Correct_Value()
{
var color = new Color(0.1f, 0.2f, 0.3f, 0.4f);
color[0] = 0.5f;
Assert.Equal(0.5f, color.r);
}

[Fact]
public void Indexer_Set_Throws_Exception_For_Invalid_Index()
{
var color = new Color(0.1f, 0.2f, 0.3f, 0.4f);
Assert.Throws<IndexOutOfRangeException>(() => color[4] = 0.5f);
}

[Fact]
public void Lerp_Returns_Correct_Value()
{
var color1 = new Color(0f, 0f, 0f, 1f);
var color2 = new Color(1f, 1f, 1f, 1f);
var lerpColor = Color.Lerp(color1, color2, 0.5f);
Assert.Equal(new Color(0.5f, 0.5f, 0.5f, 1f), lerpColor);
}

[Theory]
[InlineData(0, 0, 0, 1, 0, 0, 0, 1)]
[InlineData(1, 1, 1, 1, 1, 1, 1, 1)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f)]
public void Equality_Operator_Works_Correctly(float r1, float g1, float b1, float a1, float r2, float g2, float b2,
float a2)
{
var color1 = new Color(r1, g1, b1, a1);
var color2 = new Color(r2, g2, b2, a2);
Assert.Equal(r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2, color1 == color2);
}

[Theory]
[InlineData(0, 0, 0, 1, 0, 0, 0, 1)]
[InlineData(1, 1, 1, 1, 0, 0, 0, 1)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 1, 1, 1, 1)]
public void Inequality_Operator_Works_Correctly(float r1, float g1, float b1, float a1, float r2, float g2,
float b2, float a2)
{
var color1 = new Color(r1, g1, b1, a1);
var color2 = new Color(r2, g2, b2, a2);
Assert.Equal(r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2, color1 != color2);
}

[Theory]
[InlineData(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2)]
[InlineData(1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 2)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 1, 1, 1, 1, 1.5f, 1.5f, 1.5f, 1.5f)]
public void Addition_Operator_Works_Correctly(float r1, float g1, float b1, float a1, float r2, float g2, float b2,
float a2, float r3, float g3, float b3, float a3)
{
var color1 = new Color(r1, g1, b1, a1);
var color2 = new Color(r2, g2, b2, a2);
var result = color1 + color2;
Assert.Equal(new Color(r3, g3, b3, a3), result);
}

[Theory]
[InlineData(0, 0, 0, 1, 2, 0, 0, 0, 0.5f)]
[InlineData(1, 1, 1, 1, 2, 0.5f, 0.5f, 0.5f, 0.5f)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 2, 0.25f, 0.25f, 0.25f, 0.25f)]
public void Division_Operator_Works_Correctly(float r1, float g1, float b1, float a1, float divisor, float r2, float g2, float b2, float a2)
{
var color1 = new Color(r1, g1, b1, a1);
var result = color1 / divisor;
Assert.Equal(new Color(r2, g2, b2, a2), result);
}

[Theory]
[InlineData(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)]
[InlineData(1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 1, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f, 0.25f)]
public void Multiplication_Operator_Works_Correctly(float r1, float g1, float b1, float a1, float r2, float g2,
float b2, float a2, float r3, float g3, float b3, float a3)
{
var color1 = new Color(r1, g1, b1, a1);
var color2 = new Color(r2, g2, b2, a2);
var result = color1 * color2;
Assert.Equal(new Color(r3, g3, b3, a3), result);
}

[Theory]
[InlineData(0, 0, 0, 1, 2, 0, 0, 0, 2)]
[InlineData(1, 1, 1, 1, 2, 2, 2, 2, 2)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 2, 1, 1, 1, 1)]
public void Multiplication_By_Scalar_Works_Correctly(float r1, float g1, float b1, float a1, float scalar, float r2,
float g2, float b2, float a2)
{
var color1 = new Color(r1, g1, b1, a1);
var result = color1 * scalar;
Assert.Equal(new Color(r2, g2, b2, a2), result);
}

[Theory]
[InlineData(0, 0, 0, 1, 2, 0, 0, 0, 2)]
[InlineData(1, 1, 1, 1, 2, 2, 2, 2, 2)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 2, 1, 1, 1, 1)]
public void Multiplication_By_Scalar_From_Left_Works_Correctly(float r1, float g1, float b1, float a1, float scalar,
float r2, float g2, float b2, float a2)
{
var color1 = new Color(r1, g1, b1, a1);
var result = scalar * color1;
Assert.Equal(new Color(r2, g2, b2, a2), result);
}

[Theory]
[InlineData(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0)]
[InlineData(1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0)]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, 1, 1, 1, 1, -0.5f, -0.5f, -0.5f, -0.5f)]
public void Subtraction_Operator_Works_Correctly(float r1, float g1, float b1, float a1, float r2, float g2,
float b2, float a2, float r3, float g3, float b3, float a3)
{
var color1 = new Color(r1, g1, b1, a1);
var color2 = new Color(r2, g2, b2, a2);
var result = color1 - color2;
Assert.Equal(new Color(r3, g3, b3, a3), result);
}

[Theory]
[InlineData(0, 0, 0, 1, "RGBA(0, 0, 0, 1)")]
[InlineData(1, 1, 1, 1, "RGBA(1, 1, 1, 1)")]
[InlineData(0.5f, 0.5f, 0.5f, 0.5f, "RGBA(0.5, 0.5, 0.5, 0.5)")]
public void ToString_Returns_Correct_Value(float r, float g, float b, float a, string expected)
{
var color = new Color(r, g, b, a);
Assert.Equal(expected, color.ToString());
}
}
26 changes: 26 additions & 0 deletions Prowl.Runtime.Test/Prowl.Runtime.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.InMemory" Version="0.11.0" />
<PackageReference Include="Xunit" Version="2.9.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Prowl.Runtime\Prowl.Runtime.csproj" />
</ItemGroup>

</Project>
52 changes: 27 additions & 25 deletions Prowl.Runtime/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@
namespace Prowl.Runtime;

[StructLayout(LayoutKind.Sequential)]
public struct Color
public struct Color : IEquatable<Color>
{
public float r, g, b, a;

public float grayscale => 0.299f * r + 0.587f * g + 0.114f * b;

public static Color black => new Color(0f, 0f, 0f, 1f);
public static Color black => new(0f, 0f, 0f, 1f);

public static Color blue => new Color(0f, 0f, 1f, 1f);
public static Color blue => new(0f, 0f, 1f, 1f);

public static Color clear => new Color(0f, 0f, 0f, 0f);
public static Color clear => new(0f, 0f, 0f, 0f);

public static Color cyan => new Color(0f, 1f, 1f, 1f);
public static Color cyan => new(0f, 1f, 1f, 1f);

public static Color gray => new Color(0.5f, 0.5f, 0.5f, 1f);
public static Color gray => new(0.5f, 0.5f, 0.5f, 1f);

public static Color green => new Color(0f, 1f, 0f, 1f);
public static Color green => new(0f, 1f, 0f, 1f);

public static Color grey => new Color(0.5f, 0.5f, 0.5f, 1f);
public static Color grey => new(0.5f, 0.5f, 0.5f, 1f);

public static Color magenta => new Color(1f, 0f, 1f, 1f);
public static Color magenta => new(1f, 0f, 1f, 1f);

public static Color red => new Color(1f, 0f, 0f, 1f);
public static Color red => new(1f, 0f, 0f, 1f);

public static Color white => new Color(1f, 1f, 1f, 1f);
public static Color white => new(1f, 1f, 1f, 1f);

public static Color yellow => new Color(1f, 0.9215f, 0.0156f, 1f);
public static Color yellow => new(1f, 0.9215f, 0.0156f, 1f);

public float this[int index]
{
Expand Down Expand Up @@ -121,7 +121,7 @@ public static Color FromHSV(float h, float s, float v, float a = 1)
float t = v * (1 - s * (1 - f));

// build our rgb color
Color color = new Color(0, 0, 0, a);
Color color = new(0, 0, 0, a);

switch (i)
{
Expand Down Expand Up @@ -165,27 +165,27 @@ public static Color FromHSV(float h, float s, float v, float a = 1)
return color;
}

public static Color operator +(Color a, Color b) => new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
public static Color operator +(Color a, Color b) => new(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);

public static Color operator /(Color a, float b) => new Color(a.r / b, a.g / b, a.b / b, a.a / b);
public static Color operator /(Color a, float b) => new(a.r / b, a.g / b, a.b / b, a.a / b);

public static bool operator ==(Color lhs, Color rhs) => lhs == rhs;
public static bool operator ==(Color lhs, Color rhs) => lhs.Equals(rhs);

public static implicit operator Vector4(Color c) => new Vector4(c.r, c.g, c.b, c.a);
public static implicit operator System.Numerics.Vector4(Color c) => new System.Numerics.Vector4(c.r, c.g, c.b, c.a);
public static implicit operator Vector4(Color c) => new(c.r, c.g, c.b, c.a);
public static implicit operator System.Numerics.Vector4(Color c) => new(c.r, c.g, c.b, c.a);

public static implicit operator Color(Vector4 v) => new Color((float)v.x, (float)v.y, (float)v.z, (float)v.w);
public static implicit operator Color(System.Numerics.Vector4 v) => new Color(v.X, v.Y, v.Z, v.W);
public static implicit operator Color(Vector4 v) => new((float)v.x, (float)v.y, (float)v.z, (float)v.w);
public static implicit operator Color(System.Numerics.Vector4 v) => new(v.X, v.Y, v.Z, v.W);

public static bool operator !=(Color lhs, Color rhs) => lhs != rhs;
public static bool operator !=(Color lhs, Color rhs) => !lhs.Equals(rhs);

public static Color operator *(Color a, Color b) => new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
public static Color operator *(Color a, Color b) => new(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);

public static Color operator *(Color a, float b) => new Color(a.r * b, a.g * b, a.b * b, a.a * b);
public static Color operator *(Color a, float b) => new(a.r * b, a.g * b, a.b * b, a.a * b);

public static Color operator *(float b, Color a) => new Color(a.r * b, a.g * b, a.b * b, a.a * b);
public static Color operator *(float b, Color a) => new(a.r * b, a.g * b, a.b * b, a.a * b);

public static Color operator -(Color a, Color b) => new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);
public static Color operator -(Color a, Color b) => new(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);

public override bool Equals(object? other)
{
Expand All @@ -199,4 +199,6 @@ public override int GetHashCode()
{
throw new NotImplementedException();
}

public bool Equals(Color other) => r.Equals(other.r) && g.Equals(other.g) && b.Equals(other.b) && a.Equals(other.a);
}
6 changes: 6 additions & 0 deletions Prowl.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Veldrid.SDL2", "External\Pr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Veldrid.StartupUtilities", "External\Prowl.Veldrid\src\Veldrid.StartupUtilities\Veldrid.StartupUtilities.csproj", "{254969BF-FADA-4DEA-B6EC-4B0E222BFFE4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prowl.Runtime.Test", "Prowl.Runtime.Test\Prowl.Runtime.Test.csproj", "{1255E3CE-2675-41C1-89B4-877DCB1EBEA0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -142,6 +144,10 @@ Global
{AA2920ED-64FE-4652-B46F-853537791175}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA2920ED-64FE-4652-B46F-853537791175}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA2920ED-64FE-4652-B46F-853537791175}.Release|Any CPU.Build.0 = Release|Any CPU
{1255E3CE-2675-41C1-89B4-877DCB1EBEA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1255E3CE-2675-41C1-89B4-877DCB1EBEA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1255E3CE-2675-41C1-89B4-877DCB1EBEA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1255E3CE-2675-41C1-89B4-877DCB1EBEA0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading