ROOT logo
//____________________________________________________________________
//
// $Id$
//
/** @file    TestIndex.C
    @author  Christian Holm Christensen <cholm@nbi.dk>
    @date    Thu Mar 30 01:20:02 2006
    @brief   Test AliFMDIndex and AliFMDObjIndex
*/
#ifndef __CINT__
#include <AliFMDIndex.h>
#include <iostream>
#include <TFile.h>
#endif
/** @defgroup index_test Test of AliFMDIndex and AliFMDObjIndex 
    @ingroup FMD_script
*/
/** Write an AliFMDIndex object to output stream
    @ingroup index_test
    @param o Stream
    @param i Index object 
    @return @a o */
std::ostream&
operator << (std::ostream& o, const AliFMDIndex& i) 
{
  return o << i.Name();
}

/** Do the comparision, and print to standard out
    @ingroup index_test
    @param lhs Left hand side
    @param rhs Right hand side
    @return @f$ lhs < rhs @f$ */
bool 
cmp(const AliFMDIndex& lhs, const AliFMDIndex& rhs)
{
  bool ret = lhs < rhs;
  std::cout << "    (" << lhs << " <  " << rhs << "): " << ret << std::endl;
  return ret;
}

/** Test if two index objects are equivilant (are the same).
    Equivilance is defined as 
    @f[
    lhs \equiv rhs: \not (lhs < rhs \vee rhs < lhs)
    @f]
    @ingroup index_test
    @param lhs Left hand side 
    @param rhs Right hand side 
    @return @c true if @a lhs and @a rhs are equivilant */
bool 
equiv(const AliFMDIndex& lhs, const AliFMDIndex& rhs)
{
  bool ret = !(cmp(lhs,rhs) || cmp(rhs,lhs));
  std::cout << "    (" << lhs << " <> " << rhs << "): " << ret << std::endl;
  return ret;
}

/** Check that @f$ \not (x < x)@f$
    @ingroup index_test
    @param x Object to test
    @return @c true if @a x is not less than itself */
bool
self(const AliFMDIndex& x)
{
  bool ret = !cmp(x,x);
  std::cout << "  !(" << x << " < " << x << "): " << ret << std::endl;
  return ret;
}

/** Check if @f$ a \wedge b \Rightarrow c@f$
    @ingroup index_test
    @param a First condition
    @param b Second condition
    @param c Implication
    @return @c true if the implication is valid */
bool
imply(bool a, bool b, bool c) 
{
  bool ret = ((a && b) && c) || (!(a && b));
  return ret;
}

/** Check if the comparison operator is transitive, that is 
    @f[
    (x < y \wedge y < z) \Rightarrow x < z
    @f]
    @ingroup index_test
    @param x First object
    @param y Second object
    @param z Third object
    @return @c true if the implication is met. */
bool
trans(const AliFMDIndex& x, const AliFMDIndex& y, const AliFMDIndex& z)
{
  bool ret  = imply(cmp(x,y), cmp(y,z), cmp(x,z));
  std::cout << "  (" << x << " < " << y << " && " << y << " < " << z 
	    << ") => " << x << " < " << z << " " 
	    << (ret ? "holds" : "violated") << std::endl;
  return ret;
  
}

/** Check that the comparison operator preserves equivilance, that is 
    @f[ 
    (x \equiv y \wedge y \equiv z) \Rightarrow (x \equiv z)
    @f]
    @ingroup index_test
    @param x First object
    @param y Second argument
    @param z Third object
    @return @c true if the implication holds. */
bool
equiv(const AliFMDIndex& x, const AliFMDIndex& y, const AliFMDIndex& z)
{
  bool ret  = imply(equiv(x,y), equiv(y,z), equiv(x,z));
  std::cout << "  (" << x << " <> " << y << " && " << y << " <> " << z 
	    << ") => " << x << " <> " << z << " " 
	    << (ret ? "holds" : "violated") << std::endl;
  return ret;
}


