1
0

HdfFileTest.cpp 6.9 KB

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