/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include <catch2/catch_all.hpp>

#include "Math/RGB10A2.hpp"

using namespace OpenVulkano::Math;

namespace
{
	bool almostEqual(float a, float b, float epsilon = 0.001f) { return std::fabs(a - b) < epsilon; }
}

TEST_CASE("Default Constructor", "[RGB10A2U]")
{
	RGB10A2U color;
	REQUIRE(color.value == 0);
}

TEST_CASE("Set Method with Vector3", "[RGB10A2U]")
{
	RGB10A2U color;
	Vector3<uint32_t> vec3 { 1000, 500, 250 };
	color.Set(vec3);
	REQUIRE(color.r == 1000);
	REQUIRE(color.g == 500);
	REQUIRE(color.b == 250);
	REQUIRE(color.a == 3);
}

TEST_CASE("Set Method with Vector4", "[RGB10A2U]")
{
	RGB10A2U color;
	Vector4<uint32_t> vec4 { 1000, 500, 250, 1 };
	color.Set(vec4);
	REQUIRE(color.r == 1000);
	REQUIRE(color.g == 500);
	REQUIRE(color.b == 250);
	REQUIRE(color.a == 1);
}

TEST_CASE("SetNormalized Method with Vector3", "[RGB10A2U]")
{
	RGB10A2U color;
	Vector3<float> vec3 { 0.5f, 0.25f, 0.125f };
	color.SetNormalized(vec3);
	REQUIRE(color.r == int(0.5 * 1023));
	REQUIRE(color.g == int(0.25 * 1023));
	REQUIRE(color.b == int(0.125 * 1023));
	REQUIRE(color.a == 3);
}

TEST_CASE("Equality Operators", "[RGB10A2U]")
{
	RGB10A2U color;
	RGB10A2U color2 { color };
	REQUIRE(color == color2);
	color2.r = 1;
	REQUIRE(color != color2);
}

TEST_CASE("Conversion to Vector4", "[RGB10A2U]")
{
	RGB10A2U color;
	color.Set(Vector4<uint32_t> { 1000, 500, 250, 2 });
	Vector4f vec4 = color;
	REQUIRE(almostEqual(vec4.x, 1000.0f / 1023.0f));
	REQUIRE(almostEqual(vec4.y, 500.0f / 1023.0f));
	REQUIRE(almostEqual(vec4.z, 250.0f / 1023.0f));
	REQUIRE(almostEqual(vec4.w, 2.0f / 3.0f));
}

TEST_CASE("Default Constructor", "[RGB10A2S]")
{
	RGB10A2S color;
	REQUIRE(color.value == 0);
}

TEST_CASE("Set Method with Vector3", "[RGB10A2S]")
{
	RGB10A2S color;
	Vector3<int32_t> vec3 { 1000, 500, 250 };
	color.Set(vec3);
	REQUIRE(color.r == -24);
	REQUIRE(color.g == 500);
	REQUIRE(color.b == 250);
	REQUIRE(color.a == 1);
}

TEST_CASE("Set Method with Vector4", "[RGB10A2S]")
{
	RGB10A2S color;
	Vector4<int32_t> vec4 { 1000, 500, 250, 1 };
	color.Set(vec4);
	REQUIRE(color.r == -24); // because it is RGB10A2S
	REQUIRE(color.g == 500);
	REQUIRE(color.b == 250);
	REQUIRE(color.a == 1);
}

TEST_CASE("SetNormalized Method with Vector3", "[RGB10A2S]")
{
	RGB10A2S color;
	Vector3<float> vec3 { 0.5f, 0.25f, 0.125f };
	color.SetNormalized(vec3);
	REQUIRE(color.r == 255); // because it is RGB10A2S
	REQUIRE(color.g == 127); // because it is RGB10A2S
	REQUIRE(color.b == 63); // because it is RGB10A2S
	REQUIRE(color.a == 1); // because it is RGB10A2S
}

TEST_CASE("Equality Operators", "[RGB10A2S]")
{
	RGB10A2S color;
	RGB10A2S color2 { color };
	REQUIRE(color == color2);
	color2.r = 1;
	REQUIRE(color != color2);
}

TEST_CASE("Conversion to Vector4", "[RGB10A2S]")
{
	RGB10A2S color;
	color.Set(Vector4<int32_t> { 1000, 500, 250, 2 });
	Vector4f vec4 = color;
	REQUIRE(almostEqual(vec4.x, -0.0469667315)); // because the type is Signed. Ugh!
	REQUIRE(almostEqual(vec4.y, 0.978474)); // because the type is Signed. Ugh!
	REQUIRE(almostEqual(vec4.z, 0.489237)); // because the type is Signed. Ugh!
	REQUIRE(almostEqual(vec4.w, -2.0f)); // because the type is Signed. Ugh!
}