]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/functional/not_fn.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / functional / not_fn.cc
1 // Copyright (C) 2014-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++14 } }
19
20 #include <experimental/functional>
21 #include <testsuite_hooks.h>
22
23 using std::experimental::not_fn;
24
25 int func(int, char) { return 0; }
26
27 struct F
28 {
29 bool operator()() { return false; }
30 bool operator()() const { return true; }
31 bool operator()(int) { return false; }
32 };
33
34 void
35 test01()
36 {
37 auto f1 = not_fn(func);
38 VERIFY( f1(1, '2') == true );
39
40 auto f2 = not_fn( [] { return true; } );
41 VERIFY( f2() == false );
42
43 auto f3 = not_fn( F{} );
44 VERIFY( f3() == true );
45 VERIFY( f3(1) == true );
46 const auto f4 = f3;
47 VERIFY( f4() == false );
48 }
49
50 template<typename F, typename Arg>
51 auto foo(F f, Arg arg) -> decltype(not_fn(f)(arg)) { return not_fn(f)(arg); }
52
53 template<typename F, typename Arg>
54 auto foo(F f, Arg arg) -> decltype(not_fn(f)()) { return not_fn(f)(); }
55
56 struct negator
57 {
58 bool operator()(int) const { return false; }
59 void operator()() const {}
60 };
61
62 void
63 test02()
64 {
65 foo(negator{}, 1); // PR libstdc++/66998
66 }
67
68 void
69 test03()
70 {
71 struct X { bool b; };
72 X x{ false };
73 VERIFY( not_fn(&X::b)(x) );
74 }
75
76 void
77 test04()
78 {
79 struct abstract { virtual void f() = 0; };
80 struct derived : abstract { void f() { } };
81 struct F { bool operator()(abstract&) { return false; } };
82 F f;
83 derived d;
84 VERIFY( not_fn(f)(d) );
85 }
86
87 void
88 test05()
89 {
90 auto nf = std::experimental::not_fn([] { return false; });
91 auto copy(nf); // PR libstdc++/70564
92 }
93
94 int
95 main()
96 {
97 test01();
98 test02();
99 test03();
100 test04();
101 test05();
102 }