[libcxx-commits] [libcxx] [libc++] Implement P2442R1 `std::views::chunk` (PR #171234)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 21 08:54:45 PDT 2026


================
@@ -0,0 +1,108 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++23
+
+// <ranges>
+
+//   V models only input_range
+//     friend constexpr bool operator==(const outer_iterator& x, default_sentinel_t);
+//     friend constexpr bool operator==(const inner_iterator& x, default_sentinel_t);
+
+//   V models forward_range
+//     friend constexpr bool operator==(const iterator& x, const iterator& y);
+//     friend constexpr bool operator==(const iterator& x, default_sentinel_t);
+//     friend constexpr bool operator<(const iterator& x, const iterator& y)
+//       requires random_access_range<Base>;
+//     friend constexpr bool operator>(const iterator& x, const iterator& y)
+//       requires random_access_range<Base>;
+//     friend constexpr bool operator<=(const iterator& x, const iterator& y)
+//       requires random_access_range<Base>;
+//     friend constexpr bool operator>=(const iterator& x, const iterator& y)
+//       requires random_access_range<Base>;
+//     friend constexpr auto operator<=>(const iterator& x, const iterator& y)
+//       requires random_access_range<Base> &&
+//                three_way_comparable<iterator_t<Base>>;
+
+#include <cassert>
+#include <compare>
+#include <iterator>
+#include <ranges>
+#include <vector>
+
+#include "test_range.h"
+#include "../types.h"
+
+constexpr bool test() {
+  std::vector<int> vector                                                  = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+  std::ranges::chunk_view<std::ranges::ref_view<std::vector<int>>> chunked = vector | std::views::chunk(3);
+  std::ranges::chunk_view<input_span<int>> input_chunked = input_span<int>(vector) | std::views::chunk(3);
+
+  // Test `friend constexpr bool opreator==(const outer_iterator& x, default_sentinel_t)`
----------------
huixie90 wrote:

I have not look at the tests in details. But I think the coverage is bit insufficient. Could you please add tests for the cases

- All the constraints need to be tested (with static_assert and concepts)
- negative cases. (for example, for this test `==` we also need to test when it does not equal to default sentinel)
- empty ranges
- a range with only 1 element
- a range with more than 1 element
- fully divisible ranges e.g. chunk of 3 with 12 elements
- not fully divisible ranges chunk of 3 with 11 elements
- chunk of 1
- chunk of N > 1
- chunk of N > size of the range
- different types of underlying iterators , we have lots of iterator types in test_iterators.h and test_ranges.h, and usually we template the tests with different types of the iterators/sentinel types

These comments applies to all of the tests


https://github.com/llvm/llvm-project/pull/171234


More information about the libcxx-commits mailing list