diff options
Diffstat (limited to 'libstdc++-v3/testsuite/std/time')
-rw-r--r-- | libstdc++-v3/testsuite/std/time/format/empty_spec.cc | 51 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/std/time/format/precision.cc | 107 |
2 files changed, 158 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/std/time/format/empty_spec.cc b/libstdc++-v3/testsuite/std/time/format/empty_spec.cc index 99cbd74..a94eee1 100644 --- a/libstdc++-v3/testsuite/std/time/format/empty_spec.cc +++ b/libstdc++-v3/testsuite/std/time/format/empty_spec.cc @@ -170,6 +170,13 @@ test_duration() verify( -di, WIDEN("-40ms") ); res = std::format(WIDEN("{:>6}"), -di); VERIFY( res == WIDEN(" -40ms") ); +} + +template<typename _CharT> +void +test_duration_fp() +{ + std::basic_string<_CharT> res; const duration<double> df(11.22); verify( df, WIDEN("11.22s") ); @@ -179,6 +186,10 @@ test_duration() verify( -df, WIDEN("-11.22s") ); res = std::format(WIDEN("{:=^12}"), -df); VERIFY( res == WIDEN("==-11.22s===") ); + + // precision accepted but ignored + res = std::format(WIDEN("{:.6}"), df); + VERIFY( res == WIDEN("11.22s") ); } template<typename _CharT> @@ -294,6 +305,44 @@ test_hh_mm_ss() template<typename _CharT> void +test_hh_mm_ss_fp() +{ + duration<double> dt = 22h + 24min + 54s + 111222333ns; + // period controls number of subseconds + verify( hms<nanoseconds>(dt), + WIDEN("22:24:54.111222333") ); + verify( hms<microseconds>(dt), + WIDEN("22:24:54.111222") ); + verify( hms<milliseconds>(dt), + WIDEN("22:24:54.111") ); + verify( hms<deciseconds>(dt), + WIDEN("22:24:54.1") ); + verify( hms<seconds>(dt), + WIDEN("22:24:54") ); + verify( hms<nanoseconds>(-dt), + WIDEN("-22:24:54.111222333") ); + verify( hms<microseconds>(-dt), + WIDEN("-22:24:54.111222") ); + verify( hms<milliseconds>(-dt), + WIDEN("-22:24:54.111") ); + verify( hms<deciseconds>(-dt), + WIDEN("-22:24:54.1") ); + verify( hms<seconds>(-dt), + WIDEN("-22:24:54") ); + + // but hour and minutes are preserved + verify( hms<minutes>(dt), + WIDEN("22:24:54") ); + verify( hms<hours>(dt), + WIDEN("22:24:54") ); + verify( hms<minutes>(-dt), + WIDEN("-22:24:54") ); + verify( hms<hours>(-dt), + WIDEN("-22:24:54") ); +} + +template<typename _CharT> +void test_hh_mm_ss_cust() { const duration<char, deciseconds::period> charRep(123); @@ -339,9 +388,11 @@ void test_durations() { test_duration<CharT>(); + test_duration_fp<CharT>(); test_duration_cust<CharT>(); test_hh_mm_ss<CharT>(); + test_hh_mm_ss_fp<CharT>(); test_hh_mm_ss_cust<CharT>(); } diff --git a/libstdc++-v3/testsuite/std/time/format/precision.cc b/libstdc++-v3/testsuite/std/time/format/precision.cc new file mode 100644 index 0000000..5a9acbf --- /dev/null +++ b/libstdc++-v3/testsuite/std/time/format/precision.cc @@ -0,0 +1,107 @@ +// { dg-do run { target c++20 } } + +#include <chrono> +#include <ranges> +#include <testsuite_hooks.h> + +using namespace std::chrono; + +#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) +#define WIDEN(S) WIDEN_(_CharT, S) + +template<typename _CharT> +void +test_empty() +{ + std::basic_string<_CharT> res; + + const duration<double> d(33.111222); + res = std::format(WIDEN("{:.3}"), d); + VERIFY( res == WIDEN("33.1112s") ); + res = std::format(WIDEN("{:.6}"), d); + VERIFY( res == WIDEN("33.1112s") ); + res = std::format(WIDEN("{:.9}"), d); + VERIFY( res == WIDEN("33.1112s") ); + + // Uses ostream operator<< + const duration<double, std::nano> nd = d; + res = std::format(WIDEN("{:.3}"), nd); + VERIFY( res == WIDEN("3.31112e+10ns") ); + res = std::format(WIDEN("{:.6}"), nd); + VERIFY( res == WIDEN("3.31112e+10ns") ); + res = std::format(WIDEN("{:.9}"), nd); + VERIFY( res == WIDEN("3.31112e+10ns") ); +} + +template<typename _CharT> +void +test_Q() +{ + std::basic_string<_CharT> res; + + const duration<double> d(7.111222); + res = std::format(WIDEN("{:.3%Q}"), d); + VERIFY( res == WIDEN("7.111222") ); + res = std::format(WIDEN("{:.6%Q}"), d); + VERIFY( res == WIDEN("7.111222") ); + res = std::format(WIDEN("{:.9%Q}"), d); + VERIFY( res == WIDEN("7.111222") ); + + const duration<double, std::nano> nd = d; + res = std::format(WIDEN("{:.3%Q}"), nd); + VERIFY( res == WIDEN("7111222000") ); + res = std::format(WIDEN("{:.6%Q}"), nd); + VERIFY( res == WIDEN("7111222000") ); + res = std::format(WIDEN("{:.9%Q}"), nd); + VERIFY( res == WIDEN("7111222000") ); +} + +template<typename _CharT> +void +test_S() +{ + std::basic_string<_CharT> res; + + // Precision is ignored, but period affects output + const duration<double> d(5.111222); + res = std::format(WIDEN("{:.3%S}"), d); + VERIFY( res == WIDEN("05") ); + res = std::format(WIDEN("{:.6%S}"), d); + VERIFY( res == WIDEN("05") ); + res = std::format(WIDEN("{:.9%S}"), d); + VERIFY( res == WIDEN("05") ); + + const duration<double, std::milli> md = d; + res = std::format(WIDEN("{:.3%S}"), md); + VERIFY( res == WIDEN("05.111") ); + res = std::format(WIDEN("{:.6%S}"), md); + VERIFY( res == WIDEN("05.111") ); + res = std::format(WIDEN("{:.9%S}"), md); + VERIFY( res == WIDEN("05.111") ); + + const duration<double, std::nano> nd = d; + res = std::format(WIDEN("{:.3%S}"), nd); + VERIFY( res == WIDEN("05.111222000") ); + res = std::format(WIDEN("{:.6%S}"), nd); + VERIFY( res == WIDEN("05.111222000") ); + res = std::format(WIDEN("{:.9%S}"), nd); + VERIFY( res == WIDEN("05.111222000") ); +} + +template<typename CharT> +void +test_all() +{ + test_empty<CharT>(); + test_Q<CharT>(); + test_S<CharT>(); +} + +int main() +{ + test_all<char>(); + +#ifdef _GLIBCXX_USE_WCHAR_T + test_all<wchar_t>(); +#endif // _GLIBCXX_USE_WCHAR_T +} |