// ---------------------------------------------------------------------------
//
//  This file is part of PermLib.
//
// Copyright (c) 2009-2010 Thomas Rehn <thomas@carmen76.de>
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ---------------------------------------------------------------------------


#ifndef TEST_COMMON_H_
#define TEST_COMMON_H_

struct GroupInformation {
    std::string filename;
    unsigned int n;
    unsigned int order;
    
    GroupInformation(const std::string &f, unsigned int _n, unsigned int _order) : filename(f), n(_n), order(_order) {}
};

const GroupInformation info_empty("../data/G_empty42", 42, 1);
const GroupInformation info_psu4_3("../data/G_psu4_3", 820, 3265920);
const GroupInformation info_1997("../data/G_test1997", 13, 13*12*9*4);
const GroupInformation info_e6("../data/G_e6", 36, 51840);
const GroupInformation info_e7("../data/G_e7", 63, 1451520);
const GroupInformation info_e8("../data/G_e8", 120, 348364800);
const GroupInformation info_cyclic500("../data/G_cyclic500", 500, 500);


#include <list>
#include <boost/shared_ptr.hpp>

#include <permlib/common.h>
#include <permlib/bsgs.h>
#include <permlib/construct/schreier_sims_construction.h>
#include <permlib/construct/random_schreier_sims_construction.h>
#include <permlib/generator/product_replacement_generator.h>

template<class PERM>
unsigned int readGroup(const std::string &filename, std::list<typename PERM::ptr> &groupGenerators) {
    std::ifstream file;
    file.open(filename.c_str());
	if (!file.is_open()) {
		std::cerr << "opening " << filename << " failed" << std::endl;
        BOOST_REQUIRE(file.is_open());
		return 0;
	}
    
    unsigned int n;
    file >> n;

    std::string line;
    while (!file.eof()) {
        std::getline(file, line);
        if (line.length() < 1)
            continue;
		boost::shared_ptr<PERM> gen(new PERM(n, line));
        groupGenerators.push_back(gen);
    }
    file.close();
    
    return n;
}

using namespace permlib;

template<class PERM,class TRANS>
BSGS<PERM,TRANS> construct(const std::string &filename) {
    std::list<typename PERM::ptr> groupGenerators;
    const unsigned int n = readGroup<PERM>(filename, groupGenerators);

    SchreierSimsConstruction<PERM, TRANS> ssc(n);
    BSGS<PERM, TRANS> bsgs = ssc.construct(groupGenerators.begin(), groupGenerators.end());
    
    return bsgs;
}

template<class PERM,class TRANS>
BSGS<PERM,TRANS> construct2(const std::string &filename, unsigned long order) {
	std::list<typename PERM::ptr> groupGenerators;
	const unsigned int n = readGroup<PERM>(filename, groupGenerators);

	ProductReplacementGenerator<PERM> rng(groupGenerators.begin(), groupGenerators.end());
	RandomSchreierSimsConstruction<PERM, TRANS> ssc(n, &rng, order);
	bool result = false;
	BSGS<PERM, TRANS> bsgs = ssc.construct(groupGenerators.begin(), groupGenerators.end(), result);
	BOOST_REQUIRE(result);
	
	return bsgs;
}

template<class PERM>
void checkImage(const unsigned long* Delta, unsigned long DeltaSize, const unsigned long* Phi, const boost::shared_ptr<PERM> &repr) {
	std::list<unsigned long> imgList(Phi, Phi+DeltaSize);
	for (unsigned int j = 0; j < DeltaSize; ++j) {
		std::list<unsigned long>::iterator it = std::find(imgList.begin(), imgList.end(), *repr / Delta[j]);
		BOOST_REQUIRE( it != imgList.end() );
		imgList.erase(it);
	}
	BOOST_CHECK( imgList.size() == 0 );
}

#endif // - TEST_COMMON_H_
