]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/net/timer/waitable/ops.cc
1 // Copyright (C) 2015-2019 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 } }
20 #include <experimental/timer>
21 #include <testsuite_hooks.h>
23 using std::experimental::net::system_timer
;
24 using std::experimental::net::io_context
;
25 using std::error_code
;
30 bool test
__attribute__((unused
)) = false;
34 bool complete
= false;
36 auto then
= system_timer::clock_type::now() + system_timer::duration(100);
38 system_timer
timer(ctx
, then
);
39 VERIFY( timer
.cancel_one() == 0 );
40 VERIFY( timer
.cancel() == 0 );
42 timer
.async_wait([&](error_code e
) { ec
= e
; complete
= true; });
43 VERIFY( timer
.cancel_one() == 1 );
46 VERIFY( timer
.cancel_one() == 0 );
47 VERIFY( timer
.cancel() == 0 );
49 VERIFY( ctx
.run() == 1 );
50 VERIFY( ctx
.stopped() );
52 VERIFY( ec
== std::errc::operation_canceled
);
58 bool test
__attribute__((unused
)) = false;
63 const auto now
= system_timer::clock_type::now();
64 const auto t1
= now
+ std::chrono::seconds(100);
65 const auto t2
= t1
+ std::chrono::seconds(100);
67 system_timer
timer(ctx
, t1
);
68 VERIFY( timer
.expiry() == t1
);
70 VERIFY( timer
.expires_at(t2
) == 0 );
71 VERIFY( timer
.expiry() == t2
);
73 timer
.async_wait([&ec1
](error_code e
) { ec1
= e
; });
74 timer
.async_wait([&ec2
](error_code e
) { ec2
= e
; });
75 auto n
= timer
.expires_at(t1
);
77 VERIFY( timer
.expiry() == t1
);
79 VERIFY( ctx
.run_one() == 1 );
80 VERIFY( ! ctx
.stopped() );
81 VERIFY( ctx
.run_one() == 1 );
82 VERIFY( ctx
.stopped() );
83 VERIFY( ec1
== std::errc::operation_canceled
);
84 VERIFY( ec2
== std::errc::operation_canceled
);
86 VERIFY( timer
.expires_after(std::chrono::seconds(50)) == 0 );
87 VERIFY( timer
.expiry() < t1
);
92 timer
.async_wait([&ec1
](error_code e
) { ec1
= e
; });
93 timer
.async_wait([&ec2
](error_code e
) { ec2
= e
; });
94 VERIFY( timer
.expires_after(std::chrono::seconds(10)) == 2 );
95 VERIFY( timer
.expiry() < t1
);
97 VERIFY( ec1
== std::errc::operation_canceled
);
98 VERIFY( ec2
== std::errc::operation_canceled
);