]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/net/timer/waitable/ops.cc
1 // Copyright (C) 2015-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++14 } }
19 // { dg-add-options libatomic }
20 // { dg-xfail-if "poll not available" { *-*-rtems* } }
22 #include <experimental/timer>
23 #include <testsuite_hooks.h>
25 using std::experimental::net::system_timer
;
26 using std::experimental::net::io_context
;
27 using std::error_code
;
32 bool test
__attribute__((unused
)) = false;
36 bool complete
= false;
38 auto then
= system_timer::clock_type::now() + system_timer::duration(100);
40 system_timer
timer(ctx
, then
);
41 VERIFY( timer
.cancel_one() == 0 );
42 VERIFY( timer
.cancel() == 0 );
44 timer
.async_wait([&](error_code e
) { ec
= e
; complete
= true; });
45 VERIFY( timer
.cancel_one() == 1 );
48 VERIFY( timer
.cancel_one() == 0 );
49 VERIFY( timer
.cancel() == 0 );
51 VERIFY( ctx
.run() == 1 );
52 VERIFY( ctx
.stopped() );
54 VERIFY( ec
== std::errc::operation_canceled
);
60 bool test
__attribute__((unused
)) = false;
65 const auto now
= system_timer::clock_type::now();
66 const auto t1
= now
+ std::chrono::seconds(100);
67 const auto t2
= t1
+ std::chrono::seconds(100);
69 system_timer
timer(ctx
, t1
);
70 VERIFY( timer
.expiry() == t1
);
72 VERIFY( timer
.expires_at(t2
) == 0 );
73 VERIFY( timer
.expiry() == t2
);
75 timer
.async_wait([&ec1
](error_code e
) { ec1
= e
; });
76 timer
.async_wait([&ec2
](error_code e
) { ec2
= e
; });
77 auto n
= timer
.expires_at(t1
);
79 VERIFY( timer
.expiry() == t1
);
81 VERIFY( ctx
.run_one() == 1 );
82 VERIFY( ! ctx
.stopped() );
83 VERIFY( ctx
.run_one() == 1 );
84 VERIFY( ctx
.stopped() );
85 VERIFY( ec1
== std::errc::operation_canceled
);
86 VERIFY( ec2
== std::errc::operation_canceled
);
88 VERIFY( timer
.expires_after(std::chrono::seconds(50)) == 0 );
89 VERIFY( timer
.expiry() < t1
);
94 timer
.async_wait([&ec1
](error_code e
) { ec1
= e
; });
95 timer
.async_wait([&ec2
](error_code e
) { ec2
= e
; });
96 VERIFY( timer
.expires_after(std::chrono::seconds(10)) == 2 );
97 VERIFY( timer
.expiry() < t1
);
99 VERIFY( ec1
== std::errc::operation_canceled
);
100 VERIFY( ec2
== std::errc::operation_canceled
);