11#include " ntuple_test.hxx"
22#include < ROOT/RNTupleAttrWriting.hxx>
3+ #include < ROOT/RNTupleAttrReading.hxx>
34
45TEST (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
152159TEST (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
223236TEST (RNTupleAttributes, AttributeInvalidModel)
0 commit comments