| 1 | // Copyright (C) 2015-2025 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 | // { dg-add-options libatomic } |
| 20 | |
| 21 | #include <experimental/timer> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | using std::experimental::net::system_timer; |
| 25 | using std::experimental::net::io_context; |
| 26 | |
| 27 | void |
| 28 | test01() |
| 29 | { |
| 30 | bool test __attribute__((unused)) = false; |
| 31 | |
| 32 | io_context ctx1, ctx2; |
| 33 | |
| 34 | system_timer timer1(ctx1); |
| 35 | VERIFY( timer1.get_executor() == ctx1.get_executor() ); |
| 36 | VERIFY( timer1.expiry() == system_timer::time_point() ); |
| 37 | |
| 38 | system_timer timer2(ctx2); |
| 39 | VERIFY( timer2.get_executor() == ctx2.get_executor() ); |
| 40 | VERIFY( timer2.get_executor() != timer1.get_executor() ); |
| 41 | VERIFY( timer2.expiry() == system_timer::time_point() ); |
| 42 | |
| 43 | system_timer timer3(std::move(timer1)); |
| 44 | VERIFY( timer3.get_executor() == ctx1.get_executor() ); |
| 45 | VERIFY( timer3.expiry() == system_timer::time_point() ); |
| 46 | VERIFY( timer1.expiry() == system_timer::time_point() ); |
| 47 | |
| 48 | system_timer timer4(std::move(timer2)); |
| 49 | VERIFY( timer4.get_executor() == ctx2.get_executor() ); |
| 50 | VERIFY( timer4.expiry() == system_timer::time_point() ); |
| 51 | VERIFY( timer2.expiry() == system_timer::time_point() ); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | test02() |
| 56 | { |
| 57 | bool test __attribute__((unused)) = false; |
| 58 | |
| 59 | io_context ctx1, ctx2; |
| 60 | auto t1 = system_timer::clock_type::now(); |
| 61 | auto t2 = t1 + system_timer::duration(10); |
| 62 | |
| 63 | system_timer timer1(ctx1, t1); |
| 64 | VERIFY( timer1.get_executor() == ctx1.get_executor() ); |
| 65 | VERIFY( timer1.expiry() == t1 ); |
| 66 | |
| 67 | system_timer timer2(ctx2, t2); |
| 68 | VERIFY( timer2.get_executor() == ctx2.get_executor() ); |
| 69 | VERIFY( timer2.get_executor() != timer1.get_executor() ); |
| 70 | VERIFY( timer2.expiry() == t2 ); |
| 71 | |
| 72 | system_timer timer3(std::move(timer1)); |
| 73 | VERIFY( timer3.get_executor() == ctx1.get_executor() ); |
| 74 | VERIFY( timer3.expiry() == t1 ); |
| 75 | VERIFY( timer1.expiry() == system_timer::time_point() ); |
| 76 | |
| 77 | system_timer timer4(std::move(timer2)); |
| 78 | VERIFY( timer4.get_executor() == ctx2.get_executor() ); |
| 79 | VERIFY( timer4.expiry() == t2 ); |
| 80 | VERIFY( timer2.expiry() == system_timer::time_point() ); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | test03() |
| 85 | { |
| 86 | bool test __attribute__((unused)) = false; |
| 87 | |
| 88 | io_context ctx1, ctx2; |
| 89 | auto now = system_timer::clock_type::now(); |
| 90 | auto d1 = system_timer::duration(10); |
| 91 | auto d2 = system_timer::duration(100); |
| 92 | |
| 93 | system_timer timer1(ctx1, d1); |
| 94 | VERIFY( timer1.get_executor() == ctx1.get_executor() ); |
| 95 | VERIFY( timer1.expiry() > now ); |
| 96 | |
| 97 | system_timer timer2(ctx2, d2); |
| 98 | VERIFY( timer2.get_executor() == ctx2.get_executor() ); |
| 99 | VERIFY( timer2.get_executor() != timer1.get_executor() ); |
| 100 | VERIFY( timer2.expiry() > now ); |
| 101 | VERIFY( timer2.expiry() != timer1.expiry() ); |
| 102 | |
| 103 | system_timer timer3(std::move(timer1)); |
| 104 | VERIFY( timer3.get_executor() == ctx1.get_executor() ); |
| 105 | VERIFY( timer3.expiry() > now ); |
| 106 | VERIFY( timer1.expiry() == system_timer::time_point() ); |
| 107 | |
| 108 | system_timer timer4(std::move(timer2)); |
| 109 | VERIFY( timer4.get_executor() == ctx2.get_executor() ); |
| 110 | VERIFY( timer4.expiry() > now ); |
| 111 | VERIFY( timer2.expiry() == system_timer::time_point() ); |
| 112 | } |
| 113 | |
| 114 | int |
| 115 | main() |
| 116 | { |
| 117 | test01(); |
| 118 | test02(); |
| 119 | test03(); |
| 120 | } |