Add SingleWriterConcurrentVector

This commit is contained in:
Eric Harding committed 2019-05-20 15:32:48 -04:00
1 parent 523002ac56
commit 5ae8a66558
5 files changed
+322 -3

No files matched your search

@@ -0,0 +1,69 @@
#include "../SingleWriterConcurrentVector.h"
#include "catch/catch.hpp"
#include <thread>
namespace bts {
struct Chunk {
size_t a,b,c,d;
};
TEST_CASE("SingleWriterConcurrentVector", "[collection]") {
SECTION("SEQUENTIAL") {
SingleWriterConcurrentVector<size_t,5> intvec;
SingleWriterConcurrentVector<Chunk,5> chunkvec;
// Insert 9999 elements
for (size_t x=0; x< 9999; ++x) {
intvec.push_back(x);
chunkvec.push_back({ x,x,x,x });
}
size_t expected = 0;
for(const auto& i : intvec) {
REQUIRE(i == expected);
++expected;
}
REQUIRE(expected == 9999);
expected = 0;
for(const auto& i : chunkvec) {
REQUIRE(i.a == expected);
REQUIRE(i.b == expected);
REQUIRE(i.c == expected);
REQUIRE(i.d == expected);
++expected;
}
REQUIRE(expected == 9999);
for (size_t x=0; x< 9999; ++x) {
REQUIRE(intvec[x] == x);
REQUIRE(chunkvec[x].a == x);
REQUIRE(chunkvec[x].b == x);
REQUIRE(chunkvec[x].c == x);
REQUIRE(chunkvec[x].d == x);
}
auto iter = intvec.begin();
REQUIRE(*iter == 0);
++iter;
REQUIRE(*iter == 1);
iter++;
REQUIRE(*iter == 2);
--iter;
REQUIRE(*iter == 1);
iter--;
REQUIRE(*iter == 0);
REQUIRE(intvec.size() == 9999);
REQUIRE(chunkvec.size() == 9999);
}
SECTION("ONCE MORE, WITH THREADS") {
// SingleWriterConcurrentVector<size_t> intvec;
// SingleWriterConcurrentVector<Chunk> chunkvec;
// std::thread writer([&]() {
// });
// auto reader = [&]() {
// };
// std::vector<std::thread> readers;
// writer.join();
}
}
}//ns