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() : i(T(0)) { }
type_emulatortype_emulator31   type_emulator(T j) : i(j) { }
type_emulatortype_emulator32   type_emulator(const type_emulator& e) : i(e.i) { }
33 
operator *=type_emulator34   type_emulator& operator*=(type_emulator a)
35   { i *= a.i; return *this; }
36 
operator +=type_emulator37   type_emulator& operator+=(type_emulator a)
38   { i += a.i; return *this; }
39 
operator Ttype_emulator40   operator T () { return i; }
41   T i;
42 };
43 
44 template<typename T>
operator ==(type_emulator<T> a,type_emulator<T> b)45 bool operator==(type_emulator<T> a, type_emulator<T> b)
46 { return a.i == b.i; }
47 
48 template<typename T>
operator <(type_emulator<T> a,type_emulator<T> b)49 bool operator<(type_emulator<T> a, type_emulator<T> b)
50 { return a.i < b.i; }
51 
52 template<typename T>
operator +(type_emulator<T> a,type_emulator<T> b)53 type_emulator<T> operator+(type_emulator<T> a, type_emulator<T> b)
54 { return a += b; }
55 
56 template<typename T>
operator *(type_emulator<T> a,type_emulator<T> b)57 type_emulator<T> operator*(type_emulator<T> a, type_emulator<T> b)
58 { return a *= b; }
59 
60 namespace std
61 {
62   template<typename T, typename U>
63   struct common_type<type_emulator<T>, U>
64   { typedef typename common_type<T,U>::type type; };
65 
66   template<typename T, typename U>
67   struct common_type<U, type_emulator<T>>
68   { typedef typename common_type<U,T>::type type; };
69 
70   template<typename T, typename U>
71   struct common_type<type_emulator<T>, type_emulator<U>>
72   { typedef typename common_type<T,U>::type type; };
73 
74   namespace chrono
75   {
76     template<typename T>
77     struct treat_as_floating_point<type_emulator<T>>
78     : is_floating_point<T>
79     { };
80   }
81 }
82 
83 typedef type_emulator<int> int_emulator;
84 typedef type_emulator<double> dbl_emulator;
85 
86 // 20.8.3.1 duration constructors [time.duration.cons]
87 void
test01()88 test01()
89 {
90   using namespace std::chrono;
91 
92   duration<int> d0(3);
93   duration<int> d0_copy(d0);
94   VERIFY(d0_copy.count() == d0.count());
95 
96   duration<int, std::milli> d1(5);
97   duration<int, std::micro> d1_copy(d1);
98   VERIFY(d1.count() * 1000 == d1_copy.count());
99 
100   duration<double, std::micro> d2(8.0);
101   duration<double, std::milli> d2_copy(d2);
102   VERIFY(d2.count() == d2_copy.count() * 1000.0);
103 
104   duration<int_emulator, std::milli> d3(5);
105   duration<int_emulator, std::micro> d3_copy(d3);
106   VERIFY(d3.count() * 1000 == d3_copy.count());
107 
108   duration<dbl_emulator, std::micro> d4(5.0);
109   duration<dbl_emulator, std::milli> d4_copy(d4);
110   VERIFY(d4.count() == d4_copy.count() * dbl_emulator(1000.0));
111 }
112 
113 int
main()114 main()
115 {
116   test01();
117   return 0;
118 }
119