pool.cc
Go to the documentation of this file.
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
26 #include <catch.hpp>
27 
28 #include <momemta/Pool.h>
29 
30 TEST_CASE("memory pool", "[pool]") {
31  std::unique_ptr<Pool> pool(new Pool());
32 
33  SECTION("Put and get") {
34  InputTag tag("module", "parameter");
35 
36  auto ptr = pool->put<double>(tag);
37  REQUIRE(ptr.get());
38 
39  *ptr = 12.5;
40 
41  auto read = pool->get<double>(tag);
42  REQUIRE(*read == Approx(12.5));
43  }
44 
45  SECTION("Get should allocate memory") {
46  InputTag tag("module", "parameter");
47 
48  auto value = pool->get<double>(tag);
49  REQUIRE(value.get());
50 
51  auto ptr = pool->put<double>(tag);
52  *ptr = 12.5;
53 
54  REQUIRE(*value == Approx(12.5));
55  }
56 
57  SECTION("Put is allowed only once") {
58  InputTag tag("module", "parameter");
59 
60  auto value = pool->put<double>(tag);
61  REQUIRE(value.get());
62 
63  CHECK_THROWS_AS(pool->put<double>(tag), Pool::duplicated_tag_error);
64  }
65 
66  SECTION("Indexed InputTag") {
67  InputTag tag("module", "parameter");
68  InputTag indexed_tag("module", "parameter", 1);
69 
70  auto ptr = pool->put<std::vector<double>>(tag);
71  REQUIRE(ptr.get());
72 
73  ptr->push_back(0);
74  ptr->push_back(1);
75 
76  auto value = pool->get<double>(indexed_tag);
77  REQUIRE(*value == Approx(1));
78  }
79 
80  SECTION("Get should allocate a vector for indexed InputTag") {
81  InputTag tag("module", "parameter", 1);
82 
83  auto value = pool->get<double>(tag);
84 
85  auto ptr = pool->put<std::vector<double>>(tag);
86  ptr->push_back(0);
87  ptr->push_back(1);
88 
89  REQUIRE(*value == Approx(1));
90  }
91 }
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
Definition: Pool.h:40