Debian Codegraph @

libstdc++-v3/testsuite/20_util/duration/cons/1.cc

raw
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 20.8.3 Class template duration [time.duration]
21
22 #include <chrono>
23 #include <type_traits>
24 #include <testsuite_hooks.h>
25
26 template<typename T>
27 struct type_emulator
28 {
29 type_emulator()
30 : i(T(0)) { }
31
32 type_emulator(T j)
33 : i(j) { }
34
35 type_emulator(const type_emulator& e)
36 : i(e.i) { }
37
38 type_emulator&
39 operator*=(type_emulator a)
40 {
41 i *= a.i;
42 return *this;
43 }
44
45 type_emulator&
46 operator+=(type_emulator a)
47 {
48 i += a.i;
49 return *this;
50 }
51
52 operator T ()
53 { return i; }
54
55 T i;
56 };
57
58 template<typename T>
59 bool
60 operator==(type_emulator<T> a, type_emulator<T> b)
61 { return a.i == b.i; }
62
63 template<typename T>
64 bool
65 operator<(type_emulator<T> a, type_emulator<T> b)
66 { return a.i < b.i; }
67
68 template<typename T>
69 type_emulator<T>
70 operator+(type_emulator<T> a, type_emulator<T> b)
71 { return a += b; }
72
73 template<typename T>
74 type_emulator<T>
75 operator*(type_emulator<T> a, type_emulator<T> b)
76 { return a *= b; }
77
78 namespace std
79 {
80 template<typename T, typename U>
81 struct common_type<type_emulator<T>, U>
82 { typedef typename common_type<T,U>::type type; };
83
84 template<typename T, typename U>
85 struct common_type<U, type_emulator<T>>
86 { typedef typename common_type<U,T>::type type; };
87
88 template<typename T, typename U>
89 struct common_type<type_emulator<T>, type_emulator<U>>
90 { typedef typename common_type<T,U>::type type; };
91
92 namespace chrono
93 {
94 template<typename T>
95 struct treat_as_floating_point<type_emulator<T>>
96 : is_floating_point<T>
97 { };
98 }
99 }
100
101 typedef type_emulator<int> int_emulator;
102 typedef type_emulator<double> dbl_emulator;
103
104 // 20.8.3.1 duration constructors [time.duration.cons]
105 void
106 test01()
107 {
108 using std::chrono::duration;
109
110 int r = 3;
111 duration<int> d1(r);
112 VERIFY(d1.count() == static_cast<duration<int>::rep>(r));
113
114 double s = 8.0;
115 duration<double> d2(s);
116 VERIFY(d2.count() == static_cast<duration<double>::rep>(s));
117
118 int_emulator ie(3);
119 duration<int_emulator> d3(ie);
120 VERIFY(d3.count() == static_cast<duration<int_emulator>::rep>(ie));
121
122 dbl_emulator de(4.0);
123 duration<dbl_emulator> d4(de);
124 VERIFY(d4.count() == static_cast<duration<dbl_emulator>::rep>(de));
125 }
126
127 int
128 main()
129 {
130 test01();
131 return 0;
132 }