File: itkDOMTest7.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (436 lines) | stat: -rw-r--r-- 12,697 bytes parent folder | download | duplicates (5)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

/*
This program tests operations of itk::FancyString.
*/

#include "itkFancyString.h"

#include <iostream>
#include "itkMacro.h"
#include "itkMath.h"

void testFancyStringWithBasicType();

void testFancyStringWithStdVector();

void testFancyStringWithItkArray();

void testFancyStringForStringOperations();

int itkDOMTest7( int, char*[] )
{
  try
    {
    testFancyStringWithBasicType();

    testFancyStringWithStdVector();

    testFancyStringWithItkArray();

    testFancyStringForStringOperations();
    }
  catch ( const itk::ExceptionObject& eo )
    {
    eo.Print( std::cerr );
    return EXIT_FAILURE;
    }
  catch ( const char* emsg )
    {
    std::cerr << emsg << std::endl;
    return EXIT_FAILURE;
    }
  catch ( ... )
    {
    std::cerr << "Unknown exception caught!" << std::endl;
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}

// test for basic data type
void testFancyStringWithBasicType()
{
  // for unsigned char
    {
    typedef unsigned char DataType;

    itk::FancyString s;

    // write out
    DataType dataIn = '*';
    s << dataIn;

    // read back
    DataType dataOut = ' ';
    s >> dataOut;

    // check result
    if ( dataIn != dataOut )
      {
      throw "uchar: input and output data do not match";
      }

    std::cout << "testFancyStringWithBasicType: uchar OK!" << std::endl;
    }

  // for short
    {
    typedef short DataType;

    itk::FancyString s;

    // write out
    DataType dataIn = -1024;
    s << dataIn;

    // read back
    DataType dataOut = 0;
    s >> dataOut;

    // check result
    if ( dataIn != dataOut )
      {
      throw "short: input and output data do not match";
      }

    std::cout << "testFancyStringWithBasicType: short OK!" << std::endl;
    }

  // for double
    {
    typedef double DataType;

    itk::FancyString s;

    // write out
    DataType dataIn = -0.1;
    s << dataIn;

    // read back
    DataType dataOut = 0.0;
    s >> dataOut;

    // check result
    if ( itk::Math::NotExactlyEquals(dataIn, dataOut) )
      {
      throw "double: input and output data do not match";
      }

    std::cout << "testFancyStringWithBasicType: double OK!" << std::endl;
    }

  // all testings were successful if reached here
}

// test for std::vector<T>
void testFancyStringWithStdVector()
{
  itk::FancyString svalue;
  itk::FancyString s;

  std::vector<float> dataIn( 10, -0.1f );
  svalue << dataIn;
  // add one more data element to the end of the string
  svalue.Append( " 10 " );

  // read all data elements in the string
  std::vector<float> dataOut1;
  s = svalue;
  s.ToData( dataOut1 );
  // check successful or not
  if ( dataOut1.size() != (dataIn.size()+1) && dataOut1.back() != 10.0f )
    {
    throw "testFancyStringWithStdVector: failed reading all elements in the string (1)";
    }
  for ( size_t i = 0; i < dataIn.size(); i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut1[i]) )
      {
      throw "testFancyStringWithStdVector: failed reading all elements in the string (2)";
      }
    }
  std::cout << "testFancyStringWithStdVector: dataOut1 OK!" << std::endl;

  // read all data elements for the output vector
  std::vector<float> dataOut2( 5, 0.0f );
  s = svalue;
  s >> dataOut2;
  // check successful or not
  if ( dataOut2.size() != 5 )
    {
    throw "testFancyStringWithStdVector: failed reading all elements for the output vector (1)";
    }
  for ( size_t i = 0; i < dataOut2.size(); i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut2[i]) )
      {
      throw "testFancyStringWithStdVector: failed reading all elements for the output vector (2)";
      }
    }
  std::cout << "testFancyStringWithStdVector: dataOut2 OK!" << std::endl;

  // read user-specified number of data elements (output data exist)
  std::vector<float> dataOut3( 10, 0.0f );
  s = svalue;
  s.ToData( dataOut3, 5 );
  // check successful or not
  if ( dataOut3.size() != 10 && dataOut3[5] != 0.0f )
    {
    throw "testFancyStringWithStdVector: failed reading user-specified number of elements (1.1)";
    }
  for ( size_t i = 0; i < 5; i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut3[i]) )
      {
      throw "testFancyStringWithStdVector: failed reading user-specified number of elements (1.2)";
      }
    }
  std::cout << "testFancyStringWithStdVector: dataOut3 OK!" << std::endl;

  // read user-specified number of data elements (output data do not exist)
  std::vector<float> dataOut4;
  s = svalue;
  s.ToData( dataOut4, 5 );
  // check successful or not
  if ( dataOut4.size() != 5 )
    {
    throw "testFancyStringWithStdVector: failed reading user-specified number of elements (2.1)";
    }
  for ( size_t i = 0; i < 5; i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut4[i]) )
      {
      throw "testFancyStringWithStdVector: failed reading user-specified number of elements (2.2)";
      }
    }
  std::cout << "testFancyStringWithStdVector: dataOut4 OK!" << std::endl;

  // all testings were successful if reached here
}

