/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "currytest.h"
#include <memory>
#include <QtTest>
#include <curry.h>

QTEST_APPLESS_MAIN (LC::Util::CurryTest)

namespace LC::Util
{
	void CurryTest::testBasic ()
	{
		auto sum = [] (int a, int b, int c) { return a + b + c; };
		QCOMPARE (Curry (sum) (1) (2) (3), 6);

		auto stored = Curry (sum);
		QCOMPARE (stored (1) (2) (3), 6);
		QCOMPARE (stored (0) (2) (3), 5);

		auto storedApplied = Curry (sum) (0);
		QCOMPARE (storedApplied (2) (3), 5);
	}

	void CurryTest::testMoveArgs ()
	{
		auto func = [] (std::unique_ptr<int> a, std::unique_ptr<int> b) { return *a + *b; };
		QCOMPARE (Curry (func) (std::make_unique<int> (1)) (std::make_unique<int> (2)), 3);

		auto curried = Curry (func) (std::make_unique<int> (1));
		QCOMPARE (std::move (curried) (std::make_unique<int> (2)), 3);
	}

	void CurryTest::testMoveFun ()
	{
		auto ptr = std::make_unique<int> (10);
		auto func = [ptr = std::move (ptr)] (std::unique_ptr<int> a, std::unique_ptr<int> b) { return *ptr +  *a + *b; };
		QCOMPARE (Curry (std::move (func)) (std::make_unique<int> (1)) (std::make_unique<int> (2)), 13);
	}

	void CurryTest::testRValueFun ()
	{
		auto sum = [] (int&& a, int&& b, int&& c) { return a + b + c; };
		QCOMPARE (Curry (sum) (1) (2) (3), 6);
	}

	void CurryTest::testRefModifications ()
	{
		int a = 5;
		int b = 6;
		auto func = [] (int& a, int& b) { ++a; ++b; return a + b; };
		QCOMPARE (Curry (func) (a) (b), 13);
		QCOMPARE (a, 6);
		QCOMPARE (b, 7);
	}

	namespace
	{
		template<typename T>
		struct Counter
		{
			static inline int DefConstrs_ = 0;
			static inline int CopyConstrs_ = 0;
			static inline int CopyAssignments_ = 0;
			static inline int MoveConstrs_ = 0;
			static inline int MoveAssignments_ = 0;
			static inline int Dtors_ = 0;

			Counter ()
			{
				++DefConstrs_;
			}

			Counter (const Counter&)
			{
				++CopyConstrs_;
			}

			Counter (Counter&&)
			{
				++MoveConstrs_;
			}

			Counter& operator= (const Counter&)
			{
				++CopyAssignments_;
				return *this;
			}

			Counter& operator= (Counter&&)
			{
				++MoveAssignments_;
				return *this;
			}

			~Counter ()
			{
				++Dtors_;
			}
		};
	}

	void CurryTest::testNoExtraCopiesByValue ()
	{
		using C1 = Counter<struct Tag1>;
		using C2 = Counter<struct Tag2>;

		auto func = [] (C1, C2) { return 0; };
		QCOMPARE (Curry (func) (C1 {}) (C2 {}), 0);

		QCOMPARE (C1::CopyConstrs_, 0);
		QCOMPARE (C2::CopyConstrs_, 0);
		QCOMPARE (C1::CopyAssignments_, 0);
		QCOMPARE (C2::CopyAssignments_, 0);

		QCOMPARE (C1::MoveConstrs_, 2);
		QCOMPARE (C2::MoveConstrs_, 1);
		QCOMPARE (C1::MoveAssignments_, 0);
		QCOMPARE (C2::MoveAssignments_, 0);
	}

	void CurryTest::testNoExtraCopiesByRef ()
	{
		using C1 = Counter<struct Tag1>;
		using C2 = Counter<struct Tag2>;

		auto func = [] (C1&&, C2&&) { return 0; };
		QCOMPARE (Curry (func) (C1 {}) (C2 {}), 0);

		QCOMPARE (C1::CopyConstrs_, 0);
		QCOMPARE (C2::CopyConstrs_, 0);
		QCOMPARE (C1::CopyAssignments_, 0);
		QCOMPARE (C2::CopyAssignments_, 0);

		QCOMPARE (C1::MoveConstrs_, 1);
		QCOMPARE (C2::MoveConstrs_, 0);
		QCOMPARE (C1::MoveAssignments_, 0);
		QCOMPARE (C2::MoveAssignments_, 0);
	}

	void CurryTest::testNoExtraCopiesByConstRef ()
	{
		using C1 = Counter<struct Tag1>;
		using C2 = Counter<struct Tag2>;

		auto func = [] (const C1&, const C2&) { return 0; };
		QCOMPARE (Curry (func) (C1 {}) (C2 {}), 0);

		QCOMPARE (C1::CopyConstrs_, 0);
		QCOMPARE (C2::CopyConstrs_, 0);
		QCOMPARE (C1::CopyAssignments_, 0);
		QCOMPARE (C2::CopyAssignments_, 0);

		QCOMPARE (C1::MoveConstrs_, 1);
		QCOMPARE (C2::MoveConstrs_, 0);
		QCOMPARE (C1::MoveAssignments_, 0);
		QCOMPARE (C2::MoveAssignments_, 0);
	}

	void CurryTest::testNoExtraCopiesByConstRefToExisting ()
	{
		using C1 = Counter<struct Tag1>;
		using C2 = Counter<struct Tag2>;

		auto func = [] (const C1&, const C2&) { return 0; };
		C1 c1;
		C2 c2;
		QCOMPARE (Curry (func) (c1) (c2), 0);

		QCOMPARE (C1::CopyConstrs_, 0);
		QCOMPARE (C2::CopyConstrs_, 0);
		QCOMPARE (C1::CopyAssignments_, 0);
		QCOMPARE (C2::CopyAssignments_, 0);

		QCOMPARE (C1::MoveConstrs_, 0);
		QCOMPARE (C2::MoveConstrs_, 0);
		QCOMPARE (C1::MoveAssignments_, 0);
		QCOMPARE (C2::MoveAssignments_, 0);
	}
}
