Skip to content

Commit

Permalink
Bonded interactions refactoring: add BondedInteractionsMap unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
biermanncarl committed Oct 7, 2021
1 parent 2fdce04 commit ec54494
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ unit_test(NAME thermostats_test SRC thermostats_test.cpp DEPENDS EspressoCore)
unit_test(NAME random_test SRC random_test.cpp DEPENDS EspressoUtils Random123)
unit_test(NAME BondList_test SRC BondList_test.cpp DEPENDS EspressoCore)
unit_test(NAME energy_test SRC energy_test.cpp DEPENDS EspressoCore)
unit_test(NAME bonded_interactions_map_test SRC
bonded_interactions_map_test.cpp DEPENDS EspressoCore)
75 changes: 75 additions & 0 deletions src/core/unit_tests/bonded_interactions_map_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2021 The ESPResSo project
*
* This file is part of ESPResSo.
*
* ESPResSo 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 3 of the License, or
* (at your option) any later version.
*
* ESPResSo 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define BOOST_TEST_MODULE BondedInteractionsMap test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <unordered_map>

#include "bonded_interactions/angle_cosine.hpp"
#include "bonded_interactions/bonded_interaction_data.hpp"
#include "bonded_interactions/fene.hpp"

BOOST_AUTO_TEST_CASE(insert_bond_types) {
BondedInteractionsMap bond_map{};
std::unordered_map<int, std::shared_ptr<Bonded_IA_Parameters>> mock_core{};
// check defaulted maps are empty
BOOST_TEST(bond_map.empty());
BOOST_TEST(mock_core.empty());
// insert first element
int first_key = 1;
auto const fene_bond = FeneBond(1.2, 3.0, 0.8);
auto const fene_bond_ia = std::make_shared<Bonded_IA_Parameters>(fene_bond);
bond_map.insert(first_key, fene_bond_ia);
mock_core[first_key] = fene_bond_ia;
BOOST_TEST(bond_map.at(first_key) == fene_bond_ia);
BOOST_TEST(mock_core.at(first_key) == fene_bond_ia);
BOOST_REQUIRE_EQUAL(bond_map.size(), 1);
BOOST_REQUIRE_EQUAL(mock_core.size(), 1);
// insert second element
auto const acos_bond = AngleCosineBond(4.5, 3.14);
auto const acos_bond_ia = std::make_shared<Bonded_IA_Parameters>(acos_bond);
auto second_key = bond_map.insert(acos_bond_ia);
BOOST_TEST(first_key != second_key);
mock_core[second_key] = acos_bond_ia;
BOOST_TEST(bond_map.at(second_key) == acos_bond_ia);
BOOST_TEST(mock_core.at(second_key) == acos_bond_ia);
BOOST_REQUIRE_EQUAL(bond_map.size(), 2);
BOOST_REQUIRE_EQUAL(mock_core.size(), 2);
// try to access non-existent element
BOOST_CHECK_THROW(bond_map.at(bond_map.get_next_key()), std::out_of_range);
BOOST_CHECK_THROW(mock_core.at(bond_map.get_next_key()), std::out_of_range);
// check that both elements are "contained" in the map
BOOST_TEST(bond_map.contains(first_key));
BOOST_TEST(bond_map.contains(second_key));

BOOST_REQUIRE_EQUAL(bond_map.count(first_key), 1);
BOOST_REQUIRE_EQUAL(mock_core.count(first_key), 1);
BOOST_REQUIRE_EQUAL(bond_map.count(second_key), 1);
BOOST_REQUIRE_EQUAL(mock_core.count(second_key), 1);
BOOST_REQUIRE_EQUAL(bond_map.count(bond_map.get_next_key()), 0);
BOOST_REQUIRE_EQUAL(mock_core.count(bond_map.get_next_key()), 0);

// delete an element
bond_map.erase(first_key);
mock_core.erase(first_key);
BOOST_TEST(!bond_map.contains(first_key));
BOOST_REQUIRE_EQUAL(bond_map.size(), 1);
BOOST_REQUIRE_EQUAL(mock_core.size(), 1);
}

0 comments on commit ec54494

Please sign in to comment.