Skip to content

Commit 10517c7

Browse files
committed
[ntuple] Add basic reading tests
1 parent 78ec66c commit 10517c7

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

tree/ntuple/test/ntuple_attributes.cxx

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ntuple_test.hxx"
22
#include <ROOT/RNTupleAttrWriting.hxx>
3+
#include <ROOT/RNTupleAttrReading.hxx>
34

45
TEST(RNTupleAttributes, CreateWriter)
56
{
@@ -63,33 +64,36 @@ TEST(RNTupleAttributes, AttributeSetDuplicateName)
6364
}
6465
}
6566

66-
TEST(RNTupleAttributes, BasicWriting)
67+
TEST(RNTupleAttributes, BasicReadingWriting)
6768
{
68-
FileRaii fileGuard("ntuple_attr_basic_writing.root");
69+
FileRaii fileGuard("ntuple_attr_basic_readwriting.root");
6970

7071
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
7172
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "RNTuple Attributes are experimental", false);
7273

73-
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str(), "RECREATE"));
74-
auto model = RNTupleModel::Create();
75-
auto pInt = model->MakeField<int>("int");
76-
auto writer = RNTupleWriter::Append(std::move(model), "ntuple", *file);
74+
/// Writing
75+
{
76+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str(), "RECREATE"));
77+
auto model = RNTupleModel::Create();
78+
auto pInt = model->MakeField<int>("int");
79+
auto writer = RNTupleWriter::Append(std::move(model), "ntuple", *file);
7780

78-
auto attrModel = RNTupleModel::Create();
79-
auto pAttr = attrModel->MakeField<std::string>("attr");
80-
auto attrSetWriter = writer->CreateAttributeSet(std::move(attrModel), "AttrSet1");
81+
auto attrModel = RNTupleModel::Create();
82+
auto pAttr = attrModel->MakeField<std::string>("attr");
83+
auto attrSetWriter = writer->CreateAttributeSet(std::move(attrModel), "AttrSet1");
8184

82-
auto attrRange = attrSetWriter->BeginRange();
83-
*pAttr = "My Attribute";
84-
for (int i = 0; i < 100; ++i) {
85-
*pInt = i;
86-
writer->Fill();
87-
}
88-
attrSetWriter->CommitRange(std::move(attrRange));
89-
writer.reset();
85+
auto attrRange = attrSetWriter->BeginRange();
86+
*pAttr = "My Attribute";
87+
for (int i = 0; i < 100; ++i) {
88+
*pInt = i;
89+
writer->Fill();
90+
}
91+
attrSetWriter->CommitRange(std::move(attrRange));
92+
writer.reset();
9093

91-
// Cannot create new ranges after closing the main writer
92-
EXPECT_THROW((attrRange = attrSetWriter->BeginRange()), ROOT::RException);
94+
// Cannot create new ranges after closing the main writer
95+
EXPECT_THROW((attrRange = attrSetWriter->BeginRange()), ROOT::RException);
96+
}
9397

9498
// Cannot directly fetch the attribute RNTuple from the TFile
9599
{
@@ -147,6 +151,9 @@ TEST(RNTupleAttributes, BasicWritingWithExplicitEntry)
147151
for (const auto &attrSetIt : reader->GetDescriptor().GetAttrSetIterable()) {
148152
EXPECT_EQ(attrSetIt.GetName(), "AttrSet1");
149153
}
154+
155+
auto attrSetReader = reader->OpenAttributeSet("AttrSet1");
156+
EXPECT_EQ(attrSetReader->GetNEntries(), 1);
150157
}
151158

152159
TEST(RNTupleAttributes, NoCommitRange)
@@ -185,6 +192,7 @@ TEST(RNTupleAttributes, MultipleSets)
185192
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
186193
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "RNTuple Attributes are experimental", false);
187194

195+
/// Writing
188196
{
189197
auto model = RNTupleModel::Create();
190198
auto pInt = model->MakeField<int>("int");
@@ -213,6 +221,7 @@ TEST(RNTupleAttributes, MultipleSets)
213221
attrSet2->CommitRange(std::move(attrRange2));
214222
}
215223

224+
/// Reading
216225
auto tfile = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str()));
217226
auto ntpl = tfile->Get<ROOT::RNTuple>("ntpl");
218227
auto reader = RNTupleReader::Open(*ntpl);
@@ -223,6 +232,16 @@ TEST(RNTupleAttributes, MultipleSets)
223232
++n;
224233
}
225234

235+
auto sets = reader->GetDescriptor().GetAttrSetIterable();
236+
// NOTE: there is no guaranteed order in which the attribute sets appear in the iterable
237+
EXPECT_NE(std::find_if(sets.begin(), sets.end(), [](auto &&s) { return s.GetName() == "MyAttrSet1"; }), sets.end());
238+
EXPECT_NE(std::find_if(sets.begin(), sets.end(), [](auto &&s) { return s.GetName() == "MyAttrSet2"; }), sets.end());
239+
240+
auto attrSetReader1 = reader->OpenAttributeSet("MyAttrSet1");
241+
EXPECT_EQ(attrSetReader1->GetNEntries(), 100);
242+
auto attrSetReader2 = reader->OpenAttributeSet("MyAttrSet2");
243+
EXPECT_EQ(attrSetReader2->GetNEntries(), 1);
244+
226245
// Verify compression
227246
auto tkeys = tfile->WalkTKeys();
228247
int nHeader = 0;

0 commit comments

Comments
 (0)