//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17 // UNSUPPORTED: libcpp-has-no-incomplete-ranges // friend constexpr iterator operator-(iterator i, difference_type n) // requires advanceable; // friend constexpr difference_type operator-(const iterator& x, const iterator& y) // requires advanceable; #include #include #include #include "test_macros.h" #include "../types.h" // If we're compiling for 32 bit or windows, int and long are the same size, so long long is the correct difference type. #if INTPTR_MAX == INT32_MAX || defined(_WIN32) using IntDiffT = long long; #else using IntDiffT = long; #endif constexpr bool test() { // - difference_type { // When "_Start" is signed integer like. { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 == iter2); assert(iter1 - 5 != iter2); assert(iter1 - 5 == std::ranges::prev(iter2, 5)); static_assert(!std::is_reference_v); } // When "_Start" is not integer like. { std::ranges::iota_view io(SomeInt(0)); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 == iter2); assert(iter1 - 5 != iter2); assert(iter1 - 5 == std::ranges::prev(iter2, 5)); static_assert(!std::is_reference_v); } // When "_Start" is unsigned integer like and n is greater than or equal to zero. { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 == iter2); assert(iter1 - 5 != iter2); assert(iter1 - 5 == std::ranges::prev(iter2, 5)); static_assert(!std::is_reference_v); } { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 - 0 == iter2); } // When "_Start" is unsigned integer like and n is less than zero. { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 - 5 != iter2); assert(iter1 - 5 == std::ranges::prev(iter2, 5)); static_assert(!std::is_reference_v); } } // - { // When "_Start" is signed integer like. { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 5); assert(iter1 - iter2 == 5); LIBCPP_STATIC_ASSERT(std::same_as); } { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 - iter2 == 0); LIBCPP_STATIC_ASSERT(std::same_as); } { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 5); auto iter2 = std::next(io.begin(), 10); assert(iter1 - iter2 == -5); LIBCPP_STATIC_ASSERT(std::same_as); } // When "_Start" is unsigned integer like and y > x. { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 5); auto iter2 = std::next(io.begin(), 10); assert(iter1 - iter2 == -5); LIBCPP_STATIC_ASSERT(std::same_as); } // When "_Start" is unsigned integer like and x >= y. { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 5); assert(iter1 - iter2 == 5); LIBCPP_STATIC_ASSERT(std::same_as); } { std::ranges::iota_view io(0); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 - iter2 == 0); LIBCPP_STATIC_ASSERT(std::same_as); } // When "_Start" is not integer like. { std::ranges::iota_view io(SomeInt(0)); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 5); assert(iter1 - iter2 == 5); static_assert(std::same_as); } { std::ranges::iota_view io(SomeInt(0)); auto iter1 = std::next(io.begin(), 10); auto iter2 = std::next(io.begin(), 10); assert(iter1 - iter2 == 0); static_assert(std::same_as); } { std::ranges::iota_view io(SomeInt(0)); auto iter1 = std::next(io.begin(), 5); auto iter2 = std::next(io.begin(), 10); assert(iter1 - iter2 == -5); static_assert(std::same_as); } } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }