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