HdfFileTest.cpp 6.7 KB

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