Merge from trunk @ 138209
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / duration / cons / 1.cc
blob6d12dbb5bd73dc23e885cbb0400b6bfabb31729b
1 // { dg-options "-std=gnu++0x" }
2 // { dg-require-cstdint "" }
4 // Copyright (C) 2008 Free Software Foundation
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // 20.8.3 Class template duration [time.duration]
24 #include <chrono>
25 #include <type_traits>
26 #include <testsuite_hooks.h>
28 template<typename T>
29 struct type_emulator
31 type_emulator()
32 : i(T(0)) { }
34 type_emulator(T j)
35 : i(j) { }
37 type_emulator(const type_emulator& e)
38 : i(e.i) { }
40 type_emulator&
41 operator*=(type_emulator a)
43 i *= a.i;
44 return *this;
47 type_emulator&
48 operator+=(type_emulator a)
50 i += a.i;
51 return *this;
54 operator T ()
55 { return i; }
57 T i;
60 template<typename T>
61 bool
62 operator==(type_emulator<T> a, type_emulator<T> b)
63 { return a.i == b.i; }
65 template<typename T>
66 bool
67 operator<(type_emulator<T> a, type_emulator<T> b)
68 { return a.i < b.i; }
70 template<typename T>
71 type_emulator<T>
72 operator+(type_emulator<T> a, type_emulator<T> b)
73 { return a += b; }
75 template<typename T>
76 type_emulator<T>
77 operator*(type_emulator<T> a, type_emulator<T> b)
78 { return a *= b; }
80 namespace std
82 template<typename T, typename U>
83 struct common_type<type_emulator<T>, U>
84 { typedef typename common_type<T,U>::type type; };
86 template<typename T, typename U>
87 struct common_type<U, type_emulator<T>>
88 { typedef typename common_type<U,T>::type type; };
90 template<typename T, typename U>
91 struct common_type<type_emulator<T>, type_emulator<U>>
92 { typedef typename common_type<T,U>::type type; };
94 namespace chrono
96 template<typename T>
97 struct treat_as_floating_point<type_emulator<T>>
98 : is_floating_point<T>
99 { };
103 typedef type_emulator<int> int_emulator;
104 typedef type_emulator<double> dbl_emulator;
106 // 20.8.3.1 duration constructors [time.duration.cons]
107 void
108 test01()
110 bool test __attribute__((unused)) = true;
111 using std::chrono::duration;
113 duration<int> d0;
114 VERIFY(d0.count() == static_cast<duration<int>::rep>(0));
116 int r = 3;
117 duration<int> d1(r);
118 VERIFY(d1.count() == static_cast<duration<int>::rep>(r));
120 double s = 8.0;
121 duration<double> d2(s);
122 VERIFY(d2.count() == static_cast<duration<double>::rep>(s));
124 int_emulator ie(3);
125 duration<int_emulator> d3(ie);
126 VERIFY(d3.count() == static_cast<duration<int_emulator>::rep>(ie));
128 dbl_emulator de(4.0);
129 duration<dbl_emulator> d4(de);
130 VERIFY(d4.count() == static_cast<duration<dbl_emulator>::rep>(de));
134 main()
136 test01();
137 return 0;