ROOT logo
// $Id: $

/**************************************************************************
 * This file is property of and copyright by the ALICE HLT Project        *
 * ALICE Experiment at CERN, All rights reserved.                         *
 *                                                                        *
 * Primary Authors: Artur Szostak <artursz@iafrica.com>                   *
 *                  for The ALICE HLT Project.                            *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/// @file   testAliHLTScalars.C
/// @author Artur Szostak <artursz@iafrica.com>
/// @date   27 Oct 2010
/// @brief  Test program for the AliHLTScalars class.
///

#if !defined(__CINT__) || defined(__MAKECINT__)
#include "AliHLTScalars.h"
#include "TObjArray.h"
#include "TString.h"
#include "Riostream.h"
#endif

/**
 * Tests basic functionality of the AliHLTScalars::AliScalar class.
 */
bool CheckScalarItemClass()
{
	AliHLTScalars::AliScalar s1("s1", "scalar one", 1);
	AliHLTScalars::AliScalar s2("s2", "scalar two", 2);
	AliHLTScalars::AliScalar s3("s2", "scalar two", 3);
	if (TString(s1.GetName()) != s1.Name())
	{
		cerr << "ERROR: AliHLTScalars::AliScalar::GetName() returns a different value than AliHLTScalars::AliScalar::Name()." << endl;
		return false;
	}
	if (TString(s1.GetTitle()) != s1.Description())
	{
		cerr << "ERROR: AliHLTScalars::AliScalar::GetTitle() returns a different value than AliHLTScalars::AliScalar::Description()." << endl;
		return false;
	}
	if (s2 == s3)
	{
		cerr << "ERROR: equals operator for AliHLTScalars::AliScalar returns the wrong value." << endl;
		return false;
	}
	if (! s2.IsEqual(&s3))
	{
		cerr << "ERROR: AliHLTScalars::AliScalar::IsEqual returns the wrong value." << endl;
		return false;
	}
	s2.Increment();
	if (! (s2 == s3))
	{
		cerr << "ERROR: equals operator for AliHLTScalars::AliScalar returns the wrong value." << endl;
		return false;
	}
	TObjArray list;
	list.Add(&s2);
	list.Add(&s1);
	list.Sort();
	if (TString(list.At(0)->GetName()) != "s1")
	{
		cerr << "ERROR: Sorting objects of type AliHLTScalars::AliScalar is not working correctly." << endl;
		return false;
	}
	return true;
}

/**
 * Tests functionality of the AliHLTScalars class.
 */
