ctkDICOMDatabaseTest2.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <QCoreApplication>
  16. #include <QDir>
  17. #include <QTimer>
  18. // ctkDICOMCore includes
  19. #include "ctkDICOMDatabase.h"
  20. // STD includes
  21. #include <iostream>
  22. #include <cstdlib>
  23. int ctkDICOMDatabaseTest2( int argc, char * argv [] )
  24. {
  25. QCoreApplication app(argc, argv);
  26. if (argc < 2)
  27. {
  28. std::cerr << "ctkDICOMDatabaseTest2: missing dicom filePath argument";
  29. std::cerr << std::endl;
  30. return EXIT_FAILURE;
  31. }
  32. QString dicomFilePath(argv[1]);
  33. ctkDICOMDatabase database;
  34. QDir databaseDirectory = QDir::temp();
  35. QFileInfo databaseFile(databaseDirectory, QString("database.test"));
  36. database.openDatabase(databaseFile.absoluteFilePath());
  37. if (!database.lastError().isEmpty())
  38. {
  39. std::cerr << "ctkDICOMDatabase::openDatabase() failed: "
  40. << qPrintable(database.lastError()) << std::endl;
  41. return EXIT_FAILURE;
  42. }
  43. if (!database.database().isValid())
  44. {
  45. std::cerr << "ctkDICOMDatabase::openDatabase() failed: "
  46. << "invalid sql database" << std::endl;
  47. return EXIT_FAILURE;
  48. }
  49. if (database.isInMemory())
  50. {
  51. std::cerr << "ctkDICOMDatabase::openDatabase() failed: "
  52. << "database should not be in memory" << std::endl;
  53. return EXIT_FAILURE;
  54. }
  55. bool res = database.initializeDatabase();
  56. if (!res)
  57. {
  58. std::cerr << "ctkDICOMDatabase::initializeDatabase() failed." << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. //
  62. // Test that the tag interface works to parse ascii
  63. //
  64. QString tag("0008,103e");
  65. unsigned short group, element;
  66. if ( !database.tagToGroupElement(tag, group, element) )
  67. {
  68. std::cerr << "ctkDICOMDatabase: could not parse tag" << std::endl;
  69. return EXIT_FAILURE;
  70. }
  71. if ( group != 0x8 || element != 0x103e )
  72. {
  73. std::cerr << "ctkDICOMDatabase: expected: " << "0008,103e" << std::endl;
  74. std::cerr << "ctkDICOMDatabase: got: " << group << " " << element << std::endl;
  75. std::cerr << "ctkDICOMDatabase: parsed tag does not match group/element" << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. //
  79. // Basic test:
  80. // - insert the file specified on the command line
  81. // - ask for tag values and compare to known results
  82. //
  83. database.insert(dicomFilePath, false, false);
  84. QString instanceUID("1.2.840.113619.2.135.3596.6358736.4843.1115808177.83");
  85. QString foundFile = database.fileForInstance(instanceUID);
  86. if (foundFile != dicomFilePath)
  87. {
  88. std::cerr << "ctkDICOMDatabase: didn't get back the original file path" << std::endl;
  89. return EXIT_FAILURE;
  90. }
  91. QString knownSeriesDescription("3D Cor T1 FAST IR-prepped GRE");
  92. QString foundSeriesDescription = database.instanceValue(instanceUID, tag);
  93. if (foundSeriesDescription != knownSeriesDescription)
  94. {
  95. std::cerr << "ctkDICOMDatabase: invalid element value returned" << std::endl;
  96. return EXIT_FAILURE;
  97. }
  98. database.closeDatabase();
  99. database.initializeDatabase();
  100. return EXIT_SUCCESS;
  101. }