]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / duration / requirements / reduced_period.cc
1 // Copyright (C) 2020-2023 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 compile { target c++11 } }
19
20 // Test the changes introduced by P0548R1 "common_type and duration".
21 // Specifically, duration<R,P>::period should be the reduced ratio,
22 // and common_type<D1, D2>::type should be a duration using the
23 // reduced ratio.
24
25 #include <chrono>
26
27 using std::chrono::duration;
28 using std::ratio;
29 using std::common_type;
30 using std::is_same;
31
32 void
33 test01()
34 {
35 using D1 = duration<int, ratio<10, 10>>;
36 static_assert( is_same<D1::period, ratio<1, 1>>::value,
37 "duration<R, P>::period is P::type, not P" );
38
39 using C1 = common_type<D1>::type;
40 static_assert( is_same<C1, duration<int, D1::period>>::value,
41 "common_type_t<duration<R, P1> is duration<R, P1::type>");
42 static_assert( is_same<common_type<D1, D1>::type, C1>::value,
43 "common_type_t<D1, D1> is common_type_t<D1>" );
44 static_assert( is_same<common_type<D1, D1, D1>::type, C1>::value,
45 "common_type_t<D1, D1, D1> is common_type_t<D1>" );
46
47 using D2 = duration<int, ratio<30, 15>>;
48 static_assert( is_same<D2::period, ratio<2, 1>>::value,
49 "duration<R, P2>::period is P2::type, not P2" );
50
51 using C2 = common_type<D2>::type;
52 static_assert( is_same<C2, duration<int, D2::period>>::value,
53 "common_type_t<duration<R, P2> is duration<R, P2::type>");
54 static_assert( is_same<common_type<D2, D2>::type, C2>::value,
55 "common_type_t<D2, D2> is common_type_t<D2>" );
56 static_assert( is_same<common_type<D2, D2, D2>::type, C2>::value,
57 "common_type_t<D2, D2, D2> is common_type_t<D2>" );
58
59 using D3 = duration<int, ratio<4, 12>>;
60 static_assert( is_same<D3::period, ratio<1, 3>>::value,
61 "duration<R, P3>::period is P3::type, not P3" );
62
63 using C3 = common_type<D3>::type;
64 static_assert( is_same<C3, duration<int, D3::period>>::value,
65 "common_type_t<duration<R, P3> is duration<R, P3::type>");
66 static_assert( is_same<common_type<D3, D3>::type, C3>::value,
67 "common_type_t<D3, D3> is common_type_t<D3>" );
68 static_assert( is_same<common_type<D3, D3, D3>::type, C3>::value,
69 "common_type_t<D3, D3, D3> is common_type_t<D3>" );
70
71 using C12 = common_type<D1, D2>::type;
72 static_assert( is_same<C12, C1>::value,
73 "common_type_t<D1, D2> uses the right period" );
74 using C21 = common_type<D2, D1>::type;
75 static_assert( is_same<C21, C12>::value,
76 "common_type_t<D1, D2> is common_type_t<D2, D1>" );
77
78 using C13 = common_type<D1, D3>::type;
79 static_assert( is_same<C13, C3>::value,
80 "common_type_t<D1, D3> uses the right period" );
81 using C31 = common_type<D3, D1>::type;
82 static_assert( is_same<C31, C13>::value,
83 "common_type_t<D1, D3> is common_type_t<D3, D1>" );
84
85 using C23 = common_type<D2, D3>::type;
86 static_assert( is_same<C23, C3>::value,
87 "common_type_t<D2, D3> uses the right period" );
88 using C32 = common_type<D3, D2>::type;
89 static_assert( is_same<C32, C23>::value,
90 "common_type_t<D2, D3> is common_type_t<D3, D2>" );
91
92 using C123 = common_type<D1, D2, D3>::type;
93 static_assert( is_same<C123, C3>::value,
94 "common_type of three durations uses the right period" );
95 using C132 = common_type<D1, D3, D2>::type;
96 static_assert( is_same<C132, C123>::value, "order doesn't matter" );
97 using C312 = common_type<D3, D1, D2>::type;
98 static_assert( is_same<C312, C123>::value, "order doesn't matter" );
99 using C321 = common_type<D3, D2, D1>::type;
100 static_assert( is_same<C321, C123>::value, "order doesn't matter" );
101
102 using C = common_type<duration<short, ratio<1, 3>>,
103 duration<unsigned, ratio<1, 2>>>::type;
104 static_assert( is_same<C, duration<common_type<short, unsigned>::type,
105 ratio<1, 6>>>::value, "" );
106 }
107
108 void
109 test02()
110 {
111 using D1 = duration<int, ratio<10, 10>>;
112 D1 d1;
113 static_assert( is_same<decltype(+d1), common_type<D1>::type>::value,
114 "unary + returns the reduced duration" );
115 static_assert( is_same<decltype(-d1), common_type<D1>::type>::value,
116 "unary - returns the reduced duration" );
117
118 using D2 = duration<int, ratio<30, 15>>;
119 D2 d2;
120 static_assert( is_same<decltype(+d2), common_type<D2>::type>::value,
121 "unary + returns the reduced duration" );
122 static_assert( is_same<decltype(-d2), common_type<D2>::type>::value,
123 "unary - returns the reduced duration" );
124
125 using D3 = duration<int, ratio<4, 12>>;
126 D3 d3;
127 static_assert( is_same<decltype(+d3), common_type<D3>::type>::value,
128 "unary + returns the reduced duration" );
129 static_assert( is_same<decltype(-d3), common_type<D3>::type>::value,
130 "unary - returns the reduced duration" );
131 }
132
133 template<typename T>
134 struct Number
135 {
136 explicit
137 Number(T t = 0) : i(t)
138 { }
139
140 template<typename U, bool B = std::is_convertible<U, T>::value,
141 typename = typename std::enable_if<B>::type>
142 explicit
143 Number(Number<U> n) : i(n.i)
144 { }
145
146 T i = 0;
147
148 Number& operator+=(Number n) { i += n.i; return *this; }
149 Number& operator-=(Number n) { i -= n.i; return *this; }
150 Number& operator*=(Number n) { i *= n.i; return *this; }
151 Number& operator/=(Number n) { i /= n.i; return *this; }
152 Number& operator%=(Number n) { i %= n.i; return *this; }
153
154 Number operator+(Number n) { return Number{ i + n.i }; }
155 Number operator-(Number n) { return Number{ i - n.i }; }
156 Number operator*(Number n) { return Number{ i * n.i }; }
157 Number operator/(Number n) { return Number{ i / n.i }; }
158 Number operator%(Number n) { return Number{ i % n.i }; }
159 };
160
161 namespace std
162 {
163 // Specialise common_type to give a different type
164 template<>
165 struct common_type<Number<int>, Number<int>>
166 { using type = Number<long>; };
167 }
168
169 void
170 test03()
171 {
172
173 using D4 = duration<Number<int>, ratio<49, 21>>;
174 static_assert( is_same<common_type<D4>::type,
175 duration<Number<long>, ratio<7, 3>>>::value,
176 "common_type_t<duration<R,P>> uses common_type_t<R>" );
177
178 D4 d4;
179 static_assert( is_same<decltype(+d4), common_type<D4>::type>::value,
180 "unary + returns type with common_type_t<D4::rep> as rep" );
181 static_assert( is_same<decltype(-d4), common_type<D4>::type>::value,
182 "unary - returns type with common_type_t<D4::rep> as rep" );
183 }