// Main file: TesterOfTSACIMHas.cpp
// last change: 2011-06-30 Khu

#include <cstdlib>
#include <iostream>

#include "ToolsetArrayComparableM.hpp"

	typedef long Element;
	
	static const Element min = -1000;
	static const Element max = 1000;
	
	static Element random_item () 
    {
		Element result = 
			Element((double(std::rand() - 1) / (RAND_MAX - 2)) * (max - min) + min);
		assert(min <= result);									// postcondition
		assert(result <= max);									// postcondition
		return result;
	}
	
	static void init_random (std::vector<Element> & array) 
    {
		for (int index = 0; index < array.size(); ++index) 
        {
			array[index] = random_item();
		}
	}
	
	static std::ostream & operator<<
        (std::ostream & s, const std::vector<Element> & array) 
    {
		s << "[";
		for (int index = 0; index < array.size(); ++index) 
        {
			s << array[index] << " ";
		}
		return s << "]" << std::endl;
	}
	
	int main (int argc, char * argv[]) 
    {
		if (argc < 2 || std::atoi(argv[1]) <= 0) 
        {
			std::cout 
                << "Please call this command with a positive integer argument "
                << "for the size of the array!";
		} 
        else 
        {
			std::vector<Element> array (std::atoi(argv[1]));
			init_random(array);
			ToolsetArrayComparable::gapsort(array);
			init_random(array);
			ToolsetArrayComparable::quicksort(array);
			Element item = random_item();
			std::cout
                << "array: " << array
                << "Is item " << item << " in array? By" << std::endl << " has_1r"
                << ((ToolsetArrayComparable::has_1r(array, item)) ? "" : " not")
				<< " found," << std::endl << " has_1i"
                << ((ToolsetArrayComparable::has_1i(array, item)) ? "" : " not")
				<< " found," << std::endl << " has_2r"
                << ((ToolsetArrayComparable::has_2r(array, item)) ? "" : " not")
				<< " found," << std::endl << " has_2i"
				<< ((ToolsetArrayComparable::has_2i(array, item)) ? "" : " not")
				<< " found,"
                << std::endl << " index_in_of_1r at "
				<< ToolsetArrayComparable::index_in_of_1r(array, item)
				<< "," << std::endl << " index_in_of_1i at "
				<< ToolsetArrayComparable::index_in_of_1i(array, item)
                << "," << std::endl << " min_index_in_of_2r at "
				<< ToolsetArrayComparable::min_index_in_of_2r(array, item)
                << "," << std::endl << " min_index_in_of_2i at "
				<< ToolsetArrayComparable::min_index_in_of_2i(array, item)
                << "." << std::endl;
		}
		std::cout << std::endl;
		return 0;
	}
