Skip to content

Commit 2bf80d0

Browse files
committed
Add test for std::tuple
Closes #18
1 parent 70538e6 commit 2bf80d0

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
* [`fundamental`](fundamental): fundamental column types
44
* [`pair`](pair): `std::pair` with different element types
55
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
6+
* [`tuple`](tuple): `std::tuple` with different element types
67
* [`variant`](variant): `std::variant` with `Switch` column type
78
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

types/tuple/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `std::pair`
2+
3+
## Fields
4+
5+
One field `Tuple` with type `std::tuple<std::int32_t, std::string, std::vector<std::int32_t>>` and the default column types.
6+
7+
## Entries
8+
9+
1. Simple values
10+
2. Zero / empty values

types/tuple/read.C

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
4+
using ROOT::Experimental::REntry;
5+
using ROOT::Experimental::RNTupleReader;
6+
7+
#include <cstdint>
8+
#include <fstream>
9+
#include <ostream>
10+
#include <string>
11+
#include <string_view>
12+
#include <tuple>
13+
#include <vector>
14+
15+
using Vector = std::vector<std::int32_t>;
16+
17+
template <typename T> static void PrintValue(const T &value, std::ostream &os);
18+
19+
template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
20+
os << value;
21+
}
22+
23+
template <> void PrintValue(const std::string &value, std::ostream &os) {
24+
os << "\"" << value << "\"";
25+
}
26+
27+
template <> void PrintValue(const Vector &value, std::ostream &os) {
28+
os << "[";
29+
bool first = true;
30+
for (auto element : value) {
31+
if (first) {
32+
first = false;
33+
} else {
34+
os << ",";
35+
}
36+
os << "\n " << element;
37+
}
38+
if (!value.empty()) {
39+
os << "\n ";
40+
}
41+
os << "]";
42+
}
43+
44+
static void PrintTupleValue(const REntry &entry, std::string_view name,
45+
std::ostream &os, bool last = false) {
46+
auto &value =
47+
*entry.GetPtr<std::tuple<std::int32_t, std::string, Vector>>(name);
48+
os << " \"" << name << "\": [\n";
49+
os << " ";
50+
PrintValue(std::get<0>(value), os);
51+
os << ",\n";
52+
os << " ";
53+
PrintValue(std::get<1>(value), os);
54+
os << ",\n";
55+
os << " ";
56+
PrintValue(std::get<2>(value), os);
57+
os << "\n";
58+
os << " ]";
59+
if (!last) {
60+
os << ",";
61+
}
62+
os << "\n";
63+
}
64+
65+
void read(std::string_view input = "types.tuple.root",
66+
std::string_view output = "types.tuple.json") {
67+
std::ofstream os(std::string{output});
68+
os << "[\n";
69+
70+
auto reader = RNTupleReader::Open("ntpl", input);
71+
auto &entry = reader->GetModel().GetDefaultEntry();
72+
bool first = true;
73+
for (auto index : *reader) {
74+
reader->LoadEntry(index);
75+
76+
if (first) {
77+
first = false;
78+
} else {
79+
os << ",\n";
80+
}
81+
os << " {\n";
82+
83+
PrintTupleValue(entry, "Tuple", os, /*last=*/true);
84+
85+
os << " }";
86+
// Newline is intentionally missing, may need to print a comma before the
87+
// next entry.
88+
}
89+
os << "\n";
90+
os << "]\n";
91+
}

types/tuple/write.C

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <ROOT/RField.hxx>
2+
#include <ROOT/RNTupleModel.hxx>
3+
#include <ROOT/RNTupleUtil.hxx>
4+
#include <ROOT/RNTupleWriteOptions.hxx>
5+
#include <ROOT/RNTupleWriter.hxx>
6+
7+
using ROOT::Experimental::EColumnType;
8+
using ROOT::Experimental::RField;
9+
using ROOT::Experimental::RNTupleModel;
10+
using ROOT::Experimental::RNTupleWriteOptions;
11+
using ROOT::Experimental::RNTupleWriter;
12+
13+
#include <cstdint>
14+
#include <memory>
15+
#include <string>
16+
#include <string_view>
17+
#include <tuple>
18+
#include <vector>
19+
20+
using Vector = std::vector<std::int32_t>;
21+
22+
void write(std::string_view filename = "types.tuple.root") {
23+
auto model = RNTupleModel::Create();
24+
25+
auto Tuple =
26+
model->MakeField<std::tuple<std::int32_t, std::string, Vector>>("Tuple");
27+
28+
RNTupleWriteOptions options;
29+
options.SetCompression(0);
30+
auto writer =
31+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
32+
33+
// First entry: simple values
34+
*Tuple = {1, "abc", {1, 2, 3}};
35+
writer->Fill();
36+
37+
// Second entry: zero / empty values
38+
*Tuple = {0, "", {}};
39+
writer->Fill();
40+
}

0 commit comments

Comments
 (0)