]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/test/testAliHLT_C_Component_WrapperInterface.C
8c0519eb06c12eb6026e636247e2388aca736a95
[u/mrichter/AliRoot.git] / HLT / BASE / test / testAliHLT_C_Component_WrapperInterface.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: Matthias Richter <Matthias.Richter@ift.uib.no>        *
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   testAliHLT_C_Component_WrapperInterface.C
20     @author Matthias Richter
21     @date   
22     @brief  Test program for the external wrapper interface
23  */
24
25 #include "AliHLTProcessor.h"
26 #include "AliHLTModuleAgent.h"
27 #include "AliHLT_C_Component_WrapperInterface.h"
28
29
30 const char* gDummy="dummy";
31 AliHLTUInt32_t gRunNo=kAliHLTVoidRunNo;
32 const char* gChainId="<void>";
33 AliHLTComponentDataType gInputDt=kAliHLTVoidDataType;
34
35 class TestProcessor : public AliHLTProcessor
36 {
37 public:
38   TestProcessor();
39   ~TestProcessor();
40
41   const char* GetComponentID() {return "TestProcessor";}
42
43   void GetInputDataTypes( vector<AliHLTComponentDataType>& list)
44   {list.push_back(kAliHLTAnyDataType);}
45
46   AliHLTComponentDataType GetOutputDataType() {return kAliHLTAnyDataType;}
47
48   void GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
49   {constBase=100; inputMultiplier=2.0;}
50   
51   AliHLTComponent* Spawn() {return new TestProcessor;}
52 private:
53   int DoInit( int argc, const char** argv ) {
54     if (argc>0 && argv) {
55     }
56     gRunNo=GetRunNo();
57     gChainId=GetChainId();
58     return 0;
59   }
60   int DoDeinit() {
61     return 0;
62   }
63
64   int DoEvent( const AliHLTComponentEventData& /*evtData*/, AliHLTComponentTriggerData& /*trigData*/) {
65     int iResult=0;
66     HLTInfo("processing event");
67     for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock();
68          pBlock!=NULL; 
69          pBlock=GetNextInputBlock()) {
70       gInputDt=pBlock->fDataType;
71       AliHLTComponentDataType dt;
72       SetDataType(dt, "-OUTPUT-", "MYCO");
73       iResult=PushBack(pBlock->fPtr, pBlock->fSize/2, dt, ~pBlock->fSpecification);
74       Forward();
75     }
76     return iResult;
77   }
78 };
79
80 TestProcessor::TestProcessor()
81   : AliHLTProcessor() 
82 {
83 }
84
85 TestProcessor::~TestProcessor()
86 {
87 }
88
89 class TestAgent : public AliHLTModuleAgent
90 {
91 public:
92   TestAgent() : AliHLTModuleAgent("TEST") {}
93   ~TestAgent() {}
94
95   int RegisterComponents(AliHLTComponentHandler* pHandler) const
96   {
97     pHandler->AddComponent(new TestProcessor);
98     return 0;
99   }
100   
101 };
102
103 TestAgent gAgent;
104
105 int Logging( void* /*param*/, 
106              AliHLTComponentLogSeverity severity,
107              const char* origin,
108              const char* keyword,
109              const char* message)
110 {
111   cout << "Logging: "<< severity << " " << origin << " " << keyword << " " << message << endl;
112   return 0;
113 }
114
115 void* AllocMemory( void* param, unsigned long size )
116 {
117   if (param!=&gDummy) {
118     cerr << "AllocMemoryFunc callback with wrong parameter " << endl;
119     abort();
120   }
121   if (size==0) return NULL;
122   return new AliHLTUInt8_t[size];
123 }
124
125 const char* GetComponentDescription(void* param)
126 {
127   if (param!=&gDummy) {
128     cerr << "GetComponentDescription callback with wrong parameter " << endl;
129     abort();
130   }
131   return "-chainid=test";
132 }
133
134 int main(int /*argc*/, const char** /*argv*/)
135 {
136   int iResult=0;
137   AliHLTComponentEnvironment environment;
138   memset(&environment, 0, sizeof(environment));
139   environment.fStructSize=sizeof(environment);
140   environment.fAllocMemoryFunc=AllocMemory;
141   environment.fLoggingFunc=Logging;
142   //environment.fGetComponentDescription=GetComponentDescription;
143   if ((iResult=AliHLT_C_Component_InitSystem( &environment ))<0) {
144     cerr << "error: AliHLT_C_Component_InitSystem failed with " << iResult << endl;
145     return iResult;
146   }
147
148   if ((iResult=AliHLT_C_Component_LoadLibrary("libAliHLTUtil.so"))<0) {
149     cerr << "error: AliHLT_C_Component_LoadLibrary failed with " << iResult << endl;
150     return iResult;
151   }
152
153   AliHLTRunDesc desc;
154   memset(&desc, 0, sizeof(desc));
155   desc.fStructSize=sizeof(desc);
156   desc.fRunNo=0xbeef;
157   if ((iResult=AliHLT_C_SetRunDescription(&desc, "dummy run"))<0) {
158     cerr << "error: AliHLT_C_Component_SetRunDescription failed with " << iResult << endl;
159     return iResult;
160   }
161
162   AliHLTComponentHandle handle;
163   if ((iResult=AliHLT_C_CreateComponent("TestProcessor", &gDummy, 0, NULL, &handle ))<0) {
164     cerr << "error: AliHLT_C_Component_CreateComponent failed with " << iResult << endl;
165     return iResult;
166   }
167
168   if (gRunNo!=0xbeef) {
169     cerr << "error: propagation of run number failed " << hex << gRunNo << " vs. 0xbeef" << endl;
170     return -1;
171   }
172
173   // can be used in the new interface again
174 //   if (strcmp(gChainId, "test")) {
175 //     cerr << "propagation of chain id failed: '" << gChainId << "' vs. test" << endl;
176 //     return -1;
177 //   }
178
179
180   const char* inputData="some data to be copied";
181   AliHLTComponentBlockData inputBlock;
182   memset(&inputBlock, 0, sizeof(inputBlock));
183   inputBlock.fStructSize=sizeof(inputBlock);
184   inputBlock.fPtr=(void*)inputData;
185   inputBlock.fSize=strlen(inputData);
186   inputBlock.fDataType=kAliHLTDataTypeDDLRaw;
187   inputBlock.fSpecification=0xdead;
188
189   AliHLTComponentEventData evtData;
190   memset(&evtData, 0, sizeof(evtData));
191   evtData.fStructSize=sizeof(evtData);
192   evtData.fBlockCnt=1;
193
194   AliHLTComponentTriggerData trigData;
195   memset(&trigData, 0, sizeof(trigData));
196   trigData.fStructSize=sizeof(trigData);
197
198   AliHLTUInt8_t outputPtr[100];
199   AliHLTUInt32_t size=sizeof(outputPtr);
200
201   AliHLTUInt32_t outputBlockCnt=0;
202   AliHLTComponentBlockData* outputBlocks=NULL;
203   AliHLTComponentEventDoneData* edd=NULL;
204
205   if ((iResult=AliHLT_C_ProcessEvent( handle, &evtData, &inputBlock, &trigData, outputPtr,
206                                       &size, &outputBlockCnt, &outputBlocks, &edd ))<0) {
207     cerr << "error: AliHLT_C_Component_ProcessEvent failed with " << iResult << endl;
208     return iResult;
209   }
210
211   if (outputBlockCnt<2) {
212     cerr << "error: mismatch in output block count, expecting >2, got " << outputBlockCnt << endl;
213     return -1;
214   }
215
216   if (outputBlocks==NULL) {
217     cerr << "error: did not get output block array " << endl;
218     return -1;
219   }
220
221   AliHLTComponentDataType outdt;
222   AliHLTComponent::SetDataType(outdt, "-OUTPUT-", "MYCO");
223   bool bHaveForwarded=false;
224   bool bHaveCopied=false;
225   for (unsigned int i=0; i<outputBlockCnt; i++) {
226     if (outputBlocks[i].fDataType==kAliHLTDataTypeDDLRaw) {
227       if (outputBlocks[i].fPtr!=inputData ||
228           outputBlocks[i].fSize!=strlen(inputData)) {
229         cerr << "error: failed comparing forwarded input block" << endl;
230         return -1;
231       }
232       bHaveForwarded=true;
233     } else if (outputBlocks[i].fDataType==outdt) {
234       if (outputBlocks[i].fSize!=strlen(inputData)/2) {
235         cerr << "error: wrong size of copied block" << endl;
236         return -1;
237       }
238       if (memcmp(inputData, outputPtr+outputBlocks[i].fOffset, outputBlocks[i].fSize)) {
239         cerr << "error: failed comparing copied block" << endl;
240         return -1;
241       }
242       bHaveCopied=true;
243     }
244   }
245
246   if (!bHaveForwarded) {
247     cerr << "error: did not get forwarded data block" << endl;
248     return -1;
249   }
250
251   if (!bHaveCopied) {
252     cerr << "error: did not get copied data block" << endl;
253     return -1;
254   }
255
256   AliHLT_C_DestroyComponent(handle);
257
258   if ((iResult=AliHLT_C_Component_DeinitSystem( ))<0) {
259     cerr << "AliHLT_C_Component_DeinitSystem failed with " << iResult << endl;
260     return iResult;
261   }
262
263   return 0;
264 }