Skip to content

Add tests for std::pair and std::tuple #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions types/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Types

* [`fundamental`](fundamental): fundamental column types
* [`pair`](pair): `std::pair` with different element types
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
* [`tuple`](tuple): `std::tuple` with different element types
* [`variant`](variant): `std::variant` with `Switch` column type
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types
15 changes: 15 additions & 0 deletions types/pair/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `std::pair`

## Fields

* `Int32_String`: `std::pair<std::int32_t, std::string>`
* `Variant_Vector`: `std::pair<std::variant<std::int32_t, std::string>, std::vector<std::int32_t>>`
* `Pair`: `std::pair<std::pair<std::int32_t, std::string>, std::int32_t>`
* `VectorPair`: `std::vector<std::pair<std::int32_t, std::string>>`

with the default column types.

## Entries

1. Simple values
2. Zero / empty values
133 changes: 133 additions & 0 deletions types/pair/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>

using Pair_Int32_String = std::pair<std::int32_t, std::string>;
using Variant = std::variant<std::int32_t, std::string>;
using VectorInt32 = std::vector<std::int32_t>;

template <typename T> static void PrintValue(const T &value, std::ostream &os);

template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
os << value;
}

template <> void PrintValue(const std::string &value, std::ostream &os) {
os << "\"" << value << "\"";
}

template <> void PrintValue(const Variant &value, std::ostream &os) {
if (value.index() == 0) {
PrintValue(std::get<std::int32_t>(value), os);
} else if (value.index() == 1) {
PrintValue(std::get<std::string>(value), os);
}
}

template <> void PrintValue(const VectorInt32 &value, std::ostream &os) {
os << "[";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
}

template <> void PrintValue(const Pair_Int32_String &value, std::ostream &os) {
os << "[\n";
os << " " << value.first << ",\n";
os << " \"" << value.second << "\"\n";
os << " ]";
}

template <typename T, typename U>
static void PrintPairValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::pair<T, U>>(name);
os << " \"" << name << "\": [\n";
os << " ";
PrintValue(value.first, os);
os << ",\n";
os << " ";
PrintValue(value.second, os);
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}

static void PrintVectorPairValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::vector<Pair_Int32_String>>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto &&element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n ";
PrintValue(element, os);
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.pair.root",
std::string_view output = "types.pair.json") {
std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintPairValue<std::int32_t, std::string>(entry, "Int32_String", os);
PrintPairValue<Variant, VectorInt32>(entry, "Variant_Vector", os);
PrintPairValue<Pair_Int32_String, std::int32_t>(entry, "Pair", os);
PrintVectorPairValue(entry, "VectorPair", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
50 changes: 50 additions & 0 deletions types/pair/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>

using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>

using Pair_Int32_String = std::pair<std::int32_t, std::string>;
using Variant = std::variant<std::int32_t, std::string>;
using VectorInt32 = std::vector<std::int32_t>;

void write(std::string_view filename = "types.pair.root") {
auto model = RNTupleModel::Create();

auto Int32_String = model->MakeField<Pair_Int32_String>("Int32_String");
auto Variant_Vector =
model->MakeField<std::pair<Variant, VectorInt32>>("Variant_Vector");
auto Pair =
model->MakeField<std::pair<Pair_Int32_String, std::int32_t>>("Pair");
auto VectorPair =
model->MakeField<std::vector<Pair_Int32_String>>("VectorPair");

RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);

// First entry: simple values
*Int32_String = {1, "abc"};
*Variant_Vector = {"def", {2, 3, 4}};
*Pair = {{5, "ghi"}, 6};
*VectorPair = {{7, "jkl"}, {8, "mno"}};
writer->Fill();

// Second entry: zero / empty values
*Int32_String = {0, ""};
*Variant_Vector = {0, {}};
*Pair = {{0, ""}, 0};
*VectorPair = {};
writer->Fill();
}
15 changes: 15 additions & 0 deletions types/tuple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `std::tuple`

## Fields

* `Int32_String_Vector`: `std::tuple<std::int32_t, std::string, std::vector<std::int32_t>>`
* `Variant`: `std::tuple<std::variant<std::int32_t, std::string>>`
* `Tuple`: `std::tuple<std::tuple<std::int32_t, std::string>>`
* `VectorTuple`: `std::vector<std::tuple<std::int32_t, std::string>>`

with the default column types.

## Entries

1. Simple values
2. Zero / empty values
160 changes: 160 additions & 0 deletions types/tuple/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

using Tuple_Int32_String = std::tuple<std::int32_t, std::string>;
using VectorInt32 = std::vector<std::int32_t>;

template <typename T> static void PrintValue(const T &value, std::ostream &os);

template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
os << value;
}

template <> void PrintValue(const std::string &value, std::ostream &os) {
os << "\"" << value << "\"";
}

template <> void PrintValue(const VectorInt32 &value, std::ostream &os) {
os << "[";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
}

template <> void PrintValue(const Tuple_Int32_String &value, std::ostream &os) {
os << "[\n";
os << " " << std::get<0>(value) << ",\n";
os << " \"" << std::get<1>(value) << "\"\n";
os << " ]";
}

static void PrintTupleValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value =
*entry.GetPtr<std::tuple<std::int32_t, std::string, VectorInt32>>(name);
os << " \"" << name << "\": [\n";
os << " ";
PrintValue(std::get<0>(value), os);
os << ",\n";
os << " ";
PrintValue(std::get<1>(value), os);
os << ",\n";
os << " ";
PrintValue(std::get<2>(value), os);
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}

static void PrintVariantValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value =
*entry.GetPtr<std::tuple<std::variant<std::int32_t, std::string>>>(name);
os << " \"" << name << "\": [\n";
os << " ";
auto &variant = std::get<0>(value);
if (variant.index() == 0) {
PrintValue(std::get<std::int32_t>(variant), os);
} else if (variant.index() == 1) {
PrintValue(std::get<std::string>(variant), os);
}
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}

static void PrintNestedValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::tuple<Tuple_Int32_String>>(name);
os << " \"" << name << "\": [\n";
os << " ";
PrintValue(std::get<0>(value), os);
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}

static void PrintVectorTupleValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::vector<Tuple_Int32_String>>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto &&element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n ";
PrintValue(element, os);
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.tuple.root",
std::string_view output = "types.tuple.json") {
std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintTupleValue(entry, "Int32_String_Vector", os);
PrintVariantValue(entry, "Variant", os);
PrintNestedValue(entry, "Tuple", os);
PrintVectorTupleValue(entry, "VectorTuple", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
Loading