/** 
 * @file
 * @author (C) 2006 Vassilii Khachaturov <vassilii@tarunz.org>, under GPLv2
 * @brief a test of the patience sort and the generator iterator wrapper
 * @version $Id: t-generator52.cpp,v 1.5 2006/06/05 07:50:18 vassilii Exp $
 */

#include <iterator>
#include <functional>
#include <algorithm>
#include <vector>

#include "generator-iter.hpp"
#include "patience_sort.hpp"
#include "card.hpp"

#include "stats.h"

#include <cmath>
#include <iostream>

namespace test {

const int N = 52, TRIALS = 10000;

struct Dealer
{
	typedef Card result_type;
	std::vector<Card> deck;
	Card c;

	Dealer() : deck(N) {
	for (int i = 0; i < N; i++)
		deck[i] = Card(i);
		random_shuffle(deck.begin(), deck.end());
	}

	const result_type& operator()() { 
		c = deck.back(); 
		deck.pop_back(); 
		return c; 
	}
};

}

/** @test Testing both the generator iterator wrapper and the patience sort */
int main()
{
	using namespace std;
	using namespace test;

	Stats<double> stats;
	for (int trials = 0; trials < TRIALS; trials++) {
		Dealer d;
		countdown_generator<Dealer> round(d, N);
		int l = longest_increasing_subsequence(round.begin(), round.end());
		stats.add(l);
	}

	cout << N << " cards deck: longest increasing subsequence mean: " 
		<< stats << endl
		<< "\tover " << TRIALS << " trials." << endl;

	// Aldous & Diaconis say it should be ~11.6 for N=52 and 10000 trials
	return !(abs(stats.mean() - 11.6) <= 0.05);
}
