Commit 7e9e1c1e authored by Xavier Thompson's avatar Xavier Thompson

Add unit test for deque

parent 5b626121
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <thread>
#include <typon/fundamental/deque.hpp>
using namespace typon;
using u64 = std::uint_fast64_t;
using uint = unsigned int;
static uint thieves = 1;
static u64 N = u64(1) << 20;
static fdt::lock_free::deque<int> deque {thieves + 1};
static std::atomic<bool> full = false;
static std::vector<std::vector<int>> results(thieves + 1);
int main()
{
std::vector<std::thread> v;
for (uint i = 0; i < thieves; i++)
{
v.push_back(std::thread([i]() {
while (true)
{
auto p = deque.steal();
if (p)
{
results[i].push_back(*p);
}
else
{
if (full.load())
{
break;
}
}
}
}));
}
for (u64 j = 0; j < N; j++)
{
deque.push(j);
}
full.store(true);
while (auto p = deque.pop())
{
results[thieves].push_back(*p);
}
for (auto & t : v)
{
t.join();
}
std::vector<int> tally;
for (auto & result : results)
{
for (auto j : result)
{
tally.push_back(j);
}
}
if (tally.size() != N)
{
puts("error: lost reads");
return 1;
}
std::sort(tally.begin(), tally.end());
for (uint i = 0; i < tally.size() - 1; i++)
{
if (tally[i] != tally[i + 1] - 1)
{
puts("error: corrupted reads");
return 1;
}
}
}
CXX = clang++-14
CXXFLAGS = -g -O3 -flto -Werror -Wall -Wextra -std=c++20 -stdlib=libc++ -I../../include
LDLIBS = -pthread
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment