Skip to content

Commit c53f3d6

Browse files
committed
[ntuple] Add basic reading tests
1 parent fd964b3 commit c53f3d6

3 files changed

Lines changed: 40 additions & 25 deletions

File tree

tree/ntuple/src/RNTupleAttrReading.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <ROOT/RNTupleAttrReading.hxx>
99
#include <ROOT/RNTupleAttrUtils.hxx>
1010
#include <ROOT/RNTupleReader.hxx>
11+
#include <ROOT/RNTupleModel.hxx>
1112

1213
using namespace ROOT::Experimental::Internal::RNTupleAttributes;
1314

@@ -81,3 +82,4 @@ std::unique_ptr<ROOT::REntry> ROOT::Experimental::RNTupleAttrSetReader::CreateEn
8182
{
8283
return fUserModel->CreateEntry();
8384
}
85+

tree/ntuple/src/RNTupleReader.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include <ROOT/RField.hxx>
1717
#include <ROOT/RFieldVisitor.hxx>
18-
#include <ROOT/RNTupleAttributes.hxx>
18+
#include <ROOT/RNTupleAttrReading.hxx>
1919
#include <ROOT/RNTupleImtTaskScheduler.hxx>
2020
#include <ROOT/RNTuple.hxx>
2121
#include <ROOT/RNTupleModel.hxx>

tree/ntuple/test/ntuple_attributes.cxx

Lines changed: 37 additions & 24 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->GetNAttrEntries(), 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");
@@ -211,13 +219,18 @@ TEST(RNTupleAttributes, MultipleSets)
211219
attrSet2->CommitRange(std::move(attrRange2));
212220
}
213221

222+
/// Reading
214223
auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
215224
EXPECT_EQ(reader->GetDescriptor().GetNAttributeSets(), 2);
216-
int n = 1;
217-
for (const auto &attrSetIt : reader->GetDescriptor().GetAttrSetIterable()) {
218-
EXPECT_EQ(attrSetIt.GetName(), "MyAttrSet" + std::to_string(n));
219-
++n;
220-
}
225+
auto sets = reader->GetDescriptor().GetAttrSetIterable();
226+
// NOTE: there is no guaranteed order in which the attribute sets appear in the iterable
227+
EXPECT_NE(std::find_if(sets.begin(), sets.end(), [](auto &&s) { return s.GetName() == "MyAttrSet1"; }), sets.end());
228+
EXPECT_NE(std::find_if(sets.begin(), sets.end(), [](auto &&s) { return s.GetName() == "MyAttrSet2"; }), sets.end());
229+
230+
auto attrSetReader1 = reader->OpenAttributeSet("MyAttrSet1");
231+
EXPECT_EQ(attrSetReader1->GetNAttrEntries(), 100);
232+
auto attrSetReader2 = reader->OpenAttributeSet("MyAttrSet2");
233+
EXPECT_EQ(attrSetReader2->GetNAttrEntries(), 1);
221234
}
222235

223236
TEST(RNTupleAttributes, AttributeInvalidModel)

0 commit comments

Comments
 (0)