|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <gmock/gmock.h> |
| 3 | + |
| 4 | +#include "uzuki2/parse_hdf5.hpp" |
| 5 | + |
| 6 | +#include "utils.h" |
| 7 | + |
| 8 | +TEST(Hdf5VlsTest, Basic) { |
| 9 | + auto path = "TEST-vls.h5"; |
| 10 | + std::string heap = "abcdefghijklmno"; |
| 11 | + size_t nlen = 10; |
| 12 | + |
| 13 | + { |
| 14 | + H5::H5File handle(path, H5F_ACC_TRUNC); |
| 15 | + auto vhandle = vector_opener(handle, "blub", "vls"); |
| 16 | + add_version(vhandle, "1.4"); |
| 17 | + |
| 18 | + auto hhandle = create_dataset(vhandle, "heap", heap.size(), H5::PredType::NATIVE_UINT8); |
| 19 | + const unsigned char* hptr = reinterpret_cast<const unsigned char*>(heap.c_str()); |
| 20 | + hhandle.write(hptr, H5::PredType::NATIVE_UCHAR); |
| 21 | + |
| 22 | + std::vector<ritsuko::hdf5::vls::Pointer<uint64_t, uint64_t> > pointers(nlen); |
| 23 | + size_t n = 0; |
| 24 | + for (size_t i = 0; i < nlen; ++i) { |
| 25 | + pointers[i].offset = n; |
| 26 | + size_t count = (i % 2) + 1; // for some interesting differences. |
| 27 | + pointers[i].length = count; |
| 28 | + n += count; |
| 29 | + } |
| 30 | + auto ptype = ritsuko::hdf5::vls::define_pointer_datatype<uint64_t, uint64_t>(); |
| 31 | + auto phandle = create_dataset(vhandle, "data", pointers.size(), ptype); |
| 32 | + phandle.write(pointers.data(), ptype); |
| 33 | + } |
| 34 | + |
| 35 | + // Check that it works correctly. |
| 36 | + { |
| 37 | + auto parsed = load_hdf5(path, "blub"); |
| 38 | + EXPECT_EQ(parsed->type(), uzuki2::STRING); |
| 39 | + auto sptr = static_cast<const DefaultStringVector*>(parsed.get()); |
| 40 | + EXPECT_EQ(sptr->size(), nlen); |
| 41 | + std::vector<std::string> expected { "a", "bc", "d", "ef", "g", "hi", "j", "kl", "m", "no" }; |
| 42 | + EXPECT_EQ(sptr->base.values, expected); |
| 43 | + } |
| 44 | + |
| 45 | + // Adding a missing value placeholder. |
| 46 | + { |
| 47 | + { |
| 48 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 49 | + auto vhandle = handle.openDataSet("blub/data"); |
| 50 | + H5::StrType stype(0, H5T_VARIABLE); |
| 51 | + auto ahandle = vhandle.createAttribute("missing-value-placeholder", stype, H5S_SCALAR); |
| 52 | + ahandle.write(stype, std::string("hi")); |
| 53 | + } |
| 54 | + |
| 55 | + auto parsed = load_hdf5(path, "blub"); |
| 56 | + EXPECT_EQ(parsed->type(), uzuki2::STRING); |
| 57 | + auto sptr = static_cast<const DefaultStringVector*>(parsed.get()); |
| 58 | + EXPECT_EQ(sptr->base.values[5], "ich bin missing"); // the test's missing placeholder. |
| 59 | + |
| 60 | + // Adding the wrong missing value placeholder. |
| 61 | + { |
| 62 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 63 | + auto vhandle = handle.openDataSet("blub/data"); |
| 64 | + vhandle.removeAttr("missing-value-placeholder"); |
| 65 | + vhandle.createAttribute("missing-value-placeholder", H5::PredType::NATIVE_INT, H5S_SCALAR); |
| 66 | + } |
| 67 | + expect_hdf5_error(path, "blub", "string datatype"); |
| 68 | + |
| 69 | + // Removing for the next checks. |
| 70 | + { |
| 71 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 72 | + auto vhandle = handle.openDataSet("blub/data"); |
| 73 | + vhandle.removeAttr("missing-value-placeholder"); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +TEST(Hdf5VlsTest, Failures) { |
| 79 | + auto path = "TEST-vls.h5"; |
| 80 | + std::string heap = "abcdefghijklmno"; |
| 81 | + size_t nlen = 10; |
| 82 | + |
| 83 | + // Shortening the heap to check that we perform bounds checks on the pointers. |
| 84 | + { |
| 85 | + H5::H5File handle(path, H5F_ACC_TRUNC); |
| 86 | + auto ghandle = vector_opener(handle, "blub", "vls"); |
| 87 | + add_version(ghandle, "1.4"); |
| 88 | + |
| 89 | + hsize_t zero = 0; |
| 90 | + H5::DataSpace hspace(1, &zero); |
| 91 | + ghandle.createDataSet("heap", H5::PredType::NATIVE_UINT8, hspace); |
| 92 | + |
| 93 | + std::vector<ritsuko::hdf5::vls::Pointer<uint64_t, uint64_t> > pointers(nlen); |
| 94 | + for (size_t i = 0; i < nlen; ++i) { |
| 95 | + pointers[i].offset = i; |
| 96 | + pointers[i].length = 1; |
| 97 | + } |
| 98 | + auto ptype = ritsuko::hdf5::vls::define_pointer_datatype<uint64_t, uint64_t>(); |
| 99 | + auto phandle = create_dataset(ghandle, "data", pointers.size(), ptype); |
| 100 | + phandle.write(pointers.data(), ptype); |
| 101 | + } |
| 102 | + expect_hdf5_error(path, "blub", "out of range"); |
| 103 | + |
| 104 | + // Checking that we check for 64-bit unsigned integer types. |
| 105 | + { |
| 106 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 107 | + auto ghandle = handle.openGroup("blub"); |
| 108 | + ghandle.unlink("data"); |
| 109 | + |
| 110 | + std::vector<ritsuko::hdf5::vls::Pointer<int, int> > pointers(3); |
| 111 | + for (auto& p : pointers) { |
| 112 | + p.offset = 0; |
| 113 | + p.length = 0; |
| 114 | + } |
| 115 | + hsize_t plen = pointers.size(); |
| 116 | + H5::DataSpace pspace(1, &plen); |
| 117 | + auto ptype = ritsuko::hdf5::vls::define_pointer_datatype<int, int>(); |
| 118 | + auto phandle = ghandle.createDataSet("data", ptype, pspace); |
| 119 | + phandle.write(pointers.data(), ptype); |
| 120 | + } |
| 121 | + expect_hdf5_error(path, "blub", "64-bit unsigned integer"); |
| 122 | + |
| 123 | + // Checking that this only works in the latest version. |
| 124 | + { |
| 125 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 126 | + auto vhandle = handle.openGroup("blub"); |
| 127 | + vhandle.removeAttr("uzuki_version"); |
| 128 | + } |
| 129 | + expect_hdf5_error(path, "blub", "unknown vector type"); |
| 130 | +} |
| 131 | + |
| 132 | +TEST(Hdf5VlsTest, Scalar) { |
| 133 | + auto path = "TEST-vls.h5"; |
| 134 | + std::string heap = "abcdefghijklmno"; |
| 135 | + |
| 136 | + { |
| 137 | + H5::H5File handle(path, H5F_ACC_TRUNC); |
| 138 | + auto ghandle = vector_opener(handle, "blub", "vls"); |
| 139 | + add_version(ghandle, "1.4"); |
| 140 | + |
| 141 | + auto hhandle = create_dataset(ghandle, "heap", heap.size(), H5::PredType::NATIVE_UINT8); |
| 142 | + const unsigned char* hptr = reinterpret_cast<const unsigned char*>(heap.c_str()); |
| 143 | + hhandle.write(hptr, H5::PredType::NATIVE_UCHAR); |
| 144 | + |
| 145 | + ritsuko::hdf5::vls::Pointer<uint8_t, uint8_t> ptr; |
| 146 | + ptr.offset = 0; ptr.length = 10; |
| 147 | + auto ptype = ritsuko::hdf5::vls::define_pointer_datatype<uint8_t, uint8_t>(); |
| 148 | + auto phandle = ghandle.createDataSet("data", ptype, H5S_SCALAR); |
| 149 | + phandle.write(&ptr, ptype); |
| 150 | + } |
| 151 | + { |
| 152 | + auto parsed = load_hdf5(path, "blub"); |
| 153 | + EXPECT_EQ(parsed->type(), uzuki2::STRING); |
| 154 | + auto sptr = static_cast<const DefaultStringVector*>(parsed.get()); |
| 155 | + EXPECT_EQ(sptr->size(), 1); |
| 156 | + EXPECT_EQ(sptr->base.values.front(), "abcdefghij"); |
| 157 | + } |
| 158 | + |
| 159 | + // Checking that it works correctly with early termination. |
| 160 | + { |
| 161 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 162 | + auto ghandle = handle.openGroup("blub"); |
| 163 | + auto hhandle = ghandle.openDataSet("heap"); |
| 164 | + std::vector<uint8_t> replacement(heap.size()); |
| 165 | + hhandle.write(replacement.data(), H5::PredType::NATIVE_UINT8); |
| 166 | + } |
| 167 | + { |
| 168 | + auto parsed = load_hdf5(path, "blub"); |
| 169 | + EXPECT_EQ(parsed->type(), uzuki2::STRING); |
| 170 | + auto sptr = static_cast<const DefaultStringVector*>(parsed.get()); |
| 171 | + EXPECT_EQ(sptr->size(), 1); |
| 172 | + EXPECT_EQ(sptr->base.values.front(), ""); |
| 173 | + } |
| 174 | + |
| 175 | + // Checking that scalar works correctly with missing values. |
| 176 | + { |
| 177 | + H5::H5File handle(path, H5F_ACC_RDWR); |
| 178 | + auto ghandle = handle.openGroup("blub"); |
| 179 | + auto dhandle = ghandle.openDataSet("data"); |
| 180 | + H5::StrType stype(0, 10); |
| 181 | + auto ahandle = dhandle.createAttribute("missing-value-placeholder", stype, H5S_SCALAR); |
| 182 | + ahandle.write(stype, std::string{}); |
| 183 | + } |
| 184 | + { |
| 185 | + auto parsed = load_hdf5(path, "blub"); |
| 186 | + EXPECT_EQ(parsed->type(), uzuki2::STRING); |
| 187 | + auto sptr = static_cast<const DefaultStringVector*>(parsed.get()); |
| 188 | + EXPECT_EQ(sptr->size(), 1); |
| 189 | + EXPECT_EQ(sptr->base.values.front(), "ich bin missing"); |
| 190 | + } |
| 191 | +} |
0 commit comments