[libcxx-commits] [libcxx] [libc++] Implement ranges::iota (PR #68494)

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 2 09:52:59 PDT 2024


================
@@ -0,0 +1,215 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <algorithm>
+#include <array>
+#include <cassert>
+#include <numeric>
+#include <utility>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+//
+// Testing constraints
+//
+
+// 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)); };
+
+// 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>);
----------------
cjdb wrote:

We should fix those ones too! Here's roughly how I like to write verify files these days:
```cpp
// Each test case becomes its own function, but you can combine iterator/sentinel-based test
// cases with range-based test cases if they're testing the same thing.
void not_input_or_output_iterator(InputIteratorNotInputOrOutputIterator first, int* last, int value)
{
  std::ranges::iota(first, last, value);
  // expected-error at -1{{copy the **error** clang diagnoses here}}
  // expected-note at -2{{copy the **first note** clang diagnoses here}}
  // expected-note at -3{{and so on...}}

  auto r = std::ranges::subrange(first, last);
  std::ranges::iota(r, value);
  // expected-error at -1{{copy the **error** clang diagnoses here}}
  // expected-note at -2{{copy the **first note** clang diagnoses here}}
  // expected-note at -3{{and so on...}}

  std::ranges::iota(std::as_const(r), value);
  // expected-error at -1{{copy the **error** clang diagnoses here}}
  // expected-note at -2{{copy the **first note** clang diagnoses here}}
  // expected-note at -3{{and so on...}}

  std::ranges::iota(std::move(r), value);
  // expected-error at -1{{copy the **error** clang diagnoses here}}
  // expected-note at -2{{copy the **first note** clang diagnoses here}}
  // expected-note at -3{{and so on...}}

  std::ranges::iota(static_cast<decltype(r) const&&>(r), value);
  // expected-error at -1{{copy the **error** clang diagnoses here}}
  // expected-note at -2{{copy the **first note** clang diagnoses here}}
  // expected-note at -3{{and so on...}}
}
```
It's highly likely that we won't want all of the notes, but it might be a good exercise for us to go through them all together (just for one test case: I trust you'll be able to do the others independently).

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


More information about the libcxx-commits mailing list