Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / time / time.clock / time.clock.system / ostream.pass.cpp
blob553b74487961938e8592bd12347d4c156e5d0ed9
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
13 // TODO FMT This test should not require std::to_chars(floating-point)
14 // XFAIL: availability-fp_to_chars-missing
16 // REQUIRES: locale.fr_FR.UTF-8
17 // REQUIRES: locale.ja_JP.UTF-8
19 // <chrono>
21 // class system_clock;
23 // template<class charT, class traits, class Duration>
24 // basic_ostream<charT, traits>&
25 // operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp);
27 #include <chrono>
28 #include <cassert>
29 #include <ratio>
30 #include <sstream>
32 #include "make_string.h"
33 #include "platform_support.h" // locale name macros
34 #include "test_macros.h"
36 #define SV(S) MAKE_STRING_VIEW(CharT, S)
38 template <class CharT, class Duration>
39 static std::basic_string<CharT> stream_c_locale(std::chrono::sys_time<Duration> time_point) {
40 std::basic_stringstream<CharT> sstr;
41 sstr << std::fixed << time_point;
42 return sstr.str();
45 template <class CharT, class Duration>
46 static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::sys_time<Duration> time_point) {
47 std::basic_stringstream<CharT> sstr;
48 const std::locale locale(LOCALE_fr_FR_UTF_8);
49 sstr.imbue(locale);
50 sstr << std::fixed << time_point;
51 return sstr.str();
54 template <class CharT, class Duration>
55 static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::sys_time<Duration> time_point) {
56 std::basic_stringstream<CharT> sstr;
57 const std::locale locale(LOCALE_ja_JP_UTF_8);
58 sstr.imbue(locale);
59 sstr << std::fixed << time_point;
60 return sstr.str();
63 template <class CharT>
64 static void test_c() {
65 using namespace std::literals::chrono_literals;
67 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) ==
68 SV("2000-01-01 01:02:03.123456789"));
69 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::microseconds>{946'688'523'123'456us}) ==
70 SV("2000-01-01 01:02:03.123456"));
72 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::milliseconds>{946'684'800'123ms}) ==
73 SV("2000-01-01 00:00:00.123"));
74 assert(stream_c_locale<CharT>(std::chrono::sys_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30"));
75 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::minutes>{20'576'131min}) ==
76 SV("2009-02-13 23:31:00"));
77 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00"));
78 assert(stream_c_locale<CharT>(std::chrono::sys_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00"));
79 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::weeks>{std::chrono::weeks{2041}}) ==
80 SV("2009-02-12 00:00:00"));
82 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{
83 std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));
84 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<short, std::ratio<1, 2>>>{
85 std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0"));
86 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<int, std::ratio<1, 4>>>{
87 std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00"));
88 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long, std::ratio<1, 10>>>{
89 std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1"));
90 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long long, std::ratio<1, 100>>>{
91 std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10"));
93 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<float, std::ratio<1, 1>>>{
94 std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));
95 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<double, std::ratio<1, 10>>>{
96 std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3"));
97 assert(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long double, std::ratio<1, 100>>>{
98 std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23"));
101 template <class CharT>
102 static void test_fr_FR() {
103 using namespace std::literals::chrono_literals;
105 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) ==
106 SV("2000-01-01 01:02:03,123456789"));
107 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::microseconds>{946'688'523'123'456us}) ==
108 SV("2000-01-01 01:02:03,123456"));
110 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::milliseconds>{946'684'800'123ms}) ==
111 SV("2000-01-01 00:00:00,123"));
112 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30"));
113 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::minutes>{20'576'131min}) ==
114 SV("2009-02-13 23:31:00"));
115 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00"));
116 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00"));
117 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::weeks>{std::chrono::weeks{2041}}) ==
118 SV("2009-02-12 00:00:00"));
120 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{
121 std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));
122 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<short, std::ratio<1, 2>>>{
123 std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00,0"));
124 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<int, std::ratio<1, 4>>>{
125 std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00,00"));
126 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long, std::ratio<1, 10>>>{
127 std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01,1"));
128 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long long, std::ratio<1, 100>>>{
129 std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30,10"));
131 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<float, std::ratio<1, 1>>>{
132 std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));
133 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<double, std::ratio<1, 10>>>{
134 std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12,3"));
135 assert(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long double, std::ratio<1, 100>>>{
136 std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01,23"));
139 template <class CharT>
140 static void test_ja_JP() {
141 using namespace std::literals::chrono_literals;
143 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) ==
144 SV("2000-01-01 01:02:03.123456789"));
145 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::microseconds>{946'688'523'123'456us}) ==
146 SV("2000-01-01 01:02:03.123456"));
148 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::milliseconds>{946'684'800'123ms}) ==
149 SV("2000-01-01 00:00:00.123"));
150 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30"));
151 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::minutes>{20'576'131min}) ==
152 SV("2009-02-13 23:31:00"));
153 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00"));
154 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00"));
155 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::weeks>{std::chrono::weeks{2041}}) ==
156 SV("2009-02-12 00:00:00"));
158 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{
159 std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));
160 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<short, std::ratio<1, 2>>>{
161 std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0"));
162 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<int, std::ratio<1, 4>>>{
163 std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00"));
164 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long, std::ratio<1, 10>>>{
165 std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1"));
166 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long long, std::ratio<1, 100>>>{
167 std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10"));
169 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<float, std::ratio<1, 1>>>{
170 std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));
171 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<double, std::ratio<1, 10>>>{
172 std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3"));
173 assert(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::duration<long double, std::ratio<1, 100>>>{
174 std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23"));
177 template <class CharT>
178 static void test() {
179 test_c<CharT>();
180 test_fr_FR<CharT>();
181 test_ja_JP<CharT>();
184 int main(int, char**) {
185 test<char>();
187 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
188 test<wchar_t>();
189 #endif
191 return 0;