File: not_fn.nttp.pass.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (310 lines) | stat: -rw-r--r-- 9,763 bytes parent folder | download | duplicates (6)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//===----------------------------------------------------------------------===//
//
// 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, c++20, c++23

// <functional>

// template<auto f> constexpr unspecified not_fn() noexcept;

#include <functional>

#include <bit>
#include <cassert>
#include <concepts>
#include <type_traits>
#include <utility>

#include "test_macros.h"

class BooleanTestable {
  bool val_;

public:
  constexpr explicit BooleanTestable(bool val) : val_(val) {}
  constexpr operator bool() const { return val_; }
  constexpr BooleanTestable operator!() const { return BooleanTestable{!val_}; }
};

LIBCPP_STATIC_ASSERT(std::__boolean_testable<BooleanTestable>);

class FakeBool {
  int val_;

public:
  constexpr FakeBool(int val) : val_(val) {}
  constexpr FakeBool operator!() const { return FakeBool{-val_}; }
  constexpr bool operator==(int other) const { return val_ == other; }
};

template <bool IsNoexcept>
struct MaybeNoexceptFn {
  bool operator()() const noexcept(IsNoexcept); // not defined
};

template <bool IsNoexcept>
struct MaybeNoexceptNegation {
  bool operator!() noexcept(IsNoexcept); // not defined
};

template <bool IsNoexcept>
MaybeNoexceptNegation<IsNoexcept> maybe_noexcept_negation() noexcept {
  return {};
}

constexpr void basic_tests() {
  { // Test constant functions
    auto false_fn = std::not_fn<std::false_type{}>();
    assert(false_fn());

    auto true_fn = std::not_fn<std::true_type{}>();
    assert(!true_fn());

    static_assert(noexcept(std::not_fn<std::false_type{}>()));
    static_assert(noexcept(std::not_fn<std::true_type{}>()));
  }

  { // Test function with one argument
    auto is_odd = std::not_fn<[](auto x) { return x % 2 == 0; }>();
    assert(is_odd(1));
    assert(!is_odd(2));
    assert(is_odd(3));
    assert(!is_odd(4));
    assert(is_odd(5));
  }

  { // Test function with multiple arguments
    auto at_least_10 = [](auto... vals) { return (vals + ... + 0) >= 10; };
    auto at_most_9   = std::not_fn<at_least_10>();
    assert(at_most_9());
    assert(at_most_9(1));
    assert(at_most_9(1, 2, 3, 4, -1));
    assert(at_most_9(3, 3, 2, 1, -2));
    assert(!at_most_9(10, -1, 2));
    assert(!at_most_9(5, 5));
    static_assert(noexcept(std::not_fn<at_least_10>()));
  }

  { // Test function that returns boolean-testable type other than bool
    auto is_product_even = [](auto... vals) { return BooleanTestable{(vals * ... * 1) % 2 == 0}; };
    auto is_product_odd  = std::not_fn<is_product_even>();
    assert(is_product_odd());
    assert(is_product_odd(1, 3, 5, 9));
    assert(is_product_odd(3, 3, 3, 3));
    assert(!is_product_odd(3, 5, 9, 11, 0));
    assert(!is_product_odd(11, 7, 5, 3, 2));
    static_assert(noexcept(std::not_fn<is_product_even>()));
  }

  { // Test function that returns non-boolean-testable type
    auto sum         = [](auto... vals) -> FakeBool { return (vals + ... + 0); };
    auto negated_sum = std::not_fn<sum>();
    assert(negated_sum() == 0);
    assert(negated_sum(3) == -3);
    assert(negated_sum(4, 5, 1, 3) == -13);
    assert(negated_sum(4, 2, 5, 6, 1) == -18);
    assert(negated_sum(-1, 3, 2, -8) == 4);
    static_assert(noexcept(std::not_fn<sum>()));
  }

  { // Test member pointers
    struct MemberPointerTester {
      bool value = true;
      constexpr bool not_value() const { return !value; }
      constexpr bool value_and(bool other) noexcept { return value && other; }
    };

    MemberPointerTester tester;

    auto not_mem_object = std::not_fn<&MemberPointerTester::value>();
    assert(!not_mem_object(tester));
    assert(!not_mem_object(std::as_const(tester)));
    static_assert(noexcept(not_mem_object(tester)));
    static_assert(noexcept(not_mem_object(std::as_const(tester))));

    auto not_nullary_mem_fn = std::not_fn<&MemberPointerTester::not_value>();
    assert(not_nullary_mem_fn(tester));
    static_assert(!noexcept(not_nullary_mem_fn(tester)));

    auto not_unary_mem_fn = std::not_fn<&MemberPointerTester::value_and>();
    assert(not_unary_mem_fn(tester, false));
    static_assert(noexcept(not_unary_mem_fn(tester, false)));
    static_assert(!std::is_invocable_v<decltype(not_unary_mem_fn), const MemberPointerTester&, bool>);
  }
}

