forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoveTest.h
More file actions
115 lines (88 loc) · 2.84 KB
/
MoveTest.h
File metadata and controls
115 lines (88 loc) · 2.84 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
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
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include "gtest/gtest.h"
#include "react/Domain.h"
#include "react/Signal.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace {
using namespace react;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// MoveTest fixture
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TParams>
class MoveTest : public testing::Test
{
public:
template <EPropagationMode mode>
class MyEngine : public TParams::template EngineT<mode> {};
REACTIVE_DOMAIN(MyDomain, TParams::mode, MyEngine)
struct Stats
{
int moveCount = 0;
};
struct CopyCounter
{
int v = 0;
Stats* stats = nullptr;
CopyCounter(int x, Stats* s) :
v( x ),
stats( s )
{}
CopyCounter(const CopyCounter& other) = delete;
CopyCounter& operator=(const CopyCounter& other) = delete;
CopyCounter(CopyCounter&& other) :
v( other.v ),
stats( other.stats )
{
stats->moveCount++;
}
CopyCounter& operator=(CopyCounter&& other)
{
v = other.v;
stats = other.stats;
stats->moveCount++;
return *this;
}
CopyCounter operator+(const CopyCounter& r) const
{
return CopyCounter{ v + r.v, stats };
}
bool operator==(const CopyCounter& other) const
{
return v == other.v;
}
};
};
TYPED_TEST_CASE_P(MoveTest);
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Copy1
///////////////////////////////////////////////////////////////////////////////////////////////////
TYPED_TEST_P(MoveTest, Copy1)
{
using D = typename Copy1::MyDomain;
using CopyCounter = typename Copy1::CopyCounter;
using Stats = typename Copy1::Stats;
Stats stats1;
auto a = MakeVar<D>(CopyCounter{1,&stats1});
auto b = MakeVar<D>(CopyCounter{10,&stats1});
auto c = MakeVar<D>(CopyCounter{100,&stats1});
auto d = MakeVar<D>(CopyCounter{1000,&stats1});
// 4x move to value_
ASSERT_EQ(4, stats1.moveCount);
auto x = a + b + c + d;
ASSERT_EQ(7, stats1.moveCount);
ASSERT_EQ(1111, x().v);
a <<= CopyCounter{2,&stats1};
ASSERT_EQ(10, stats1.moveCount);
ASSERT_EQ(1112, x().v);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
REGISTER_TYPED_TEST_CASE_P
(
MoveTest,
Copy1
);
} // ~namespace