1
0

HdfFileTest.cpp 6.8 KB

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