ctkBinaryFileDescriptorTest1.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QFile>
  16. // CTK includes
  17. #include "ctkBinaryFileDescriptor.h"
  18. // STD includes
  19. #include <cstdlib>
  20. #include <iostream>
  21. //-----------------------------------------------------------------------------
  22. int ctkBinaryFileDescriptorTest1(int argc, char * argv[])
  23. {
  24. if (argc <= 1)
  25. {
  26. std::cerr << "Missing argument" << std::endl;
  27. return EXIT_FAILURE;
  28. }
  29. QString filePath(argv[1]);
  30. if (!QFile::exists(filePath))
  31. {
  32. std::cerr << "Line " << __LINE__ << " - "
  33. << "filePath [" << qPrintable(filePath)
  34. << "] do *NOT* exist" << std::endl;
  35. return EXIT_FAILURE;
  36. }
  37. ctkBinaryFileDescriptor bfd;
  38. if (bfd.load())
  39. {
  40. // Should fail to load without any valid filename
  41. std::cerr << "Line " << __LINE__ << " - "
  42. << "Problem with load() method" << std::endl;
  43. return EXIT_FAILURE;
  44. }
  45. if (!bfd.unload())
  46. {
  47. // Unload inconditionnally return True
  48. std::cerr << "Line " << __LINE__ << " - "
  49. << "Problem with unload() method" << std::endl;
  50. return EXIT_FAILURE;
  51. }
  52. if (bfd.isLoaded())
  53. {
  54. std::cerr << "Line " << __LINE__ << " - "
  55. << "Problem with isLoaded() method" << std::endl;
  56. return EXIT_FAILURE;
  57. }
  58. if (!bfd.fileName().isEmpty())
  59. {
  60. std::cerr << "Line " << __LINE__ << " - "
  61. << "Problem with fileName() method" << std::endl;
  62. return EXIT_FAILURE;
  63. }
  64. bfd.setFileName(filePath);
  65. if (bfd.fileName() != filePath)
  66. {
  67. std::cerr << "Line " << __LINE__ << " - "
  68. << "Problem with fileName() method" << std::endl;
  69. return EXIT_FAILURE;
  70. }
  71. if (!bfd.load())
  72. {
  73. // Should succeed since a valid filename is provided
  74. std::cerr << "Line " << __LINE__ << " - "
  75. << "Problem with load() method" << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. void * main_pointer = bfd.resolve("main");
  79. if (!main_pointer)
  80. {
  81. std::cerr << "Line " << __LINE__ << " - "
  82. << "Problem with resolve() method - "
  83. << "Failed to revolve 'main' symbol !" << std::endl;
  84. return EXIT_FAILURE;
  85. }
  86. void * mtBlancElevationInMeters_pointer = bfd.resolve("MtBlancElevationInMeters");
  87. int * mtBlancElevationInMeters = reinterpret_cast<int*>(mtBlancElevationInMeters_pointer);
  88. if (!mtBlancElevationInMeters)
  89. {
  90. std::cerr << "Line " << __LINE__ << " - "
  91. << "Failed to case 'mtBlancElevationInMeters' pointer !" << std::endl;
  92. return EXIT_FAILURE;
  93. }
  94. if (*mtBlancElevationInMeters != 4810)
  95. {
  96. std::cerr << "Line " << __LINE__ << " - "
  97. << "Failed to invoke function associated with symbol 'MtBlancElevationInMeters' !" << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. return EXIT_SUCCESS;
  101. }