File: ostream.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (156 lines) | stat: -rw-r--r-- 7,391 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: no-localization
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME

// XFAIL: availability-fp_to_chars-missing

// REQUIRES: locale.fr_FR.UTF-8
// REQUIRES: locale.ja_JP.UTF-8

// <chrono>

// class hh_mm_ss;

// template<class charT, class traits, class Duration>
//   basic_ostream<charT, traits>&
//     operator<<(basic_ostream<charT, traits>& os, const hh_mm_ss<Duration>& hms);

#include <cassert>
#include <chrono>
#include <ratio>
#include <sstream>

#include "make_string.h"
#include "platform_support.h" // locale name macros
#include "test_macros.h"

#define SV(S) MAKE_STRING_VIEW(CharT, S)

template <class CharT, class Duration>
static std::basic_string<CharT> stream_c_locale(std::chrono::hh_mm_ss<Duration> hms) {
  std::basic_stringstream<CharT> sstr;
  sstr << hms;
  return sstr.str();
}

template <class CharT, class Duration>
static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::hh_mm_ss<Duration> hms) {
  std::basic_stringstream<CharT> sstr;
  const std::locale locale(LOCALE_fr_FR_UTF_8);
  sstr.imbue(locale);
  sstr << hms;
  return sstr.str();
}

template <class CharT, class Duration>
static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::hh_mm_ss<Duration> hms) {
  std::basic_stringstream<CharT> sstr;
  const std::locale locale(LOCALE_ja_JP_UTF_8);
  sstr.imbue(locale);
  sstr << hms;
  return sstr.str();
}

template <class CharT>
static void test() {
  // Note std::atto can't be tested since the ratio conversion from std::atto
  // std::chrono::seconds to std::chrono::hours overflows when intmax_t is a
  // 64-bit type. This is a limitiation in the constructor of
  // std::chrono::hh_mm_ss.

  // C locale - integral power of 10 ratios
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::femto>{1'234'567'890}}) ==
         SV("00:00:00.000001234567890"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::pico>{1'234'567'890}}) ==
         SV("00:00:00.001234567890"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::nano>{1'234'567'890}}) ==
         SV("00:00:01.234567890"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::micro>{1'234'567}}) ==
         SV("00:00:01.234567"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::milli>{123'456}}) ==
         SV("00:02:03.456"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::centi>{12'345}}) ==
         SV("00:02:03.45"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::deci>{1'234}}) ==
         SV("00:02:03.4"));

  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t>{123}}) == SV("00:02:03"));

  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::deca>{-366}}) ==
         SV("-01:01:00"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::hecto>{-72}}) ==
         SV("-02:00:00"));
  assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::kilo>{-86}}) ==
         SV("-23:53:20"));

  // Starting at mega it will pass one day

  // fr_FR locale - integral power of not 10 ratios
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{
             std::chrono::duration<std::intmax_t, std::ratio<1, 5'000'000>>{5'000}}) == SV("00:00:00,0010000"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 8'000>>{3}}) ==
         SV("00:00:00,000375"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 4'000>>{1}}) ==
         SV("00:00:00,00025"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 5'000>>{5}}) ==
         SV("00:00:00,0010"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 8>>{-4}}) ==
         SV("-00:00:00,500"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 4>>{-8}}) ==
         SV("-00:00:02,00"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 5>>{-5}}) ==
         SV("-00:00:01,0"));

  // TODO FMT Note there's no wording on the rounding
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 9>>{5}}) ==
         SV("00:00:00,555555"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 7>>{7}}) ==
         SV("00:00:01,000000"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 6>>{1}}) ==
         SV("00:00:00,166666"));
  assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 3>>{2}}) ==
         SV("00:00:00,666666"));

  // ja_JP locale - floating points
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{
             std::chrono::duration<long double, std::femto>{1'234'567'890.123}}) == SV("00:00:00.000001234567890"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{
             std::chrono::duration<long double, std::pico>{1'234'567'890.123}}) == SV("00:00:00.001234567890"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{
             std::chrono::duration<long double, std::nano>{1'234'567'890.123}}) == SV("00:00:01.234567890"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::micro>{1'234'567.123}}) ==
         SV("00:00:01.234567"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::milli>{123'456.123}}) ==
         SV("00:02:03.456"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::centi>{12'345.123}}) ==
         SV("00:02:03.45"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<float, std::deci>{1'234.123}}) ==
         SV("00:02:03.4"));

  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<float>{123.123}}) == SV("00:02:03"));

  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::deca>{-366.5}}) ==
         SV("-01:01:05"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::hecto>{-72.64}}) ==
         SV("-02:01:04"));
  assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::kilo>{-86}}) ==
         SV("-23:53:20"));
}

int main(int, char**) {
  test<char>();

#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  test<wchar_t>();
#endif

  return 0;
}