tests/cpp11_mutex: fix NDEBUG compile problem
This commit is contained in:
parent
944fb3909e
commit
bc071044d7
@ -19,7 +19,6 @@
|
|||||||
*/
|
*/
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cassert>
|
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
|
|
||||||
#include "riot/mutex.hpp"
|
#include "riot/mutex.hpp"
|
||||||
@ -27,6 +26,8 @@
|
|||||||
#include "riot/thread.hpp"
|
#include "riot/thread.hpp"
|
||||||
#include "riot/condition_variable.hpp"
|
#include "riot/condition_variable.hpp"
|
||||||
|
|
||||||
|
#include "test_utils/expect.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace riot;
|
using namespace riot;
|
||||||
|
|
||||||
@ -46,16 +47,22 @@ int main() {
|
|||||||
m.unlock();
|
m.unlock();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
#ifndef NDEBUG
|
||||||
|
/* We can't use expect here, otherwise cppcheck will produce errors */
|
||||||
assert(resource == 0);
|
assert(resource == 0);
|
||||||
|
#endif
|
||||||
auto start = std::chrono::system_clock::now();
|
auto start = std::chrono::system_clock::now();
|
||||||
thread t1(f);
|
thread t1(f);
|
||||||
thread t2(f);
|
thread t2(f);
|
||||||
t1.join();
|
t1.join();
|
||||||
t2.join();
|
t2.join();
|
||||||
|
#ifndef NDEBUG
|
||||||
|
/* We can't use expect here, otherwise cppcheck will produce errors */
|
||||||
assert(resource == 6);
|
assert(resource == 6);
|
||||||
|
#endif
|
||||||
auto duration = std::chrono::duration_cast
|
auto duration = std::chrono::duration_cast
|
||||||
<chrono::milliseconds>(std::chrono::system_clock::now() - start);
|
<chrono::milliseconds>(std::chrono::system_clock::now() - start);
|
||||||
assert(duration.count() >= 600);
|
expect(duration.count() >= 600);
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
@ -65,7 +72,7 @@ int main() {
|
|||||||
m.lock();
|
m.lock();
|
||||||
thread([&m] {
|
thread([&m] {
|
||||||
auto res = m.try_lock();
|
auto res = m.try_lock();
|
||||||
assert(res == false);
|
expect(res == false);
|
||||||
}).detach();
|
}).detach();
|
||||||
m.unlock();
|
m.unlock();
|
||||||
}
|
}
|
||||||
@ -74,7 +81,7 @@ int main() {
|
|||||||
mutex m;
|
mutex m;
|
||||||
thread([&m] {
|
thread([&m] {
|
||||||
auto res = m.try_lock();
|
auto res = m.try_lock();
|
||||||
assert(res == true);
|
expect(res == true);
|
||||||
m.unlock();
|
m.unlock();
|
||||||
}).detach();
|
}).detach();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,6 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cassert>
|
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
|
|
||||||
#include "riot/mutex.hpp"
|
#include "riot/mutex.hpp"
|
||||||
@ -28,6 +27,8 @@
|
|||||||
#include "riot/thread.hpp"
|
#include "riot/thread.hpp"
|
||||||
#include "riot/condition_variable.hpp"
|
#include "riot/condition_variable.hpp"
|
||||||
|
|
||||||
|
#include "test_utils/expect.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace riot;
|
using namespace riot;
|
||||||
|
|
||||||
@ -35,89 +36,89 @@ using namespace riot;
|
|||||||
int main() {
|
int main() {
|
||||||
puts("\n************ C++ thread test ***********");
|
puts("\n************ C++ thread test ***********");
|
||||||
|
|
||||||
assert(sched_num_threads == 2); // main + idle
|
expect(sched_num_threads == 2); // main + idle
|
||||||
|
|
||||||
puts("Creating one thread and passing an argument ...");
|
puts("Creating one thread and passing an argument ...");
|
||||||
{
|
{
|
||||||
constexpr int i = 3;
|
constexpr int i = 3;
|
||||||
thread t([=](const int j) { assert(j == i); }, i);
|
thread t([=](const int j) { expect(j == i); }, i);
|
||||||
try {
|
try {
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
catch (const std::system_error& e) {
|
catch (const std::system_error& e) {
|
||||||
assert(false);
|
expect(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Creating detached thread ...");
|
puts("Creating detached thread ...");
|
||||||
{
|
{
|
||||||
thread t([] {
|
thread t([] {
|
||||||
// nop
|
// nop
|
||||||
});
|
});
|
||||||
assert(t.joinable() == 1);
|
expect(t.joinable() == 1);
|
||||||
t.detach();
|
t.detach();
|
||||||
assert(t.joinable() == 0);
|
expect(t.joinable() == 0);
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Join on 'finished' thread ...");
|
puts("Join on 'finished' thread ...");
|
||||||
{
|
{
|
||||||
thread t;
|
thread t;
|
||||||
assert(t.joinable() == 0);
|
expect(t.joinable() == 0);
|
||||||
t = thread([] {
|
t = thread([] {
|
||||||
// nop
|
// nop
|
||||||
});
|
});
|
||||||
assert(t.joinable() == 1);
|
expect(t.joinable() == 1);
|
||||||
try {
|
try {
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
catch (const std::system_error& e) {
|
catch (const std::system_error& e) {
|
||||||
assert(false);
|
expect(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Join on 'running' thread ...");
|
puts("Join on 'running' thread ...");
|
||||||
{
|
{
|
||||||
mutex m;
|
mutex m;
|
||||||
thread t1, t2;
|
thread t1, t2;
|
||||||
condition_variable cv;
|
condition_variable cv;
|
||||||
assert(t1.joinable() == 0);
|
expect(t1.joinable() == 0);
|
||||||
assert(t2.joinable() == 0);
|
expect(t2.joinable() == 0);
|
||||||
t1 = thread([&m, &cv] {
|
t1 = thread([&m, &cv] {
|
||||||
unique_lock<mutex> lk(m);
|
unique_lock<mutex> lk(m);
|
||||||
cv.wait(lk);
|
cv.wait(lk);
|
||||||
});
|
});
|
||||||
assert(t1.joinable() == 1);
|
expect(t1.joinable() == 1);
|
||||||
t2 = thread([&t1] {
|
t2 = thread([&t1] {
|
||||||
try {
|
try {
|
||||||
t1.join();
|
t1.join();
|
||||||
}
|
}
|
||||||
catch (const std::system_error& e) {
|
catch (const std::system_error& e) {
|
||||||
assert(false);
|
expect(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
assert(t2.joinable() == 1);
|
expect(t2.joinable() == 1);
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
try {
|
try {
|
||||||
t2.join();
|
t2.join();
|
||||||
}
|
}
|
||||||
catch (const std::system_error& e) {
|
catch (const std::system_error& e) {
|
||||||
assert(false);
|
expect(false);
|
||||||
}
|
}
|
||||||
assert(t1.joinable() == 0);
|
expect(t1.joinable() == 0);
|
||||||
assert(t2.joinable() == 0);
|
expect(t2.joinable() == 0);
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Testing sleep_for ...");
|
puts("Testing sleep_for ...");
|
||||||
{
|
{
|
||||||
@ -126,11 +127,11 @@ int main() {
|
|||||||
this_thread::sleep_for(chrono::seconds(1));
|
this_thread::sleep_for(chrono::seconds(1));
|
||||||
xtimer_now_timex(&after);
|
xtimer_now_timex(&after);
|
||||||
auto diff = timex_sub(after, before);
|
auto diff = timex_sub(after, before);
|
||||||
assert(diff.seconds >= 1);
|
expect(diff.seconds >= 1);
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Testing sleep_until ...");
|
puts("Testing sleep_until ...");
|
||||||
{
|
{
|
||||||
@ -139,11 +140,11 @@ int main() {
|
|||||||
this_thread::sleep_until(riot::now() += chrono::seconds(1));
|
this_thread::sleep_until(riot::now() += chrono::seconds(1));
|
||||||
xtimer_now_timex(&after);
|
xtimer_now_timex(&after);
|
||||||
auto diff = timex_sub(after, before);
|
auto diff = timex_sub(after, before);
|
||||||
assert(diff.seconds >= 1);
|
expect(diff.seconds >= 1);
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Swapping two threads ...");
|
puts("Swapping two threads ...");
|
||||||
{
|
{
|
||||||
@ -156,14 +157,14 @@ int main() {
|
|||||||
auto t1_old = t1.get_id();
|
auto t1_old = t1.get_id();
|
||||||
auto t2_old = t2.get_id();
|
auto t2_old = t2.get_id();
|
||||||
t1.swap(t2);
|
t1.swap(t2);
|
||||||
assert(t1_old == t2.get_id());
|
expect(t1_old == t2.get_id());
|
||||||
assert(t2_old == t1.get_id());
|
expect(t2_old == t1.get_id());
|
||||||
t1.join();
|
t1.join();
|
||||||
t2.join();
|
t2.join();
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Move constructor ...");
|
puts("Move constructor ...");
|
||||||
{
|
{
|
||||||
@ -171,13 +172,13 @@ int main() {
|
|||||||
// nop
|
// nop
|
||||||
});
|
});
|
||||||
thread t2(move(t1));
|
thread t2(move(t1));
|
||||||
assert(t1.joinable() == 0);
|
expect(t1.joinable() == 0);
|
||||||
assert(t2.joinable() == 1);
|
expect(t2.joinable() == 1);
|
||||||
t2.join();
|
t2.join();
|
||||||
}
|
}
|
||||||
puts("Done\n");
|
puts("Done\n");
|
||||||
|
|
||||||
assert(sched_num_threads == 2);
|
expect(sched_num_threads == 2);
|
||||||
|
|
||||||
puts("Bye, bye.");
|
puts("Bye, bye.");
|
||||||
puts("******************************************");
|
puts("******************************************");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user