aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/26_numerics/midpoint/pointer.cc
blob: 4cc6c1c2878b907f648dedea11825a29f1a09de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (C) 2019-2025 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-do run { target c++20 } }

#include <numeric>
#include <testsuite_hooks.h>

const int* p = nullptr;
static_assert(std::is_same_v<decltype(std::midpoint(p, p)), decltype(p)>);
// This is a GNU extension:
static_assert(noexcept(std::midpoint(p, p)));

struct test_type { };
template<typename T> decltype(std::midpoint((T*)0, (T*)0)) try_midpoint(int);
template<typename T> test_type try_midpoint(...);
template<typename T> constexpr bool no_midpoint()
{ return std::is_same_v<decltype(try_midpoint<T>()), test_type>; }

static_assert(no_midpoint<void>());
static_assert(no_midpoint<int()>());
static_assert(no_midpoint<int&>());

constexpr int ca[3] = {};
static_assert( std::midpoint(ca, ca+3) == ca+1 );

void
test01()
{
  int a[4];
  VERIFY( std::midpoint(a, a) == a );
  VERIFY( std::midpoint(a, a+1) == a );
  VERIFY( std::midpoint(a, a+2) == a+1 );
  VERIFY( std::midpoint(a, a+3) == a+1 );
  VERIFY( std::midpoint(a, a+4) == a+2 );
  VERIFY( std::midpoint(a+1, a) == a+1 );
  VERIFY( std::midpoint(a+2, a) == a+1 );
  VERIFY( std::midpoint(a+3, a) == a+2 );
  VERIFY( std::midpoint(a+4, a) == a+2 );
}

int main()
{
  test01();
}