]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/net/timer/waitable/ops.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / net / timer / waitable / ops.cc
1 // Copyright (C) 2015-2019 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/timer>
21 #include <testsuite_hooks.h>
22
23 using std::experimental::net::system_timer;
24 using std::experimental::net::io_context;
25 using std::error_code;
26
27 void
28 test01()
29 {
30 bool test __attribute__((unused)) = false;
31
32 io_context ctx;
33 error_code ec;
34 bool complete = false;
35
36 auto then = system_timer::clock_type::now() + system_timer::duration(100);
37
38 system_timer timer(ctx, then);
39 VERIFY( timer.cancel_one() == 0 );
40 VERIFY( timer.cancel() == 0 );
41
42 timer.async_wait([&](error_code e) { ec = e; complete = true; });
43 VERIFY( timer.cancel_one() == 1 );
44 VERIFY( !complete );
45
46 VERIFY( timer.cancel_one() == 0 );
47 VERIFY( timer.cancel() == 0 );
48
49 VERIFY( ctx.run() == 1 );
50 VERIFY( ctx.stopped() );
51 VERIFY( complete );
52 VERIFY( ec == std::errc::operation_canceled );
53 }
54
55 void
56 test02()
57 {
58 bool test __attribute__((unused)) = false;
59
60 io_context ctx;
61 error_code ec1, ec2;
62
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);
66
67 system_timer timer(ctx, t1);
68 VERIFY( timer.expiry() == t1 );
69
70 VERIFY( timer.expires_at(t2) == 0 );
71 VERIFY( timer.expiry() == t2 );
72
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);
76 VERIFY( n == 2 );
77 VERIFY( timer.expiry() == t1 );
78
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 );
85
86 VERIFY( timer.expires_after(std::chrono::seconds(50)) == 0 );
87 VERIFY( timer.expiry() < t1 );
88
89 ec1.clear();
90 ec2.clear();
91 ctx.restart();
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 );
96 ctx.run();
97 VERIFY( ec1 == std::errc::operation_canceled );
98 VERIFY( ec2 == std::errc::operation_canceled );
99 }
100
101 int
102 main()
103 {
104 test01();
105 test02();
106 }