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