CYCAMORE
src/sink_tests.cc
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "facility_tests.h"
4 #include "agent_tests.h"
5 #include "resource_helpers.h"
6 #include "infile_tree.h"
7 #include "xml_parser.h"
8 
9 #include "sink_tests.h"
10 
11 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12 void SinkTest::SetUp() {
13  src_facility = new cycamore::Sink(tc_.get());
14  trader = tc_.trader();
16  SetUpSink();
17 }
18 
19 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20 void SinkTest::TearDown() {
21  delete src_facility;
22 }
23 
24 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26  commod1_ = "acommod";
27  commod2_ = "bcommod";
28  commod3_ = "ccommod";
29  capacity_ = 5;
30  inv_ = capacity_ * 2;
31  qty_ = capacity_ * 0.5;
32  ncommods_ = 2;
33 }
34 
35 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36 void SinkTest::SetUpSink() {
41 }
42 
43 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 TEST_F(SinkTest, InitialState) {
45  EXPECT_DOUBLE_EQ(0.0, src_facility->InventorySize());
46  EXPECT_DOUBLE_EQ(capacity_, src_facility->Capacity());
47  EXPECT_DOUBLE_EQ(inv_, src_facility->MaxInventorySize());
48  EXPECT_DOUBLE_EQ(capacity_, src_facility->RequestAmt());
49  EXPECT_DOUBLE_EQ(0.0, src_facility->InventorySize());
50  std::string arr[] = {commod1_, commod2_};
51  std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
52  EXPECT_EQ(vexp, src_facility->input_commodities());
53 
55  double pref[] = {cyclus::kDefaultPref, cyclus::kDefaultPref};
56  std::vector<double> vpref (pref, pref + sizeof(pref) / sizeof(pref[0]) );
57  EXPECT_EQ(vpref, src_facility->input_commodity_preferences());
58 }
59 
60 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61 TEST_F(SinkTest, Clone) {
62  using cycamore::Sink;
63  Sink* cloned_fac = dynamic_cast<cycamore::Sink*>
64  (src_facility->Clone());
65 
66  EXPECT_DOUBLE_EQ(0.0, cloned_fac->InventorySize());
67  EXPECT_DOUBLE_EQ(capacity_, cloned_fac->Capacity());
68  EXPECT_DOUBLE_EQ(inv_, cloned_fac->MaxInventorySize());
69  EXPECT_DOUBLE_EQ(capacity_, cloned_fac->RequestAmt());
70  std::string arr[] = {commod1_, commod2_};
71  std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
72  EXPECT_EQ(vexp, cloned_fac->input_commodities());
73 
74  delete cloned_fac;
75 }
76 
77 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78 TEST_F(SinkTest, DISABLED_XMLInit) {
79  std::stringstream ss;
80  ss << "<start>"
81  << "<name>fooname</name>"
82  << "<config>"
83  << "<UNSPECIFIED>"
84  << "<input>"
85  << " <commodities>"
86  << " <incommodity>" << commod1_ << "</incommodity>"
87  << " <incommodity>" << commod2_ << "</incommodity>"
88  << " </commodities>"
89  << " <input_capacity>" << capacity_ << "</input_capacity>"
90  << " <inventorysize>" << inv_ << "</inventorysize>"
91  << "</input>"
92  << "</UNSPECIFIED>"
93  << "</config>"
94  << "</start>";
95 
96  cyclus::XMLParser p;
97  p.Init(ss);
98  cyclus::InfileTree engine(p);
99  cycamore::Sink fac(tc_.get());
100 
101  // EXPECT_NO_THROW(fac.InitFrom(&engine););
102  std::string arr[] = {commod1_, commod2_};
103  std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
104  EXPECT_EQ(vexp, fac.input_commodities());
105  EXPECT_DOUBLE_EQ(capacity_, fac.Capacity());
106  EXPECT_DOUBLE_EQ(inv_, fac.MaxInventorySize());
107  EXPECT_DOUBLE_EQ(capacity_, fac.RequestAmt());
108  EXPECT_DOUBLE_EQ(0.0, fac.InventorySize());
109 }
110 
111 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112 TEST_F(SinkTest, Requests) {
113  using cyclus::Request;
114  using cyclus::RequestPortfolio;
115  using cyclus::CapacityConstraint;
116  using cyclus::Material;
117 
118  std::string arr[] = {commod1_, commod2_};
119  std::vector<std::string> commods (arr, arr + sizeof(arr) / sizeof(arr[0]) );
120 
122  std::set<RequestPortfolio<Material>::Ptr> ports =
124 
125  ASSERT_EQ(ports.size(), 1);
126  ASSERT_EQ(ports.begin()->get()->qty(), capacity_);
127  const std::vector<Request<Material>*>& requests =
128  ports.begin()->get()->requests();
129  ASSERT_EQ(requests.size(), 2);
130 
131  for (int i = 0; i < ncommods_; ++i) {
132  Request<Material>* req = *(requests.begin() + i);
133  EXPECT_EQ(req->requester(), src_facility);
134  EXPECT_EQ(req->commodity(), commods[i]);
135  }
136 
137  const std::set< CapacityConstraint<Material> >& constraints =
138  ports.begin()->get()->constraints();
139  EXPECT_EQ(constraints.size(), 0);
140 }
141 
142 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143 TEST_F(SinkTest, EmptyRequests) {
144  using cyclus::Material;
145  using cyclus::RequestPortfolio;
146 
148  std::set<RequestPortfolio<Material>::Ptr> ports =
150  EXPECT_TRUE(ports.empty());
151 }
152 
153 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154 TEST_F(SinkTest, Accept) {
155  using cyclus::Bid;
156  using cyclus::Material;
157  using cyclus::Request;
158  using cyclus::Trade;
159  using test_helpers::get_mat;
160 
161  double qty = qty_ * 2;
162  std::vector< std::pair<cyclus::Trade<cyclus::Material>,
163  cyclus::Material::Ptr> > responses;
164 
165  Request<Material>* req1 =
166  Request<Material>::Create(get_mat(922350000, qty_), src_facility,
167  commod1_);
168  Bid<Material>* bid1 = Bid<Material>::Create(req1, get_mat(), trader);
169 
170  Request<Material>* req2 =
171  Request<Material>::Create(get_mat(922350000, qty_), src_facility,
172  commod2_);
173  Bid<Material>* bid2 =
174  Bid<Material>::Create(req2, get_mat(922350000, qty_), trader);
175 
176  Trade<Material> trade1(req1, bid1, qty_);
177  responses.push_back(std::make_pair(trade1, get_mat(922350000, qty_)));
178  Trade<Material> trade2(req2, bid2, qty_);
179  responses.push_back(std::make_pair(trade2, get_mat(922350000, qty_)));
180 
181  EXPECT_DOUBLE_EQ(0.0, src_facility->InventorySize());
182  src_facility->AcceptMatlTrades(responses);
183  EXPECT_DOUBLE_EQ(qty, src_facility->InventorySize());
184 }
185 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186 TEST_F(SinkTest, InRecipe){
187 // Create a context
188  using cyclus::RequestPortfolio;
189  using cyclus::Material;
190  using cyclus::Request;
191  cyclus::Recorder rec;
192  cyclus::Timer ti;
193  cyclus::Context ctx(&ti, &rec);
194 
195  // define some test material in the context
196  cyclus::CompMap m;
197  m[922350000] = 1;
198  m[922580000] = 2;
199 
200  cyclus::Composition::Ptr c = cyclus::Composition::CreateFromMass(m);
201  ctx.AddRecipe("some_u",c) ;
202 
203  // create a sink facility to interact with the DRE
204  cycamore::Sink* snk = new cycamore::Sink(&ctx);
205  snk->AddCommodity("some_u");
206  snk->EnterNotify();
207 
208  std::set<RequestPortfolio<Material>::Ptr> ports =
209  snk->GetMatlRequests();
210  ASSERT_EQ(ports.size(), 1);
211 
212  const std::vector<Request<Material>*>& requests =
213  ports.begin()->get()->requests();
214  ASSERT_EQ(requests.size(), 1);
215 
216  Request<Material>* req = *requests.begin();
217  EXPECT_EQ(req->requester(), snk);
218  EXPECT_EQ(req->commodity(),"some_u");
219 }
220 
221 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222 TEST_F(SinkTest, BidPrefs) {
223  using cyclus::QueryResult;
224  using cyclus::Cond;
225 
226  std::string config =
227  " <in_commods>"
228  " <val>commods_1</val>"
229  " <val>commods_2</val>"
230  " </in_commods>"
231  " <in_commod_prefs>"
232  " <val>10</val> "
233  " <val>1</val> "
234  " </in_commod_prefs>"
235  " <capacity>1</capacity>"
236  " <input_capacity>1.0</input_capacity> ";
237 
238  int simdur = 1;
239  cyclus::MockSim sim(cyclus::AgentSpec
240  (":cycamore:Sink"), config, simdur);
241 
242  sim.AddSource("commods_1")
243  .capacity(1)
244  .Finalize();
245 
246  sim.AddSource("commods_2")
247  .capacity(1)
248  .Finalize();
249 
250  int id = sim.Run();
251 
252  std::vector<Cond> conds;
253  conds.push_back(Cond("Commodity", "==", std::string("commods_1")));
254  QueryResult qr = sim.db().Query("Transactions", &conds);
255 
256  // should trade only with #1 since it has highier priority
257  EXPECT_EQ(1, qr.rows.size());
258 
259  std::vector<Cond> conds2;
260  conds2.push_back(Cond("Commodity", "==", std::string("commods_2")));
261  QueryResult qr2 = sim.db().Query("Transactions", &conds2);
262 
263  // should trade only with #1 since it has highier priority
264  EXPECT_EQ(0, qr2.rows.size());
265 
266 }
267 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
268 TEST_F(SinkTest, Print) {
269  EXPECT_NO_THROW(std::string s = src_facility->str());
270 }
271 
272 TEST_F(SinkTest, PositionInitialize) {
273  using cyclus::QueryResult;
274  using cyclus::Cond;
275 
276  std::string config =
277  " <in_commods>"
278  " <val>commods_1</val>"
279  " <val>commods_2</val>"
280  " </in_commods>"
281  " <in_commod_prefs>"
282  " <val>10</val> "
283  " <val>1</val> "
284  " </in_commod_prefs>"
285  " <capacity>1</capacity>"
286  " <input_capacity>1.0</input_capacity> ";
287 
288  int simdur = 1;
289  cyclus::MockSim sim(cyclus::AgentSpec
290  (":cycamore:Sink"), config, simdur);
291 
292  sim.AddSource("commods_1")
293  .capacity(1)
294  .Finalize();
295 
296  sim.AddSource("commods_2")
297  .capacity(1)
298  .Finalize();
299 
300  int id = sim.Run();
301 
302  QueryResult qr = sim.db().Query("AgentPosition", NULL);
303  EXPECT_EQ(qr.GetVal<double>("Latitude"), 0.0);
304  EXPECT_EQ(qr.GetVal<double>("Longitude"), 0.0);
305 }
306 
307 TEST_F(SinkTest, PositionInitialize2) {
308  using cyclus::QueryResult;
309  using cyclus::Cond;
310 
311  std::string config =
312  " <in_commods>"
313  " <val>commods_1</val>"
314  " <val>commods_2</val>"
315  " </in_commods>"
316  " <in_commod_prefs>"
317  " <val>10</val> "
318  " <val>1</val> "
319  " </in_commod_prefs>"
320  " <capacity>1</capacity>"
321  " <input_capacity>1.0</input_capacity> "
322  " <latitude>50.0</latitude> "
323  " <longitude>35.0</longitude> ";
324 
325  int simdur = 1;
326  cyclus::MockSim sim(cyclus::AgentSpec
327  (":cycamore:Sink"), config, simdur);
328 
329  sim.AddSource("commods_1")
330  .capacity(1)
331  .Finalize();
332 
333  sim.AddSource("commods_2")
334  .capacity(1)
335  .Finalize();
336 
337  int id = sim.Run();
338 
339  QueryResult qr = sim.db().Query("AgentPosition", NULL);
340  EXPECT_EQ(qr.GetVal<double>("Longitude"), 35.0);
341  EXPECT_EQ(qr.GetVal<double>("Latitude"), 50.0);
342 
343 }
344 
345 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
346 cyclus::Agent* SinkConstructor(cyclus::Context* ctx) {
347  return new cycamore::Sink(ctx);
348 }
349 
350 // required to get functionality in cyclus agent unit tests library
351 #ifndef CYCLUS_AGENT_TESTS_CONNECTED
352 int ConnectAgentTests();
354 #define CYCLUS_AGENT_TESTS_CONNECTED cyclus_agent_tests_connected
355 #endif // CYCLUS_AGENT_TESTS_CONNECTED
356 
357 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
358 INSTANTIATE_TEST_CASE_P(SinkFac, FacilityTests, Values(&SinkConstructor));
359 INSTANTIATE_TEST_CASE_P(SinkFac, AgentTests, Values(&SinkConstructor));
virtual std::set< cyclus::RequestPortfolio< cyclus::Material >::Ptr > GetMatlRequests()
SinkFacilities request Materials of their given commodity.
static int cyclus_agent_tests_connected
This facility acts as a sink of materials and products with a fixed throughput (per time step) capaci...
double RequestAmt() const
determines the amount to request
double InventorySize() const
const std::vector< std::string > & input_commodities() const
virtual cyclus::Agent * Clone()
void AddCommodity(std::string name)
add a commodity to the set of input commodities
void Capacity(double cap)
sets the capacity of a material generated at any given time step
cyclus::Agent * SinkConstructor(cyclus::Context *ctx)
const std::vector< double > & input_commodity_preferences() const
INSTANTIATE_TEST_CASE_P(SinkFac, FacilityTests, Values(&SinkConstructor))
cycamore::GrowthRegion string
cyclus::TestContext tc_
int ConnectAgentTests()
TEST_F(SinkTest, InitialState)
double MaxInventorySize() const
virtual void AcceptMatlTrades(const std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
SinkFacilities place accepted trade Materials in their Inventory.
void SetMaxInventorySize(double size)
sets the size of the storage inventory for received material
virtual std::string str()
cycamore::Sink * src_facility