-
Notifications
You must be signed in to change notification settings - Fork 3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>` | ||
pcanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
with the default column types. | ||
|
||
## Entries | ||
|
||
1. Simple values | ||
2. Zero / empty values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#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 <variant> | ||
#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"; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.