Skip to content

Commit 3bcb319

Browse files
Using std::ranges::iota_view for loops
1 parent 33cfe14 commit 3bcb319

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

test/wait_queue_test.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <set>
2020
#include <optional>
2121
#include <chrono>
22+
#include <ranges> // std::views::iota
2223
#include <type_traits> // std::is_arithmetic
2324

2425
#include <thread>
@@ -45,12 +46,12 @@ void non_threaded_push_test(Q& wq, const typename Q::value_type& val, int count)
4546
REQUIRE (wq.empty());
4647
REQUIRE (wq.size() == 0);
4748

48-
for (int i {0}; i < count; ++i) {
49+
for (int i : std::ranges::iota_view{0, count}) {
4950
REQUIRE(wq.push(val));
5051
}
5152
REQUIRE_FALSE (wq.empty());
5253
REQUIRE (wq.size() == count);
53-
for (int i {0}; i < count; ++i) {
54+
for (int i : std::ranges::iota_view{0, count}) {
5455
auto ret = wq.try_pop();
5556
REQUIRE(*ret == val);
5657
}
@@ -69,22 +70,22 @@ void non_threaded_arithmetic_test(Q& wq, int count) {
6970

7071
REQUIRE (wq.empty());
7172

72-
for (int i {0}; i < count; ++i) {
73+
for (int i : std::ranges::iota_view{0, count}) {
7374
REQUIRE(wq.push(base_val));
7475
}
7576
val_type sum { 0 };
7677
wq.apply( [&sum] (const val_type& x) { sum += x; } );
7778
REQUIRE (sum == expected_sum);
7879

79-
for (int i {0}; i < count; ++i) {
80+
for (int i : std::ranges::iota_view{0, count}) {
8081
REQUIRE(*(wq.try_pop()) == base_val);
8182
}
8283
REQUIRE (wq.empty());
8384

84-
for (int i {0}; i < count; ++i) {
85+
for (int i : std::ranges::iota_view{0, count}) {
8586
wq.push(base_val+i);
8687
}
87-
for (int i {0}; i < count; ++i) {
88+
for (int i : std::ranges::iota_view{0, count}) {
8889
REQUIRE(*(wq.try_pop()) == (base_val+i));
8990
}
9091
REQUIRE (wq.size() == 0);
@@ -389,25 +390,25 @@ TEST_CASE ( "Fixed size ring_span, testing wrap around with int type",
389390
constexpr int Answer = 42;
390391
constexpr int AnswerPlus = 42+5;
391392

392-
for (int i {0}; i < N; ++i) {
393+
for (int i : std::ranges::iota_view{0, N}) {
393394
wq.push(Answer);
394395
}
395396
REQUIRE (wq.size() == N);
396397
wq.apply([Answer] (const int& i) { REQUIRE(i == Answer); } );
397398

398-
for (int i {0}; i < N; ++i) {
399+
for (int i : std::ranges::iota_view{0, N}) {
399400
wq.push(Answer);
400401
}
401-
for (int i {0}; i < (N/2); ++i) {
402+
for (int i : std::ranges::iota_view{0, N/2}) {
402403
wq.push(AnswerPlus);
403404
}
404405
// the size is full but half match answer and half answer plus, since there's been wrap
405406
REQUIRE (wq.size() == N);
406407
// wait_pop should immediately return if the queue is non empty
407-
for (int i {0}; i < (N/2); ++i) {
408+
for (int i : std::ranges::iota_view{0, N/2}) {
408409
REQUIRE (wq.wait_and_pop() == Answer);
409410
}
410-
for (int i {0}; i < (N/2); ++i) {
411+
for (int i : std::ranges::iota_view{0, N/2}) {
411412
REQUIRE (wq.wait_and_pop() == AnswerPlus);
412413
}
413414
REQUIRE (wq.empty());

0 commit comments

Comments
 (0)