Skip to content

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 3 commits into from
Feb 27, 2025
Merged
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
3 changes: 3 additions & 0 deletions types/README.md
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
4 changes: 2 additions & 2 deletions types/fundamental/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* [`real`](real): `Real{16,32,64}`, `SplitReal{32,64}`

__Covered under a different category:__
* `[Split]Index{32,64}`
* `Switch`
* `[Split]Index{32,64}`: with [`std::string`](../string) and [`std::vector`](../vector)
* `Switch`: with [`std::variant`](../variant)

__Missing:__
* `Real32Trunc`
Expand Down
15 changes: 15 additions & 0 deletions types/string/README.md
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.
51 changes: 51 additions & 0 deletions types/string/read.C
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";
}
71 changes: 71 additions & 0 deletions types/string/write.C
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();
}
17 changes: 17 additions & 0 deletions types/variant/README.md
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.
76 changes: 76 additions & 0 deletions types/variant/read.C
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";
}
47 changes: 47 additions & 0 deletions types/variant/write.C
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;
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();
}
4 changes: 4 additions & 0 deletions types/vector/README.md
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>>`
15 changes: 15 additions & 0 deletions types/vector/fundamental/README.md
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.
68 changes: 68 additions & 0 deletions types/vector/fundamental/read.C
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";
}
Loading