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