File: TestDataElement.cxx

package info (click to toggle)
gdcm 3.0.24-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,856 kB
  • sloc: cpp: 203,722; ansic: 76,471; xml: 48,131; python: 3,473; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 97
file content (235 lines) | stat: -rw-r--r-- 6,780 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "gdcmDataElement.h"
#include "gdcmExplicitDataElement.h"
#include "gdcmTag.h"

#include "gdcmSwapper.h"

#include <fstream>

inline void DebugElement(std::stringstream const &os)
{
  std::ofstream of("/tmp/bla.bin", std::ios::binary);
  std::string str = os.str();
  of.write(str.c_str(), str.size());
  of.close();
}


inline void WriteRead(gdcm::DataElement const &w, gdcm::DataElement &r)
{
  // w will be written
  // r will be read back
  std::stringstream ss;
  w.Write<gdcm::ExplicitDataElement,gdcm::SwapperNoOp>(ss);
  r.Read<gdcm::ExplicitDataElement,gdcm::SwapperNoOp>(ss);
}

int TestDataElement1(const uint16_t group, const uint16_t element,
  const uint16_t vl)
{
  const char *str;
  std::stringstream ss;
  // SimpleData Element, just group,element and length
  str = reinterpret_cast<const char*>(&group);
  ss.write(str, sizeof(group));
  str = reinterpret_cast<const char*>(&element);
  ss.write(str, sizeof(element));
  str = reinterpret_cast<const char*>(&vl);
  ss.write(str, sizeof(vl));

//  gdcm::DataElement de;
//  de.Read( ss );
//  if( de.GetTag().GetGroup()   != group ||
//      de.GetTag().GetElement() != element ||
//      de.GetVL()               != vl )
//    {
//    std::cerr << de << std::endl;
//    return 1;
//    }
//
//  gdcm::DataElement de2;
//  WriteRead(de, de2);
//  if( !(de == de2) )
//    {
//    std::cerr << de << std::endl;
//    std::cerr << de2 << std::endl;
//    return 1;
//    }

  return 0;
}

// Explicit
int TestDataElement2(const uint16_t group, const uint16_t element,
  const char* vr, const char* value)
{
  const char *str;
  const uint32_t vl = (uint32_t)strlen( value );
  std::stringstream ss;
  // SimpleData Element, just group,element and length
  str = reinterpret_cast<const char*>(&group);
  ss.write(str, sizeof(group));
  str = reinterpret_cast<const char*>(&element);
  ss.write(str, sizeof(element));
  if( gdcm::VR::GetVRType(vr) == gdcm::VR::INVALID )
    {
    std::cerr << "Test buggy" << std::endl;
    return 1;
    }
  ss.write(vr, strlen(vr) );
  str = reinterpret_cast<const char*>(&vl);
  ss.write(str, sizeof(vl));
  assert( !(strlen(value) % 2) );
  ss.write(value, strlen(value) );
  //DebugElement(ss);

  gdcm::ExplicitDataElement de;
  de.Read<gdcm::SwapperNoOp>( ss );
  if( de.GetTag().GetGroup()   != group ||
      de.GetTag().GetElement() != element ||
      de.GetVL()               != vl )
    {
    std::cerr << de << std::endl;
    return 1;
    }

  gdcm::ExplicitDataElement de2;
  WriteRead(de, de2);
  if( !(de == de2) )
    {
    std::cerr << de << std::endl;
    std::cerr << de2 << std::endl;
    return 1;
    }

  return 0;
}

namespace
{

  // Tests operator== and operator!= for various gdcm::DataElement objects.
  // Also tests comparing to itself, symmetry of the comparison operators
  // with respect to their operands, and consistency between 
  // operator== and operator!=.
  // Note: This function recursively calls itself, in order to get a pointer
  // to an equivalent array of gdcm::DataElement objects.
  int TestDataElementEqualityComparison(
    const gdcm::DataElement* const equivalentDataElements = nullptr)
  {
    const unsigned int numberOfDataElements = 6;

    gdcm::DataElement dataElements[numberOfDataElements] =
    {
      gdcm::DataElement(gdcm::Tag(0, 0), 0, gdcm::VR::INVALID),
      gdcm::DataElement(gdcm::Tag(1, 1), 0, gdcm::VR::INVALID),
      gdcm::DataElement(gdcm::Tag(1, 1), 1, gdcm::VR::INVALID),
      gdcm::DataElement(gdcm::Tag(1, 1), 1, gdcm::VR::AE)
    };

    dataElements[4].SetByteValue("\0", 2);
    dataElements[5].SetByteValue("123", 4);

    // Now all data elements of the array dataElements are different. 

    if ( equivalentDataElements == nullptr )
    {
      return TestDataElementEqualityComparison(dataElements);
    }

    // equivalentDataElements != NULL, and because this function 
    // is called recursively, equivalentDataElements[i] is equivalent 
    // to dataElements[i].

    for (unsigned int i = 0; i < numberOfDataElements; ++i)
    {
      const gdcm::DataElement& dataElement = dataElements[i]; 

      if ( ! (dataElement == dataElement) )
      {
        std::cerr <<
          "Error: A data element should compare equal to itself!\n";
        return 1;
      }
      if ( dataElement != dataElement )
      {
        std::cerr <<
          "Error: A data element should not compare unequal to itself!\n";
        return 1;
      }
      const gdcm::DataElement& equivalentDataElement = equivalentDataElements[i]; 

      if (  ! (dataElement == equivalentDataElement) ||
        ! (equivalentDataElement == dataElement ) )
      {
        std::cerr <<
          "Error: A data element should compare equal to an equivalent one!\n";
        return 1;
      }

      if ( (dataElement != equivalentDataElement) ||
        (equivalentDataElement != dataElement) )
      {
        std::cerr <<
          "Error: A data element should not compare unequal to an equivalent one!\n";
        return 1;
      }

      for (unsigned int j = i + 1; j < numberOfDataElements; ++j)
      {
        // dataElements[j] is different from dataElements[i].

        const gdcm::DataElement& differentDataElement = dataElements[j];

        if ( (dataElement == differentDataElement) ||
          (differentDataElement == dataElement) )
        {
          std::cerr <<
            "Error: A data element should not compare equal to a different one!\n";
          return 1;
        }

        if ( !(dataElement != differentDataElement) ||
          !(differentDataElement != dataElement) )
        {
          std::cerr <<
            "Error: A data element should compare unequal to a different one!\n";
          return 1;
        }
      }
    }
    return 0;
  }

} // End of unnamed namespace.

// Test Data Element
int TestDataElement(int , char *[])
{
  const uint16_t group = 0x0010;
  const uint16_t element = 0x0012;
  const uint16_t vl = 0x0;
  int r = 0;
  r += TestDataElement1(group, element, vl);
  r += TestDataElementEqualityComparison();

  // Full DataElement
  //const char vr[] = "UN";
  //const char value[] = "ABCDEFGHIJKLMNOP";
  //r += TestDataElement2(group, element, vr, value);

  return r;
}