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