1 // { dg-do run { target c++11 } }
2 // { dg-require-cstdint "" }
3 
4 // Copyright (C) 2008-2018 Free Software Foundation, Inc.
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 3, or (at your option)
10 // any later version.
11 
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.
16 
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3.  If not see
19 // <http://www.gnu.org/licenses/>.
20 
21 // 20.8.3 Class template duration [time.duration]
22 
23 #include <chrono>
24 #include <type_traits>
25 #include <testsuite_hooks.h>
26 
27 template<typename T>
28 struct type_emulator
29 {
type_emulatortype_emulator30   type_emulator()
31   : i(T(0)) { }
32 
type_emulatortype_emulator33   type_emulator(T j)
34   : i(j) { }
35 
type_emulatortype_emulator36   type_emulator(const type_emulator& e)
37   : i(e.i) { }
38 
39   type_emulator&
operator *=type_emulator40   operator*=(type_emulator a)
41   {
42     i *= a.i;
43     return *this;
44   }
45 
46   type_emulator&
operator +=type_emulator47   operator+=(type_emulator a)
48   {
49     i += a.i;
50     return *this;
51   }
52 
operator Ttype_emulator53   operator T ()
54   { return i; }
55 
56   T i;
57 };
58 
59 template<typename T>
60 bool
operator ==(type_emulator<T> a,type_emulator<T> b)61 operator==(type_emulator<T> a, type_emulator<T> b)
62 { return a.i == b.i; }
63 
64 template<typename T>
65 bool
operator <(type_emulator<T> a,type_emulator<T> b)66 operator<(type_emulator<T> a, type_emulator<T> b)
67 { return a.i < b.i; }
68 
69 template<typename T>
70 type_emulator<T>
operator +(type_emulator<T> a,type_emulator<T> b)71 operator+(type_emulator<T> a, type_emulator<T> b)
72 { return a += b; }
73 
74 template<typename T>
75 type_emulator<T>
operator *(type_emulator<T> a,type_emulator<T> b)76 operator*(type_emulator<T> a, type_emulator<T> b)
77 { return a *= b; }
78 
79 namespace std
80 {
81   template<typename T, typename U>
82   struct common_type<type_emulator<T>, U>
83   { typedef typename common_type<T,U>::type type; };
84 
85   template<typename T, typename U>
86   struct common_type<U, type_emulator<T>>
87   { typedef typename common_type<U,T>::type type; };
88 
89   template<typename T, typename U>
90   struct common_type<type_emulator<T>, type_emulator<U>>
91   { typedef typename common_type<T,U>::type type; };
92 
93   namespace chrono
94   {
95     template<typename T>
96     struct treat_as_floating_point<type_emulator<T>>
97     : is_floating_point<T>
98     { };
99   }
100 }
101 
102 typedef type_emulator<int> int_emulator;
103 typedef type_emulator<double> dbl_emulator;
104 
105 // 20.8.3.1 duration constructors [time.duration.cons]
106 void
test01()107 test01()
108 {
109   using std::chrono::duration;
110 
111   int r = 3;
112   duration<int> d1(r);
113   VERIFY(d1.count() == static_cast<duration<int>::rep>(r));
114 
115   double s = 8.0;
116   duration<double> d2(s);
117   VERIFY(d2.count() == static_cast<duration<double>::rep>(s));
118 
119   int_emulator ie(3);
120   duration<int_emulator> d3(ie);
121   VERIFY(d3.count() == static_cast<duration<int_emulator>::rep>(ie));
122 
123   dbl_emulator de(4.0);
124   duration<dbl_emulator> d4(de);
125   VERIFY(d4.count() == static_cast<duration<dbl_emulator>::rep>(de));
126 }
127 
128 int
main()129 main()
130 {
131   test01();
132   return 0;
133 }
134