bool CheckScalarsListClass()
{
	AliHLTScalars s;
	s.Add("a", "one", 1);
	s.Add("b", "two", 2);
	if (s.NumberOfScalars() != 2)
	{
		cerr << "ERROR: The number of added scalars is wrong for class AliHLTScalars." << endl;
		return false;
	}
	if (! s.Exists("a"))
	{
		cerr << "ERROR: AliHLTScalars claims scalar 'a' does not exist event though it was added." << endl;
		return false;
	}
	if (! s.Exists("b"))
	{
		cerr << "ERROR: AliHLTScalars claims scalar 'b' does not exist event though it was added." << endl;
		return false;
	}
	s.Remove("a");
	if (s.Exists("a"))
	{
		cerr << "ERROR: AliHLTScalars claims scalar 'a' does not exist event though it was removed." << endl;
		return false;
	}
	s.Add("a", "one", 1);
	const AliHLTScalars& p = s;
	if (p.GetScalar("a").Value() != 1)
	{
		cerr << "ERROR: Constant version of AliHLTScalars::GetScalar(\"a\") returns the wrong scalar object." << endl;
		return false;
	}
	if (TString(p.GetScalar("c").Name()) != "" || TString(p.GetScalar("c").Description()) != "" || p.GetScalar("c").Value() != 0)
	{
		cerr << "ERROR: Constant version of AliHLTScalars::GetScalar(\"c\") does not return a sentinel object." << endl;
		return false;
	}
	if (s.GetScalar("a").Value() != 1)
	{
		cerr << "ERROR: AliHLTScalars::GetScalar(\"a\") returns the wrong scalar object." << endl;
		return false;
	}
	s.GetScalar("c").Value(3);
	if (TString(s.GetScalar("c").Name()) != "c" || TString(s.GetScalar("c").Description()) != "")
	{
		cerr << "ERROR: AliHLTScalars::GetScalar(\"c\") does not create a new object." << endl;
		return false;
	}
	s.Add("c", "three", 33);
	if (s.GetScalar("c").Value() != 33 || TString(s.GetScalar("c").Description()) != "")
	{
		cerr << "ERROR: AliHLTScalars::Add did not update an exisiting scalar correctly." << endl;
		return false;
	}
	if (TString(p.GetScalarN(0).Name()) != "b" || TString(p.GetScalarN(1).Name()) != "a" || TString(p.GetScalarN(2).Name()) != "c")
	{
		cerr << "ERROR: Constant version of AliHLTScalars::GetScalarN(0) returns the wrong scalar object." << endl;
		return false;
	}
	if (TString(s.GetScalarN(0).Name()) != "b" || TString(s.GetScalarN(1).Name()) != "a" || TString(s.GetScalarN(2).Name()) != "c")
	{
		cerr << "ERROR: AliHLTScalars::GetScalarN(0) returns the wrong scalar object." << endl;
		return false;
	}
	if (TString(p.GetScalarN(4).Name()) != "" || TString(p.GetScalarN(4).Description()) != "" || p.GetScalarN(4).Value() != 0)
	{
		cerr << "ERROR: Constant version of AliHLTScalars::GetScalarN(4) returns the wrong scalar object." << endl;
		return false;
	}
	s.GetScalarN(4).Value(5);
	if (TString(s.GetScalarN(4).Name()) != "Scalar4" || TString(s.GetScalarN(4).Description()) != "" || s.GetScalarN(4).Value() != 5)
	{
		cerr << "ERROR: AliHLTScalars::GetScalarN(4) does not create a new scalar object correctly." << endl;
		return false;
	}
	if (TString(p.GetScalarN(3).Name()) != "Scalar3" || TString(p.GetScalarN(3).Description()) != "" || p.GetScalarN(3).Value() != 0)
	{
		cerr << "ERROR: AliHLTScalars::GetScalarN(4) did not initialise the third scalar as expected." << endl;
		return false;
	}
	
	// The following is a special check to check for compilation ambiguity
	// rather than runtime behaviour.
	if (s[4].Value() != s["Scalar4"].Value())
	{
		cerr << "ERROR: AliHLTScalars::operator[](UInt_t) did not return the same value as AliHLTScalars::operator[](const char*)." << endl;
		return false;
	}
	
	// Here we check to see that the AliHLTScalars::GetScalarN class correctly
	// checks and finds an unused name.
	s.Add("Scalar7", "six", 6);
	s.Add("Scalar7_0", "seven", 7);
	s.GetScalarN(7).Value(8);
	if (! s.Exists("Scalar7_1") || s.GetScalarN(7).Value() != 8 || s.GetScalar("Scalar7_1").Value() != 8)
	{
		cerr << "ERROR: AliHLTScalars::GetScalarN is not creating a scalar object with a unique name as expected." << endl;
		return false;
	}
	
	// Check the copying of the object.
	AliHLTScalars* c1 = (AliHLTScalars*) s.Clone();
	AliHLTScalars c2;
	c2 = s;
	AliHLTScalars c3;
	s.Copy(c3);
	if (! (*c1 == s) || *c1 != s)
	{
		cerr << "ERROR: The equals operator of AliHLTScalars is not working as expected." << endl;
		return false;
	}
	if (c2 != s)
	{
		cerr << "ERROR: The assignment operator of AliHLTScalars is not working as expected." << endl;
		return false;
	}
	if (c3 != s)
	{
		cerr << "ERROR: The method AliHLTScalars::Copy is not working as expected." << endl;
		return false;
	}
	delete c1;
	
	// Now check the IsEqual and Reset methods:
	if (! c2.IsEqual(&c3))
	{
		cerr << "ERROR: The AliHLTScalars::IsEqual method is not working as expected." << endl;
		return false;
	}
	
	c3.Reset();
	for (UInt_t i = 0; i < c3.NumberOfScalars(); ++i)
	{
		if (c3[i].Value() != 0)
		{
			cerr << "ERROR: AliHLTScalars::Reset did not reset all scalar values to zero." << endl;
			return false;
		}
		if (TString(c3[i].Name()) != c2[i].Name())
		{
			cerr << "ERROR: AliHLTScalars::Reset modified the name by mistake." << endl;
			return false;
		}
		if (TString(c3[i].Description()) != c2[i].Description())
		{
			cerr << "ERROR: AliHLTScalars::Reset modified the description by mistake." << endl;
			return false;
		}
	}
	if (! c2.IsEqual(&c3))
	{
		cerr << "ERROR: The AliHLTScalars::IsEqual method is not working as expected after call to Reset." << endl;
		return false;
	}
	if (c2 == c3)
	{
		cerr << "ERROR: The equals operator for AliHLTScalars is not working as expected after call to Reset." << endl;
		return false;
	}
	
	c2.Remove("c");
	if (c2.IsEqual(&c3))
	{
		cerr << "ERROR: The AliHLTScalars::IsEqual method is not working as expected after call to Remove." << endl;
		return false;
	}
	if (c2 == c3)
	{
		cerr << "ERROR: The equals operator for AliHLTScalars is not working as expected after call to Remove." << endl;
		return false;
	}
	
	return true;
}

