31 #include <condition_variable>
37 #define CATCH_CONFIG_MAIN
38 #include <catch2/catch_amalgamated.hpp>
62 constexpr
int number(
const Defaults rhs) noexcept {
63 return static_cast<int>(rhs);
67 int array[array_size] = { 0 };
69 std::condition_variable cvRead;
70 std::condition_variable cvWrite;
72 void reset(
int v1,
int array_value) {
73 std::unique_lock<std::mutex> lock(mtx_value);
75 for(
int i=0; i<array_size; i++) {
76 array[i] = array_value;
80 void putThreadType01(
int _len,
int startValue) {
81 const int len = std::min(
number(array_size), _len);
83 std::unique_lock<std::mutex> lock(mtx_value);
84 for(
int i=0; i<len; i++) {
85 array[i] = startValue+i;
91 void getThreadType01(
const std::string msg,
int _len,
int startValue) {
92 const int len = std::min(
number(array_size), _len);
94 std::unique_lock<std::mutex> lock(mtx_value);
95 while( startValue != value1 ) {
98 REQUIRE_MSG(msg+
": %s: value at read value1 (start)", startValue == value1);
100 for(
int i=0; i<len; i++) {
106 void putThreadType11(
int indexAndValue) {
107 const int idx = std::min(
number(array_size)-1, indexAndValue);
112 std::unique_lock<std::mutex> lock(mtx_value);
114 while( idx != (value1 * -1) - 1 ) {
123 void getThreadType11(
const std::string msg,
int _idx) {
124 const int idx = std::min(
number(array_size)-1, _idx);
130 std::unique_lock<std::mutex> lock(mtx_value);
131 while( idx != value1 ) {
138 int next_idx = (idx+1)%array_size;
139 next_idx = ( next_idx + 1 ) * -1;
142 cvWrite.notify_all();
152 INFO_STR(
"\n\ntest01_Read1Write1.a\n");
155 std::thread getThread01(&TestMemModelSCDRF01::getThreadType01,
this,
"test01.get01", array_size, 3);
156 std::thread putThread01(&TestMemModelSCDRF01::putThreadType01,
this, array_size, 3);
162 INFO_STR(
"\n\ntest01_Read2Write1.a\n");
165 std::thread getThread00(&TestMemModelSCDRF01::getThreadType01,
this,
"test01.get00", array_size, 4);
166 std::thread getThread01(&TestMemModelSCDRF01::getThreadType01,
this,
"test01.get01", array_size, 4);
167 std::thread putThread01(&TestMemModelSCDRF01::putThreadType01,
this, array_size, 4);
173 INFO_STR(
"\n\ntest01_Read2Write1.b\n");
176 std::thread putThread01(&TestMemModelSCDRF01::putThreadType01,
this, array_size, 5);
177 std::thread getThread00(&TestMemModelSCDRF01::getThreadType01,
this,
"test01.get00", array_size, 5);
178 std::thread getThread01(&TestMemModelSCDRF01::getThreadType01,
this,
"test01.get01", array_size, 5);
186 INFO_STR(
"\n\ntest02_Read4Write1\n");
189 std::thread getThread01(&TestMemModelSCDRF01::getThreadType01,
this,
"test02.get01", array_size, 6);
190 std::thread getThread02(&TestMemModelSCDRF01::getThreadType01,
this,
"test02.get02", array_size, 6);
191 std::thread putThread01(&TestMemModelSCDRF01::putThreadType01,
this, array_size, 6);
192 std::thread getThread03(&TestMemModelSCDRF01::getThreadType01,
this,
"test02.get03", array_size, 6);
193 std::thread getThread04(&TestMemModelSCDRF01::getThreadType01,
this,
"test02.get04", array_size, 6);
202 INFO_STR(
"\n\ntest11_Read10Write10\n");
205 std::thread reader[array_size];
206 std::thread writer[array_size];
207 for(
int i=0; i<
number(array_size); i++) {
208 reader[i] = std::thread(&TestMemModelSCDRF01::getThreadType11,
this,
"test11.get11", i);
210 for(
int i=0; i<
number(array_size); i++) {
211 writer[i] = std::thread(&TestMemModelSCDRF01::putThreadType11,
this, i);
213 for(
int i=0; i<
number(array_size); i++) {
216 for(
int i=0; i<
number(array_size); i++) {
222 INFO_STR(
"\n\ntest12_Read10Write10\n");
225 std::thread reader[array_size];
226 std::thread writer[array_size];
227 for(
int i=0; i<
number(array_size); i++) {
228 writer[i] = std::thread(&TestMemModelSCDRF01::putThreadType11,
this, i);
230 for(
int i=0; i<
number(array_size); i++) {
231 reader[i] = std::thread(&TestMemModelSCDRF01::getThreadType11,
this,
"test12.get11", i);
233 for(
int i=0; i<
number(array_size); i++) {
236 for(
int i=0; i<
number(array_size); i++) {
242 for(
int i=
loops; i>0; i--) { test01_Read1Write1(); }
243 for(
int i=
loops; i>0; i--) { test02_Read2Write1(); }
244 for(
int i=
loops; i>0; i--) { test03_Read4Write1(); }
245 for(
int i=
loops; i>0; i--) { test11_Read10Write10(); }
246 for(
int i=
loops; i>0; i--) { test12_Read10Write10(); }