summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/25_algorithms/lower_bound/partitioned.cc
blob: 6fbde5888fbdb6ba584fe9d277b2227bcb7baab3 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2016-2019 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-options "-D_GLIBCXX_DEBUG" }
// { dg-do run { target c++11 } }
// { dg-skip-if "" { *-*-* } { "-D_GLIBCXX_PROFILE" } }

#include <algorithm>
#include <functional>
#include <testsuite_iterators.h>
#include <testsuite_hooks.h>

using __gnu_test::test_container;
using __gnu_test::forward_iterator_wrapper;

struct X
{
  int val;

  bool odd() const { return val % 2; }

  // Partitioned so that all odd values come before even values:
  bool operator<(const X& x) const { return this->odd() && !x.odd(); }
};

void
test01()
{
  // Test with range that is partitioned, but not sorted.
  X seq[] = { 1, 3, 5, 7, 1, 6, 4, 2 };
  test_container<X, forward_iterator_wrapper> c(seq);

  auto part1 = std::lower_bound(c.begin(), c.end(), X{2});
  VERIFY( part1 != c.end() );
  VERIFY( part1->val == 6 );
  auto part2 = std::lower_bound(c.begin(), c.end(), X{2}, std::less<X>{});
  VERIFY( part2 != c.end() );
  VERIFY( part2->val == 6 );

  auto part3 = std::lower_bound(c.begin(), c.end(), X{9});
  VERIFY( part3 != c.end() );
  VERIFY( part3->val == 1 );
  auto part4 = std::lower_bound(c.begin(), c.end(), X{9}, std::less<X>{});
  VERIFY( part4 != c.end() );
  VERIFY( part4->val == 1 );
}

struct Y
{
  double val;

  // Not irreflexive, so not a strict weak order.
  bool operator<(const Y& y) const { return val < int(y.val); }
};

void
test02()
{
  // Test that Debug Mode checks don't fire (libstdc++/71545)

  Y seq[] = { -0.1, 1.2, 5.0, 5.2, 5.1, 5.9, 5.5, 6.0 };
  test_container<Y, forward_iterator_wrapper> c(seq);

  auto part1 = std::lower_bound(c.begin(), c.end(), Y{5.5});
  VERIFY( part1 != c.end() );
  VERIFY( part1->val == 5.0 );
  auto part2 = std::lower_bound(c.begin(), c.end(), Y{5.5}, std::less<Y>{});
  VERIFY( part2 != c.end() );
  VERIFY( part2->val == 5.0 );

  auto part3 = std::lower_bound(c.begin(), c.end(), Y{1.0});
  VERIFY( part3 != c.end() );
  VERIFY( part3->val == 1.2 );
  auto part4 = std::lower_bound(c.begin(), c.end(), Y{1.0}, std::less<Y>{});
  VERIFY( part4 != c.end() );
  VERIFY( part4->val == 1.2 );
}

int
main()
{
  test01();
  test02();
}