/**
 * Runs the unit test for the AliHLTScalars class.
 * \returns true if the class passed the test and false otherwise.
 */
bool testAliHLTScalars()
{
	if (! CheckScalarItemClass()) return false;
	if (! CheckScalarsListClass()) return false;
	return true;
}

#ifndef __MAKECINT__

int main(int /*argc*/, const char** /*argv*/)
{
	bool resultOk = testAliHLTScalars();
	if (not resultOk) return 1;
	return 0;
}

#endif // __MAKECINT__
 testAliHLTScalars.C:1
 testAliHLTScalars.C:2
 testAliHLTScalars.C:3
 testAliHLTScalars.C:4
 testAliHLTScalars.C:5
 testAliHLTScalars.C:6
 testAliHLTScalars.C:7
 testAliHLTScalars.C:8
 testAliHLTScalars.C:9
 testAliHLTScalars.C:10
 testAliHLTScalars.C:11
 testAliHLTScalars.C:12
 testAliHLTScalars.C:13
 testAliHLTScalars.C:14
 testAliHLTScalars.C:15
 testAliHLTScalars.C:16
 testAliHLTScalars.C:17
 testAliHLTScalars.C:18
 testAliHLTScalars.C:19
 testAliHLTScalars.C:20
 testAliHLTScalars.C:21
 testAliHLTScalars.C:22
 testAliHLTScalars.C:23
 testAliHLTScalars.C:24
 testAliHLTScalars.C:25
 testAliHLTScalars.C:26
 testAliHLTScalars.C:27
 testAliHLTScalars.C:28
 testAliHLTScalars.C:29
 testAliHLTScalars.C:30
 testAliHLTScalars.C:31
 testAliHLTScalars.C:32
 testAliHLTScalars.C:33
 testAliHLTScalars.C:34
 testAliHLTScalars.C:35
 testAliHLTScalars.C:36
 testAliHLTScalars.C:37
 testAliHLTScalars.C:38
 testAliHLTScalars.C:39
 testAliHLTScalars.C:40
 testAliHLTScalars.C:41
 testAliHLTScalars.C:42
 testAliHLTScalars.C:43
 testAliHLTScalars.C:44
 testAliHLTScalars.C:45
 testAliHLTScalars.C:46
 testAliHLTScalars.C:47
 testAliHLTScalars.C:48
 testAliHLTScalars.C:49
 testAliHLTScalars.C:50
 testAliHLTScalars.C:51
 testAliHLTScalars.C:52
 testAliHLTScalars.C:53
 testAliHLTScalars.C:54
 testAliHLTScalars.C:55
 testAliHLTScalars.C:56
 testAliHLTScalars.C:57
 testAliHLTScalars.C:58
 testAliHLTScalars.C:59
 testAliHLTScalars.C:60
 testAliHLTScalars.C:61
 testAliHLTScalars.C:62
 testAliHLTScalars.C:63
 testAliHLTScalars.C:64
 testAliHLTScalars.C:65
 testAliHLTScalars.C:66
 testAliHLTScalars.C:67
 testAliHLTScalars.C:68
 testAliHLTScalars.C:69
 testAliHLTScalars.C:70
 testAliHLTScalars.C:71
 testAliHLTScalars.C:72
 testAliHLTScalars.C:73
 testAliHLTScalars.C:74
 testAliHLTScalars.C:75
 testAliHLTScalars.C:76
 testAliHLTScalars.C:77
 testAliHLTScalars.C:78
 testAliHLTScalars.C:79
 testAliHLTScalars.C:80
 testAliHLTScalars.C:81
 testAliHLTScalars.C:82
 testAliHLTScalars.C:83
 testAliHLTScalars.C:84
 testAliHLTScalars.C:85
 testAliHLTScalars.C:86
 testAliHLTScalars.C:87
 testAliHLTScalars.C:88
 testAliHLTScalars.C:89
 testAliHLTScalars.C:90
 testAliHLTScalars.C:91
 testAliHLTScalars.C:92
 testAliHLTScalars.C:93
 testAliHLTScalars.C:94
 testAliHLTScalars.C:95
 testAliHLTScalars.C:96
 testAliHLTScalars.C:97
 testAliHLTScalars.C:98
 testAliHLTScalars.C:99
 testAliHLTScalars.C:100
 testAliHLTScalars.C:101
 testAliHLTScalars.C:102
 testAliHLTScalars.C:103
 testAliHLTScalars.C:104
 testAliHLTScalars.C:105
 testAliHLTScalars.C:106
 testAliHLTScalars.C:107
 testAliHLTScalars.C:108
 testAliHLTScalars.C:109
 testAliHLTScalars.C:110
 testAliHLTScalars.C:111
 testAliHLTScalars.C:112
 testAliHLTScalars.C:113
 testAliHLTScalars.C:114
 testAliHLTScalars.C:115
 testAliHLTScalars.C:116
 testAliHLTScalars.C:117
 testAliHLTScalars.C:118
 testAliHLTScalars.C:119
 testAliHLTScalars.C:120
 testAliHLTScalars.C:121
 testAliHLTScalars.C:122
 testAliHLTScalars.C:123
 testAliHLTScalars.C:124
 testAliHLTScalars.C:125
 testAliHLTScalars.C:126
 testAliHLTScalars.C:127
 testAliHLTScalars.C:128
 testAliHLTScalars.C:129
 testAliHLTScalars.C:130
 testAliHLTScalars.C:131
 testAliHLTScalars.C:132
 testAliHLTScalars.C:133
 testAliHLTScalars.C:134
 testAliHLTScalars.C:135
 testAliHLTScalars.C:136
 testAliHLTScalars.C:137
 testAliHLTScalars.C:138
 testAliHLTScalars.C:139
 testAliHLTScalars.C:140
 testAliHLTScalars.C:141
 testAliHLTScalars.C:142
 testAliHLTScalars.C:143
 testAliHLTScalars.C:144
 testAliHLTScalars.C:145
 testAliHLTScalars.C:146
 testAliHLTScalars.C:147
 testAliHLTScalars.C:148
 testAliHLTScalars.C:149
 testAliHLTScalars.C:150
 testAliHLTScalars.C:151
 testAliHLTScalars.C:152
 testAliHLTScalars.C:153
 testAliHLTScalars.C:154
 testAliHLTScalars.C:155
 testAliHLTScalars.C:156
 testAliHLTScalars.C:157
 testAliHLTScalars.C:158
 testAliHLTScalars.C:159
 testAliHLTScalars.C:160
 testAliHLTScalars.C:161
 testAliHLTScalars.C:162
 testAliHLTScalars.C:163
 testAliHLTScalars.C:164
 testAliHLTScalars.C:165
 testAliHLTScalars.C:166
 testAliHLTScalars.C:167
 testAliHLTScalars.C:168
 testAliHLTScalars.C:169
 testAliHLTScalars.C:170
 testAliHLTScalars.C:171
 testAliHLTScalars.C:172
 testAliHLTScalars.C:173
 testAliHLTScalars.C:174
 testAliHLTScalars.C:175
 testAliHLTScalars.C:176
 testAliHLTScalars.C:177
 testAliHLTScalars.C:178
 testAliHLTScalars.C:179
 testAliHLTScalars.C:180
 testAliHLTScalars.C:181
 testAliHLTScalars.C:182
 testAliHLTScalars.C:183
 testAliHLTScalars.C:184
 testAliHLTScalars.C:185
 testAliHLTScalars.C:186
 testAliHLTScalars.C:187
 testAliHLTScalars.C:188
 testAliHLTScalars.C:189
 testAliHLTScalars.C:190
 testAliHLTScalars.C:191
 testAliHLTScalars.C:192
 testAliHLTScalars.C:193
 testAliHLTScalars.C:194
 testAliHLTScalars.C:195
 testAliHLTScalars.C:196
 testAliHLTScalars.C:197
 testAliHLTScalars.C:198
 testAliHLTScalars.C:199
 testAliHLTScalars.C:200
 testAliHLTScalars.C:201
 testAliHLTScalars.C:202
 testAliHLTScalars.C:203
 testAliHLTScalars.C:204
 testAliHLTScalars.C:205
 testAliHLTScalars.C:206
 testAliHLTScalars.C:207
 testAliHLTScalars.C:208
 testAliHLTScalars.C:209
 testAliHLTScalars.C:210
 testAliHLTScalars.C:211
 testAliHLTScalars.C:212
 testAliHLTScalars.C:213
 testAliHLTScalars.C:214
 testAliHLTScalars.C:215
 testAliHLTScalars.C:216
 testAliHLTScalars.C:217
 testAliHLTScalars.C:218
 testAliHLTScalars.C:219
 testAliHLTScalars.C:220
 testAliHLTScalars.C:221
 testAliHLTScalars.C:222
 testAliHLTScalars.C:223
 testAliHLTScalars.C:224
 testAliHLTScalars.C:225
 testAliHLTScalars.C:226
 testAliHLTScalars.C:227
 testAliHLTScalars.C:228
 testAliHLTScalars.C:229
 testAliHLTScalars.C:230
 testAliHLTScalars.C:231
 testAliHLTScalars.C:232
 testAliHLTScalars.C:233
 testAliHLTScalars.C:234
 testAliHLTScalars.C:235
 testAliHLTScalars.C:236
 testAliHLTScalars.C:237
 testAliHLTScalars.C:238
 testAliHLTScalars.C:239
 testAliHLTScalars.C:240
 testAliHLTScalars.C:241
 testAliHLTScalars.C:242
 testAliHLTScalars.C:243
 testAliHLTScalars.C:244
 testAliHLTScalars.C:245
 testAliHLTScalars.C:246
 testAliHLTScalars.C:247
 testAliHLTScalars.C:248
 testAliHLTScalars.C:249
 testAliHLTScalars.C:250
 testAliHLTScalars.C:251
 testAliHLTScalars.C:252
 testAliHLTScalars.C:253
 testAliHLTScalars.C:254
 testAliHLTScalars.C:255
 testAliHLTScalars.C:256
 testAliHLTScalars.C:257
 testAliHLTScalars.C:258
 testAliHLTScalars.C:259
 testAliHLTScalars.C:260
 testAliHLTScalars.C:261
 testAliHLTScalars.C:262
 testAliHLTScalars.C:263
 testAliHLTScalars.C:264
 testAliHLTScalars.C:265
 testAliHLTScalars.C:266
 testAliHLTScalars.C:267
 testAliHLTScalars.C:268
 testAliHLTScalars.C:269
 testAliHLTScalars.C:270
 testAliHLTScalars.C:271
 testAliHLTScalars.C:272
 testAliHLTScalars.C:273
 testAliHLTScalars.C:274
 testAliHLTScalars.C:275
 testAliHLTScalars.C:276
 testAliHLTScalars.C:277
 testAliHLTScalars.C:278