Skip to content

Commit

Permalink
Added ranges compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
torrentg committed Jan 15, 2023
1 parent fc075c9 commit d04b6de
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
"variant": "cpp",
"ranges": "cpp",
"span": "cpp"
}
}
58 changes: 58 additions & 0 deletions cqueue-tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define CATCH_CONFIG_MAIN

#include <limits>
#include <ranges>
#include "catch.hpp"
#include "cqueue.hpp"

Expand Down Expand Up @@ -1128,4 +1129,61 @@ TEST_CASE("cqueue") {
CHECK(queue[4] == 1);
}

SECTION("ranges") {
gto::cqueue<int> numbers;
for (int i = 1; i <= 6; i++) {
numbers.push_back(i);
}

gto::cqueue<int> results;
for (auto num : numbers | std::views::filter([](int n){ return n % 2 == 0; })
| std::views::transform([](int n){ return n * 2; })
| std::views::reverse) {
results.push_back(num);
}

CHECK(results.size() == 3);
CHECK(results[0] == 12);
CHECK(results[1] == 8);
CHECK(results[2] == 4);
}

SECTION("subranges") {
gto::cqueue<int> numbers;
for (int i = 1; i <= 10; i++) {
numbers.push_back(i);
}

std::ranges::subrange<gto::cqueue<int>::iterator> range0 = std::ranges::subrange(numbers.begin(), numbers.end());
int count = 0;
int sum = 0;
for (auto x : range0) {
count++;
sum += x;
}
CHECK(count == 10);
CHECK(sum == 55);

auto range1 = std::ranges::subrange(numbers.begin(), numbers.begin() + 5);
count = 0;
sum = 0;
for (auto x : range1) {
count++;
sum += x;
}
CHECK(count == 5);
CHECK(sum == 15);

auto range2 = std::ranges::subrange(numbers.begin() + 5, numbers.end());
count = 0;
sum = 0;
for (auto x : range2) {
count++;
sum += x;
}

CHECK(count == 5);
CHECK(sum == 40);
}

}
10 changes: 4 additions & 6 deletions cqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace gto {
* @see https://en.wikipedia.org/wiki/Circular_buffer
* @see https://github.com/torrentg/cqueue
* @note This class is not thread-safe.
* @version 1.0.2
* @version 1.0.3
*/
template<std::copyable T, typename Allocator = std::allocator<T>>
class cqueue {
Expand Down Expand Up @@ -49,7 +49,7 @@ class cqueue {
return (n < 0 ? queue->size() : static_cast<size_type>(n));
}
public:
explicit iter(queue_type *other, difference_type position = 0) :
explicit iter(queue_type *other = nullptr, difference_type position = 0) :
queue{other}, pos{position} {}
iter(const iter<std::remove_const_t<value_type>> &other) requires std::is_const_v<value_type> :
queue{other.queue}, pos{other.pos} {}
Expand Down Expand Up @@ -146,18 +146,16 @@ class cqueue {

public: // methods

//! Constructor.
constexpr explicit cqueue(const_alloc_reference alloc = Allocator()) : cqueue(0, alloc) {}
//! Constructor (capacity=0 means unlimited).
constexpr explicit cqueue(size_type capacity, const_alloc_reference alloc = Allocator());
constexpr explicit cqueue(size_type capacity = 0, const_alloc_reference alloc = Allocator());
//! Copy constructor.
constexpr cqueue(const cqueue &other) :
cqueue{other, allocator_traits::select_on_container_copy_construction(other.get_allocator())} {}
//! Copy constructor with allocator.
constexpr cqueue(const cqueue &other, const_alloc_reference alloc);
//! Move constructor.
constexpr cqueue(cqueue &&other) noexcept { this->swap(other); }
//! Move constructor.
//! Move constructor with allocator.
constexpr cqueue(cqueue &&other, const_alloc_reference alloc);
//! Destructor.
~cqueue() noexcept { reset(); };
Expand Down

0 comments on commit d04b6de

Please sign in to comment.