/*
 * Copyright 2015-2020 Michele "King_DuckZ" Santullo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "vectorwrapper/vectorwrapper.hpp"
#include <gtest/gtest.h>
#include <cstddef>

namespace {
struct Point {
	unsigned short int x, y, z;
};
} //unnamed namespace

namespace vwr {
template <> struct VectorWrapperInfo<unsigned short int[3]> {
	enum { dimensions = 3 };
	using scalar_type = unsigned short int;
	using vector_type = unsigned short int[3];
	using higer_vector_type = unsigned short int[4];
	enum {
		offset_x = sizeof(scalar_type) * 0,
		offset_y = sizeof(scalar_type) * 1,
		offset_z = sizeof(scalar_type) * 2
	};
};

template <> struct VectorWrapperInfo<unsigned short int[4]> {
	enum { dimensions = 4 };
	using scalar_type = unsigned short int;
	using vector_type = unsigned short int[4];
	using lower_vector_type = unsigned short int[3];
	enum {
		offset_x = sizeof(scalar_type) * 0,
		offset_y = sizeof(scalar_type) * 1,
		offset_z = sizeof(scalar_type) * 2,
		offset_w = sizeof(scalar_type) * 3
	};
};

template <> struct VectorWrapperInfo<Point> {
	enum { dimensions = 3 };
	using scalar_type = unsigned short int;
	using vector_type = Point;
	enum {
		offset_x = offsetof(Point, x),
		offset_y = offsetof(Point, y),
		offset_z = offsetof(Point, z)
	};
};
} //namespace vwr

namespace {
typedef vwr::Vec<unsigned short int[3]> vec3;
typedef vwr::Vec<unsigned short int[4]> vec4;
typedef vwr::Vec<Point> struct_vec3;
} //unnamed namespace

TEST(vwr, c_array) {
	constexpr const unsigned short int test_val = 0b1010101010101010;
	vec4 myvec4(test_val);
	vec3 myvec3(myvec4.xyz());

	EXPECT_EQ(myvec3, myvec4.xyz());
	EXPECT_EQ(test_val, myvec3.x());
	EXPECT_EQ(test_val, myvec3.y());
	EXPECT_EQ(test_val, myvec3.z());

	for (int z = 0; z < 4; ++z) {
		const auto val = myvec4.data()[z];
		EXPECT_EQ(test_val, val);
	}

	myvec4.data()[1] = 5;
	{
		unsigned short int vals[4] { test_val, 5, test_val, test_val };
		for (int z = 0; z < 4; ++z) {
			const auto val = myvec4.data()[z];
			EXPECT_EQ(vals[z], val);
		}
	}

	myvec4.w() = 0xFFFFU;
	{
		unsigned short int vals[4] { test_val, 5, test_val, 0xFFFFU };
		for (int z = 0; z < 4; ++z) {
			const auto val = myvec4.data()[z];
			EXPECT_EQ(vals[z], val);
		}
	}

	myvec3.x() = 1001U;
	myvec3.y() = 1503U;
	myvec3.z() = 2007U;
	EXPECT_EQ(4511U, myvec3.x() + myvec3.y() + myvec3.z());

	EXPECT_NE(myvec3, myvec4.xyz());

	myvec3 = vec3{800U, 900U, 1000U};
	EXPECT_EQ(800U, myvec3.x());
	EXPECT_EQ(900U, myvec3.y());
	EXPECT_EQ(1000U, myvec3.z());
}

TEST(vwr, c_array_interactions) {
	vec3 myvec3 {7, 11, 13};
	struct_vec3 mystructvec3 { 17, 19, 23 };
	EXPECT_NE(myvec3, mystructvec3);

	myvec3 = mystructvec3;
	EXPECT_EQ(myvec3, mystructvec3);

	mystructvec3 = vec3{29, 31, 37};
	EXPECT_NE(myvec3, mystructvec3);
	EXPECT_EQ(29, mystructvec3.x());
	EXPECT_EQ(31, mystructvec3.y());
	EXPECT_EQ(37, mystructvec3.z());
}
