Skip to content

Commit bdf5d81

Browse files
committed
Add test for std::string
... with all possible index column types. Closes #2
1 parent 186e42f commit bdf5d81

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-1
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Types
22

33
* [`fundamental`](fundamental): fundamental column types
4+
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types

types/fundamental/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* [`real`](real): `Real{16,32,64}`, `SplitReal{32,64}`
66

77
__Covered under a different category:__
8-
* `[Split]Index{32,64}`
8+
* `[Split]Index{32,64}`: with [`std::string`](../string)
99
* `Switch`
1010

1111
__Missing:__

types/string/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::string`
2+
3+
## Fields
4+
5+
* `[Split]Index{32,64}`
6+
7+
with the corresponding column type for the first (principal) column.
8+
9+
## Entries
10+
11+
1. One character strings, with ascending values
12+
2. Empty strings
13+
3. Unicode characters as UTF-8
14+
4. Increasing length of strings:
15+
one character in the first field, two characters in the second field, etc.

types/string/read.C

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
4+
using ROOT::Experimental::REntry;
5+
using ROOT::Experimental::RNTupleReader;
6+
7+
#include <fstream>
8+
#include <ostream>
9+
#include <string>
10+
#include <string_view>
11+
12+
static void PrintStringValue(const REntry &entry, std::string_view name,
13+
std::ostream &os, bool last = false) {
14+
std::string &value = *entry.GetPtr<std::string>(name);
15+
os << " \"" << name << "\": \"" << value << "\"";
16+
if (!last) {
17+
os << ",";
18+
}
19+
os << "\n";
20+
}
21+
22+
void read(std::string_view input = "types.string.root",
23+
std::string_view output = "types.string.json") {
24+
std::ofstream os(std::string{output});
25+
os << "[\n";
26+
27+
auto reader = RNTupleReader::Open("ntpl", input);
28+
auto &entry = reader->GetModel().GetDefaultEntry();
29+
bool first = true;
30+
for (auto index : *reader) {
31+
reader->LoadEntry(index);
32+
33+
if (first) {
34+
first = false;
35+
} else {
36+
os << ",\n";
37+
}
38+
os << " {\n";
39+
40+
PrintStringValue(entry, "Index32", os);
41+
PrintStringValue(entry, "Index64", os);
42+
PrintStringValue(entry, "SplitIndex32", os);
43+
PrintStringValue(entry, "SplitIndex64", os, /*last=*/true);
44+
45+
os << " }";
46+
// Newline is intentionally missing, may need to print a comma before the
47+
// next entry.
48+
}
49+
os << "\n";
50+
os << "]\n";
51+
}

types/string/write.C

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 <memory>
14+
#include <string>
15+
#include <string_view>
16+
17+
static std::shared_ptr<std::string> MakeStringField(RNTupleModel &model,
18+
std::string_view name,
19+
EColumnType indexType) {
20+
auto field = std::make_unique<RField<std::string>>(name);
21+
field->SetColumnRepresentatives({{indexType, EColumnType::kChar}});
22+
model.AddField(std::move(field));
23+
return model.GetDefaultEntry().GetPtr<std::string>(name);
24+
}
25+
26+
void write(std::string_view filename = "types.string.root") {
27+
auto model = RNTupleModel::Create();
28+
29+
// Non-split index encoding
30+
auto Index32 = MakeStringField(*model, "Index32", EColumnType::kIndex32);
31+
auto Index64 = MakeStringField(*model, "Index64", EColumnType::kIndex64);
32+
33+
// Split index encoding
34+
auto SplitIndex32 =
35+
MakeStringField(*model, "SplitIndex32", EColumnType::kSplitIndex32);
36+
auto SplitIndex64 =
37+
MakeStringField(*model, "SplitIndex64", EColumnType::kSplitIndex64);
38+
39+
RNTupleWriteOptions options;
40+
options.SetCompression(0);
41+
auto writer =
42+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
43+
44+
// First entry: one character strings, with ascending values
45+
*Index32 = "a";
46+
*Index64 = "b";
47+
*SplitIndex32 = "c";
48+
*SplitIndex64 = "d";
49+
writer->Fill();
50+
51+
// Second entry: empty strings
52+
*Index32 = "";
53+
*Index64 = "";
54+
*SplitIndex32 = "";
55+
*SplitIndex64 = "";
56+
writer->Fill();
57+
58+
// Third entry: Unicode characters as UTF-8
59+
*Index32 = "🐣";
60+
*Index64 = "🐣";
61+
*SplitIndex32 = "🐣";
62+
*SplitIndex64 = "🐣";
63+
writer->Fill();
64+
65+
// Fourth entry: increasing length of strings
66+
*Index32 = "a";
67+
*Index64 = "bc";
68+
*SplitIndex32 = "def";
69+
*SplitIndex64 = "ghij";
70+
writer->Fill();
71+
}

0 commit comments

Comments
 (0)