-
Notifications
You must be signed in to change notification settings - Fork 3
Add tests for first C++ types #36
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Types | ||
|
||
* [`fundamental`](fundamental): fundamental column types | ||
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types | ||
* [`variant`](variant): `std::variant` with `Switch` column type | ||
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types |
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::string` | ||
|
||
## Fields | ||
|
||
* `[Split]Index{32,64}` | ||
|
||
with the corresponding column type for the first (principal) column. | ||
|
||
## Entries | ||
|
||
1. One character strings, with ascending values | ||
2. Empty strings | ||
3. Unicode characters as UTF-8 | ||
4. Increasing length of strings: | ||
one character in the first field, two characters in the second field, etc. |
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,51 @@ | ||
#include <ROOT/REntry.hxx> | ||
#include <ROOT/RNTupleReader.hxx> | ||
|
||
using ROOT::Experimental::REntry; | ||
using ROOT::Experimental::RNTupleReader; | ||
|
||
#include <fstream> | ||
#include <ostream> | ||
#include <string> | ||
#include <string_view> | ||
|
||
static void PrintStringValue(const REntry &entry, std::string_view name, | ||
std::ostream &os, bool last = false) { | ||
std::string &value = *entry.GetPtr<std::string>(name); | ||
os << " \"" << name << "\": \"" << value << "\""; | ||
if (!last) { | ||
os << ","; | ||
} | ||
os << "\n"; | ||
} | ||
|
||
void read(std::string_view input = "types.string.root", | ||
std::string_view output = "types.string.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"; | ||
|
||
PrintStringValue(entry, "Index32", os); | ||
PrintStringValue(entry, "Index64", os); | ||
PrintStringValue(entry, "SplitIndex32", os); | ||
PrintStringValue(entry, "SplitIndex64", 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,71 @@ | ||
#include <ROOT/RField.hxx> | ||
#include <ROOT/RNTupleModel.hxx> | ||
#include <ROOT/RNTupleUtil.hxx> | ||
#include <ROOT/RNTupleWriteOptions.hxx> | ||
#include <ROOT/RNTupleWriter.hxx> | ||
|
||
using ROOT::Experimental::EColumnType; | ||
using ROOT::Experimental::RField; | ||
using ROOT::Experimental::RNTupleModel; | ||
using ROOT::Experimental::RNTupleWriteOptions; | ||
using ROOT::Experimental::RNTupleWriter; | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <string_view> | ||
|
||
static std::shared_ptr<std::string> MakeStringField(RNTupleModel &model, | ||
std::string_view name, | ||
EColumnType indexType) { | ||
auto field = std::make_unique<RField<std::string>>(name); | ||
field->SetColumnRepresentatives({{indexType, EColumnType::kChar}}); | ||
model.AddField(std::move(field)); | ||
return model.GetDefaultEntry().GetPtr<std::string>(name); | ||
} | ||
|
||
void write(std::string_view filename = "types.string.root") { | ||
auto model = RNTupleModel::Create(); | ||
|
||
// Non-split index encoding | ||
auto Index32 = MakeStringField(*model, "Index32", EColumnType::kIndex32); | ||
auto Index64 = MakeStringField(*model, "Index64", EColumnType::kIndex64); | ||
|
||
// Split index encoding | ||
auto SplitIndex32 = | ||
MakeStringField(*model, "SplitIndex32", EColumnType::kSplitIndex32); | ||
auto SplitIndex64 = | ||
MakeStringField(*model, "SplitIndex64", EColumnType::kSplitIndex64); | ||
|
||
RNTupleWriteOptions options; | ||
options.SetCompression(0); | ||
auto writer = | ||
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options); | ||
|
||
// First entry: one character strings, with ascending values | ||
*Index32 = "a"; | ||
*Index64 = "b"; | ||
*SplitIndex32 = "c"; | ||
*SplitIndex64 = "d"; | ||
writer->Fill(); | ||
|
||
// Second entry: empty strings | ||
*Index32 = ""; | ||
*Index64 = ""; | ||
*SplitIndex32 = ""; | ||
*SplitIndex64 = ""; | ||
writer->Fill(); | ||
|
||
// Third entry: Unicode characters as UTF-8 | ||
*Index32 = "🐣"; | ||
*Index64 = "🐣"; | ||
*SplitIndex32 = "🐣"; | ||
*SplitIndex64 = "🐣"; | ||
writer->Fill(); | ||
|
||
// Fourth entry: increasing length of strings | ||
*Index32 = "a"; | ||
*Index64 = "bc"; | ||
*SplitIndex32 = "def"; | ||
*SplitIndex64 = "ghij"; | ||
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,17 @@ | ||
# `std::variant` | ||
|
||
## Fields | ||
|
||
* `f` of type `std::variant<std::int32_t, std::string, std::vector<std::int32_t>>` | ||
|
||
## Entries | ||
|
||
1. `std::int32_t` with value `1` | ||
2. `std::string` with value `"abc"` | ||
3. `std::vector<std::int32_t>` with value `{1, 2, 3}` | ||
4. Empty `std::string` | ||
5. Empty `std::vector` | ||
|
||
__Missing:__ | ||
A `std::variant` that is `valueless_by_exception`. | ||
This requires a type that throws during move assignment. |
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,76 @@ | ||
#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 <variant> | ||
#include <vector> | ||
|
||
using Vector = std::vector<std::int32_t>; | ||
using Variant = std::variant<std::int32_t, std::string, Vector>; | ||
|
||
static void PrintVariantValue(const REntry &entry, std::string_view name, | ||
std::ostream &os, bool last = false) { | ||
Variant &value = *entry.GetPtr<Variant>(name); | ||
os << " \"" << name << "\": "; | ||
if (value.index() == 0) { | ||
os << std::get<std::int32_t>(value); | ||
} else if (value.index() == 1) { | ||
os << "\"" << std::get<std::string>(value) << "\""; | ||
} else if (value.index() == 2) { | ||
Vector &vectorValue = std::get<Vector>(value); | ||
os << "["; | ||
bool first = true; | ||
for (auto element : vectorValue) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
os << ","; | ||
} | ||
os << "\n " << element; | ||
} | ||
if (!vectorValue.empty()) { | ||
os << "\n "; | ||
} | ||
os << "]"; | ||
} | ||
|
||
if (!last) { | ||
os << ","; | ||
} | ||
os << "\n"; | ||
} | ||
|
||
void read(std::string_view input = "types.variant.root", | ||
std::string_view output = "types.variant.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"; | ||
|
||
PrintVariantValue(entry, "f", 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,47 @@ | ||
#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 <string> | ||
#include <string_view> | ||
#include <variant> | ||
#include <vector> | ||
|
||
using Vector = std::vector<std::int32_t>; | ||
using Variant = std::variant<std::int32_t, std::string, Vector>; | ||
|
||
void write(std::string_view filename = "types.variant.root") { | ||
auto model = RNTupleModel::Create(); | ||
|
||
auto value = model->MakeField<Variant>("f"); | ||
|
||
RNTupleWriteOptions options; | ||
options.SetCompression(0); | ||
auto writer = | ||
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options); | ||
|
||
// First entry: std::int32_t | ||
*value = 1; | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
writer->Fill(); | ||
|
||
// Second entry: std::string | ||
*value = "abc"; | ||
writer->Fill(); | ||
|
||
// Third entry: std::vector<std::int32_t> | ||
*value = Vector{1, 2, 3}; | ||
writer->Fill(); | ||
|
||
// Fourth entry: empty std::string | ||
*value = ""; | ||
writer->Fill(); | ||
|
||
// Fifth entry: empty std::vector | ||
*value = {}; | ||
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,4 @@ | ||
# `std::vector` | ||
|
||
* [`fundamental`](fundamental): `std::vector<std::int32_t>` | ||
* [`nested`](nested): `std::vector<std::vector<std::int32_t>>` |
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::vector<std::int32_t>` | ||
|
||
## Fields | ||
|
||
* `[Split]Index{32,64}` | ||
|
||
with the corresponding column type for offset column of the collection parent field. | ||
All child fields use the default column encoding `Int32`. | ||
|
||
## Entries | ||
|
||
1. Single-element vectors, with ascending values | ||
2. Empty vectors | ||
3. Increasing number of elements in the vector: | ||
one element in the first field, two elements in the second field, etc. |
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,68 @@ | ||
#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 <vector> | ||
|
||
using Vector = std::vector<std::int32_t>; | ||
|
||
static void PrintVectorValue(const REntry &entry, std::string_view name, | ||
std::ostream &os, bool last = false) { | ||
Vector &value = *entry.GetPtr<Vector>(name); | ||
os << " \"" << name << "\": ["; | ||
bool first = true; | ||
for (auto element : value) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
os << ","; | ||
} | ||
os << "\n " << element; | ||
} | ||
if (!value.empty()) { | ||
os << "\n "; | ||
} | ||
os << "]"; | ||
if (!last) { | ||
os << ","; | ||
} | ||
os << "\n"; | ||
} | ||
|
||
void read(std::string_view input = "types.vector.fundamental.root", | ||
std::string_view output = "types.vector.fundamental.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"; | ||
|
||
PrintVectorValue(entry, "Index32", os); | ||
PrintVectorValue(entry, "Index64", os); | ||
PrintVectorValue(entry, "SplitIndex32", os); | ||
PrintVectorValue(entry, "SplitIndex64", 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.