CYCAMORE
Loading...
Searching...
No Matches
build/cycamore/source_tests.cc
Go to the documentation of this file.
1#include "source_tests.h"
2
3#include <gtest/gtest.h>
4
5#include <sstream>
6
7#include "cyc_limits.h"
8#include "resource_helpers.h"
9#include "test_context.h"
10
11namespace cycamore {
12
14 src_facility = new cycamore::Source(tc.get());
15 trader = tc.trader();
18 src_facility->Build(NULL);
19}
20
22 delete src_facility;
23}
24
26 commod = "commod";
27 recipe_name = "recipe";
28 package_name = "testpackage";
29 tu_name = "testtu";
30 capacity = 5; // some magic number..
31
32 recipe = cyclus::Composition::CreateFromAtom(cyclus::CompMap());
33 tc.get()->AddRecipe(recipe_name, recipe);
34}
35
41
43 cyclus::Context* ctx = tc.get();
44 cycamore::Source* cloned_fac = dynamic_cast<cycamore::Source*>
45 (src_facility->Clone());
46
47 EXPECT_EQ(outcommod(src_facility), outcommod(cloned_fac));
48 EXPECT_EQ(throughput(src_facility), throughput(cloned_fac));
49 EXPECT_EQ(outrecipe(src_facility), outrecipe(cloned_fac));
50
51 delete cloned_fac;
52}
53
55 EXPECT_NO_THROW(std::string s = src_facility->str());
56}
57
58TEST_F(SourceTest, AddBids) {
59 using cyclus::Bid;
60 using cyclus::BidPortfolio;
61 using cyclus::CapacityConstraint;
62 using cyclus::ExchangeContext;
63 using cyclus::Material;
64
65 int nreqs = 5;
66
67 boost::shared_ptr< ExchangeContext<Material> >
68 ec = GetContext(nreqs, commod);
69
70 src_facility->EnterNotify();
71
72 std::set<BidPortfolio<Material>::Ptr> ports =
73 src_facility->GetMatlBids(ec.get()->commod_requests);
74
75 ASSERT_TRUE(ports.size() > 0);
76 EXPECT_EQ(ports.size(), 1);
77
78 BidPortfolio<Material>::Ptr port = *ports.begin();
79 EXPECT_EQ(port->bidder(), src_facility);
80 EXPECT_EQ(port->bids().size(), nreqs);
81
82 const std::set< CapacityConstraint<Material> >& constrs = port->constraints();
83 ASSERT_TRUE(constrs.size() > 0);
84 EXPECT_EQ(constrs.size(), 1);
85 EXPECT_EQ(*constrs.begin(), CapacityConstraint<Material>(capacity));
86}
87
88TEST_F(SourceTest, Response) {
89 using cyclus::Bid;
90 using cyclus::Material;
91 using cyclus::Request;
92 using cyclus::Trade;
93 using test_helpers::get_mat;
94
95 std::vector< Trade<Material> > trades;
96 std::vector<std::pair<Trade<Material>,
97 Material::Ptr> > responses;
98
99 // Null response
100 EXPECT_NO_THROW(src_facility->GetMatlTrades(trades, responses));
101 EXPECT_EQ(responses.size(), 0);
102
103 double qty = capacity / 3;
104 Request<Material>* request =
105 Request<Material>::Create(get_mat(), trader, commod);
106 Bid<Material>* bid =
107 Bid<Material>::Create(request, get_mat(), src_facility);
108
109 Trade<Material> trade(request, bid, qty);
110 trades.push_back(trade);
111
112 // 1 trade
113 src_facility->GetMatlTrades(trades, responses);
114 EXPECT_EQ(responses.size(), 1);
115 EXPECT_EQ(responses[0].second->quantity(), qty);
116 EXPECT_EQ(responses[0].second->comp(), recipe);
117
118 // 2 trades, total qty = capacity
119 trades.push_back(trade);
120 responses.clear();
121 EXPECT_NO_THROW(src_facility->GetMatlTrades(trades, responses));
122 EXPECT_EQ(responses.size(), 2);
123
124 // reset!
125 src_facility->Tick();
126
127 delete request;
128 delete bid;
129}
130
131TEST_F(SourceTest, PositionInitialize) {
132 std::string config =
133 "<outcommod>spent_fuel</outcommod>"
134 ;
135 int simdur = 3;
136 cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Source"), config, simdur);
137 int id = sim.Run();
138
139 cyclus::QueryResult qr = sim.db().Query("AgentPosition", NULL);
140 EXPECT_EQ(qr.GetVal<double>("Latitude"), 0.0);
141 EXPECT_EQ(qr.GetVal<double>("Longitude"), 0.0);
142
143}
144
145TEST_F(SourceTest, Longitude) {
146 std::string config =
147 "<outcommod>spent_fuel</outcommod>"
148 "<latitude>-0.01</latitude>"
149 "<longitude>0.01</longitude>"
150 ;
151 int simdur = 3;
152 cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Source"), config, simdur);
153 int id = sim.Run();
154
155 cyclus::QueryResult qr = sim.db().Query("AgentPosition", NULL);
156 EXPECT_EQ(qr.GetVal<double>("Latitude"), -0.01);
157 EXPECT_EQ(qr.GetVal<double>("Longitude"), 0.01);
158
159}
160
162 using cyclus::QueryResult;
163 using cyclus::Cond;
164
165 std::string config =
166 "<outcommod>commod</outcommod>"
167 "<outrecipe>recipe</outrecipe>"
168 "<package>testpackage</package>"
169 "<throughput>5</throughput>";
170
171 int simdur = 3;
172 cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Source"), config, simdur);
173
174 sim.context()->AddRecipe(recipe_name, recipe);
175 sim.context()->AddPackage(package_name, 3, 4, "first");
176 package = sim.context()->GetPackage(package_name);
177
178 sim.AddSink("commod").Finalize();
179
180 EXPECT_NO_THROW(sim.Run());
181
182 QueryResult qr_tr = sim.db().Query("Transactions", NULL);
183 EXPECT_EQ(qr_tr.rows.size(), 3);
184
185 std::vector<Cond> conds;
186 conds.push_back(Cond("PackageName", "==", package->name()));
187 QueryResult qr_res = sim.db().Query("Resources", &conds);
188
189 EXPECT_EQ(qr_res.rows.size(), 3);
190}
191
192TEST_F(SourceTest, TransportUnit) {
193 using cyclus::QueryResult;
194 using cyclus::Cond;
195
196 std::string config =
197 "<outcommod>commod</outcommod>"
198 "<outrecipe>recipe</outrecipe>"
199 "<package>testpackage</package>"
200 "<transport_unit>testtu</transport_unit>"
201 "<throughput>10</throughput>";
202
203 int simdur = 2;
204 cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Source"), config, simdur);
205
206 sim.context()->AddRecipe(recipe_name, recipe);
207 sim.context()->AddPackage(package_name, 3, 4, "equal");
208 package = sim.context()->GetPackage(package_name);
209 sim.context()->AddTransportUnit(tu_name, 2, 2);
210 tu = sim.context()->GetTransportUnit(tu_name);
211
212 sim.AddSink("commod").Finalize();
213
214 EXPECT_NO_THROW(sim.Run());
215
216 QueryResult qr_tr = sim.db().Query("Transactions", NULL);
217 EXPECT_EQ(qr_tr.rows.size(), 4);
218
219 std::vector<Cond> conds;
220 conds.push_back(Cond("PackageName", "==", package->name()));
221 QueryResult qr_res = sim.db().Query("Resources", &conds);
222
223 EXPECT_EQ(qr_res.rows.size(), 4);
224
225 QueryResult qr_allres = sim.db().Query("Resources", NULL);
226}
227
228boost::shared_ptr< cyclus::ExchangeContext<cyclus::Material> >
229SourceTest::GetContext(int nreqs, std::string commod) {
230 using cyclus::Material;
231 using cyclus::Request;
232 using cyclus::ExchangeContext;
233 using test_helpers::get_mat;
234
235 double qty = 3;
236 boost::shared_ptr< ExchangeContext<Material> >
237 ec(new ExchangeContext<Material>());
238 for (int i = 0; i < nreqs; i++) {
239 ec->AddRequest(Request<Material>::Create(get_mat(), trader, commod));
240 }
241 return ec;
242}
243
244} // namespace cycamore
245
246cyclus::Agent* SourceConstructor(cyclus::Context* ctx) {
247 return new cycamore::Source(ctx);
248}
249
250// required to get functionality in cyclus agent unit tests library
251#ifndef CYCLUS_AGENT_TESTS_CONNECTED
254#define CYCLUS_AGENT_TESTS_CONNECTED cyclus_agent_tests_connected
255#endif // CYCLUS_AGENT_TESTS_CONNECTED
256
257INSTANTIATE_TEST_SUITE_P(SourceFac, FacilityTests, Values(&SourceConstructor));
258INSTANTIATE_TEST_SUITE_P(SourceFac, AgentTests, Values(&SourceConstructor));
259
static int cyclus_agent_tests_connected
INSTANTIATE_TEST_SUITE_P(DeployInst, InstitutionTests, Values(&DeployInstitutionConstructor))
int ConnectAgentTests()
cyclus::Agent * SourceConstructor(cyclus::Context *ctx)
std::string outrecipe(cycamore::Source *s)
cyclus::Composition::Ptr recipe
boost::shared_ptr< cyclus::ExchangeContext< cyclus::Material > > GetContext(int nreqs, std::string commodity)
std::string outcommod(cycamore::Source *s)
double throughput(cycamore::Source *s)
This facility acts as a source of material with a fixed throughput (per time step) capacity and a lif...
virtual void Build(cyclus::Agent *parent)
— Facility Members — perform module-specific tasks when entering the simulation
TEST_F(EnrichmentTest, RequestQty)