stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
ring.cpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#include "pch.h"
7
8using namespace std;
9#ifdef _WIN32
10using namespace Microsoft::VisualStudio::CppUnitTestFramework;
11#endif
12
13namespace UnitTests
14{
15 constexpr size_t capacity = 50;
16
17 TEST_CLASS(ring)
18 {
19 public:
20 TEST_METHOD(test)
21 {
22 using ring_t = stdex::ring<int, capacity>;
23 ring_t ring;
24 thread writer([](_Inout_ ring_t& ring)
25 {
26 int seed = 0;
27 for (size_t retries = 1000; retries--;) {
28 for (auto to_write = static_cast<size_t>(static_cast<uint64_t>(::rand()) * capacity / 5 / RAND_MAX); to_write;) {
29 int* ptr; size_t num_write;
30 tie(ptr, num_write) = ring.back();
31 if (to_write < num_write)
32 num_write = to_write;
33 for (size_t i = 0; i < num_write; i++)
34 ptr[i] = seed++;
35 ring.push(num_write);
36 to_write -= num_write;
37 }
38 }
39 ring.quit();
40 }, ref(ring));
41
42 int seed = 0;
43 for (;;) {
44 int* ptr; size_t num_read;
45 tie(ptr, num_read) = ring.front();
46 if (!ptr) _Unlikely_
47 break;
48 if (num_read > 7)
49 num_read = 7;
50 for (size_t i = 0; i < num_read; ++i)
51 Assert::AreEqual(seed++, ptr[i]);
52 ring.pop(num_read);
53 }
54 writer.join();
55 }
56 };
57}
Ring buffer.
Definition ring.hpp:24
std::tuple< T *, size_t > front()
Peeks the data at the ring head. Use pop() after the data was consumed.
Definition ring.hpp:73