// test for itk::Array<T>
void testFancyStringWithItkArray()
{
  typedef itk::Array<double> DataType;

  itk::FancyString svalue;
  itk::FancyString s;

  DataType dataIn( 10 );
  dataIn.Fill( -0.1 );
  svalue << dataIn;
  // add one more data element to the end of the string
  svalue.Append( " 10 " );

  // read all data elements in the string
  DataType dataOut1;
  s = svalue;
  s.ToData( dataOut1 );
  // check successful or not
  if ( dataOut1.GetSize() != (dataIn.GetSize()+1) && dataOut1[10] != 10.0 )
    {
    throw "testFancyStringWithItkArray: failed reading all elements in the string (1)";
    }
  for ( unsigned int i = 0; i < dataIn.GetSize(); i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut1[i]) )
      {
      throw "testFancyStringWithItkArray: failed reading all elements in the string (2)";
      }
    }
  std::cout << "testFancyStringWithItkArray: dataOut1 OK!" << std::endl;

  // read all data elements for the output vector
  DataType dataOut2( 5 );
  dataOut2.Fill( 0.0 );
  s = svalue;
  s >> dataOut2;
  // check successful or not
  if ( dataOut2.GetSize() != 5 )
    {
    throw "testFancyStringWithItkArray: failed reading all elements for the output vector (1)";
    }
  for ( unsigned int i = 0; i < dataOut2.GetSize(); i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut2[i]) )
      {
      throw "testFancyStringWithItkArray: failed reading all elements for the output vector (2)";
      }
    }
  std::cout << "testFancyStringWithItkArray: dataOut2 OK!" << std::endl;

  // read user-specified number of data elements (output data exist)
  DataType dataOut3( 10 );
  dataOut3.Fill( 0.0 );
  s = svalue;
  s.ToData( dataOut3, 5 );
  // check successful or not
  if ( dataOut3.GetSize() != 10 && dataOut3[5] != 0.0 )
    {
    throw "testFancyStringWithItkArray: failed reading user-specified number of elements (1.1)";
    }
  for ( unsigned int i = 0; i < 5; i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut3[i]) )
      {
      throw "testFancyStringWithItkArray: failed reading user-specified number of elements (1.2)";
      }
    }
  std::cout << "testFancyStringWithItkArray: dataOut3 OK!" << std::endl;

  // read user-specified number of data elements (output data do not exist)
  DataType dataOut4;
  s = svalue;
  s.ToData( dataOut4, 5 );
  // check successful or not
  if ( dataOut4.GetSize() != 5 )
    {
    throw "testFancyStringWithItkArray: failed reading user-specified number of elements (2.1)";
    }
  for ( unsigned int i = 0; i < 5; i++ )
    {
    if ( itk::Math::NotExactlyEquals(dataIn[i], dataOut4[i]) )
      {
      throw "testFancyStringWithItkArray: failed reading user-specified number of elements (2.2)";
      }
    }
  std::cout << "testFancyStringWithItkArray: dataOut4 OK!" << std::endl;

  // all testings were successful if reached here
}

