[libc] [compiler-rt] [libunwind] [libcxx] [llvm] [clang] [clang-tools-extra] [lldb] [flang] [libc++] Implement ranges::iota (PR #68494)

James E T Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 5 08:47:06 PST 2024


================
@@ -0,0 +1,123 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Testing std::ranges::iota
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+#include <cassert>
+#include <numeric>
+#include <algorithm>
+#include <array>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+#include "almost_satisfies_types.h"
+
+// Concepts to check different overloads of std::ranges::iota
+template <class Iter = int*, class Sent = int*, class Value = int>
+concept HasIotaIter = requires(Iter&& iter, Sent&& sent, Value&& val) {
+  std::ranges::iota(std::forward<Iter>(iter), std::forward<Sent>(sent), std::forward<Value>(val));
+};
+
+template <class Range, class Value = int>
+concept HasIotaRange =
+    requires(Range&& range, Value&& val) { std::ranges::iota(std::forward<Range>(range), std::forward<Value>(val)); };
+
+constexpr void test_constraints() {
+  // Test constraints of the iterator/sentinel overload
+  // ==================================================
+  static_assert(HasIotaIter<int*, int*, int>);
+
+  // !input_or_output_iterator<O>
+  static_assert(!HasIotaIter<InputIteratorNotInputOrOutputIterator>);
+
+  // !sentinel_for<S, O>
+  static_assert(!HasIotaIter<int*, SentinelForNotSemiregular>);
+  static_assert(!HasIotaIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
+
+  // !weakly_incrementable<T>
+  static_assert(!HasIotaIter<int*, int*, WeaklyIncrementableNotMovable>);
+
+  // !indirectly writable <O, T>
+  static_assert(!HasIotaIter<OutputIteratorNotIndirectlyWritable, int*, int>);
+
+  // Test constraints for the range overload
+  // =======================================
+  static_assert(HasIotaRange<UncheckedRange<int*>, int>);
+
+  // !weakly_incrementable<T>
+  static_assert(!HasIotaRange<UncheckedRange<int*>, WeaklyIncrementableNotMovable>);
+
+  // !ranges::output_range<const _Tp&>
+  static_assert(!HasIotaRange<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);
+}
+
+template <class Iter, class Sent, std::size_t N>
+constexpr void test_result(std::array<int, N> input, int starting_value, std::array<int, N> const expected) {
+  { // (iterator, sentinel) overload
+    auto in_begin = Iter(input.data());
+    auto in_end   = Sent(Iter(input.data() + input.size()));
+    std::same_as<std::ranges::out_value_result<Iter, int>> decltype(auto) result =
+        std::ranges::iota(std::move(in_begin), std::move(in_end), starting_value);
+    assert(result.out == in_end);
+    if constexpr (expected.size() > 0) {
+      assert(result.value == expected.back() + 1);
+    } else {
+      assert(result.value == starting_value);
+    }
+    assert(std::ranges::equal(input, expected));
+  }
+
+  // The range overload adds the additional constraint that it must be an outputrange
+  // so skip this for the input iterators we test
+  if constexpr (!std::is_same_v<Iter, cpp17_input_iterator<int*>> &&
+                !std::is_same_v<Iter, cpp20_input_iterator<int*>>) { // (range) overload
----------------
jamesETsmith wrote:

I was running into an error similar to the one below when I wrote the test (which is why I added the `if constexpr`). Do you know where the `void` is coming from that's causing the failure in the `output_iterator` concept?

```
# | /home/james/apps/llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp:84:7: error: no matching function for call to object of type 'const __ranges_iota::__iota_fn'
# |    84 |       std::ranges::iota(range, starting_value);
# |       |       ^~~~~~~~~~~~~~~~~
# | /home/james/apps/llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp:96:3: note: in instantiation of function template specialization 'test_result<cpp20_input_iterator<int *>, sentinel_wrapper<cpp20_input_iterator<int *>>, 1UL>' requested here
# |    96 |   test_result<Iter, Sent, 1>({1}, 0, {0});
# |       |   ^
# | /home/james/apps/llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp:102:78: note: in instantiation of function template specialization 'test_results<cpp20_input_iterator<int *>, sentinel_wrapper<cpp20_input_iterator<int *>>>' requested here
# |   102 |   types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter> { test_results<Iter>(); });
# |       |                                                                              ^
# | /home/james/apps/llvm-project/libcxx/test/support/type_algorithms.h:52:23: note: in instantiation of function template specialization 'test_results()::(anonymous class)::operator()<cpp20_input_iterator<int *>>' requested here
# |    52 |   swallow((f.template operator()<Types>(), 0)...);
# |       |                       ^
# | /home/james/apps/llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp:102:10: note: in instantiation of function template specialization 'types::for_each<int *, contiguous_iterator<int *>, random_access_iterator<int *>, bidirectional_iterator<int *>, forward_iterator<int *>, cpp20_input_iterator<int *>, cpp17_input_iterator<int *>, (lambda at /home/james/apps/llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp:102:61)>' requested here
# |   102 |   types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter> { test_results<Iter>(); });
# |       |          ^
# | /home/james/apps/llvm-project/build_llvmorg-18-init-15302-g028ce92e61f1/include/c++/v1/__numeric/ranges_iota.h:56:3: note: candidate template ignored: constraints not satisfied [with _Tp = int, _Range = std::ranges::subrange<cpp20_input_iterator<int *>, sentinel_wrapper<cpp20_input_iterator<int *>>> &]
# |    56 |   operator()(_Range&& __r, _Tp __value) {
# |       |   ^
# | /home/james/apps/llvm-project/build_llvmorg-18-init-15302-g028ce92e61f1/include/c++/v1/__numeric/ranges_iota.h:54:39: note: because 'ranges::output_range<std::ranges::subrange<cpp20_input_iterator<int *>, sentinel_wrapper<cpp20_input_iterator<int *> > > &, const int &>' evaluated to false
# |    54 |   template <weakly_incrementable _Tp, ranges::output_range<const _Tp&> _Range>
# |       |                                       ^
# | /home/james/apps/llvm-project/build_llvmorg-18-init-15302-g028ce92e61f1/include/c++/v1/__ranges/concepts.h:103:38: note: because 'output_iterator<iterator_t<subrange<cpp20_input_iterator<int *>, sentinel_wrapper<cpp20_input_iterator<int *> >, std::ranges::subrange_kind::unsized> &>, const int &>' evaluated to false
# |   103 | concept output_range = range<_Rp> && output_iterator<iterator_t<_Rp>, _Tp>;
# |       |                                      ^
# | /home/james/apps/llvm-project/build_llvmorg-18-init-15302-g028ce92e61f1/include/c++/v1/__iterator/concepts.h:135:7: note: because '*__it++ = std::forward<_Tp>(__t)' would be invalid: indirection requires pointer operand ('void' invalid)
# |   135 |       *__it++ = std::forward<_Tp>(__t); // not required to be equality-preserving
# |       |       ^
```

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


More information about the cfe-commits mailing list