constexpr void test_perfect_forwarding_call_wrapper() {
  { // Make sure we call the correctly cv-ref qualified operator()
    // based on the value category of the not_fn<NTTP> unspecified-type.
    struct X {
      constexpr FakeBool operator()() & { return 1; }
      constexpr FakeBool operator()() const& { return 2; }
      constexpr FakeBool operator()() && { return 3; }
      constexpr FakeBool operator()() const&& { return 4; }
    };

    auto f  = std::not_fn<X{}>();
    using F = decltype(f);
    assert(static_cast<F&>(f)() == -2);
    assert(static_cast<const F&>(f)() == -2);
    assert(static_cast<F&&>(f)() == -2);
    assert(static_cast<const F&&>(f)() == -2);
  }

  // Call to `not_fn<NTTP>` unspecified-type's operator() should always result in call to the const& overload of the underlying function object.
  {
    { // Make sure unspecified-type is still callable when we delete the & overload.
      struct X {
        FakeBool operator()() & = delete;
        FakeBool operator()() const&;
        FakeBool operator()() &&;
        FakeBool operator()() const&&;
      };

      using F = decltype(std::not_fn<X{}>());
      static_assert(std::invocable<F&>);
      static_assert(std::invocable<const F&>);
      static_assert(std::invocable<F>);
      static_assert(std::invocable<const F>);
    }

    { // Make sure unspecified-type is not callable when we delete the const& overload.
      struct X {
        FakeBool operator()() &;
        FakeBool operator()() const& = delete;
        FakeBool operator()() &&;
        FakeBool operator()() const&&;
      };

      using F = decltype(std::not_fn<X{}>());
      static_assert(!std::invocable<F&>);
      static_assert(!std::invocable<const F&>);
      static_assert(!std::invocable<F>);
      static_assert(!std::invocable<const F>);
    }

    { // Make sure unspecified-type is still callable when we delete the && overload.
      struct X {
        FakeBool operator()() &;
        FakeBool operator()() const&;
        FakeBool operator()() && = delete;
        FakeBool operator()() const&&;
      };

      using F = decltype(std::not_fn<X{}>());
      static_assert(std::invocable<F&>);
      static_assert(std::invocable<const F&>);
      static_assert(std::invocable<F>);
      static_assert(std::invocable<const F>);
    }

    { // Make sure unspecified-type is still callable when we delete the const&& overload.
      struct X {
        FakeBool operator()() &;
        FakeBool operator()() const&;
        FakeBool operator()() &&;
        FakeBool operator()() const&& = delete;
      };

      using F = decltype(std::not_fn<X{}>());
      static_assert(std::invocable<F&>);
      static_assert(std::invocable<const F&>);
      static_assert(std::invocable<F>);
      static_assert(std::invocable<const F>);
    }
  }

  { // Test perfect forwarding
    auto f = [](int& val) {
      val = 5;
      return false;
    };

    auto not_f = std::not_fn<f>();
    int val    = 0;
    assert(not_f(val));
    assert(val == 5);

    using NotF = decltype(not_f);
    static_assert(std::invocable<NotF, int&>);
    static_assert(!std::invocable<NotF, int>);
  }
}

constexpr void test_return_type() {
  { // Test constructors and assignment operators
    struct IsPowerOfTwo {
      constexpr bool operator()(unsigned int x) const { return std::has_single_bit(x); }
    };

    auto is_not_power_of_2 = std::not_fn<IsPowerOfTwo{}>();
    assert(is_not_power_of_2(5));
    assert(!is_not_power_of_2(4));

    auto moved = std::move(is_not_power_of_2);
    assert(moved(5));
    assert(!moved(4));

    auto copied = is_not_power_of_2;
    assert(copied(7));
    assert(!copied(8));

    moved = std::move(copied);
    assert(copied(9));
    assert(!copied(16));

    copied = moved;
    assert(copied(11));
    assert(!copied(32));
  }

  { // Make sure `not_fn<NTTP>` unspecified-type's operator() is SFINAE-friendly.
    using F = decltype(std::not_fn<[](int x) { return !x; }>());
    static_assert(!std::is_invocable<F>::value);
    static_assert(std::is_invocable<F, int>::value);
    static_assert(!std::is_invocable<F, void*>::value);
    static_assert(!std::is_invocable<F, int, int>::value);
  }

  { // Test noexceptness
    auto always_noexcept = std::not_fn<MaybeNoexceptFn<true>{}>();
    static_assert(noexcept(always_noexcept()));

    auto never_noexcept = std::not_fn<MaybeNoexceptFn<false>{}>();
    static_assert(!noexcept(never_noexcept()));

    auto always_noexcept_negation = std::not_fn<maybe_noexcept_negation<true>>();
    static_assert(noexcept(always_noexcept_negation()));

    auto never_noexcept_negation = std::not_fn<maybe_noexcept_negation<false>>();
    static_assert(!noexcept(never_noexcept_negation()));
  }

  { // Test calling volatile wrapper
    using NotFn = decltype(std::not_fn<std::false_type{}>());
    static_assert(!std::invocable<volatile NotFn&>);
    static_assert(!std::invocable<const volatile NotFn&>);
    static_assert(!std::invocable<volatile NotFn>);
    static_assert(!std::invocable<const volatile NotFn>);
  }
}

constexpr bool test() {
  basic_tests();
  test_perfect_forwarding_call_wrapper();
  test_return_type();

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}