Skip to content

Commit 7977b24

Browse files
committed
Add test for std::atomic
Closes #8
1 parent 7a27276 commit 7977b24

4 files changed

Lines changed: 175 additions & 0 deletions

File tree

types/README.md

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

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

types/atomic/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `std::atomic`
2+
3+
## Fields
4+
5+
* `Bit`
6+
* `Byte`
7+
* `Char`
8+
* `[U]Int{8,16,32,64}`
9+
* `Real{32,64}`
10+
11+
with the corresponding column types.
12+
13+
## Entries
14+
15+
1. Ascending values
16+
2. Zero values

types/atomic/read.C

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
4+
using ROOT::Experimental::REntry;
5+
using ROOT::Experimental::RNTupleReader;
6+
7+
#include <atomic>
8+
#include <cstddef> // for std::byte
9+
#include <cstdint>
10+
#include <fstream>
11+
#include <ostream>
12+
#include <string>
13+
#include <string_view>
14+
15+
template <typename T>
16+
static void PrintIntegerValue(const REntry &entry, std::string_view name,
17+
std::ostream &os, bool last = false) {
18+
T value = *entry.GetPtr<std::atomic<T>>(name);
19+
os << " \"" << name << "\": ";
20+
// We want to print the integer value even if it is a character; use the unary
21+
// + operator (https://stackoverflow.com/a/28414758).
22+
os << +value;
23+
if (!last) {
24+
os << ",";
25+
}
26+
os << "\n";
27+
}
28+
29+
template <typename T>
30+
static void PrintRealValue(const REntry &entry, std::string_view name,
31+
std::ostream &os, bool last = false) {
32+
T value = *entry.GetPtr<std::atomic<T>>(name);
33+
os << " \"" << name << "\": \"" << value << "\"";
34+
if (!last) {
35+
os << ",";
36+
}
37+
os << "\n";
38+
}
39+
40+
void read(std::string_view input = "types.atomic.root",
41+
std::string_view output = "types.atomic.json") {
42+
std::ofstream os(std::string{output});
43+
// Print floating-point numbers as hexadecimal literals.
44+
os << std::hexfloat;
45+
os << "[\n";
46+
47+
auto reader = RNTupleReader::Open("ntpl", input);
48+
auto &entry = reader->GetModel().GetDefaultEntry();
49+
bool first = true;
50+
for (auto index : *reader) {
51+
reader->LoadEntry(index);
52+
53+
if (first) {
54+
first = false;
55+
} else {
56+
os << ",\n";
57+
}
58+
os << " {\n";
59+
60+
PrintIntegerValue<bool>(entry, "Bit", os);
61+
os << " \"Byte\": ";
62+
os << std::to_integer<int>(*entry.GetPtr<std::atomic<std::byte>>("Byte"));
63+
os << ",\n";
64+
PrintIntegerValue<char>(entry, "Char", os);
65+
PrintIntegerValue<std::int8_t>(entry, "Int8", os);
66+
PrintIntegerValue<std::uint8_t>(entry, "UInt8", os);
67+
PrintIntegerValue<std::int16_t>(entry, "Int16", os);
68+
PrintIntegerValue<std::uint16_t>(entry, "UInt16", os);
69+
PrintIntegerValue<std::int32_t>(entry, "Int32", os);
70+
PrintIntegerValue<std::uint32_t>(entry, "UInt32", os);
71+
PrintIntegerValue<std::int64_t>(entry, "Int64", os);
72+
PrintIntegerValue<std::uint64_t>(entry, "UInt64", os);
73+
PrintRealValue<float>(entry, "Real32", os);
74+
PrintRealValue<double>(entry, "Real64", os, /*last=*/true);
75+
76+
os << " }";
77+
// Newline is intentionally missing, may need to print a comma before the
78+
// next entry.
79+
}
80+
os << "\n";
81+
os << "]\n";
82+
}

types/atomic/write.C

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 <atomic>
10+
#include <cstddef> // for std::byte
11+
#include <cstdint>
12+
#include <memory>
13+
#include <string_view>
14+
15+
template <typename T>
16+
static std::shared_ptr<std::atomic<T>> MakeAtomicField(RNTupleModel &model,
17+
std::string_view name) {
18+
return model.MakeField<std::atomic<T>>(name);
19+
}
20+
21+
void write(std::string_view filename = "types.atomic.root") {
22+
auto model = RNTupleModel::Create();
23+
24+
auto Bit = MakeAtomicField<bool>(*model, "Bit");
25+
auto Byte = MakeAtomicField<std::byte>(*model, "Byte");
26+
auto Char = MakeAtomicField<char>(*model, "Char");
27+
28+
auto Int8 = MakeAtomicField<std::int8_t>(*model, "Int8");
29+
auto UInt8 = MakeAtomicField<std::uint8_t>(*model, "UInt8");
30+
auto Int16 = MakeAtomicField<std::int16_t>(*model, "Int16");
31+
auto UInt16 = MakeAtomicField<std::uint16_t>(*model, "UInt16");
32+
auto Int32 = MakeAtomicField<std::int32_t>(*model, "Int32");
33+
auto UInt32 = MakeAtomicField<std::uint32_t>(*model, "UInt32");
34+
auto Int64 = MakeAtomicField<std::int64_t>(*model, "Int64");
35+
auto UInt64 = MakeAtomicField<std::uint64_t>(*model, "UInt64");
36+
37+
auto Real32 = MakeAtomicField<float>(*model, "Real32");
38+
auto Real64 = MakeAtomicField<double>(*model, "Real64");
39+
40+
RNTupleWriteOptions options;
41+
options.SetCompression(0);
42+
auto writer =
43+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
44+
45+
// First entry: ascending values
46+
*Bit = 1;
47+
*Byte = std::byte{2};
48+
*Char = 3;
49+
*Int8 = 4;
50+
*UInt8 = 5;
51+
*Int16 = 6;
52+
*UInt16 = 7;
53+
*Int32 = 8;
54+
*UInt32 = 9;
55+
*Int64 = 10;
56+
*UInt64 = 11;
57+
*Real32 = 12.0f;
58+
*Real64 = 13.0;
59+
writer->Fill();
60+
61+
// Second entry: zero values
62+
*Bit = 0;
63+
*Byte = std::byte{0};
64+
*Char = 0;
65+
*Int8 = 0;
66+
*UInt8 = 0;
67+
*Int16 = 0;
68+
*UInt16 = 0;
69+
*Int32 = 0;
70+
*UInt32 = 0;
71+
*Int64 = 0;
72+
*UInt64 = 0;
73+
*Real32 = 0;
74+
*Real64 = 0;
75+
writer->Fill();
76+
}

0 commit comments

Comments
 (0)