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