Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPointSetLocaleTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkIOUtil.h"
18 #include "mitkPointSet.h"
20 #include "mitkTestingMacros.h"
21 #include <fstream>
22 #include <iostream>
23 #include <list>
24 #include <string>
25 
26 bool ChangeLocale(const std::string &locale)
27 {
28  try
29  {
30  MITK_TEST_OUTPUT(<< "\n** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'");
31  setlocale(LC_ALL, locale.c_str());
32 
33  std::locale l(locale.c_str());
34  std::cin.imbue(l);
35  std::cout.imbue(l);
36 
37  return true;
38  }
39  catch (...)
40  {
41  MITK_TEST_OUTPUT(<< "Could not activate locale " << locale << "\n");
42  return false;
43  }
44 }
45 
46 void ReaderLocaleTest(mitk::Point3D &refPoint, std::string filename)
47 {
48  MITK_TEST_OUTPUT(<< "---- Reader Test ---- ");
49 
51 
52  mitk::Point3D point;
53  if (pointSet->GetPointIfExists(0, &point))
54  {
55  MITK_TEST_CONDITION_REQUIRED(fabs(refPoint[0] - point[0]) < 0.00001, "read x correct");
56  MITK_TEST_CONDITION_REQUIRED(fabs(refPoint[1] - point[1]) < 0.00001, "read y correct");
57  MITK_TEST_CONDITION_REQUIRED(fabs(refPoint[2] - point[2]) < 0.00001, "read z correct");
58  }
59  else
60  {
61  MITK_TEST_FAILED_MSG(<< "File " << filename << " can not be read - test will not applied.");
62  return;
63  }
64 }
65 
66 void WriterLocaleTest(mitk::Point3D &refPoint, std::string filename)
67 {
68  MITK_TEST_OUTPUT(<< "---- Writer Test---- ");
69  // create pointset
71  refPointSet->InsertPoint(0, refPoint);
72  // SetPoint(0, refPoint);
73 
74  std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile("testPointSet_XXXXXX.mps");
75 
76  // write point set
77  mitk::IOUtil::Save(refPointSet, tmpFilePath);
78 
79  std::ifstream stream(tmpFilePath.c_str());
80 
81  // compare two .mps files
82  std::ifstream refStream(filename.c_str());
83 
84  MITK_TEST_CONDITION_REQUIRED(refStream, "Read reference point set");
85  MITK_TEST_CONDITION_REQUIRED(stream, "Read point set");
86 
87  bool differ = false;
88  if (stream.is_open() && refStream.is_open())
89  {
90  std::string streamLine;
91  std::string refStreamLine;
92  while (!stream.eof() && !refStream.eof())
93  {
94  getline(stream, streamLine);
95  getline(refStream, refStreamLine);
96  if (streamLine.compare(refStreamLine) != 0)
97  {
98  differ = true;
99  break;
100  }
101  }
102  stream.close();
103  refStream.close();
104  }
105  MITK_TEST_CONDITION_REQUIRED(!differ, "Write point set correct");
106 }
107 
108 int mitkPointSetLocaleTest(int, char *[])
109 {
110  MITK_TEST_BEGIN("PointSetLocaleTest");
111 
112  // create reference point set
114  mitk::Point3D refPoint;
115  refPoint[0] = 32.2946;
116  refPoint[1] = -17.7359;
117  refPoint[2] = 29.6502;
118  refPointSet->SetPoint(0, refPoint);
119 
120  // create locale list
121 
122  typedef std::list<std::string> StringList;
123  StringList alllocales;
124  alllocales.push_back("de_DE");
125  alllocales.push_back("de_DE.utf8");
126  alllocales.push_back("de_DE.UTF-8");
127  alllocales.push_back("de_DE@euro");
128  alllocales.push_back("German_Germany");
129 
130 // QuickFix for MAC OS X
131 // See for more the Bug #3894 comments
132 #if defined(__APPLE__) || defined(MACOSX)
133  alllocales.push_back("C");
134 #endif
135 
136  // write a reference file using the "C" locale once
137  ChangeLocale("C");
138  std::string referenceFilePath = mitk::IOUtil::CreateTemporaryFile("refPointSet_XXXXXX.mps");
139  MITK_INFO << "Reference PointSet in " << referenceFilePath;
140 
141  // write point set
142  mitk::IOUtil::Save(refPointSet, referenceFilePath);
143 
144  unsigned int numberOfTestedGermanLocales(0);
145  for (StringList::iterator iter = alllocales.begin(); iter != alllocales.end(); ++iter)
146  {
147  if (ChangeLocale(*iter))
148  {
149  ++numberOfTestedGermanLocales;
150  WriterLocaleTest(refPoint, referenceFilePath);
151  ReaderLocaleTest(refPoint, referenceFilePath);
152  }
153  }
154 
155  if (numberOfTestedGermanLocales == 0)
156  {
157  MITK_TEST_OUTPUT(<< "Warning: No German locale was found on the system.");
158  }
159  // MITK_TEST_CONDITION_REQUIRED( numberOfTestedGermanLocales > 0, "Verify that at least one German locale has been
160  // tested.");
161  MITK_TEST_END();
162 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
#define MITK_INFO
Definition: mitkLogMacros.h:22
static Pointer New()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
bool ChangeLocale(const std::string &locale)
#define MITK_TEST_OUTPUT(x)
Output some text.
void ReaderLocaleTest(mitk::Point3D &refPoint, std::string filename)
void WriterLocaleTest(mitk::Point3D &refPoint, std::string filename)
int mitkPointSetLocaleTest(int, char *[])
static const std::string filename
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
static mitk::PointSet::Pointer LoadPointSet(const std::string &path)
LoadPointSet Convenience method to load an arbitrary mitkPointSet.
Definition: mitkIOUtil.cpp:619
std::vector< std::string > StringList
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:407
and MITK_TEST_END()