Repository navigation
Expand file tree
/
Copy pathtest_atomic.cpp
More file actions
47 lines (39 loc) · 1.49 KB
/
Copy pathtest_atomic.cpp
File metadata and controls
47 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <atomic>
#include <memory>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include "write_and_read.hpp"
namespace test_atomic {
struct Stats {
std::atomic<std::uint64_t> bytes_downloaded;
std::atomic<bool> finished;
std::atomic_flag atomic_flag;
rfl::Ref<std::atomic<int>> ref_atomic_int;
rfl::Box<std::atomic<int>> box_atomic_int;
std::shared_ptr<std::atomic<int>> shared_atomic_int;
std::unique_ptr<std::atomic<int>> unique_atomic_int;
};
TEST(json, test_atomic) {
auto stats =
Stats{.bytes_downloaded = 123456789,
.finished = true,
.atomic_flag = ATOMIC_FLAG_INIT,
.ref_atomic_int = rfl::make_ref<std::atomic<int>>(42),
.box_atomic_int = rfl::Box<std::atomic<int>>::make(7),
.shared_atomic_int = std::make_shared<std::atomic<int>>(13),
.unique_atomic_int = std::make_unique<std::atomic<int>>(21)};
const auto json_str = rfl::json::write(stats);
Stats stats2{};
const auto res =
rfl::json::read<rfl::atomic::remove_atomic_t<Stats>>(json_str).transform(
[&](auto&& t) {
return rfl::atomic::set_atomic(std::move(t), &stats2);
});
ASSERT_TRUE(res.has_value()) << res.error().what();
EXPECT_EQ(rfl::json::write(stats2), json_str);
EXPECT_EQ(
json_str,
R"({"bytes_downloaded":123456789,"finished":true,"atomic_flag":false,"ref_atomic_int":42,"box_atomic_int":7,"shared_atomic_int":13,"unique_atomic_int":21})");
}
} // namespace test_atomic