test_make_tool.cxx
Go to the documentation of this file.
1 // test_make_tool.cxx
2 
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 #include <iomanip>
13 
14 using std::string;
15 using std::cout;
16 using std::endl;
17 using std::ofstream;
18 
19 #undef NDEBUG
20 #include <cassert>
21 
22 //**********************************************************************
23 
24 using std::string;
25 using std::cout;
26 using std::endl;
27 using std::ofstream;
28 using std::ostream;
29 using std::ostringstream;
30 using std::setw;
31 using std::fixed;
32 using std::setprecision;
33 
34 using Index = unsigned int;
35 
36 
37 //**********************************************************************
38 
39 class INotTestTool {
40 
41 public:
42 
43  virtual ~INotTestTool() =default;
44 
45  virtual std::string mytype() { return "INotTestTool"; }
46 
47  virtual std::string label() { return "BadLabel"; }
48 
49  virtual std::string extraLabel() { return "BadExtraLabel"; }
50 
51 private:
52 
53 };
54 
55 
56 //**********************************************************************
57 
58 int test_make_tool(bool doCrash, bool useExistingFcl) {
59  const string myname = "test_make_tool: ";
60  cout << myname << "Starting test" << endl;
61 #ifdef NDEBUG
62  cout << myname << "NDEBUG must be off." << endl;
63  abort();
64 #endif
65  string line = "-----------------------------";
66  string scfg;
67 
68  cout << myname << line << endl;
69  string fclfile = "test_make_tool.fcl";
70  if ( ! useExistingFcl ) {
71  cout << myname << "Creating top-level FCL." << endl;
72  ofstream fout(fclfile.c_str());
73  fout << "tools: {" << endl;
74  fout << " mytool1: {" << endl;
75  fout << " tool_type: TestTool" << endl;
76  fout << " Label: \"Tool 1\"" << endl;
77  fout << " }" << endl;
78  fout << " mytool2: {" << endl;
79  fout << " tool_type: TestTool" << endl;
80  fout << " Label: \"Tool 2\"" << endl;
81  fout << " }" << endl;
82  fout << "}" << endl;
83  fout.close();
84  } else {
85  cout << myname << "Using existing top-level FCL." << endl;
86  }
87 
88  cet::filepath_lookup policy("FHICL_FILE_PATH");
89  auto psTop = fhicl::ParameterSet::make
90  (fhicl::parse_document(fclfile, policy));
91  fhicl::ParameterSet pstools = psTop.get<fhicl::ParameterSet>("tools");
92  fhicl::ParameterSet pstool1 = pstools.get<fhicl::ParameterSet>("mytool1");
93  fhicl::ParameterSet pstool2 = pstools.get<fhicl::ParameterSet>("mytool2");
94 
95  cout << myname << line << endl;
96  cout << "Retrieve with the correct interface type." << endl;
97  std::unique_ptr<ITestTool> pt1 = art::make_tool<ITestTool>(pstool1);
98  cout << myname << "Tool pointer: " << pt1.get() << endl;
99  cout << myname << "Tool type: " << pt1->mytype() << endl;
100  assert( pt1->mytype() == "TestTool" );
101  cout << myname << "Tool label: " << pt1->label() << endl;
102  assert( pt1->label() == "Tool 1" );
103 
104  cout << myname << line << endl;
105  cout << "Retrieve with the incorrect interface type." << endl;
106  bool exceptionRaised = false;
107  bool nullReturn = false;
108  std::unique_ptr<INotTestTool> pt2;
109  try {
110  pt2 = art::make_tool<INotTestTool>(pstool2);
111  } catch (...) {
112  exceptionRaised = true;
113  nullReturn = pt2 == nullptr;
114  pt2.reset(nullptr);
115  }
116  if ( pt2 ) {
117  cout << myname << "Tool retrieval succeeded. This is not good." << endl;
118  cout << myname << "Tool pointer: " << pt2.get() << endl;
119  //assert( pt2.get() == nullptr );
120  cout << myname << "Tool type: " << pt2->mytype() << endl;
121  //assert( pt2->mytype() == "BadTestTool" );
122  cout << myname << "Tool label: " << pt2->label() << endl;
123  assert( pt2->label() == "Tool 2" );
124  if ( doCrash ) {
125  cout << myname << "Trying to fetch extra label..." << endl;
126  cout << myname << "Tool extra label: " << pt2->extraLabel() << endl;
127  }
128  } else {
129  cout << myname << "Tool retrieval failed. As it should." << endl;
130  if ( exceptionRaised ) cout << myname << " Exception was raised." << endl;
131  if ( nullReturn ) cout << myname << " Tool pointer was null." << endl;
132  }
133 
134  cout << myname << line << endl;
135  cout << myname << "Done." << endl;
136  return 0;
137 }
138 
139 //**********************************************************************
140 
141 int main(int narg, char** carg) {
142  bool doCrash = narg > 1;
143  bool keepFcl = narg > 2;
144  int rstat = test_make_tool(doCrash, keepFcl);
145  return rstat;
146 }
int main(int narg, char **carg)
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
virtual ~INotTestTool()=default
unsigned int Index
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
virtual std::string mytype()
int test_make_tool(bool doCrash, bool useExistingFcl)
T get(std::string const &key) const
Definition: ParameterSet.h:271
virtual std::string label()=0
virtual std::string mytype() const
Definition: ITestTool.h:19
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
virtual std::string extraLabel()
virtual std::string label()
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720
void line(double t, double *p, double &x, double &y, double &z)
QTextStream & endl(QTextStream &s)