test_AdcChannelData.cxx
Go to the documentation of this file.
1 // test_AdcChannelData.cxx
2 //
3 // David Adams
4 // January 2020
5 //
6 // Test AdcChannelData.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <vector>
13 
14 #undef NDEBUG
15 #include <cassert>
16 
17 using std::string;
18 using std::cout;
19 using std::endl;
20 using std::vector;
21 
22 using Index = AdcIndex;
24 
25 //**********************************************************************
26 
28  const string myname = "test_AdcChannelData: ";
29 #ifdef NDEBUG
30  cout << myname << "NDEBUG must be off." << endl;
31  abort();
32 #endif
33  string line = myname + "-----------------------------";
34 
35  AdcChannelData acdtop;
36  cout << line << endl;
37  cout << "Checking fill." << endl;
38  acdtop.setChannelInfo(12345, 101);
39  for ( int i=0; i<20; ++i ) acdtop.raw.push_back(i);
40  assert( acdtop.channel() == 12345 );
41  assert( acdtop.fembID() == 101 );
42  assert( acdtop.raw.size() == 20 );
43 
44  cout << "Checking copy." << endl;
45  AdcChannelData acdtopCopy(acdtop);
46  assert( acdtopCopy.channel() == 12345 );
47  assert( acdtopCopy.fembID() == 101 );
48  assert( acdtopCopy.raw.size() == 0 );
49 
50 #if 0
51  cout << "Checking assignment." << endl;
52  AdcChannelData acdtopAssg;
53  acdtopAssg = acdtop;
54  assert( acdtopAssg.channel == 12345 );
55  assert( acdtopAssg.fembID == 101 );
56  assert( acdtopAssg.raw.size() == 20 );
57 #endif
58 
59  cout << line << endl;
60  cout << myname << "Add view1." << endl;
61  assert( acdtop.viewSize() == 0 );
62  View& view1 = acdtop.updateView("view1");
63  assert ( acdtop.hasView("view1") );
64  assert ( ! acdtop.hasView("view1/view2") );
65  assert( acdtop.viewSize() == 1 );
66  Index ient1 = 0;
67  for ( ient1=0; ient1<4; ++ient1 ) view1.push_back(acdtop);
68  ient1 = 0;
69  for ( AdcChannelData& acd1 : view1 ) {
70  assert( acd1.channel() == acdtop.channel());
71  acd1.setMetadata("viewIndex1", ient1);
72  ++ient1;
73  }
74 
75  cout << line << endl;
76  cout << myname << "Check view1." << endl;
77  Index nent1 = acdtop.viewSize("view1");
78  assert( nent1 == 4 );
79  for ( ient1=0; ient1<nent1; ++ient1 ) {
80  const AdcChannelData* pacd1 = acdtop.viewEntry("view1", ient1);
81  assert( pacd1 != nullptr );
82  const AdcChannelData& acd1 = *pacd1;
83  assert( acd1.channel() == acdtop.channel());
84  assert( acd1.hasMetadata("viewIndex1") );
85  assert( acd1.getMetadata("viewIndex1", 99) == ient1 );
86  AdcChannelData* pacdm = acdtop.mutableViewEntry("view1", ient1);
87  assert( pacdm != nullptr );
88  const AdcChannelData& acdm = *pacdm;
89  assert( acdm.channel() == acdtop.channel());
90  assert( acdm.hasMetadata("viewIndex1") );
91  assert( acdm.getMetadata("viewIndex1", 99) == ient1 );
92  }
93 
94  cout << line << endl;
95  cout << myname << "Add view2." << endl;
96  Index ient2 = 0;
97  vector<Index> nent2s = { 1, 2, 2, 3 };
98  vector<Index> ient1s;
99  vector<Index> ient2s;
100  Index nent12 = 0;
101  Index ient12 = 0;
102  for ( Index ient1=0; ient1<nent1; ++ient1 ) {
103  AdcChannelData& acd1 = view1[ient1];
104  View& view2 = acd1.updateView("view2");
105  Index nent2 = nent2s[ient1];
106  nent12 += nent2;
107  for ( ient2=0; ient2<nent2; ++ient2 ) {
108  view2.push_back(acdtop);
109  AdcChannelData& acd2 = view2.back();
110  acd2.setMetadata("viewIndex1", ient1);
111  acd2.setMetadata("viewIndex2", ient2);
112  acd2.setMetadata("viewIndex12", ient12);
113  ient1s.push_back(ient1);
114  ient2s.push_back(ient2);
115  ++ient12;
116  }
117  assert ( acd1.hasView("view2") );
118  assert( acd1.viewSize() == 1 );
119  assert( ient12 = nent12 );
120  }
121  assert ( acdtop.hasView("view1") );
122  assert ( acdtop.hasView("view1/view2") );
123  assert( ient12 = nent12 );
124 
125  cout << line << endl;
126  cout << myname << "Check view2." << endl;
127  ient12 = 0;
128  for ( ient1=0; ient1<nent1; ++ient1 ) {
129  const AdcChannelData* pacd1 = acdtop.viewEntry("view1", ient1);
130  assert( pacd1 != nullptr );
131  const AdcChannelData& acd1 = *pacd1;
132  Index nent2 = acd1.viewSize("view2");
133  for ( ient2=0; ient2<nent2; ++ient2 ) {
134  cout << myname << " " << ient1 << " " << ient2 << " " << ient12 << endl;
135  const AdcChannelData* pacd2 = acd1.viewEntry("view2", ient2);
136  assert( pacd2 != nullptr );
137  const AdcChannelData& acd2 = *pacd2;
138  assert( acd2.channel() == 12345 );
139  assert( acd2.fembID() == 101 );
140  assert( acd2.getMetadata("viewIndex1") == ient1 );
141  assert( acd2.getMetadata("viewIndex2") == ient2 );
142  assert( acd2.getMetadata("viewIndex12") == ient12 );
143  ++ient12;
144  }
145  }
146 
147  cout << line << endl;
148  cout << myname << "Check view12." << endl;
149  assert( acdtop.viewSize("view1/view2") == nent12 );
150  for ( ient12=0; ient12<nent12; ++ient12 ) {
151  const AdcChannelData* pacd12 = acdtop.viewEntry("view1/view2", ient12);
152  assert( pacd12 != nullptr );
153  const AdcChannelData& acd12 = *pacd12;
154  ient1 = ient1s[ient12];
155  ient2 = ient2s[ient12];
156  cout << myname << " " << ient1 << " " << ient2 << " " << ient12 << endl;
157  assert( acd12.channel() == 12345 );
158  assert( acd12.fembID() == 101 );
159  assert( acd12.getMetadata("viewIndex1") == ient1 );
160  assert( acd12.getMetadata("viewIndex2") == ient2 );
161  assert( acd12.getMetadata("viewIndex12") == ient12 );
162  }
163 
164  cout << line << endl;
165  cout << myname << "Check mutable view12." << endl;
166  assert( acdtop.viewSize("view1/view2") == nent12 );
167  for ( ient12=0; ient12<nent12; ++ient12 ) {
168  const AdcChannelData* pacd12 = acdtop.mutableViewEntry("view1/view2", ient12);
169  assert( pacd12 != nullptr );
170  const AdcChannelData& acd12 = *pacd12;
171  ient1 = ient1s[ient12];
172  ient2 = ient2s[ient12];
173  cout << myname << " " << ient1 << " " << ient2 << " " << ient12;
174  cout << " ?= "
175  << acd12.getMetadata("viewIndex1", 999) << " "
176  << acd12.getMetadata("viewIndex2", 999) << " "
177  << acd12.getMetadata("viewIndex12", 999);
178  cout << endl;
179  assert( acd12.channel() == 12345 );
180  assert( acd12.fembID() == 101 );
181  assert( acd12.hasMetadata("viewIndex1") );
182  assert( acd12.hasMetadata("viewIndex2") );
183  assert( acd12.hasMetadata("viewIndex12") );
184  assert( acd12.getMetadata("viewIndex1") == ient1 );
185  assert( acd12.getMetadata("viewIndex2") == ient2 );
186  assert( acd12.getMetadata("viewIndex12") == ient12 );
187  }
188 
189  cout << line << endl;
190  cout << myname << "All tests passed." << endl;
191  return 0;
192 }
193 
194 //**********************************************************************
195 
196 int main(int argc, char* argv[]) {
197  if ( argc > 1 ) {
198  cout << "Usage: " << argv[0] << " [ARG]" << endl;
199  return 0;
200  }
201  return test_AdcChannelData();
202 }
203 
204 //**********************************************************************
int test_AdcChannelData()
std::vector< AdcChannelData > View
AdcChannelData::View View
std::string string
Definition: nybbler.cc:12
struct vector vector
unsigned int Index
Index fembID() const
void setChannelInfo(ChannelInfoPtr pchi)
bool hasMetadata(Name mname) const
float getMetadata(Name mname, float def=0.0) const
AdcCountVector raw
unsigned int AdcIndex
Definition: AdcTypes.h:15
Channel channel() const
int main(int argc, char *argv[])
AdcChannelData * mutableViewEntry(Name vpnam, AdcIndex ient)
void line(double t, double *p, double &x, double &y, double &z)
const AdcChannelData * viewEntry(Name vpnam, AdcIndex ient) const
View & updateView(Name vnam)
void setMetadata(Name mname, float val)
size_t viewSize() const
bool hasView(Name vnam) const
QTextStream & endl(QTextStream &s)