#include <coral/core/diff/MyersDiff.h>
#include <coral/core/diff/MyersPatch.h>

#include <string>

static int errors = 0;

static void myers(const std::string a, const std::string b) {
	std::string::const_iterator ab = a.begin();
	std::string::const_iterator ae = a.end();
	std::string::const_iterator bb = b.begin();
	std::string::const_iterator be = b.end();

	dlog << DEBUGLEVEL_DEBUG << "Testing the diff between "
		  << a << " and " << b << "."
		  << std::endl
		  << std::endl;

	typedef std::deque<coral::DIFF::answer<char> > ret_t;
	ret_t ret = coral::DIFF::MyersDiff(ab, ae, bb, be, ' ');

#if 0
	for (ret_t::iterator i = ret.begin();
	     i != ret.end();
	     ++i) {
		dlog << DEBUGLEVEL_DEBUG << *i << std::endl;
	}
#endif
	/*
	 * Patching a ---> b
	 */
	std::vector<char> va;
	va.insert(va.end(), a.begin(), a.end());
	coral::DIFF::MyersPatch(ret, va, false);
	std::string sa;
	sa.append(va.begin(), va.end());
	dlog << DEBUGLEVEL_DEBUG << "Got " << sa << std::endl;
	coral_assert(b == sa);

	/*
	 * Patching b ---> a
	 */
	std::vector<char> vb;
	vb.insert(vb.end(), b.begin(), b.end());
	coral::DIFF::MyersPatch(ret, vb, true);
	std::string sb;
	sb.append(vb.begin(), vb.end());
	dlog << DEBUGLEVEL_DEBUG << "Got " << sb << std::endl;
	coral_assert(a == sb);
	
	dlog << DEBUGLEVEL_DEBUG << std::endl << std::endl;
}

int main() {
	myers("", "");
	myers("abcdef", "abcdefg");
	myers("abcdef", "abcdefghj");
	myers("abcdef", "ghjabcdef");
	myers("ghjabcdef", "abcdef");
	myers("abcdefghj", "abcdef");
	myers("abcdefgh", "abcdAAefG");
	myers("hello world!", "ehllo olrd!?");
	myers("abbbabacbabc", "abbacbabcbaa");
	myers("", "a");
	myers("a", "");
	myers("", "");
	myers("marcus", "marcus foo");
	myers("marcus", "foo marcus");
	myers("bcd", "babcd");
	return errors ? EXIT_FAILURE : EXIT_SUCCESS;
}

