]> Creatis software - gdcm.git/blob - Testing/TestDataEntry.cxx
* Fix some compilation warnings
[gdcm.git] / Testing / TestDataEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestDataEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/20 08:58:15 $
7   Version:   $Revision: 1.4 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmDictEntry.h"
19 #include "gdcmDataEntry.h"
20
21 // ===============================================================
22
23 char data[]="1\\2\\3\\4\\5";
24 char fdata[]="1.1\\2.2\\3.3\\4.4\\5.5";
25
26 short svalue[]={1,2,3,4,5};
27 long lvalue[]={1,2,3,4,5};
28 float fvalue[]={1.1f,2.2f,3.3f,4.4f,5.5f};
29 double dvalue[]={1.1,2.2,3.3,4.4,5.5};
30
31 unsigned long nbvalue = 5;
32
33 /**
34   * \brief Test the DataEntry object
35   */  
36 int TestDataEntry(int , char *[])
37 {
38    unsigned int i;
39    gdcm::DictEntry *dict;
40    gdcm::DataEntry *entry;
41
42    //------------------------------------------------------------------
43    dict = new gdcm::DictEntry(0x0000,0x0000);
44    entry = new gdcm::DataEntry(dict);
45    dict->SetVR("US");
46
47    std::cout << "Test for VR = " << dict->GetVR() << "..." << std::endl;
48
49    entry->SetString("1");
50    std::cout << "1: ";
51    entry->Print(std::cout);
52    std::cout << std::endl;
53    if( entry->GetValueCount() != 1 )
54    {
55       std::cout << "   Failed" << std::endl
56                 << "   Number of content values is incorrect" << std::endl
57                 << "   Found: " << entry->GetValueCount() 
58                 << " - Must be: 1" << std::endl;
59       delete dict;
60       delete entry;
61       return(1);
62    }
63
64    entry->SetString("1\\2");
65    std::cout << "2: ";
66    entry->Print(std::cout);
67    std::cout << std::endl;
68    if( entry->GetValueCount() != 2 )
69    {
70       std::cout << "   Failed" << std::endl
71                 << "   Number of content values is incorrect" << std::endl
72                 << "   Found: " << entry->GetValueCount() 
73                 << " - Must be: 2" << std::endl;
74       delete dict;
75       delete entry;
76       return(1);
77    }
78
79    entry->SetString("");
80    std::cout << "3: ";
81    entry->Print(std::cout);
82    std::cout << std::endl;
83    if( entry->GetValueCount() != 0 )
84    {
85       std::cout << "   Failed" << std::endl
86                 << "   Number of content values is incorrect" << std::endl
87                 << "   Found: " << entry->GetValueCount() 
88                 << " - Must be: 0" << std::endl;
89       delete dict;
90       delete entry;
91       return(1);
92    }
93
94    std::cout << std::endl;
95    delete dict;
96    delete entry;
97
98    //------------------------------------------------------------------
99    dict = new gdcm::DictEntry(0x0000,0x0000);
100    entry = new gdcm::DataEntry(dict);
101    dict->SetVR("LT");
102
103    std::cout << "Test for VR = " << dict->GetVR() << "..." << std::endl;
104    entry->SetString(data);
105    entry->Print(std::cout);
106    std::cout << std::endl;
107
108    if( entry->GetLength() != strlen(data) + strlen(data)%2 )
109    {
110       std::cout << "   Failed" << std::endl
111                 << "   Size of string is incorrect" << std::endl
112                 << "   Found: " << entry->GetLength() 
113                 << " - Must be: " << strlen(data) + strlen(data)%2 << std::endl;
114       delete dict;
115       delete entry;
116       return(1);
117    }
118    if( entry->GetValueCount() != nbvalue )
119    {
120       std::cout << "   Failed" << std::endl
121                 << "   Number of content values is incorrect" << std::endl
122                 << "   Found: " << entry->GetValueCount() 
123                 << " - Must be: " << nbvalue << std::endl;
124       delete dict;
125       delete entry;
126       return(1);
127    }
128    if( memcmp(entry->GetBinArea(),data,entry->GetLength()) != 0 )
129    {
130       std::cout << "   Failed" << std::endl
131                 << "   Content of bin area is incorrect" << std::endl;
132       delete dict;
133       delete entry;
134       return(1);
135    }
136    if( memcmp(entry->GetString().c_str(),data,entry->GetLength()) != 0 )
137    {
138       std::cout << "   Failed" << std::endl
139                 << "   Content of string is incorrect" << std::endl
140                 << "   Found: " << entry->GetString().c_str()
141                 << " - Must be: " << data << std::endl;
142       delete dict;
143       delete entry;
144       return(1);
145    }
146    for(i=0;i<entry->GetValueCount();i++)
147    {
148       if( entry->GetValue(i) != svalue[i] )
149       {
150          std::cout << "   Failed" << std::endl
151                    << "   Content of entry's values is incorrect : id " << i << std::endl
152                    << "   Found " << entry->GetValue(i)
153                    << " - Must be " << svalue[i] << std::endl;
154          delete dict;
155          delete entry;
156          return(1);
157       }
158    }
159
160    std::cout << std::endl;
161    delete dict;
162    delete entry;
163
164    //------------------------------------------------------------------
165    dict = new gdcm::DictEntry(0x0000,0x0000);
166    entry = new gdcm::DataEntry(dict);
167    dict->SetVR("US");
168
169    std::cout << "Test for VR = " << dict->GetVR() << "..." << std::endl;
170    entry->SetString(data);
171    std::cout << "1: ";
172    entry->Print(std::cout);
173    std::cout << std::endl;
174
175    if( entry->GetLength() != nbvalue*sizeof(uint16_t) )
176    {
177       std::cout << "   Failed" << std::endl
178                 << "   BinArea length is incorrect" << std::endl
179                 << "   Found: " << entry->GetLength()
180                 << " - Must be: " << nbvalue*sizeof(uint16_t) << std::endl;
181       delete dict;
182       delete entry;
183       return(1);
184    }
185    if( memcmp(entry->GetString().c_str(),data,strlen(data)) != 0 )
186    {
187       std::cout << "   Failed" << std::endl
188                 << "   Content of string is incorrect" << std::endl
189                 << "   Found: " << entry->GetString().c_str()
190                 << " - Must be: " << data << std::endl;
191       delete dict;
192       delete entry;
193       return(1);
194    }
195    if( entry->GetValueCount() != nbvalue )
196    {
197       std::cout << "   Failed" << std::endl
198                 << "   Number of content values is incorrect" << std::endl
199                 << "   Found: " << entry->GetValueCount()
200                 << " - Must be: " << nbvalue << std::endl;
201       delete dict;
202       delete entry;
203       return(1);
204    }
205    for(i=0;i<entry->GetValueCount();i++)
206    {
207       if( entry->GetValue(i) != svalue[i] )
208       {
209          std::cout << "   Failed" << std::endl
210                    << "   Content of entry's values is incorrect : id " << i << std::endl
211                    << "   Found: " << entry->GetValue(i)
212                    << " - Must be: " << svalue[i] << std::endl;
213          delete dict;
214          delete entry;
215          return(1);
216       }
217    }
218
219    entry->SetLength(nbvalue*sizeof(uint16_t));
220    entry->SetBinArea((uint8_t *)svalue,false);
221    std::cout << "2: ";
222    entry->Print(std::cout);
223    std::cout << std::endl;
224
225    if( memcmp(entry->GetString().c_str(),data,strlen(data)) != 0 )
226    {
227       std::cout << "   Failed" << std::endl
228                 << "   Content of string is incorrect" << std::endl
229                 << "   Found: " << entry->GetString().c_str()
230                 << " - Must be: " << data << std::endl;
231       delete dict;
232       delete entry;
233       return(1);
234    }
235    if( entry->GetValueCount() != nbvalue )
236    {
237       std::cout << "   Failed" << std::endl
238                 << "   Number of content values is incorrect" << std::endl
239                 << "   Found: " << entry->GetValueCount() 
240                 << " - Must be: " << nbvalue << std::endl;
241       delete dict;
242       delete entry;
243       return(1);
244    }
245    for(i=0;i<entry->GetValueCount();i++)
246    {
247       if( entry->GetValue(i) != svalue[i] )
248       {
249          std::cout << "   Failed" << std::endl
250                    << "   Content of entry's values is incorrect : id " << i << std::endl
251                    << "   Found: " << entry->GetValue(i)
252                    << " - Must be: " << svalue[i] << std::endl;
253          delete dict;
254          delete entry;
255          return(1);
256       }
257    }
258
259    std::cout << std::endl;
260    delete dict;
261    delete entry;
262
263    //------------------------------------------------------------------
264    dict = new gdcm::DictEntry(0x0000,0x0000);
265    entry = new gdcm::DataEntry(dict);
266    dict->SetVR("UL");
267
268    std::cout << "Test for VR = " << dict->GetVR() << "..." << std::endl;
269    entry->SetString(data);
270    std::cout << "1: ";
271    entry->Print(std::cout);
272    std::cout << std::endl;
273
274    if( entry->GetLength() != nbvalue*sizeof(uint32_t) )
275    {
276       std::cout << "   Failed" << std::endl
277                 << "   BinArea length is incorrect" << std::endl
278                 << "   Found: " << entry->GetLength()
279                 << " - Must be: " << nbvalue*sizeof(uint32_t) << std::endl;
280       delete dict;
281       delete entry;
282       return(1);
283    }
284    if( memcmp(entry->GetString().c_str(),data,strlen(data)) != 0 )
285    {
286       std::cout << "   Failed" << std::endl
287                 << "   Content of string is incorrect" << std::endl
288                 << "   Found: " << entry->GetString().c_str()
289                 << " - Must be: " << data << std::endl;
290       delete dict;
291       delete entry;
292       return(1);
293    }
294    if( entry->GetValueCount() != nbvalue )
295    {
296       std::cout << "   Failed" << std::endl
297                 << "   Number of content values is incorrect" << std::endl
298                 << "   Found: " << entry->GetValueCount() 
299                 << " - Must be: " << nbvalue << std::endl;
300       delete dict;
301       delete entry;
302       return(1);
303    }
304    for(i=0;i<entry->GetValueCount();i++)
305    {
306       if( entry->GetValue(i) != lvalue[i] )
307       {
308          std::cout << "   Failed" << std::endl
309                    << "   Content of entry's values is incorrect : id " << i << std::endl
310                    << "   Found: " << entry->GetValue(i)
311                    << " - Must be: " << lvalue[i] << std::endl;
312          delete dict;
313          delete entry;
314          return(1);
315       }
316    }
317
318    entry->SetLength(nbvalue*sizeof(uint32_t));
319    entry->SetBinArea((uint8_t *)lvalue,false);
320    std::cout << "2: ";
321    entry->Print(std::cout);
322    std::cout << std::endl;
323
324    if( memcmp(entry->GetString().c_str(),data,strlen(data)) != 0 )
325    {
326       std::cout << "   Failed" << std::endl
327                 << "   Content of string is incorrect" << std::endl
328                 << "   Found: " << entry->GetString().c_str() 
329                 << " - Must be: " << data << std::endl;
330       delete dict;
331       delete entry;
332       return(1);
333    }
334    if( entry->GetValueCount() != nbvalue )
335    {
336       std::cout << "   Failed" << std::endl
337                 << "   Number of content values is incorrect" << std::endl
338                 << "   Found: " << entry->GetValueCount() 
339                 << " - Must be: " << nbvalue << std::endl;
340       delete dict;
341       delete entry;
342       return(1);
343    }
344    for(i=0;i<entry->GetValueCount();i++)
345    {
346       if( entry->GetValue(i) != lvalue[i] )
347       {
348          std::cout << "   Failed" << std::endl
349                    << "   Content of entry's values is incorrect : id " << i << std::endl
350                    << "   Found: " << entry->GetValue(i)
351                    << " - Must be: " << lvalue[i] << std::endl;
352          delete dict;
353          delete entry;
354          return(1);
355       }
356    }
357
358    std::cout << std::endl;
359    delete dict;
360    delete entry;
361
362    //------------------------------------------------------------------
363    dict = new gdcm::DictEntry(0x0000,0x0000);
364    entry = new gdcm::DataEntry(dict);
365    dict->SetVR("FL");
366
367    std::cout << "Test for VR = " << dict->GetVR() << "..." << std::endl;
368    entry->SetString(fdata);
369    std::cout << "1: ";
370    entry->Print(std::cout);
371    std::cout << std::endl;
372
373    if( entry->GetLength() != nbvalue*sizeof(float) )
374    {
375       std::cout << "   Failed" << std::endl
376                 << "   BinArea length is incorrect" << std::endl
377                 << "   Found: " << entry->GetLength() 
378                 << " - Must be: " << nbvalue*sizeof(float) << std::endl;
379       delete dict;
380       delete entry;
381       return(1);
382    }
383    if( memcmp(entry->GetString().c_str(),fdata,strlen(fdata)) != 0 )
384    {
385       std::cout << "   Failed" << std::endl
386                 << "   Content of string is incorrect" << std::endl
387                 << "   Found: " << entry->GetString().c_str()
388                 << " - Must be: " << fdata << std::endl;
389       delete dict;
390       delete entry;
391       return(1);
392    }
393    if( entry->GetValueCount() != nbvalue )
394    {
395       std::cout << "   Failed" << std::endl
396                 << "   Number of content values is incorrect" << std::endl
397                 << "   Found: " << entry->GetValueCount() 
398                 << " - Must be: " << nbvalue << std::endl;
399       delete dict;
400       delete entry;
401       return(1);
402    }
403    for(i=0;i<entry->GetValueCount();i++)
404    {
405       if( entry->GetValue(i) != fvalue[i] )
406       {
407          std::cout << "   Failed" << std::endl
408                    << "   Content of entry's values is incorrect : id " << i << std::endl
409                    << "   Found: " << entry->GetValue(i)
410                    << " - Must be: " << fvalue[i] << std::endl;
411          delete dict;
412          delete entry;
413          return(1);
414       }
415    }
416
417    entry->SetLength(nbvalue*sizeof(float));
418    entry->SetBinArea((uint8_t *)fvalue,false);
419    std::cout << "2: ";
420    entry->Print(std::cout);
421    std::cout << std::endl;
422
423    if( memcmp(entry->GetString().c_str(),fdata,strlen(fdata)) != 0 )
424    {
425       std::cout << "   Failed" << std::endl
426                 << "   Content of string is incorrect" << std::endl
427                 << "   Found: " << entry->GetString().c_str()
428                 << " - Must be: " << fdata << std::endl;
429       delete dict;
430       delete entry;
431       return(1);
432    }
433    if( entry->GetValueCount() != nbvalue )
434    {
435       std::cout << "   Failed" << std::endl
436                 << "   Number of content values is incorrect" << std::endl
437                 << "   Found: " << entry->GetValueCount() 
438                 << " - Must be: " << nbvalue << std::endl;
439       delete dict;
440       delete entry;
441       return(1);
442    }
443    for(i=0;i<entry->GetValueCount();i++)
444    {
445       if( entry->GetValue(i) != fvalue[i] )
446       {
447          std::cout << "   Failed" << std::endl
448                    << "   Content of entry's values is incorrect : id " << i << std::endl
449                    << "   Found: " << entry->GetValue(i)
450                    << " - Must be: " << fvalue[i] << std::endl;
451          delete dict;
452          delete entry;
453          return(1);
454       }
455    }
456
457    std::cout << std::endl;
458    delete dict;
459    delete entry;
460
461    //------------------------------------------------------------------
462    dict = new gdcm::DictEntry(0x0000,0x0000);
463    entry = new gdcm::DataEntry(dict);
464    dict->SetVR("FD");
465
466    std::cout << "Test for VR = " << dict->GetVR() << "..." << std::endl;
467    entry->SetString(fdata);
468    std::cout << "1: ";
469    entry->Print(std::cout);
470    std::cout << std::endl;
471
472    if( entry->GetLength() != nbvalue*sizeof(double) )
473    {
474       std::cout << "   Failed" << std::endl
475                 << "   BinArea length is incorrect" << std::endl
476                 << "   Found: " << entry->GetLength()
477                 << " - Must be: " << nbvalue*sizeof(double) << std::endl;
478       delete dict;
479       delete entry;
480       return(1);
481    }
482    if( memcmp(entry->GetString().c_str(),fdata,strlen(fdata)) != 0 )
483    {
484       std::cout << "   Failed" << std::endl
485                 << "   Content of string is incorrect" << std::endl
486                 << "   Found: " << entry->GetString().c_str()
487                 << " - Must be: " << fdata << std::endl;
488       delete dict;
489       delete entry;
490       return(1);
491    }
492    if( entry->GetValueCount() != nbvalue )
493    {
494       std::cout << "   Failed" << std::endl
495                 << "   Number of content values is incorrect" << std::endl
496                 << "   Found: " << entry->GetValueCount() 
497                 << " - Must be: " << nbvalue << std::endl;
498       delete dict;
499       delete entry;
500       return(1);
501    }
502    for(i=0;i<entry->GetValueCount();i++)
503    {
504       if( entry->GetValue(i) != dvalue[i] )
505       {
506          std::cout << "   Failed" << std::endl
507                    << "   Content of entry's values is incorrect : id " << i << std::endl
508                    << "   Found: " << entry->GetValue(i)
509                    << " - Must be: " << dvalue[i] << std::endl;
510          delete dict;
511          delete entry;
512          return(1);
513       }
514    }
515
516    entry->SetLength(nbvalue*sizeof(double));
517    entry->SetBinArea((uint8_t *)dvalue,false);
518    std::cout << "2: ";
519    entry->Print(std::cout);
520    std::cout << std::endl;
521
522    if( memcmp(entry->GetString().c_str(),fdata,strlen(fdata)) != 0 )
523    {
524       std::cout << "   Failed" << std::endl
525                 << "   Content of string is incorrect" << std::endl
526                 << "   Found: " << entry->GetString().c_str()
527                 << " - Must be: " << fdata << std::endl;
528       delete dict;
529       delete entry;
530       return(1);
531    }
532    if( entry->GetValueCount() != nbvalue )
533    {
534       std::cout << "   Failed" << std::endl
535                 << "   Number of content values is incorrect" << std::endl
536                 << "   Found: " << entry->GetValueCount() 
537                 << " - Must be: " << nbvalue << std::endl;
538       delete dict;
539       delete entry;
540       return(1);
541    }
542    for(i=0;i<entry->GetValueCount();i++)
543    {
544       if( entry->GetValue(i) != dvalue[i] )
545       {
546          std::cout << "   Failed" << std::endl
547                    << "   Content of entry's values is incorrect : id " << i << std::endl
548                    << "   Found: " << entry->GetValue(i)
549                    << " - Must be: " << dvalue[i] << std::endl;
550          delete dict;
551          delete entry;
552          return(1);
553       }
554    }
555
556    std::cout << std::endl;
557    delete dict;
558    delete entry;
559
560    //------------------------------------------------------------------
561    std::cout<<std::flush;
562    return 0;
563 }