PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Common/Testing/Cxx/vtkTestUtilities.h

http://github.com/Kitware/VTK
C Header | 179 lines | 120 code | 19 blank | 40 comment | 11 complexity | 4fa15887f769827aa795607d52486403 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, LGPL-2.0, LGPL-3.0, GPL-2.0, Apache-2.0
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: vtkTestUtilities.h
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. // .NAME vtkTestUtilities - Utility functions used for regression testing.
  12. // .SECTION Description
  13. // vtkTestUtilities provides methods to perform common testing operations.
  14. // These include getting a command line argument or an environment variable,
  15. // or a default value. Particularly, there are specialized methods to get the
  16. // root directory for VTK Data, expanding a filename with this root directory.
  17. #ifndef __vtkTestUtilities_h
  18. #define __vtkTestUtilities_h
  19. #include "vtkSystemIncludes.h"
  20. #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
  21. #pragma warning( disable : 4996 ) // 'function': was declared deprecated
  22. #endif
  23. struct vtkTestUtilities
  24. {
  25. // Description:
  26. // Function necessary for accessing the root directory for VTK data. Try the
  27. // -D command line argument or VTK_DATA_ROOT or a default value. The returned
  28. // string has to be deleted (with delete[]) by the user.
  29. static inline char* GetDataRoot(int argc, char* argv[]);
  30. // Description:
  31. // Given a file name, this function returns a new string which is (in theory)
  32. // the full path. This path is constructed by prepending the file name with a
  33. // command line argument (-D path) or VTK_DATA_ROOT env. variable. If slash
  34. // is true, appends a slash to the resulting string. The returned string has
  35. // to be deleted (with delete[]) by the user.
  36. static inline char* ExpandDataFileName(int argc, char* argv[],
  37. const char* fname,
  38. int slash = 0);
  39. // Description:
  40. // Function returning either a command line argument, an environment variable
  41. // or a default value. The returned string has to be deleted (with delete[])
  42. // by the user.
  43. static inline char* GetArgOrEnvOrDefault(const char* arg,
  44. int argc, char* argv[],
  45. const char* env,
  46. const char* def);
  47. // Description:
  48. // Given a file name, this function returns a new string which is (in theory)
  49. // the full path. This path is constructed by prepending the file name with a
  50. // command line argument, an environment variable or a default value. If
  51. // slash is true, appends a slash to the resulting string. The returned
  52. // string has to be deleted (with delete[]) by the user.
  53. static inline char* ExpandFileNameWithArgOrEnvOrDefault(const char* arg,
  54. int argc, char* argv[],
  55. const char* env,
  56. const char* def,
  57. const char* fname,
  58. int slash = 0);
  59. };
  60. inline
  61. char* vtkTestUtilities::GetDataRoot(int argc, char* argv[])
  62. {
  63. return vtkTestUtilities::GetArgOrEnvOrDefault(
  64. "-D", argc, argv,
  65. "VTK_DATA_ROOT",
  66. "../../../../VTKData");
  67. }
  68. inline
  69. char* vtkTestUtilities::ExpandDataFileName(int argc, char* argv[],
  70. const char* fname,
  71. int slash)
  72. {
  73. return vtkTestUtilities::ExpandFileNameWithArgOrEnvOrDefault(
  74. "-D", argc, argv,
  75. "VTK_DATA_ROOT",
  76. "../../../../VTKData",
  77. fname,
  78. slash);
  79. }
  80. inline
  81. char* vtkTestUtilities::GetArgOrEnvOrDefault(const char* arg,
  82. int argc, char* argv[],
  83. const char* env,
  84. const char *def)
  85. {
  86. int index = -1;
  87. for (int i = 0; i < argc; i++)
  88. {
  89. if (strcmp(arg, argv[i]) == 0 && i < argc - 1)
  90. {
  91. index = i + 1;
  92. }
  93. }
  94. char* value;
  95. if (index != -1)
  96. {
  97. value = new char[strlen(argv[index]) + 1];
  98. strcpy(value, argv[index]);
  99. }
  100. else
  101. {
  102. char *foundenv = getenv(env);
  103. if (foundenv)
  104. {
  105. value = new char[strlen(foundenv) + 1];
  106. strcpy(value, foundenv);
  107. }
  108. else if (def)
  109. {
  110. value = new char[strlen(def) + 1];
  111. strcpy(value, def);
  112. }
  113. else
  114. {
  115. value = NULL;
  116. }
  117. }
  118. return value;
  119. }
  120. inline
  121. char* vtkTestUtilities::ExpandFileNameWithArgOrEnvOrDefault(const char* arg,
  122. int argc,
  123. char* argv[],
  124. const char* env,
  125. const char *def,
  126. const char* fname,
  127. int slash)
  128. {
  129. char* fullName;
  130. char* value = vtkTestUtilities::GetArgOrEnvOrDefault(arg, argc, argv,
  131. env,
  132. def);
  133. if (value)
  134. {
  135. fullName = new char[strlen(value) + strlen(fname) + 2 +
  136. static_cast<size_t>(slash ? 1 : 0)];
  137. fullName[0] = 0;
  138. strcat(fullName, value);
  139. size_t len = strlen(fullName);
  140. fullName[len] = '/';
  141. fullName[len+1] = 0;
  142. strcat(fullName, fname);
  143. }
  144. else
  145. {
  146. fullName = new char[strlen(fname) + 1 + static_cast<size_t>(slash ? 1 : 0)];
  147. strcpy(fullName, fname);
  148. }
  149. if (slash)
  150. {
  151. strcat(fullName, "/");
  152. }
  153. delete[] value;
  154. return fullName;
  155. }
  156. #endif // __vtkTestUtilities_h