Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:30

0001 // -*- C++ -*-
0002 //
0003 // Package:     CondCore/CondHDF5ESSource
0004 // Class  :     test_catch2_h5cpp
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Fri, 30 Jun 2023 18:35:42 GMT
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 #include "catch.hpp"
0016 
0017 // user include files
0018 #include "FWCore/Utilities/interface/FileInPath.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 #include "CondCore/CondHDF5ESSource/plugins/h5_File.cc"
0021 #include "CondCore/CondHDF5ESSource/plugins/h5_Group.cc"
0022 #include "CondCore/CondHDF5ESSource/plugins/h5_DataSet.cc"
0023 #include "CondCore/CondHDF5ESSource/plugins/h5_Attribute.cc"
0024 
0025 namespace {
0026   std::string findFile(std::string const& iFile) { return edm::FileInPath::findFile(iFile); }
0027 }  // namespace
0028 
0029 TEST_CASE("test cms::h5 code", "[cms::h5]") {
0030   SECTION("File") {
0031     SECTION("good file") { REQUIRE_NOTHROW(cms::h5::File(findFile("test.h5"), cms::h5::File::kReadOnly)); }
0032     SECTION("missing file") { REQUIRE_THROWS_AS(cms::h5::File("missing", cms::h5::File::kReadOnly), cms::Exception); }
0033   }
0034   SECTION("Group") {
0035     cms::h5::File h5file(findFile("test.h5"), cms::h5::File::kReadOnly);
0036 
0037     SECTION("present") {
0038       std::shared_ptr<cms::h5::Group> g;
0039       REQUIRE_NOTHROW(g = h5file.findGroup("Agroup"));
0040       SECTION("name") { REQUIRE(g->name() == "/Agroup"); }
0041     }
0042     SECTION("missing") { REQUIRE_THROWS_AS(h5file.findGroup("missing"), cms::Exception); }
0043     SECTION("sub group") {
0044       auto a = h5file.findGroup("Agroup");
0045       REQUIRE_NOTHROW(a->findGroup("Bgroup"));
0046       REQUIRE(a->getNumObjs() == 5);  //group and 4 datasets
0047       REQUIRE(a->getObjnameByIdx(0) == "Bgroup");
0048     }
0049   }
0050 
0051   SECTION("DataSet") {
0052     cms::h5::File h5file(findFile("test.h5"), cms::h5::File::kReadOnly);
0053     {
0054       auto g = h5file.findGroup("Agroup");
0055       SECTION("missing") { REQUIRE_THROWS_AS(g->findDataSet("missing"), cms::Exception); }
0056       SECTION("byte array") {
0057         std::shared_ptr<cms::h5::DataSet> ds;
0058         REQUIRE_NOTHROW(ds = g->findDataSet("byte_array"));
0059         std::vector<char> b;
0060         REQUIRE_NOTHROW(b = ds->readBytes());
0061         REQUIRE(b.size() == 1);
0062         REQUIRE(b[0] == 1);
0063       }
0064     }
0065     SECTION("IOVSyncValue") {
0066       auto g = h5file.findGroup("SyncGroup");
0067       std::shared_ptr<cms::h5::DataSet> ds;
0068       REQUIRE_NOTHROW(ds = g->findDataSet("sync"));
0069       std::vector<cond::hdf5::IOVSyncValue> sv;
0070       REQUIRE_NOTHROW(sv = ds->readSyncValues());
0071       REQUIRE(sv.size() == 3);
0072       REQUIRE(sv[0].high_ == 1);
0073       REQUIRE(sv[0].low_ == 0);
0074       REQUIRE(sv[1].high_ == 2);
0075       REQUIRE(sv[1].low_ == 1);
0076       REQUIRE(sv[2].high_ == 0xFFFFFFFF);
0077       REQUIRE(sv[2].low_ == 0xFFFFFFFF);
0078     }
0079   }
0080   SECTION("refs") {
0081     cms::h5::File h5file(findFile("test.h5"), cms::h5::File::kReadOnly);
0082 
0083     std::shared_ptr<cms::h5::DataSet> ds;
0084     auto g = h5file.findGroup("RefGroup");
0085     SECTION("group") {
0086       REQUIRE_NOTHROW(ds = g->findDataSet("groupRefs"));
0087       std::vector<hobj_ref_t> r;
0088       REQUIRE_NOTHROW(r = ds->readRefs());
0089       REQUIRE(r.size() == 2);
0090 
0091       {
0092         auto deref_g = h5file.derefGroup(r[0]);
0093         REQUIRE(deref_g->name() == "/Agroup");
0094       }
0095       {
0096         auto deref_g = h5file.derefGroup(r[1]);
0097         REQUIRE(deref_g->name() == "/Agroup/Bgroup");
0098       }
0099     }
0100     SECTION("DataSet") {
0101       SECTION("refs") {
0102         REQUIRE_NOTHROW(ds = g->findDataSet("dsetRefs"));
0103         std::vector<hobj_ref_t> r;
0104         REQUIRE_NOTHROW(r = ds->readRefs());
0105         REQUIRE(r.size() == 1);
0106         auto ds = h5file.derefDataSet(r[0]);
0107         std::vector<char> b;
0108         REQUIRE_NOTHROW(b = ds->readBytes());
0109         REQUIRE(b.size() == 1);
0110         REQUIRE(b[0] == 1);
0111       }
0112       SECTION("refs 2D") {
0113         REQUIRE_NOTHROW(ds = g->findDataSet("dset2DRefs"));
0114         std::vector<hobj_ref_t> r;
0115         REQUIRE_NOTHROW(r = ds->readRefs());
0116         REQUIRE(r.size() == 4);
0117         for (size_t i = 0; i < r.size(); ++i) {
0118           auto ds = h5file.derefDataSet(r[i]);
0119           std::vector<char> b;
0120           REQUIRE_NOTHROW(b = ds->readBytes());
0121           REQUIRE(b.size() == i + 1);
0122           for (auto v : b) {
0123             REQUIRE(v == static_cast<char>(i + 1));
0124           }
0125         }
0126       }
0127     }
0128   }
0129 
0130   SECTION("Attribute") {
0131     cms::h5::File h5file(findFile("test.h5"), cms::h5::File::kReadOnly);
0132     SECTION("missing") { REQUIRE_THROWS_AS(h5file.findAttribute("missing"), cms::Exception); }
0133     SECTION("in file") {
0134       auto at = h5file.findAttribute("at");
0135       REQUIRE(at.get() != nullptr);
0136       REQUIRE(at->readString() == "fileAt");
0137     }
0138     SECTION("in group") {
0139       auto g = h5file.findGroup("Agroup");
0140       auto a = g->findAttribute("b_at");
0141       REQUIRE(a->readString() == "groupAt");
0142     }
0143   }
0144 }