-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.cpp
119 lines (102 loc) · 3.22 KB
/
test.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <type_traits>
#include <vector>
#include <stdexcept>
#undef NDEBUG
#include <cassert>
#include "rustex.h"
static_assert(!std::is_copy_constructible_v<rustex::mutex<int>>, "intrinsics error");
static_assert(!std::is_copy_assignable_v<rustex::mutex<int>>, "intrinsics error");
static_assert(!std::is_move_constructible_v<rustex::mutex<int>>, "intrinsics error");
static_assert(!std::is_move_assignable_v<rustex::mutex<int>>, "intrinsics error");
static_assert(!std::is_default_constructible_v<rustex::mutex<int>::mut_guard>, "intrinsics error");
static_assert(!std::is_copy_constructible_v<rustex::mutex<int>::mut_guard>, "intrinsics error");
static_assert(!std::is_copy_assignable_v<rustex::mutex<int>::mut_guard>, "intrinsics error");
static_assert(std::is_move_constructible_v<rustex::mutex<int>::mut_guard>, "intrinsics error");
static_assert(std::is_move_assignable_v<rustex::mutex<int>::mut_guard>, "intrinsics error");
static_assert(!std::is_default_constructible_v<rustex::mutex<int>::guard>, "intrinsics error");
static_assert(!std::is_copy_constructible_v<rustex::mutex<int>::guard>, "intrinsics error");
static_assert(!std::is_copy_assignable_v<rustex::mutex<int>::guard>, "intrinsics error");
static_assert(std::is_move_constructible_v<rustex::mutex<int>::guard>, "intrinsics error");
static_assert(std::is_move_assignable_v<rustex::mutex<int>::guard>, "intrinsics error");
static_assert(std::is_same_v<decltype(std::declval<rustex::mutex<int>::mut_guard>().operator*()), int&>, "dereference type error");
static_assert(std::is_same_v<decltype(std::declval<rustex::mutex<int>::mut_guard>().operator->()), int*>, "dereference type error");
static_assert(std::is_same_v<decltype(std::declval<rustex::mutex<int>::guard>().operator*()), const int&>, "dereference type error");
static_assert(std::is_same_v<decltype(std::declval<rustex::mutex<int>::guard>().operator->()), const int*>, "dereference type error");
rustex::mutex<int> global_val{ 563 };
int main() try
{
{
assert(*global_val.lock() == 563);
*global_val.lock_mut() = -32;
{
auto h1 = global_val.lock();
auto h2 = global_val.lock();
assert(*h1 == -32);
assert(&*h1 == &*h2);
}
}
{
rustex::mutex<int> v(55);
assert(*v.lock() == 55);
*v.lock_mut() = 66;
assert(*v.lock() == 66);
{
auto q = v.lock();
assert(!v.try_lock_mut());
}
{
auto q = v.lock_mut();
assert(!v.try_lock_mut());
assert(!v.try_lock());
}
{
(void)v.try_lock_mut();
}
{
(void)v.try_lock();
}
*v.lock_mut() = 77;
assert(*v.lock() == 77);
{
auto h = v.lock();
assert(*h == 77);
{
auto h2 = std::move(h);
assert(*h2 == 77);
}
}
}
{
rustex::mutex<std::vector<int>> m(6);
const auto &const_m = m;
{
auto h = const_m.lock();
assert(h->size() == 6);
auto h2 = const_m.lock();
assert(h2->size() == 6);
}
{
auto h = const_m.lock_mut();
assert(h->size() == 6);
}
{
(void)const_m.try_lock_mut();
}
{
(void)const_m.try_lock();
}
}
std::cout << "completed all tests successfully\n";
return 0;
}
catch (const std::exception &ex)
{
std::cerr << "UNHANDLED EXCEPTION: " << ex.what() << '\n';
return 666;
}
catch (...)
{
std::cerr << "UNHANDLED EXCEPTION OF UNKNOWN TYPE\n";
return 999;
}