/** Check if the comparison operator is a @e strictly @e weak @e ordering
    @ingroup index_test
 */
void
TestIndex() 
{
  AliFMDIndex i1(1, 'I', 5, 63);
  AliFMDIndex i2(i1);  
  AliFMDIndex i3(i1);  
  AliFMDIndex i4(1, 'I', 5, 127);
  AliFMDIndex i5(1, 'I', 15, 63);
  AliFMDIndex i6(2, 'O', 15, 60);
  std::cout << "Is !(x < x): " << std::endl;
  self(i1);
  std::cout << "Does (x < y && y < z) imply x < z: " << std::endl;
  // true, true
  trans(i1, i4, i5);
  // true, false
  trans(i1, i6, i5);
  // false, true
  trans(i4, i1, i5);
  // false, false
  trans(i6, i5, i1);
  std::cout << "Does !(x < y || x > y) && !(y < z || z > y) imply "
	    << "!(x < z || x > z)" << std::endl;
  // true, true
  equiv(i1, i2, i3);
  // true, false
  equiv(i1, i2, i4);
  // false, true
  equiv(i4, i1, i2);
  // false, false
  equiv(i1, i4, i5);
  


  TFile* file = TFile::Open("index.root", "RECREATE");
  file->WriteObject(&i1,"i1");
  file->Write();
  file->Close();
  
  AliFMDIndex* i7 = 0;
  file = TFile::Open("index.root", "READ");
  file->GetObject("i1", i7);
  file->Close();
  std::cout << *i7 << " == " << i1 << ": " << (*i7 == i1) << std::endl;
  
}

/** Check if the comparison operator is a @e strictly @e weak @e ordering
    @ingroup index_test
 */
void
TestObjIndex() 
{
  AliFMDObjIndex i1(1, 'I', 5, 63);
  AliFMDObjIndex i2(i1);  
  AliFMDObjIndex i3(i1);  
  AliFMDObjIndex i4(1, 'I', 5, 127);
  AliFMDObjIndex i5(1, 'I', 15, 63);
  AliFMDObjIndex i6(2, 'O', 15, 60);
  std::cout << "Is !(x < x): " << std::endl;
  self(i1);
  std::cout << "Does (x < y && y < z) imply x < z: " << std::endl;
  // true, true
  trans(i1, i4, i5);
  // true, false
  trans(i1, i6, i5);
  // false, true
  trans(i4, i1, i5);
  // false, false
  trans(i6, i5, i1);
  std::cout << "Does !(x < y || x > y) && !(y < z || z > y) imply "
	    << "!(x < z || x > z)" << std::endl;
  // true, true
  equiv(i1, i2, i3);
  // true, false
  equiv(i1, i2, i4);
  // false, true
  equiv(i4, i1, i2);
  // false, false
  equiv(i1, i4, i5);
  


  TFile* file = TFile::Open("index.root", "RECREATE");
  i1.Write("i1");
  file->Write();
  file->Close();
  
  file = TFile::Open("index.root", "READ");
  AliFMDObjIndex* i7 = (AliFMDObjIndex*)(file->Get("i1"));
  file->Close();
  std::cout << *i7 << " == " << i1 << ": " << (*i7 == i1) << std::endl;
  
}

/** Check that we can sort an array of index objects
    @ingroup index_test
 */
void
SortIndex()
{
  TList l;
  for (size_t i = 0; i < 30; i++) {
    UShort_t det  = gRandom->Integer(3)+1;
    Char_t   ring = (gRandom->Uniform() > .5 ? 'O' : 'I');
    UShort_t sec  = gRandom->Integer(ring == 'I' ?  20 :  40);
    UShort_t str  = gRandom->Integer(ring == 'I' ? 512 : 256);
    l.AddAt(new AliFMDObjIndex(det, ring, sec, str), i);
  }
  std::cout << "Before sort" << std::endl;
  l.ls();
  l.Sort();
  std::cout << "After sort" << std::endl;
  l.ls();
}

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