Skip to content

Commit 44db716

Browse files
committed
Add test for std::bitset
Closes #9
1 parent 7977b24 commit 44db716

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [`array`](array): `std::array`
44
* [`atomic`](atomic): `std::atomic`
5+
* [`bitset`](bitset): `std::bitset`
56
* [`fundamental`](fundamental): fundamental column types
67
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
78
* [`optional`](optional): `std::optional` with different element types

types/bitset/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::bitset`
2+
3+
## Fields
4+
5+
* `Bitset1`: `std::bitset<1>`
6+
* `Bitset64`: `std::bitset<64>`
7+
* `Bitset65`: `std::bitset<65>`
8+
9+
with the default column types.
10+
11+
## Entries
12+
13+
1. Ascending bits set
14+
2. All bits set
15+
3. All bits unset

types/bitset/read.C

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

types/bitset/write.C

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <ROOT/RNTupleModel.hxx>
2+
#include <ROOT/RNTupleWriteOptions.hxx>
3+
#include <ROOT/RNTupleWriter.hxx>
4+
5+
using ROOT::Experimental::RNTupleModel;
6+
using ROOT::Experimental::RNTupleWriteOptions;
7+
using ROOT::Experimental::RNTupleWriter;
8+
9+
#include <bitset>
10+
#include <memory>
11+
#include <string_view>
12+
13+
void write(std::string_view filename = "types.bitset.root") {
14+
auto model = RNTupleModel::Create();
15+
16+
auto Bitset1 = model->MakeField<std::bitset<1>>("Bitset1");
17+
auto Bitset64 = model->MakeField<std::bitset<64>>("Bitset64");
18+
auto Bitset65 = model->MakeField<std::bitset<65>>("Bitset65");
19+
20+
RNTupleWriteOptions options;
21+
options.SetCompression(0);
22+
auto writer =
23+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
24+
25+
// First entry: ascending bits set
26+
Bitset1->set(0);
27+
Bitset64->set(1);
28+
Bitset65->set(2);
29+
writer->Fill();
30+
31+
// Second entry: all bits set
32+
Bitset1->set();
33+
Bitset64->set();
34+
Bitset65->set();
35+
writer->Fill();
36+
37+
// Third entry: no bits set
38+
Bitset1->reset();
39+
Bitset64->reset();
40+
Bitset65->reset();
41+
writer->Fill();
42+
}

0 commit comments

Comments
 (0)