extern_interface/tests/test_queue.cpp

55 lines
1.0 KiB
C++

#include <thread>
#include <unistd.h>
#include "c_testcase.h"
#include "dataqueue.hpp"
TEST_CASE(test_init) {
DataQueue<int> q;
assert(q.Empty());
assert_eq(q.Size(), 0);
END_TEST;
}
TEST_CASE(test_push) {
DataQueue<int> q;
q.Push(10);
assert_eq(q.Size(), 1);
assert_eq(q.Pop(), 10);
END_TEST;
}
TEST_CASE(test_pop) {
DataQueue<int> q;
std::thread t([&]() {
for (int i = 0; i < 10; i++) {
q.Push(i);
usleep(100);
}
});
for (int i = 0; i < 10; i++) {
assert_eq(q.Pop(), i);
}
t.join();
END_TEST;
}
TEST_CASE(test_clear) {
DataQueue<int> q;
q.Push(10);
q.Clear();
assert(q.Empty());
std::thread t([&]() {
try {
q.Pop();
} catch (const QueueException&) {
return;
}
throw std::runtime_error("QueueException not thrown");
});
std::this_thread::sleep_for(std::chrono::milliseconds(10));
q.Clear();
t.join();
END_TEST;
}