| 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 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 |
// UNSUPPORTED: no-localization |
| 11 |
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 12 |
|
| 13 |
// XFAIL: availability-fp_to_chars-missing |
| 14 |
|
| 15 |
// REQUIRES: locale.fr_FR.UTF-8 |
| 16 |
// REQUIRES: locale.ja_JP.UTF-8 |
| 17 |
|
| 18 |
// <chrono> |
| 19 |
|
| 20 |
// class hh_mm_ss; |
| 21 |
|
| 22 |
// template<class charT, class traits, class Duration> |
| 23 |
// basic_ostream<charT, traits>& |
| 24 |
// operator<<(basic_ostream<charT, traits>& os, const hh_mm_ss<Duration>& hms); |
| 25 |
|
| 26 |
#include <cassert> |
| 27 |
#include <chrono> |
| 28 |
#include <ratio> |
| 29 |
#include <sstream> |
| 30 |
|
| 31 |
#include "make_string.h" |
| 32 |
#include "platform_support.h" // locale name macros |
| 33 |
#include "test_macros.h" |
| 34 |
|
| 35 |
#define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 36 |
|
| 37 |
template <class CharT, class Duration> |
| 38 |
static std::basic_string<CharT> stream_c_locale(std::chrono::hh_mm_ss<Duration> hms) { |
| 39 |
std::basic_stringstream<CharT> sstr; |
| 40 |
sstr << hms; |
| 41 |
return sstr.str(); |
| 42 |
} |
| 43 |
|
| 44 |
template <class CharT, class Duration> |
| 45 |
static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::hh_mm_ss<Duration> hms) { |
| 46 |
std::basic_stringstream<CharT> sstr; |
| 47 |
const std::locale locale(LOCALE_fr_FR_UTF_8); |
| 48 |
sstr.imbue(locale); |
| 49 |
sstr << hms; |
| 50 |
return sstr.str(); |
| 51 |
} |
| 52 |
|
| 53 |
template <class CharT, class Duration> |
| 54 |
static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::hh_mm_ss<Duration> hms) { |
| 55 |
std::basic_stringstream<CharT> sstr; |
| 56 |
const std::locale locale(LOCALE_ja_JP_UTF_8); |
| 57 |
sstr.imbue(locale); |
| 58 |
sstr << hms; |
| 59 |
return sstr.str(); |
| 60 |
} |
| 61 |
|
| 62 |
template <class CharT> |
| 63 |
static void test() { |
| 64 |
// Note std::atto can't be tested since the ratio conversion from std::atto |
| 65 |
// std::chrono::seconds to std::chrono::hours overflows when intmax_t is a |
| 66 |
// 64-bit type. This is a limitiation in the constructor of |
| 67 |
// std::chrono::hh_mm_ss. |
| 68 |
|
| 69 |
// C locale - integral power of 10 ratios |
| 70 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::femto>{1'234'567'890}}) == |
| 71 |
SV("00:00:00.000001234567890")); |
| 72 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::pico>{1'234'567'890}}) == |
| 73 |
SV("00:00:00.001234567890")); |
| 74 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::nano>{1'234'567'890}}) == |
| 75 |
SV("00:00:01.234567890")); |
| 76 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::micro>{1'234'567}}) == |
| 77 |
SV("00:00:01.234567")); |
| 78 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::milli>{123'456}}) == |
| 79 |
SV("00:02:03.456")); |
| 80 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::centi>{12'345}}) == |
| 81 |
SV("00:02:03.45")); |
| 82 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::deci>{1'234}}) == |
| 83 |
SV("00:02:03.4")); |
| 84 |
|
| 85 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t>{123}}) == SV("00:02:03")); |
| 86 |
|
| 87 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::deca>{-366}}) == |
| 88 |
SV("-01:01:00")); |
| 89 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::hecto>{-72}}) == |
| 90 |
SV("-02:00:00")); |
| 91 |
assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::kilo>{-86}}) == |
| 92 |
SV("-23:53:20")); |
| 93 |
|
| 94 |
// Starting at mega it will pass one day |
| 95 |
|
| 96 |
// fr_FR locale - integral power of not 10 ratios |
| 97 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{ |
| 98 |
std::chrono::duration<std::intmax_t, std::ratio<1, 5'000'000>>{5'000}}) == SV("00:00:00,0010000")); |
| 99 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 8'000>>{3}}) == |
| 100 |
SV("00:00:00,000375")); |
| 101 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 4'000>>{1}}) == |
| 102 |
SV("00:00:00,00025")); |
| 103 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 5'000>>{5}}) == |
| 104 |
SV("00:00:00,0010")); |
| 105 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 8>>{-4}}) == |
| 106 |
SV("-00:00:00,500")); |
| 107 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 4>>{-8}}) == |
| 108 |
SV("-00:00:02,00")); |
| 109 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 5>>{-5}}) == |
| 110 |
SV("-00:00:01,0")); |
| 111 |
|
| 112 |
// TODO FMT Note there's no wording on the rounding |
| 113 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 9>>{5}}) == |
| 114 |
SV("00:00:00,555555")); |
| 115 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 7>>{7}}) == |
| 116 |
SV("00:00:01,000000")); |
| 117 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 6>>{1}}) == |
| 118 |
SV("00:00:00,166666")); |
| 119 |
assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 3>>{2}}) == |
| 120 |
SV("00:00:00,666666")); |
| 121 |
|
| 122 |
// ja_JP locale - floating points |
| 123 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{ |
| 124 |
std::chrono::duration<long double, std::femto>{1'234'567'890.123}}) == SV("00:00:00.000001234567890")); |
| 125 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{ |
| 126 |
std::chrono::duration<long double, std::pico>{1'234'567'890.123}}) == SV("00:00:00.001234567890")); |
| 127 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{ |
| 128 |
std::chrono::duration<long double, std::nano>{1'234'567'890.123}}) == SV("00:00:01.234567890")); |
| 129 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::micro>{1'234'567.123}}) == |
| 130 |
SV("00:00:01.234567")); |
| 131 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::milli>{123'456.123}}) == |
| 132 |
SV("00:02:03.456")); |
| 133 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::centi>{12'345.123}}) == |
| 134 |
SV("00:02:03.45")); |
| 135 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<float, std::deci>{1'234.123}}) == |
| 136 |
SV("00:02:03.4")); |
| 137 |
|
| 138 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<float>{123.123}}) == SV("00:02:03")); |
| 139 |
|
| 140 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::deca>{-366.5}}) == |
| 141 |
SV("-01:01:05")); |
| 142 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::hecto>{-72.64}}) == |
| 143 |
SV("-02:01:04")); |
| 144 |
assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::kilo>{-86}}) == |
| 145 |
SV("-23:53:20")); |
| 146 |
} |
| 147 |
|
| 148 |
int main(int, char**) { |
| 149 |
test<char>(); |
| 150 |
|
| 151 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 152 |
test<wchar_t>(); |
| 153 |
#endif |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |