HdfFileTest.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /// \copyright Copyright (c) Catalysts GmbH
  2. /// \author Patrik Kovacs, Catalysts GmbH
  3. #include <gtest/gtest.h>
  4. #include <hdf4cpp/hdf.h>
  5. using namespace hdf4cpp;
  6. class HdfFileTest : public ::testing::Test {
  7. protected:
  8. HdfFile file{TEST_DATA_PATH "small_test.hdf"};
  9. };
  10. TEST_F(HdfFileTest, DatasetValidity) {
  11. ASSERT_NO_THROW(file.get("Data"));
  12. }
  13. TEST_F(HdfFileTest, GroupValidity) {
  14. ASSERT_NO_THROW(file.get("Group"));
  15. }
  16. TEST_F(HdfFileTest, InvalidItem) {
  17. ASSERT_THROW(file.get("InvalidKey"), HdfException);
  18. }
  19. TEST_F(HdfFileTest, ReadData1) {
  20. HdfItem item = file.get("Data");
  21. std::vector<int32> vec;
  22. item.read(vec);
  23. ASSERT_EQ(vec, std::vector<int32>({1, 2, 3, 4, 5, 6, 7, 8, 9}));
  24. }
  25. TEST_F(HdfFileTest, ReadData2) {
  26. HdfItem item = file.get("DataWithAttributes");
  27. std::vector<float32> vec;
  28. item.read(vec);
  29. ASSERT_EQ(vec, std::vector<float32>({0.0f, 0.1f, 0.2f, 1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f}));
  30. }
  31. TEST_F(HdfFileTest, ReadDatasetAttributes) {
  32. HdfItem item = file.get("DataWithAttributes");
  33. HdfAttribute attribute1 = item.getAttribute("Integer");
  34. std::vector<int32> integer;
  35. attribute1.get(integer);
  36. ASSERT_EQ(integer, std::vector<int32>({12345}));
  37. HdfAttribute attribute2 = item.getAttribute("Integers");
  38. std::vector<int32> integers;
  39. attribute2.get(integers);
  40. ASSERT_EQ(integers, std::vector<int32>({1, 12, 123, 1234, 12345}));
  41. }
  42. TEST_F(HdfFileTest, ReadGroupAttributes) {
  43. HdfItem item = file.get("GroupWithOnlyAttribute");
  44. {
  45. HdfAttribute attribute = item.getAttribute("Egy");
  46. std::vector<int8> egy;
  47. attribute.get(egy);
  48. ASSERT_EQ(egy, std::vector<int8>({1}));
  49. }
  50. {
  51. HdfAttribute attribute = item.getAttribute("One");
  52. std::vector<int16> one;
  53. attribute.get(one);
  54. ASSERT_EQ(one, std::vector<int16>({1}));
  55. }
  56. {
  57. HdfAttribute attribute = item.getAttribute("Ein");
  58. std::vector<int32> ein;
  59. attribute.get(ein);
  60. ASSERT_EQ(ein, std::vector<int32>({1}));
  61. }
  62. }
  63. TEST_F(HdfFileTest, ReadInvalidData) {
  64. ASSERT_THROW(file.get("InvalidKey"), HdfException);
  65. }
  66. TEST_F(HdfFileTest, ReadDataInRange) {
  67. {
  68. HdfItem item = file.get("Data");
  69. std::vector<int32> vec;
  70. item.read(vec, std::vector<Range>({Range(0, 2), Range(1, 2)}));
  71. ASSERT_EQ(vec, std::vector<int32>({2, 3, 5, 6}));
  72. }
  73. {
  74. HdfItem item = file.get("DataWithAttributes");
  75. std::vector<float32> vec;
  76. item.read(vec, std::vector<Range>({Range(2, 1), Range(0, 2)}));
  77. ASSERT_EQ(vec, std::vector<float32>({2.0f, 2.1f}));
  78. }
  79. }
  80. TEST_F(HdfFileTest, ReadInvalidDatasetAttribute) {
  81. HdfItem item = file.get("Data");
  82. ASSERT_THROW(HdfAttribute attribute = item.getAttribute("Attribute"), HdfException);
  83. }
  84. TEST_F(HdfFileTest, ReadInvalidGroupAttribute) {
  85. HdfItem item = file.get("Group");
  86. ASSERT_THROW(HdfAttribute attribute = item.getAttribute("Attribute"), HdfException);
  87. }
  88. TEST_F(HdfFileTest, GetAllDatsetsWithTheSameName) {
  89. std::vector<HdfItem> items = file.getAll("DoubleDataset");
  90. ASSERT_EQ(items.size(), 4);
  91. std::vector<int32> vec;
  92. items[0].read(vec);
  93. ASSERT_EQ(vec, std::vector<int32>({0}));
  94. items[1].read(vec);
  95. ASSERT_EQ(vec, std::vector<int32>({0, 1}));
  96. }
  97. TEST_F(HdfFileTest, DatasetTypeIncompatibility) {
  98. std::vector<std::string> vec;
  99. HdfItem item = file.get("Data");
  100. ASSERT_THROW(item.read(vec), HdfException);
  101. }
  102. TEST_F(HdfFileTest, DatasetAttributeTypeIncompatibility) {
  103. std::vector<std::string> vec;
  104. HdfItem item = file.get("DataWithAttributes");
  105. HdfAttribute attribute = item.getAttribute("Integer");
  106. ASSERT_THROW(attribute.get(vec), HdfException);
  107. }
  108. TEST_F(HdfFileTest, GroupAttributeTypeIncompatibility) {
  109. std::vector<std::string> vec;
  110. HdfItem item = file.get("GroupWithOnlyAttribute");
  111. HdfAttribute attribute = item.getAttribute("Egy");
  112. ASSERT_THROW(attribute.get(vec), HdfException);
  113. }
  114. TEST_F(HdfFileTest, GlobalAttribute) {
  115. std::vector<int8> vec;
  116. HdfAttribute attribute = file.getAttribute("GlobalAttribute");
  117. attribute.get(vec);
  118. ASSERT_EQ(vec, std::vector<int8>({11, 22}));
  119. }
  120. TEST_F(HdfFileTest, FileIterator) {
  121. std::ostringstream out;
  122. for (auto it : file) {
  123. out << it.getName() << '*';
  124. }
  125. ASSERT_EQ(out.str(), std::string("Group*GroupWithOnlyAttribute*RIG0.0*/home/patrik/HDF4CPP/tests/test_data/"
  126. "small_test.hdf*Egy*One*Ein*Vdata*attribute*"));
  127. }
  128. TEST_F(HdfFileTest, GroupIterator) {
  129. HdfItem item = file.get("Group");
  130. std::ostringstream out;
  131. for (auto it : item) {
  132. out << it.getName() << '*';
  133. }
  134. ASSERT_EQ(out.str(), "Data*DataWithAttributes*");
  135. }
  136. TEST_F(HdfFileTest, IteratingOverNonGroupItems) {
  137. std::ostringstream out;
  138. HdfItem sdata = file.get("DoubleDataset");
  139. for (const auto &item : sdata) {
  140. out << item.getName();
  141. }
  142. HdfItem vdata = file.get("Vdata");
  143. for (const auto &item : vdata) {
  144. out << item.getName();
  145. }
  146. ASSERT_TRUE(out.str().empty());
  147. }
  148. TEST_F(HdfFileTest, ItemIterator3) {
  149. auto it = file.begin();
  150. HdfItem item = *it;
  151. ASSERT_EQ(item.getName(), "Group");
  152. }
  153. TEST_F(HdfFileTest, HiddenGroup) {
  154. ASSERT_NO_THROW(file.get("RIG0.0"));
  155. }
  156. TEST_F(HdfFileTest, VDataRead1) {
  157. HdfItem item = file.get("Vdata");
  158. ASSERT_EQ(item.getName(), "Vdata");
  159. std::vector<int32> vec;
  160. item.read(vec, "age");
  161. ASSERT_EQ(vec, std::vector<int32>({39, 19, 55}));
  162. }
  163. TEST_F(HdfFileTest, VDataRead2) {
  164. HdfItem item = file.get("Vdata");
  165. ASSERT_EQ(item.getName(), "Vdata");
  166. std::vector<std::vector<char>> vec;
  167. item.read(vec, "name");
  168. std::vector<std::string> exp = {"Patrick Jane", "Barry Allen", "Angus MacGyver"};
  169. ASSERT_EQ(vec.size(), 3);
  170. for (int i = 0; i < 3; ++i) {
  171. ASSERT_EQ(std::string(vec[i].data()), exp[i]);
  172. }
  173. }
  174. TEST_F(HdfFileTest, VDataAttributes) {
  175. HdfItem item = file.get("Vdata");
  176. ASSERT_EQ(item.getName(), "Vdata");
  177. HdfAttribute attribute = item.getAttribute("attribute");
  178. std::vector<int32> vec;
  179. attribute.get(vec);
  180. ASSERT_EQ(vec, std::vector<int32>({1, 2, 3, 3, 2, 1}));
  181. }
  182. TEST_F(HdfFileTest, SdataDestroyer) {
  183. HdfAttribute attribute = file.get("DataWithAttributes").getAttribute("Integer");
  184. std::vector<int32> vec;
  185. attribute.get(vec);
  186. ASSERT_EQ(vec, std::vector<int32>({12345}));
  187. }
  188. TEST_F(HdfFileTest, VgroupDestroyer) {
  189. HdfAttribute attribute = file.get("GroupWithOnlyAttribute").getAttribute("Egy");
  190. std::vector<int8> vec;
  191. attribute.get(vec);
  192. ASSERT_EQ(vec, std::vector<int8>({1}));
  193. }
  194. TEST_F(HdfFileTest, VdataDestroyer) {
  195. HdfAttribute attribute = file.get("Vdata").getAttribute("attribute");
  196. std::vector<int32> vec;
  197. attribute.get(vec);
  198. ASSERT_EQ(vec, std::vector<int32>({1, 2, 3, 3, 2, 1}));
  199. }