]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/trigger/test/testAliHLTTriggerCounters.C
Coverity
[u/mrichter/AliRoot.git] / HLT / trigger / test / testAliHLTTriggerCounters.C
1 // $Id: $
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        *
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Artur Szostak <artursz@iafrica.com>                   *
8  *                  for The ALICE HLT Project.                            *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /// @file   testAliHLTTriggerCounters.C
20 /// @author Artur Szostak <artursz@iafrica.com>
21 /// @date   28 Oct 2010
22 /// @brief  Test program for the AliHLTTriggerCounters class.
23 ///
24
25 #if !defined(__CINT__) || defined(__MAKECINT__)
26 #include "AliHLTTriggerCounters.h"
27 #include "TObjArray.h"
28 #include "TString.h"
29 #include "Riostream.h"
30 #endif
31
32 /**
33  * Tests basic functionality of the AliHLTTriggerCounters::AliCounter class.
34  */
35 bool CheckCounterItemClass()
36 {
37         AliHLTTriggerCounters::AliCounter s1("s1", "counter one", 1, 0.1);
38         AliHLTTriggerCounters::AliCounter s2("s2", "counter two", 2, 0.2);
39         AliHLTTriggerCounters::AliCounter s3("s2", "counter two", 3, 0.2);
40         if (TString(s1.GetName()) != s1.Name())
41         {
42                 cerr << "ERROR: AliHLTTriggerCounters::AliCounter::GetName() returns a different value than AliHLTTriggerCounters::AliCounter::Name()." << endl;
43                 return false;
44         }
45         if (TString(s1.GetTitle()) != s1.Description())
46         {
47                 cerr << "ERROR: AliHLTTriggerCounters::AliCounter::GetTitle() returns a different value than AliHLTTriggerCounters::AliCounter::Description()." << endl;
48                 return false;
49         }
50         if (s2 == s3)
51         {
52                 cerr << "ERROR: equals operator for AliHLTTriggerCounters::AliCounter returns the wrong value." << endl;
53                 return false;
54         }
55         if (! s2.IsEqual(&s3))
56         {
57                 cerr << "ERROR: AliHLTTriggerCounters::AliCounter::IsEqual returns the wrong value." << endl;
58                 return false;
59         }
60         s2.Increment();
61         if (! (s2 == s3))
62         {
63                 cerr << "ERROR: equals operator for AliHLTTriggerCounters::AliCounter returns the wrong value." << endl;
64                 return false;
65         }
66         TObjArray list;
67         list.Add(&s2);
68         list.Add(&s1);
69         list.Sort();
70         if (TString(list.At(0)->GetName()) != "s1")
71         {
72                 cerr << "ERROR: Sorting objects of type AliHLTTriggerCounters::AliCounter is not working correctly." << endl;
73                 return false;
74         }
75         return true;
76 }
77
78 /**
79  * Tests functionality of the AliHLTTriggerCounters class.
80  */
81 bool CheckCountersListClass()
82 {
83         AliHLTTriggerCounters s;
84         s.Add("a", "one", 1);
85         s.Add("b", "two", 2, 5);
86         if (s.NumberOfScalars() != 2)
87         {
88                 cerr << "ERROR: The number of added counters is wrong for class AliHLTTriggerCounters." << endl;
89                 return false;
90         }
91         if (! s.Exists("a"))
92         {
93                 cerr << "ERROR: AliHLTTriggerCounters claims counter 'a' does not exist event though it was added." << endl;
94                 return false;
95         }
96         if (! s.Exists("b"))
97         {
98                 cerr << "ERROR: AliHLTTriggerCounters claims counter 'b' does not exist event though it was added." << endl;
99                 return false;
100         }
101         s.Remove("a");
102         if (s.Exists("a"))
103         {
104                 cerr << "ERROR: AliHLTTriggerCounters claims counter 'a' does not exist event though it was removed." << endl;
105                 return false;
106         }
107         s.Add("a", "one", 1);
108         const AliHLTTriggerCounters& p = s;
109         if (p.GetCounter("a").Rate() != 1)
110         {
111                 cerr << "ERROR: Constant version of AliHLTTriggerCounters::GetCounter(\"a\") returns the wrong counter object." << endl;
112                 return false;
113         }
114         if (TString(p.GetCounter("c").Name()) != "" || TString(p.GetCounter("c").Description()) != "" || p.GetCounter("c").Rate() != 0 || p.GetCounter("c").Counter() != 0)
115         {
116                 cerr << "ERROR: Constant version of AliHLTTriggerCounters::GetCounter(\"c\") does not return a sentinel object." << endl;
117                 return false;
118         }
119         if (s.GetCounter("a").Rate() != 1)
120         {
121                 cerr << "ERROR: AliHLTTriggerCounters::GetCounter(\"a\") returns the wrong counter object." << endl;
122                 return false;
123         }
124         s.GetCounter("c").Value(3);
125         if (TString(s.GetCounter("c").Name()) != "c" || TString(s.GetCounter("c").Description()) != "")
126         {
127                 cerr << "ERROR: AliHLTTriggerCounters::GetCounter(\"c\") does not create a new object." << endl;
128                 return false;
129         }
130         s.Add("c", "three", 33, 7);
131         if (s.GetCounter("c").Rate() != 33 || s.GetCounter("c").Counter() != 7 || TString(s.GetCounter("c").Description()) != "")
132         {
133                 cerr << "ERROR: AliHLTTriggerCounters::Add did not update an exisiting counter correctly." << endl;
134                 return false;
135         }
136         if (TString(p.GetCounterN(0).Name()) != "b" || TString(p.GetCounterN(1).Name()) != "a" || TString(p.GetCounterN(2).Name()) != "c")
137         {
138                 cerr << "ERROR: Constant version of AliHLTTriggerCounters::GetCounterN(0) returns the wrong counter object." << endl;
139                 return false;
140         }
141         if (TString(s.GetCounterN(0).Name()) != "b" || TString(s.GetCounterN(1).Name()) != "a" || TString(s.GetCounterN(2).Name()) != "c")
142         {
143                 cerr << "ERROR: AliHLTTriggerCounters::GetCounterN(0) returns the wrong counter object." << endl;
144                 return false;
145         }
146         if (TString(p.GetCounterN(4).Name()) != "" || TString(p.GetCounterN(4).Description()) != "" || p.GetCounterN(4).Rate() != 0)
147         {
148                 cerr << "ERROR: Constant version of AliHLTTriggerCounters::GetCounterN(4) returns the wrong counter object." << endl;
149                 return false;
150         }
151         s.GetCounterN(4).Value(5);
152         if (TString(s.GetCounterN(4).Name()) != "Scalar4" || TString(s.GetCounterN(4).Description()) != "" || s.GetCounterN(4).Rate() != 5)
153         {
154                 cerr << "ERROR: AliHLTTriggerCounters::GetCounterN(4) does not create a new counter object correctly." << endl;
155                 return false;
156         }
157         if (TString(p.GetCounterN(3).Name()) != "Scalar3" || TString(p.GetCounterN(3).Description()) != "" || p.GetCounterN(3).Rate() != 0)
158         {
159                 cerr << "ERROR: AliHLTTriggerCounters::GetCounterN(4) did not initialise the third counter as expected." << endl;
160                 return false;
161         }
162         
163         // The following is a special check to check for compilation ambiguity
164         // rather than runtime behaviour.
165         if (s[4].Rate() != s["Scalar4"].Rate())
166         {
167                 cerr << "ERROR: AliHLTTriggerCounters::operator[](UInt_t) did not return the same value as AliHLTTriggerCounters::operator[](const char*)." << endl;
168                 return false;
169         }
170         
171         // Here we check to see that the AliHLTTriggerCounters::GetCounterN class correctly
172         // checks and finds an unused name.
173         s.Add("Scalar7", "six", 6, 2);
174         s.Add("Scalar7_0", "seven", 7, 3);
175         s.GetCounterN(7).Value(8);
176         if (! s.Exists("Scalar7_1") || s.GetCounterN(7).Rate() != 8 || s.GetCounter("Scalar7_1").Rate() != 8)
177         {
178                 cerr << "ERROR: AliHLTTriggerCounters::GetCounterN is not creating a counter object with a unique name as expected." << endl;
179                 return false;
180         }
181         
182         // Check the copying of the object.
183         AliHLTTriggerCounters* c1 = (AliHLTTriggerCounters*) s.Clone();
184         AliHLTTriggerCounters c2;
185         c2 = s;
186         AliHLTTriggerCounters c3;
187         s.Copy(c3);
188         if (! (*c1 == s) || *c1 != s)
189         {
190                 cerr << "ERROR: The equals operator of AliHLTTriggerCounters is not working as expected." << endl;
191                 return false;
192         }
193         if (c2 != s)
194         {
195                 cerr << "ERROR: The assignment operator of AliHLTTriggerCounters is not working as expected." << endl;
196                 return false;
197         }
198         if (c3 != s)
199         {
200                 cerr << "ERROR: The method AliHLTTriggerCounters::Copy is not working as expected." << endl;
201                 return false;
202         }
203         c1->UpdateTimeStamp();
204         if (*c1 == s)
205         {
206                 cerr << "ERROR: Modification of the time stamp for AliHLTTriggerCounters did not work as expected or comparison operator is not working." << endl;
207                 return false;
208         }
209         delete c1;
210         
211         // Now check the IsEqual and Reset methods:
212         if (! c2.IsEqual(&c3))
213         {
214                 cerr << "ERROR: The AliHLTTriggerCounters::IsEqual method is not working as expected." << endl;
215                 return false;
216         }
217         
218         c3.Reset();
219         for (UInt_t i = 0; i < c3.NumberOfScalars(); ++i)
220         {
221                 if (c3[i].Rate() != 0 || c3[i].Counter() != 0)
222                 {
223                         cerr << "ERROR: AliHLTTriggerCounters::Reset did not reset all counter values to zero." << endl;
224                         return false;
225                 }
226                 if (TString(c3[i].Name()) != c2[i].Name())
227                 {
228                         cerr << "ERROR: AliHLTTriggerCounters::Reset modified the name by mistake." << endl;
229                         return false;
230                 }
231                 if (TString(c3[i].Description()) != c2[i].Description())
232                 {
233                         cerr << "ERROR: AliHLTTriggerCounters::Reset modified the description by mistake." << endl;
234                         return false;
235                 }
236         }
237         if (! c2.IsEqual(&c3))
238         {
239                 cerr << "ERROR: The AliHLTTriggerCounters::IsEqual method is not working as expected after call to Reset." << endl;
240                 return false;
241         }
242         if (c2 == c3)
243         {
244                 cerr << "ERROR: The equals operator for AliHLTTriggerCounters is not working as expected after call to Reset." << endl;
245                 return false;
246         }
247         
248         c2.Remove("c");
249         if (c2.IsEqual(&c3))
250         {
251                 cerr << "ERROR: The AliHLTTriggerCounters::IsEqual method is not working as expected after call to Remove." << endl;
252                 return false;
253         }
254         if (c2 == c3)
255         {
256                 cerr << "ERROR: The equals operator for AliHLTTriggerCounters is not working as expected after call to Remove." << endl;
257                 return false;
258         }
259         
260         return true;
261 }
262
263 /**
264  * Runs the unit test for the AliHLTTriggerCounters class.
265  * \returns true if the class passed the test and false otherwise.
266  */
267 bool testAliHLTTriggerCounters()
268 {
269         if (! CheckCounterItemClass()) return false;
270         if (! CheckCountersListClass()) return false;
271         return true;
272 }
273
274 #ifndef __MAKECINT__
275
276 int main(int /*argc*/, const char** /*argv*/)
277 {
278         bool resultOk = testAliHLTTriggerCounters();
279         if (not resultOk) return 1;
280         return 0;
281 }
282
283 #endif // __MAKECINT__