forked from intel/cpp-std-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
54 lines (43 loc) · 1.25 KB
/
memory.cpp
File metadata and controls
54 lines (43 loc) · 1.25 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
48
49
50
51
52
53
54
#include <stdx/memory.hpp>
#include <catch2/catch_test_macros.hpp>
#include <iterator>
#include <memory>
#include <vector>
TEST_CASE("to_address for pointer (constexpr)", "[memory]") {
constexpr static int a{};
constexpr static auto p = &a;
STATIC_REQUIRE(stdx::to_address(p) == p);
}
TEST_CASE("to_address for pointer (runtime)", "[memory]") {
int a; // deliberately uninitialized
auto p = &a;
CHECK(stdx::to_address(p) == p);
}
TEST_CASE("to_address for iterator", "[memory]") {
std::vector<int> v{1, 2, 3};
auto p = std::begin(v);
CHECK(stdx::to_address(p) == std::data(v));
}
namespace {
struct fancy {};
} // namespace
template <> struct std::pointer_traits<fancy> {
constexpr static auto to_address(fancy) { return 42; }
};
TEST_CASE("to_address for fancy pointer", "[memory]") {
fancy f{};
CHECK(stdx::to_address(f) == 42);
}
namespace {
struct fancier {
constexpr static int value = 17;
constexpr auto operator->() const { return &value; }
};
} // namespace
template <> struct std::pointer_traits<fancier> {
constexpr static auto to_address(fancier) { return 42; }
};
TEST_CASE("to_address for fancy pointer (prefer pointer_traits)", "[memory]") {
fancier f{};
CHECK(stdx::to_address(f) == 42);
}