FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
taskflow/unittests/test_spsc.cpp at master · taskflow/taskflow · GitHub
taskflow
/
taskflow
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
1.4k
Star
12.2k
Code
Issues
20
Pull requests
16
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
taskflow
/
unittests
/
test_spsc.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
194 lines (155 loc) · 5.09 KB
Breadcrumbs
taskflow
/
unittests
/
test_spsc.cpp
Copy path
File metadata and controls
194 lines (155 loc) · 5.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#
define
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#
include
<
doctest.h
>
#
include
<
taskflow/utility/spsc.hpp
>
#
include
<
atomic
>
#
include
<
thread
>
#
include
<
vector
>
// ============================================================================
//
Compile-time checks
// ============================================================================
static_assert
(tf::SPSCRing<
int
,
7
>::capacity() ==
127
);
static_assert
(tf::SPSCRing<
int
,
1
>::capacity() ==
1
);
//
LogSize=30 would instantiate a 4 GB std::array — verify the capacity formula
//
arithmetically rather than by instantiating the type.
static_assert
(((std::
size_t
{
1
} <<
30
) -
1
) == (std::
size_t
{
1
} <<
30
) -
1
);
static_assert
(std::is_same_v<tf::SPSCRing<
int
,
7
>::value_type, std::optional<
int
>>);
static_assert
(std::is_same_v<tf::SPSCRing<
int
*,
7
>::value_type,
int
*>);
static_assert
(std::is_same_v<tf::SPSCRing<
void
*,
7
>::value_type,
void
*>);
// ============================================================================
//
Non-pointer T (value_type = std::optional<T>)
// ============================================================================
TEST_CASE
(
"
SPSCRing<int,7>: capacity
"
) {
tf::SPSCRing<
int
,
7
> ring;
REQUIRE
(ring.
capacity
() ==
127
);
REQUIRE
(ring.
empty_approx
() ==
true
);
REQUIRE
(ring.
full_approx
() ==
false
);
REQUIRE
(ring.
size_approx
() ==
0
);
}
TEST_CASE
(
"
SPSCRing<int,3>: push/pop basic
"
) {
tf::SPSCRing<
int
,
3
> ring;
//
capacity = 7
for
(
int
i =
0
; i <
7
; ++i) {
REQUIRE
(ring.
push
(i) ==
true
);
}
REQUIRE
(ring.
full_approx
() ==
true
);
REQUIRE
(ring.
push
(
99
) ==
false
);
//
full
for
(
int
i =
0
; i <
7
; ++i) {
auto
v = ring.
pop
();
REQUIRE
(v.
has_value
() ==
true
);
REQUIRE
(*v == i);
}
REQUIRE
(ring.
empty_approx
() ==
true
);
REQUIRE
(ring.
pop
() == std::
nullopt
);
}
TEST_CASE
(
"
SPSCRing<int,3>: wrap-around
"
) {
tf::SPSCRing<
int
,
3
> ring;
//
capacity = 7
//
fill, drain, fill again - exercises the modular wrap
for
(
int
round =
0
; round <
4
; ++round) {
for
(
int
i =
0
; i <
7
; ++i) {
REQUIRE
(ring.
push
(i *
10
) ==
true
);
}
REQUIRE
(ring.
size_approx
() ==
7
);
for
(
int
i =
0
; i <
7
; ++i) {
auto
v = ring.
pop
();
REQUIRE
(v.
has_value
());
REQUIRE
(*v == i *
10
);
}
REQUIRE
(ring.
empty_approx
() ==
true
);
}
}
TEST_CASE
(
"
SPSCRing<int,3>: copy overload
"
) {
tf::SPSCRing<
int
,
3
> ring;
const
int
x =
42
;
REQUIRE
(ring.
push
(x) ==
true
);
auto
v = ring.
pop
();
REQUIRE
(v.
has_value
());
REQUIRE
(*v ==
42
);
REQUIRE
(x ==
42
);
//
original unchanged
}
TEST_CASE
(
"
SPSCRing<int,3>: size_approx
"
) {
tf::SPSCRing<
int
,
3
> ring;
REQUIRE
(ring.
size_approx
() ==
0
);
(
void
)ring.
push
(
1
);
REQUIRE
(ring.
size_approx
() ==
1
);
(
void
)ring.
push
(
2
);
REQUIRE
(ring.
size_approx
() ==
2
);
(
void
)ring.
pop
();
REQUIRE
(ring.
size_approx
() ==
1
);
(
void
)ring.
pop
();
REQUIRE
(ring.
size_approx
() ==
0
);
}
// ============================================================================
//
Pointer T (value_type = T, empty = nullptr)
// ============================================================================
TEST_CASE
(
"
SPSCRing<int*,3>: nullptr sentinel
"
) {
tf::SPSCRing<
int
*,
3
> ring;
REQUIRE
(ring.
pop
() ==
nullptr
);
int
a =
10
, b =
20
;
REQUIRE
(ring.
push
(&a) ==
true
);
REQUIRE
(ring.
push
(&b) ==
true
);
REQUIRE
(ring.
pop
() == &a);
REQUIRE
(ring.
pop
() == &b);
REQUIRE
(ring.
pop
() ==
nullptr
);
}
TEST_CASE
(
"
SPSCRing<int*,3>: full then drain
"
) {
tf::SPSCRing<
int
*,
3
> ring;
//
capacity = 7
std::vector<
int
>
data
(
7
);
for
(
int
i =
0
; i <
7
; ++i) {
data[i] = i;
REQUIRE
(ring.
push
(&data[i]) ==
true
);
}
REQUIRE
(ring.
push
(
nullptr
) ==
false
);
//
full
for
(
int
i =
0
; i <
7
; ++i) {
int
* p = ring.
pop
();
REQUIRE
(p !=
nullptr
);
REQUIRE
(*p == i);
}
REQUIRE
(ring.
pop
() ==
nullptr
);
}
// ============================================================================
//
Concurrent: one producer thread + one consumer thread
// ============================================================================
TEST_CASE
(
"
SPSCRing<int,10>: concurrent producer-consumer
"
*
doctest::timeout
(
30
)) {
tf::SPSCRing<
int
,
10
> ring;
//
capacity = 1023
constexpr
int
N =
100000
;
std::thread
producer
([&]() {
for
(
int
i =
0
; i < N; ++i) {
while
(!ring.
push
(i)) {
/*
spin - ring full
*/
}
}
});
std::thread
consumer
([&]() {
int
expected =
0
;
while
(expected < N) {
if
(
auto
v = ring.
pop
()) {
REQUIRE
(*v == expected);
++expected;
}
}
});
producer.
join
();
consumer.
join
();
REQUIRE
(ring.
empty_approx
() ==
true
);
}
TEST_CASE
(
"
SPSCRing<int*,10>: concurrent producer-consumer pointer
"
*
doctest::timeout
(
30
)) {
tf::SPSCRing<
int
*,
10
> ring;
constexpr
int
N =
100000
;
std::vector<
int
>
data
(N);
for
(
int
i =
0
; i < N; ++i) data[i] = i;
std::thread
producer
([&]() {
for
(
int
i =
0
; i < N; ++i) {
while
(!ring.
push
(&data[i])) {
/*
spin
*/
}
}
});
std::thread
consumer
([&]() {
int
expected =
0
;
while
(expected < N) {
int
* p = ring.
pop
();
if
(p !=
nullptr
) {
REQUIRE
(*p == expected);
++expected;
}
}
});
producer.
join
();
consumer.
join
();
REQUIRE
(ring.
pop
() ==
nullptr
);
}
Back
|
FazBrowse Home
|
New Git URL