/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2014 Frederik Heber. 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 .
 */
/*
 * SaturationDistanceMaximizerUnitTest.cpp
 *
 *  Created on: Aug 09, 2012
 *      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 "SaturationDistanceMaximizerUnitTest.hpp"
#include 
#include 
#include "CodePatterns/Assert.hpp"
#include "Atom/atom.hpp"
#include "Atom/AtomObserver.hpp"
#include "Bond/bond.hpp"
#include "Fragmentation/Exporters/SaturatedBond.hpp"
#include "World.hpp"
#ifdef HAVE_TESTRUNNER
#include "UnitTestMain.hpp"
#endif /*HAVE_TESTRUNNER*/
/********************************************** Test classes **************************************/
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( SaturationDistanceMaximizerTest );
void SaturationDistanceMaximizerTest::setUp()
{
  // failing asserts should be thrown
  ASSERT_DO(Assert::Throw);
}
void SaturationDistanceMaximizerTest::tearDown()
{
  World::purgeInstance();
  AtomObserver::purgeInstance();
}
size_t SaturationDistanceMaximizerTest::MaxAtoms = 5;
/** UnitTest for operator() when there is nothing to do
 */
void SaturationDistanceMaximizerTest::identityTest()
{
  // prepare SaturatedBonds each with degree one
  atomVector.resize((size_t)MaxAtoms);
  std::generate_n(atomVector.begin(), MaxAtoms,
      boost::bind(&World::createAtom, boost::ref(World::getInstance())));
  std::list bondVector;
  SaturationDistanceMaximizer::PositionContainers_t PositionContainers;
  for (unsigned int i=1;i(
        new SaturatedBond(*bondVector.back(), *atomVector.front()))
        );
  }
  {
    SaturationDistanceMaximizer maximizer(PositionContainers);
    gsl_vector *zero = gsl_vector_calloc(PositionContainers.size());
    maximizer.setAlphas(zero);
    gsl_vector_free(zero);
    // and check that all alphas are the zero
    {
      std::vector alphas = maximizer.getAlphas();
      const double result = std::accumulate(alphas.begin(), alphas.end(), 0.);
      CPPUNIT_ASSERT_EQUAL( 0., result );
    }
    // then maximize: does nothing as bond degrees are all 1
    maximizer();
    // and check that all alphas are the zero
    {
      std::vector alphas = maximizer.getAlphas();
      const double result = std::accumulate(alphas.begin(), alphas.end(), 0.);
      CPPUNIT_ASSERT_EQUAL( 0., result );
    }
  }
  // free all memory
  PositionContainers.clear();
  for (std::list::iterator iter = bondVector.begin(); iter != bondVector.end();
      ++iter) {
    (*iter)->leftatom = NULL;
    (*iter)->rightatom = NULL;
    delete *iter;
  }
  bondVector.clear();
  for (std::vector::iterator iter = atomVector.begin(); iter != atomVector.end();
      ++iter)
    World::getInstance().destroyAtom(*iter);
  atomVector.clear();
}