void testFancyStringForStringOperations()
{
  itk::FancyString s;

  s = " Hello World! ";
  if ( s.TrimLeft() != "Hello World! " )
    {
    throw "testFancyStringForStringOperations: failed trimming left";
    }
  std::cout << "testFancyStringForStringOperations: TrimLeft() OK!" << std::endl;

  s = " Hello World! ";
  if ( s.TrimRight() != " Hello World!" )
    {
    throw "testFancyStringForStringOperations: failed trimming right";
    }
  std::cout << "testFancyStringForStringOperations: TrimRight() OK!" << std::endl;

  s = " Hello World! ";
  if ( s.Trim() != "Hello World!" )
    {
    throw "testFancyStringForStringOperations: failed trimming both sides";
    }
  std::cout << "testFancyStringForStringOperations: Trim() OK!" << std::endl;

  s = "Hello World!";
  if ( s.ToUpperCase() != "HELLO WORLD!" )
    {
    throw "testFancyStringForStringOperations: failed converting to upper case";
    }
  std::cout << "testFancyStringForStringOperations: ToUpperCase() OK!" << std::endl;

  s = "Hello World!";
  if ( s.ToLowerCase() != "hello world!" )
    {
    throw "testFancyStringForStringOperations: failed converting to lower case";
    }
  std::cout << "testFancyStringForStringOperations: ToLowerCase() OK!" << std::endl;

  s = " origin = 0 0 0 ";
  std::string lpart;
  std::string rpart;
  s.Split( lpart, rpart );
  if ( lpart != "origin" || rpart != "0 0 0" )
    {
    throw "testFancyStringForStringOperations: failed splitting into two parts";
    }
  std::cout << "testFancyStringForStringOperations: Split(-,-) OK!" << std::endl;

  s = " size = 256 256 100; spacing = 0.3 0.3 0.7; origin = * ";
  std::vector<std::string> parts;
  s.Split( parts );
  if ( parts[0] != "size = 256 256 100" || parts[1] != "spacing = 0.3 0.3 0.7" || parts[2] != "origin = *" )
    {
    throw "testFancyStringForStringOperations: failed splitting into a sequence of strings";
    }
  std::cout << "testFancyStringForStringOperations: Split(vector) OK!" << std::endl;

  s = " size = 256 256 100; spacing = 0.3 0.3 0.7; origin = * ";
  std::map<std::string,std::string> items;
  s.Split( items );
  if ( items["size"] != "256 256 100" || items["spacing"] != "0.3 0.3 0.7" || items["origin"] != "*" )
    {
    throw "testFancyStringForStringOperations: failed splitting into a map or dictionary";
    }
  std::cout << "testFancyStringForStringOperations: Split(map) OK!" << std::endl;

  s = "Hello World!";
  if ( !s.MatchWith("hello world!") )
    {
    throw "testFancyStringForStringOperations: failed MatchWith testing (1)";
    }
  if ( s.MatchWith("hello world!",false) )
    {
    throw "testFancyStringForStringOperations: failed MatchWith testing (2)";
    }
  if ( s.MatchWith(" hello world! ") )
    {
    throw "testFancyStringForStringOperations: failed MatchWith testing (3)";
    }
  std::cout << "testFancyStringForStringOperations: MatchWith(-) OK!" << std::endl;

  s = "Hello World!";
  if ( !s.StartWith("hello") )
    {
    throw "testFancyStringForStringOperations: failed StartWith testing (1)";
    }
  if ( s.StartWith("hello",false) )
    {
    throw "testFancyStringForStringOperations: failed StartWith testing (2)";
    }
  std::cout << "testFancyStringForStringOperations: StartWith(-) OK!" << std::endl;

  s = "Hello World!";
  if ( !s.EndWith("world!") )
    {
    throw "testFancyStringForStringOperations: failed EndWith testing (1)";
    }
  if ( s.EndWith("world!",false) )
    {
    throw "testFancyStringForStringOperations: failed EndWith testing (2)";
    }
  std::cout << "testFancyStringForStringOperations: EndWith(-) OK!" << std::endl;

  s = "Hello World!";
  if ( !s.ContainSub("Lo wo") )
    {
    throw "testFancyStringForStringOperations: failed ContainSub testing (1)";
    }
  if ( s.ContainSub("Lo wo",false) )
    {
    throw "testFancyStringForStringOperations: failed ContainSub testing (2)";
    }
  std::cout << "testFancyStringForStringOperations: ContainSub(-) OK!" << std::endl;

  // all testings were successful if reached here
}