/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * BondGraphUnitTest.cpp
 *
 *  Created on: Oct 29, 2009
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
using namespace std;
#include 
#include 
#include 
// include headers that implement a archive in simple text format
#include 
#include 
#include 
#include 
#include 
#include "CodePatterns/Assert.hpp"
#include "Atom/atom.hpp"
#include "Bond/bond.hpp"
#include "CodePatterns/Log.hpp"
#include "Element/element.hpp"
#include "Graph/BondGraph.hpp"
#include "molecule.hpp"
#include "Element/periodentafel.hpp"
#include "World.hpp"
#include "WorldTime.hpp"
#include "BondGraphUnitTest.hpp"
#ifdef HAVE_TESTRUNNER
#include "UnitTestMain.hpp"
#endif /*HAVE_TESTRUNNER*/
/********************************************** Test classes **************************************/
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
void BondGraphTest::setUp()
{
  atom *Walker = NULL;
  // construct element
  hydrogen = World::getInstance().getPeriode()->FindElement(1);
  carbon = World::getInstance().getPeriode()->FindElement(6);
  CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
  CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
  // construct molecule (tetraeder of hydrogens)
  TestMolecule = World::getInstance().createMolecule();
  CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(carbon);
  Walker->setPosition(Vector(1., 0., 1. ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(carbon);
  Walker->setPosition(Vector(0., 1., 1. ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(carbon);
  Walker->setPosition(Vector(1., 1., 0. ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(carbon);
  Walker->setPosition(Vector(0., 0., 0. ));
  TestMolecule->AddAtom(Walker);
  // check that TestMolecule was correctly constructed
  CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
  // create stream with table
  test << ".\tH\tHe\tLi\tBe\tB\tC\n";
  test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
  test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
  // created bad stream (i.e. non-present file)
  dummy.setstate(ios::eofbit);
  CPPUNIT_ASSERT(dummy.eof());
  BG = new BondGraph(true);
  CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
};
void BondGraphTest::tearDown()
{
  // remove the file
  delete(BG);
  // remove molecule
  World::getInstance().destroyMolecule(TestMolecule);
  // note that all the atoms, molecules, the tafel and the elements
  // are all cleaned when the world is destroyed
  World::purgeInstance();
  logger::purgeInstance();
};
/** Tests whether setup worked.
 */
void BondGraphTest::SetupTest()
{
  CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
  CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size());
};
/** UnitTest for BondGraphTest::LoadBondLengthTable().
 */
void BondGraphTest::LoadTableTest()
{
  CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
  CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(1,1) );
  CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(1,6) );
  CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(6,6) );
};
/** UnitTest for BondGraphTest::CreateAdjacency().
 */
void BondGraphTest::ConstructGraphFromTableTest()
{
  molecule::iterator Walker = TestMolecule->begin();
  molecule::iterator Runner = TestMolecule->begin();
  Runner++;
  CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
  World::AtomComposite Set = TestMolecule->getAtomSet();
  BG->CreateAdjacency(Set);
  CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
  CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo(WorldTime::getTime(), (*Runner)) );
};
/** UnitTest for BondGraphTest::CreateAdjacency().
 */
void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
{
  CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(dummy) );
  World::AtomComposite Set = TestMolecule->getAtomSet();
  BG->CreateAdjacency(Set);
  CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
  // this cannot be assured using dynamic IDs
  //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
};
/** UnitTest for operator==()
 */
void BondGraphTest::EqualityTest()
{
  // compare to self
  CPPUNIT_ASSERT( *BG == *BG );
  // create other instance
  BondGraph *BG2 = new BondGraph(true);
  CPPUNIT_ASSERT( *BG == *BG2 );
  BG2->IsAngstroem = false;
  CPPUNIT_ASSERT( *BG != *BG2 );
  delete BG2;
};
/** UnitTest for serialization
 */
void BondGraphTest::SerializationTest()
{
  // write element to stream
  std::stringstream stream;
  boost::archive::text_oarchive oa(stream);
  oa << BG;
  std::cout << "Contents of archive is " << stream.str() << std::endl;
  // create and open an archive for input
  boost::archive::text_iarchive ia(stream);
  // read class state from archive
  BondGraph *BG2;
  ia >> BG2;
  CPPUNIT_ASSERT (*BG == *BG2);
  delete BG2;
};