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