#include <psemek/test/test.hpp>

#include <psemek/util/array.hpp>

using namespace psemek::util;

test_case(util_array_empty)
{
	array<int> a;
	expect(a.empty());
	expect_equal(a.size(), 0);
	expect_equal_ptr(a.data(), nullptr);
}

test_case(util_array_ctor_size)
{
	array<int> a(16, 42);
	expect(!a.empty());
	expect_equal(a.size(), 16);
	expect_different_ptr(a.data(), nullptr);

	for (int i = 0; i < a.size(); ++i)
		expect_equal(a[i], 42);

	for (int i = 0; i < a.size(); ++i)
		a[i] = i;

	for (int i = 0; i < a.size(); ++i)
		expect_equal(a[i], i);
}

test_case(util_array_ctor_range)
{
	int data[8] = {2, 3, 5, 7, 11, 13, 17, 19};

	array<int> a(std::begin(data), std::end(data));
	expect(!a.empty());
	expect_equal(a.size(), std::size(data));
	expect_different_ptr(a.data(), nullptr);

	for (int i = 0; i < a.size(); ++i)
		expect_equal(a[i], data[i]);
}

test_case(util_array_ctor_initializer__list)
{
	int data[8] = {2, 3, 5, 7, 11, 13, 17, 19};

	array<int> a{2, 3, 5, 7, 11, 13, 17, 19};
	expect(!a.empty());
	expect_equal(a.size(), std::size(data));
	expect_different_ptr(a.data(), nullptr);

	for (int i = 0; i < a.size(); ++i)
		expect_equal(a[i], data[i]);
}

test_case(util_array_ctor_move)
{
	array<int> a(16);
	for (int i = 0; i < a.size(); ++i)
		a[i] = i;

	array<int> b = std::move(a);

	expect(a.empty());
	expect_equal(a.size(), 0);
	expect_equal_ptr(a.data(), nullptr);
	expect(!b.empty());
	expect_equal(b.size(), 16);
	expect_different_ptr(b.data(), nullptr);

	for (int i = 0; i < b.size(); ++i)
		expect_equal(b[i], i);
}

test_case(util_array_resize)
{
	array<int> a(16);
	for (int i = 0; i < a.size(); ++i)
		a[i] = i;

	a.resize(32);
	for (int i = 0; i < 16; ++i)
		expect_equal(a[i], i);

	a.resize(8);
	for (int i = 0; i < 8; ++i)
		expect_equal(a[i], i);
}

test_case(util_array_clear)
{
	array<int> a(16);
	a.clear();

	expect(a.empty());
	expect_equal(a.size(), 0);
	expect_equal_ptr(a.data(), nullptr);
}

test_case(util_array_fill)
{
	array<int> a(16);
	a.fill(42);

	for (int i = 0; i < a.size(); ++i)
		expect_equal(a[i], 42);
}

test_case(util_array_index)
{
	array<int> a(16);

	for (int i = 0; i < a.size(); ++i)
		expect_equal_ptr(&a[i], a.data() + i);
}

test_case(util_array_at)
{
	array<int> a(16);

	for (int i = 0; i < a.size(); ++i)
		expect_equal_ptr(&a.at(i), a.data() + i);

	for (int i = a.size(); i < a.size() + 16; ++i)
		expect_throw(a.at(i), array_index_out_of_bounds);
}

test_case(util_array_iterator)
{
	array<int> a(16);
	for (int i = 0; i < a.size(); ++i)
		a[i] = i;

	int i = 0;
	for (auto v : a)
		expect_equal(a[i++], v);

	i = 0;
	for (auto & v : a)
		v = 42 * (i++);

	for (int i = 0; i < a.size(); ++i)
		expect_equal(a[i], 42 * i);

	array<int> const b(a.begin(), a.end());
	i = 0;
	for (auto v : b)
		expect_equal(a[i++], v);
}

test_case(util_array_copy)
{
	array<int> a(16);
	for (int i = 0; i < a.size(); ++i)
		a[i] = i;

	auto b = a.copy();
	expect_equal(a.size(), b.size());
	expect_different_ptr(a.data(), b.data());

	for (int i = 0; i < b.size(); ++i)
		expect_equal(a[i], b[i]);
}
