-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolymorphic_usage_tests.cpp
68 lines (54 loc) · 2.08 KB
/
polymorphic_usage_tests.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
#include <gtest/gtest.h>
#include <thread>
#include "tracked/policy/all_policies.hpp"
#include "tracked/tracked.hpp"
using namespace policy;
using namespace std;
class TestingClass {
public:
TestingClass() { }
~TestingClass() { }
// since tracked_ptr based on std::unique_ptr,
// we need to ensure that testing class will never copied.
TestingClass(const TestingClass&) = delete;
// but we can move object through tracked_ptr instances
TestingClass(TestingClass&&) = default;
int doubled(int val)
{
return val * 2;
}
private:
};
using test_exception = std::exception;
template<class T, class Ex = exceptions::throw_on_exception<test_exception>>
using traits = tracked_traits<T, dtl::default_deleter<T>, Ex>;
TEST(PolymorphicUsageTest, PolicyChanging)
{
auto first_policy_ptr = make_tracked_ptr<TestingClass, exceptions::throw_on_exception<test_exception>,
must_accessed_by_single_thread, should_use_max_times<2>::type>();
ASSERT_NO_THROW(first_policy_ptr->doubled(1));
ASSERT_NO_THROW(first_policy_ptr->doubled(2));
tracked_ptr<TestingClass, tracked_traits<TestingClass>> relaxed_ptr = std::move(first_policy_ptr);
std::thread th([&] { ASSERT_NO_THROW(relaxed_ptr->doubled(3)); });
th.join();
ASSERT_NO_THROW(relaxed_ptr->doubled(4));
bool catched_true_exception = false;
try {
tracked_ptr<TestingClass, traits<TestingClass>, should_use_min_times<1>::type> third_ptr = std::move(relaxed_ptr);
}
catch (const test_exception&) {
catched_true_exception = true;
}
catch (...) {
catched_true_exception = false;
}
ASSERT_TRUE(catched_true_exception);
}
TEST(PolymorphicUsageTest, PolicyChangeWith_MakeTrackedPtr)
{
auto first_policy_ptr = make_tracked_ptr<TestingClass, exceptions::throw_on_exception<test_exception>,
must_accessed_by_single_thread, should_use_max_times<2>::type>();
auto TestingClassAdd = first_policy_ptr.get();
auto relaxed_ptr = make_tracked_ptr<TestingClass>(std::move(first_policy_ptr));
ASSERT_EQ(TestingClassAdd, relaxed_ptr.get());
}