OSDN Git Service

2008-07-15 Chris Fairles <chris.fairles@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 20_util / duration / cons / 2.cc
1 // { dg-options "-std=gnu++0x" }
2 // { dg-require-cstdint "" }
3
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.
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 COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // 20.8.3 Class template duration [time.duration]
23
24 #include <chrono>
25 #include <type_traits>
26 #include <testsuite_hooks.h>
27
28 template<typename T>
29 struct type_emulator
30 {
31   type_emulator() : i(T(0)) { }
32   type_emulator(T j) : i(j) { }
33   type_emulator(const type_emulator& e) : i(e.i) { }
34
35   type_emulator& operator*=(type_emulator a)
36   { i *= a.i; return *this; }
37   
38   type_emulator& operator+=(type_emulator a)
39   { i += a.i; return *this; }
40     
41   operator T () { return i; }
42   T i;
43 };
44
45 template<typename T>
46 bool operator==(type_emulator<T> a, type_emulator<T> b)
47 { return a.i == b.i; }
48
49 template<typename T>
50 bool operator<(type_emulator<T> a, type_emulator<T> b)
51 { return a.i < b.i; }
52
53 template<typename T>
54 type_emulator<T> operator+(type_emulator<T> a, type_emulator<T> b)
55 { return a += b; }
56
57 template<typename T>
58 type_emulator<T> operator*(type_emulator<T> a, type_emulator<T> b)
59 { return a *= b; }
60
61 namespace std
62 {
63   template<typename T, typename U>
64   struct common_type<type_emulator<T>, U>
65   { typedef typename common_type<T,U>::type type; };
66   
67   template<typename T, typename U>
68   struct common_type<U, type_emulator<T>>
69   { typedef typename common_type<U,T>::type type; };
70   
71   template<typename T, typename U>
72   struct common_type<type_emulator<T>, type_emulator<U>>
73   { typedef typename common_type<T,U>::type type; };
74   
75   namespace chrono
76   {    
77     template<typename T>
78     struct treat_as_floating_point<type_emulator<T>>
79     : is_floating_point<T>
80     { };
81   }
82 }
83
84 typedef type_emulator<int> int_emulator;
85 typedef type_emulator<double> dbl_emulator;
86
87 // 20.8.3.1 duration constructors [time.duration.cons]
88 void
89 test01()
90 {
91   bool test __attribute__((unused)) = true;
92   using namespace std::chrono;
93   
94   duration<int> d0(3);
95   duration<int> d0_copy(d0);
96   VERIFY(d0_copy.count() == d0.count());
97   
98   duration<int, std::milli> d1(5);
99   duration<int, std::micro> d1_copy(d1);
100   VERIFY(d1.count() * 1000 == d1_copy.count());
101   
102   duration<double, std::micro> d2(8.0);
103   duration<double, std::milli> d2_copy(d2);
104   VERIFY(d2.count() == d2_copy.count() * 1000.0);
105   
106   duration<int_emulator, std::milli> d3(5);
107   duration<int_emulator, std::micro> d3_copy(d3);
108   VERIFY(d3.count() * 1000 == d3_copy.count());
109   
110   duration<dbl_emulator, std::micro> d4(5.0);
111   duration<dbl_emulator, std::milli> d4_copy(d4);
112   VERIFY(d4.count() == d4_copy.count() * dbl_emulator(1000.0));
113 }
114
115 int
116 main()
117 {
118   test01();
119   return 0;
120 }