-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimo_test.cpp
150 lines (126 loc) · 3.43 KB
/
mimo_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
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
/**
* @file mimo_test.cpp
* @author Jens Munk Hansen <[email protected]>
* @date Sun Apr 8 13:37:40 2018
*
* @brief
*
*
*/
#include <sps/cenv.h>
#include <gtest/gtest.h>
#include <iostream>
#include <chrono>
#include <random>
#include <thread>
#include <mutex>
#include <cstdio>
#include <sps/mimo.hpp>
#if 0 // defined(__GNUC__) && !defined(__CYGWIN__)
#include <sps/strace.hpp>
void Init() __attribute__((constructor(101)));
void Init() {
# if !defined(NDEBUG)
sps::STrace::Instance().Enable();
# endif
}
#endif
static void thread_push(void* arg) {
auto pQueue = (sps::MRMWQueue<float>*) arg;
for (size_t i = 0 ; i < 100 ; i++) {
float value = float(i);
pQueue->push(value);
}
}
static void thread_try_pop(void* arg) {
auto pQueue = (sps::MRMWQueue<float>*) arg;
float value = 0.0f;
for (size_t i = 0 ; i < 100 ; i++) {
pQueue->try_pop(value);
}
}
TEST(mimo_test, siso_queue_copyable) {
sps::MRMWQueue<float> queue;
for (size_t i = 0 ; i < 10 ; i++) {
queue.push(std::move(float(i)));
}
for (size_t i = 0 ; i < 10 ; i++) {
float val = 0.0f;
queue.try_pop(val);
}
EXPECT_EQ(1, 1);
}
TEST(mimo_test, siso_queue) {
sps::MRMWQueue<float> queue;
std::thread first (thread_push, (void*) &queue);
std::this_thread::sleep_for(std::chrono::milliseconds{10});
first.join(); // pauses until first finishes
std::thread second (thread_try_pop, (void*) &queue);
second.join(); // pauses until second finishes
EXPECT_EQ(1, 1);
}
TEST(mimo_test, circular_buffer) {
sps::MRMWCircularBuffer<int, 10> circularBuffer;
circularBuffer.push(1);
circularBuffer.push(2);
int val = 0;
circularBuffer.pop(val);
EXPECT_EQ(val, 1);
circularBuffer.pop(val);
EXPECT_EQ(val, 2);
}
// Add two threads
static void thread_push_circular(void* arg, size_t count) {
auto pQueue = (sps::MRMWCircularBuffer<int, 10>*) arg;
for (size_t i = 0 ; i < count ; i++) {
int value = int(i);
pQueue->push(value);
}
}
static void thread_try_pop_circular(void* arg, size_t count) {
auto pQueue = (sps::MRMWCircularBuffer<int, 10>*) arg;
int value = 0;
for (size_t i = 0 ; i < count ; i++) {
pQueue->try_pop(value);
// Important - if not popping, the pusher will hang and wait
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
}
TEST(mimo_test, circular_buffer2) {
sps::MRMWCircularBuffer<int, 10> queue;
std::thread first(thread_push_circular, (void*) &queue, 100);
std::this_thread::sleep_for(std::chrono::milliseconds{10});
std::thread second(thread_try_pop_circular, (void*) &queue, 100);
first.join();
second.join();
EXPECT_EQ(1, 1);
}
TEST(mimo_test, circular_buffer3) {
sps::MRMWCircularBuffer<int, 10> queue;
std::thread first(thread_push_circular, (void*) &queue, 100);
queue.invalidate();
first.join();
EXPECT_EQ(1, 1);
}
#if 0
static void thread_pop(void* arg) {
auto pQueue = (sps::MRMWQueue<float>*) arg;
float value = 0.0f;
for (size_t i = 0 ; i < 100 ; i++) {
pQueue->pop(value);
}
}
TEST(mimo_test, dtor) {
sps::MRMWQueue<float>* pQueue = new sps::MRMWQueue<float>();
std::thread second = std::thread(thread_pop, (void*) pQueue);
// TODO: Fix this using sps::thread
delete pQueue;
std::this_thread::sleep_for(std::chrono::milliseconds{500});
second.join();
EXPECT_EQ(1, 1);
}
#endif
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}