1 // Copyright (C) 2020 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-options "-std=gnu++2a" }
19 // { dg-do compile { target c++2a } }
20 
21 #include <chrono>
22 #include <slow_clock.h>
23 
24 namespace chrono = std::chrono;
25 
26 static_assert( chrono::is_clock<chrono::system_clock>::value );
27 static_assert( chrono::is_clock_v<chrono::system_clock> );
28 
29 static_assert( chrono::is_clock<chrono::high_resolution_clock>::value );
30 static_assert( chrono::is_clock_v<chrono::high_resolution_clock> );
31 
32 static_assert( chrono::is_clock<chrono::steady_clock>::value );
33 static_assert( chrono::is_clock_v<chrono::steady_clock> );
34 
35 static_assert(chrono::is_clock<chrono::file_clock>::value);
36 static_assert(chrono::is_clock_v<chrono::file_clock>);
37 
38 // Clock<xxx_clock> will not use the specialization of is_clock<xxx_clock>
39 template<typename C> struct Clock : C { };
40 
41 static_assert( chrono::is_clock<Clock<chrono::system_clock>>::value );
42 static_assert( chrono::is_clock_v<Clock<chrono::system_clock>> );
43 
44 static_assert( chrono::is_clock<Clock<chrono::high_resolution_clock>>::value );
45 static_assert( chrono::is_clock_v<Clock<chrono::high_resolution_clock>> );
46 
47 static_assert( chrono::is_clock<Clock<chrono::steady_clock>>::value );
48 static_assert( chrono::is_clock_v<Clock<chrono::steady_clock>> );
49 
50 static_assert(chrono::is_clock<Clock<chrono::file_clock>>::value);
51 static_assert(chrono::is_clock_v<Clock<chrono::file_clock>>);
52 
53 static_assert( chrono::is_clock<__gnu_test::slow_clock>::value );
54 static_assert( chrono::is_clock_v<__gnu_test::slow_clock> );
55 
56 // Negative tests
57 
58 static_assert( ! chrono::is_clock<int>::value );
59 static_assert( ! chrono::is_clock_v<int> );
60 
61 static_assert( ! chrono::is_clock<void>::value );
62 static_assert( ! chrono::is_clock_v<void> );
63 
64 struct not_a_clock_1
65 {
66   using rep = int;
67   using period = std::ratio<4, 2>;
68   using duration = chrono::duration<long, period>; // different rep
69   using time_point = chrono::time_point<not_a_clock_1>;
70   static constexpr bool is_steady = false;
71   static time_point now();
72 };
73 
74 static_assert( ! chrono::is_clock<not_a_clock_1>::value );
75 static_assert( ! chrono::is_clock_v<not_a_clock_1> );
76 
77 struct not_a_clock_2
78 {
79   using rep = int;
80   using period = int; // not a std::ratio
81   using duration = chrono::duration<rep>;
82   using time_point = chrono::time_point<not_a_clock_2>;
83   static constexpr bool is_steady = false;
84   static time_point now();
85 };
86 
87 static_assert( ! chrono::is_clock<not_a_clock_2>::value );
88 static_assert( ! chrono::is_clock_v<not_a_clock_2> );
89 
90 struct not_a_clock_3
91 {
92   using rep = int;
93   using period = std::ratio<1>;
94   using duration = chrono::duration<rep>;
95   // wrong duration:
96   using time_point = chrono::time_point<not_a_clock_3, chrono::duration<long>>;
97   static constexpr bool is_steady = false;
98   static time_point now();
99 };
100 
101 static_assert( ! chrono::is_clock<not_a_clock_3>::value );
102 static_assert( ! chrono::is_clock_v<not_a_clock_3> );
103 
104 struct not_a_clock_4
105 {
106   using rep = int;
107   using period = std::ratio<1>;
108   using duration = chrono::duration<rep>;
109   using time_point = chrono::time_point<not_a_clock_4>;
110   static constexpr int is_steady = 0; // not a const bool
111   static time_point now();
112 };
113 
114 static_assert( ! chrono::is_clock<not_a_clock_4>::value );
115 static_assert( ! chrono::is_clock_v<not_a_clock_4> );
116 
117 struct not_a_clock_5
118 {
119   using rep = int;
120   using period = std::ratio<1>;
121   using duration = chrono::duration<rep>;
122   using time_point = chrono::time_point<not_a_clock_5>;
123   static constexpr bool is_steady = false;
124   static int now(); // wrong return type
125 };
126 
127 static_assert( ! chrono::is_clock<not_a_clock_5>::value );
128 static_assert( ! chrono::is_clock_v<not_a_clock_5> );
129 
130 struct not_a_clock_6
131 {
132   using rep = int;
133   using period = std::ratio<1>;
134   using duration = chrono::duration<rep>;
135   using time_point = chrono::time_point<not_a_clock_6>;
136   const bool is_steady = false; // not static
137   static time_point now();
138 };
139 
140 static_assert( ! chrono::is_clock<not_a_clock_6>::value );
141 static_assert( ! chrono::is_clock_v<not_a_clock_6> );
142