gtestRegistry.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \program gtestRegistry
5 
6 \brief Program used for testing / debugging GENIE's Registry
7 
8 \author Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
9  University of Liverpool & STFC Rutherford Appleton Laboratory
10 
11 \created May 4, 2004
12 
13 \cpright Copyright (c) 2003-2020, The GENIE Collaboration
14  For the full text of the license visit http://copyright.genie-mc.org
15 
16 */
17 //____________________________________________________________________________
18 
19 #include <TH1F.h>
20 
23 
24 using namespace genie;
25 
26 int main(int /*argc*/, char ** /*argv*/)
27 {
28  // manual override of Registry mesg level
29  //Messenger * msg = Messenger::Instance();
30  //msg->SetPriorityLevel("Registry", pDEBUG);
31 
32  // some objects stored at the test configuration registries
33  RgAlg some_algorithm("alg-name","alg-config");
34 
35  TH1F * h1 = new TH1F("h1","a TH1F to be passed as config param", 100,-5,5);
36  h1->Fill(3,100);
37  h1->Fill(2,50);
38 
39 
40 
41  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  // Basic Tests:
43  // Build a Registry, unlock it, add some vars, lock it, print it.
44  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45  LOG("test", pINFO) << "***** Basic Registry tests *****";
46 
47  LOG("test", pINFO) << "Building, Unlocking, Setting, Locking, Printing...";
48 
49  Registry registry("example-registry");
50 
51  registry.UnLock();
52 
53  registry.Set("var-bool-1", true);
54  registry.Set("var-double-1", 2.289);
55  registry.Set("var-double-2", 4.190);
56  registry.Set("var-int-1", 0);
57  registry.Set("var-int-2", 11);
58  registry.Set(string("var-th1f-1"), h1);
59  registry.Set("myalg", some_algorithm);
60 
61  registry.Lock();
62 
63  LOG("test", pINFO) << "registry:\n" << registry;
64 
65  // try to override var-int-2 and add a new var-int-3 to the locked registry
66 
67  LOG("test", pINFO)
68  << "Trying to set variables in a locked registry - should fail";
69 
70  registry.Set("var-int-2", 12);
71  registry.Set("var-int-3", 89);
72 
73  LOG("test", pINFO) << "registry:\n" << registry;
74 
75  // do the same and add a couple of string vars, but now unlock it first
76 
77  LOG("test", pINFO)
78  << "Unlock the registry first and then set the variables - should succeed";
79 
80  registry.UnLock();
81 
82  registry.Set("var-int-2", 12);
83  registry.Set("var-int-3", 89);
84  registry.Set("var-string-1", string("this is a string"));
85  registry.Set("var-string-2", string("and this is another string"));
86 
87  registry.Lock();
88 
89  LOG("test", pINFO) << "registry\n" << registry;
90 
91  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92  // Testing copy constructor
93  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94 
95  LOG("test", pINFO)
96  << "***** Testing copy constructor: Registry registry2(registry) *****";
97 
98  Registry registry2(registry);
99 
100  LOG("test", pINFO) << "Registry clone: \n" << registry2;
101  LOG("test", pINFO) << "Original registry: \n" << registry;
102 
103  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104  // Testing operator () overloading
105  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 
107  LOG("test", pINFO) << "***** Testing operator () *****";
108 
109  registry2.UnLock();
110 
111  registry2("added an integer with ()", 7 );
112  registry2("added a boolean with ()", true );
113  registry2("added a double with ()", 3.14 );
114  registry2("added a string with ()", "ok" );
115 
116  registry2.Lock();
117 
118  LOG("test", pINFO) << "registry:\n" << registry2;
119 
120  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121  // Testing data retrieval
122  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123 
124  LOG("test", pINFO) << "***** Testing data retrieval *****";
125 
126  bool retrieved_bool = false;
127  int retrieved_int = 0;
128  double retrieved_double = 3.14;
129  string retrieved_string = "random init";
130 
131  registry.Get("var-bool-1", retrieved_bool );
132  registry.Get("var-int-1", retrieved_int );
133  registry.Get("var-double-1", retrieved_double);
134  registry.Get("var-string-1", retrieved_string);
135 
136  LOG("test", pINFO) << "retrieved-bool = " << retrieved_bool;
137  LOG("test", pINFO) << "retrieved-int = " << retrieved_int;
138  LOG("test", pINFO) << "retrieved-double = " << retrieved_double;
139  LOG("test", pINFO) << "retrieved-string = " << retrieved_string;
140 
141  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142  // Test Copy()
143  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144 
145  LOG("test", pINFO) << "***** Testing Copy(const Registry & reg) *****";
146 
147  Registry registry3; // create an empty registry
148  registry3.Copy(registry2); // copy registry2 contents to registry3
149 
150  LOG("test", pINFO) << "Registry clone: \n" << registry3;
151  LOG("test", pINFO) << "Original registry: \n" << registry2;
152 
153  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154  // Test Individual Item Locking
155  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156 
157  LOG("test", pINFO) << "***** Testing individual item locking *****";
158 
159  Registry registry4("example-registry-with-locked-items");
160 
161  registry4.UnLock();
162 
163  registry4("an int variable", 19 );
164  registry4("a bool variable", true );
165  registry4("a double variable", 2.71 );
166  registry4("a string variable", "hello" );
167 
168  LOG("test", pINFO)
169  << "Initial registry: \n" << registry4;
170 
171  // lock two of the variables
172 
173  registry4.LockItem("an int variable");
174  registry4.LockItem("a double variable");
175 
176  LOG("test", pINFO)
177  << "Registry with locked keys: \n" << registry4;
178 
179  LOG("test", pINFO)
180  << "Attempting to change locked items in unlocked registry";
181 
182  // try to change the locked variables - this should fail even though
183  // the registry itself is unclocked
184 
185  registry4.Set("an int variable", 25);
186  registry4.Set("a double variable", 129320.21);
187 
188  LOG("test", pINFO)
189  << "Should have failed to change locked entries: \n" << registry4;
190 
191  // inhibit individual item locking
192 
193  LOG("test", pINFO) << "Inhibit item locking";
194  registry4.InhibitItemLocks();
195 
196  LOG("test", pINFO)
197  << "registry with item locks inhibited: \n" << registry4;
198 
199  // re-try to change the locked variables
200  LOG("test", pINFO)
201  << "Retrying to change locked items in unlocked registry with inhibited item locking";
202 
203  registry4.Set("an int variable", 25);
204  registry4.Set("a double variable", 9.21);
205 
206  LOG("test", pINFO) << "registry: \n" << registry4;
207 
208  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209  // Test Assignment operator =
210  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211 
212  LOG("test", pINFO) << "***** Testing operator = *****";
213 
214  Registry & registry5 = registry4;
215 
216  LOG("test", pINFO) << "Printing registry set with the assignment operator = ";
217  LOG("test", pINFO) << "registry: \n" << registry5;
218 
219  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220  // Test operator +=
221  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222 
223  LOG("test", pINFO) << "***** Testing operator += *****";
224 
225  registry5 += registry;
226 
227  LOG("test", pINFO) << "Printing registry after adding values with the += operator ";
228  LOG("test", pINFO) << "registry: \n" << registry5;
229 
230  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231  // Test FindKeys()
232  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233 
234  LOG("test", pINFO) << "***** Testing FindKeys() *****";
235 
236  LOG("test", pINFO) << "Looking for all entries whose key contain the word `variable'";
237  RgKeyList klist = registry5.FindKeys("variable");
238 
239  LOG("test", pINFO) << "Found " << klist.size() << " entries";
240  RgKeyList::const_iterator kiter = klist.begin();
241  for( ; kiter != klist.end(); ++kiter) {
242  LOG("test", pINFO) << "Matching key: " << *kiter;
243  }
244 
245  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
246  // Test ItemType()
247  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
248 
249  LOG("test", pINFO) << " ";;
250  LOG("test", pINFO) << "***** Testing ItemType() *****";
251 
252  LOG("test", pINFO)
253  << "Type of var pointed to by key = `var-int-2' is: "
254  << RgType::AsString(registry5.ItemType("var-int-2"));
255  LOG("test", pINFO)
256  << "Type of var pointed to by key = `var-string-1' is: "
257  << RgType::AsString(registry5.ItemType("var-string-1"));
258  LOG("test", pINFO)
259  << "Type of var pointed to by key = `var-th1f-1' is: "
260  << RgType::AsString(registry5.ItemType("var-th1f-1"));
261  LOG("test", pINFO)
262  << "Type of var pointed to by key = `??&&bla bla ###@ :-)' is: "
263  << RgType::AsString(registry5.ItemType("??&&bla bla ###@ :-)"));
264 
265 
266  LOG("test", pINFO) << "Done!!";
267 
268  return 0;
269 }
270 
static string AsString(RgType_t rt)
THE MAIN GENIE PROJECT NAMESPACE
Definition: AlgCmp.h:25
int main(int, char **)
intermediate_table::const_iterator const_iterator
void InhibitItemLocks(void)
override individual item locks
Definition: Registry.cxx:163
void LockItem(RgKey key)
locks the registry item
Definition: Registry.cxx:234
void Copy(const Registry &)
copy the input registry
Definition: Registry.cxx:722
RgType_t ItemType(RgKey key) const
return item type
Definition: Registry.cxx:829
void Get(RgKey key, const RegistryItemI *&item) const
Definition: Registry.cxx:325
RgKeyList FindKeys(RgKey key_part) const
create list with all keys containing &#39;key_part&#39;
Definition: Registry.cxx:840
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
#define pINFO
Definition: Messenger.h:62
void Lock(void)
locks the registry
Definition: Registry.cxx:148
vector< RgKey > RgKeyList
Definition: Registry.h:50
void UnLock(void)
unlocks the registry (doesn&#39;t unlock items)
Definition: Registry.cxx:153
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:65
void Set(RgIMapPair entry)
Definition: Registry.cxx:267