\hypertarget{format_8cpp_source}{}\doxysection{format.\+cpp} \label{format_8cpp_source}\index{tests/format.cpp@{tests/format.cpp}} \mbox{\hyperlink{format_8cpp}{Go to the documentation of this file.}} \begin{DoxyCode}{0} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00001}00001 \textcolor{preprocessor}{\#include "{}catch.hpp"{}}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00002}00002 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00003}00003 \textcolor{preprocessor}{\#include "{}\mbox{\hyperlink{format_8h}{kblib/format.h}}"{}}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00004}00004 \textcolor{preprocessor}{\#include "{}\mbox{\hyperlink{iterators_8h}{kblib/iterators.h}}"{}}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00005}00005 \textcolor{preprocessor}{\#include "{}\mbox{\hyperlink{stringops_8h}{kblib/stringops.h}}"{}}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00006}00006 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00007}00007 \textcolor{preprocessor}{\#include }} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00008}00008 \textcolor{preprocessor}{\#include }} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00009}00009 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00010}00010 \textcolor{preprocessor}{\#if KBLIB\_USE\_CXX17}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00011}00011 \textcolor{keyword}{template} <\textcolor{keyword}{typename} T>} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00012}\mbox{\hyperlink{format_8cpp_a4caa847fb54d70a9dcd9ce2e60d63489}{00012}} \textcolor{keyword}{auto} \mbox{\hyperlink{format_8cpp_a4caa847fb54d70a9dcd9ce2e60d63489}{pred\_count\_digits}}(T v) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00013}00013 \textcolor{keywordflow}{return} Catch::Predicate(} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00014}00014 [](T v\_) -\/> \textcolor{keywordtype}{bool} \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00015}00015 \textcolor{keywordflow}{return} \mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(v\_)} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00016}00016 == \mbox{\hyperlink{namespacekblib_a1909a64960663825ccc4eb98e4637df2}{kblib::to\_signed}}(\mbox{\hyperlink{namespacekblib_a9d7e23caee89855a71fb84c7f245f977}{std::to\_string}}(v\_).\mbox{\hyperlink{namespacekblib_a793fd30664717fe1da593bf53e895e9d}{length}}());} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00017}00017 \},} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00018}00018 \mbox{\hyperlink{namespacekblib_a56615de712caf1b43a541f5387a4b847}{kblib::concat}}(v, \textcolor{stringliteral}{"{}: digits="{}}, \mbox{\hyperlink{namespacekblib_a9d7e23caee89855a71fb84c7f245f977}{std::to\_string}}(v).\mbox{\hyperlink{namespacekblib_a793fd30664717fe1da593bf53e895e9d}{length}}(),} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00019}00019 \textcolor{stringliteral}{"{}; calc="{}}, \mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(v)));} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00020}00020 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00021}\mbox{\hyperlink{format_8cpp_a4ec0059820fbfe1b795098072560c292}{00021}} \textcolor{preprocessor}{\# define REQUIRE\_DIGITS(v) REQUIRE\_THAT((v), pred\_count\_digits((v)))}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00022}00022 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00023}00023 \textcolor{keyword}{template} <\textcolor{keyword}{typename} T, \textcolor{keyword}{typename} U>} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00024}\mbox{\hyperlink{format_8cpp_a1184311487e3908403328f6c3fa443d7}{00024}} \textcolor{keyword}{auto} \mbox{\hyperlink{format_8cpp_a1184311487e3908403328f6c3fa443d7}{test\_count\_digits\_range}}(\mbox{\hyperlink{classkblib_1_1range__t}{kblib::range\_t}} r) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00025}00025 \textcolor{comment}{// std::clog << kblib::concat("{}["{}, *r.begin(), "{}, "{}, *r.end(), "{}) / "{},}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00026}00026 \textcolor{comment}{// r.begin().step, "{}\(\backslash\)n"{});}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00027}00027 \textcolor{keywordflow}{for} (\textcolor{keyword}{auto} v : r) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00028}00028 \textcolor{comment}{// std::clog << v << ' ';}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00029}00029 \mbox{\hyperlink{format_8cpp_a4ec0059820fbfe1b795098072560c292}{REQUIRE\_DIGITS}}(v);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00030}00030 \textcolor{keywordflow}{for} (\textcolor{keyword}{auto} d : \mbox{\hyperlink{namespacekblib_a72690688922a2d4ae9a3e46ff0c1a9a9}{kblib::range}}(1, 100)) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00031}00031 \mbox{\hyperlink{format_8cpp_a4ec0059820fbfe1b795098072560c292}{REQUIRE\_DIGITS}}(v + v / d);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00032}00032 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00033}00033 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00034}00034 \textcolor{comment}{// std::clog << '\(\backslash\)n';}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00035}00035 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00036}00036 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00037}\mbox{\hyperlink{format_8cpp_a9e9d92361fdab81c2684af3689370896}{00037}} \mbox{\hyperlink{format_8cpp_a9e9d92361fdab81c2684af3689370896}{TEST\_CASE}}(\textcolor{stringliteral}{"{}count\_digits"{}}) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00038}00038 \textcolor{keyword}{using namespace }std::literals;} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00039}00039 SECTION(\textcolor{stringliteral}{"{}signed"{}}) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00040}00040 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(0) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00041}00041 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(1) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00042}00042 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(3) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00043}00043 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(10) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00044}00044 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(42) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00045}00045 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(99) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00046}00046 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(-\/1) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00047}00047 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(\mbox{\hyperlink{namespacekblib_1_1nums_a64067ecc4167ce1cfe32860ef5c1dc47}{std::numeric\_limits::max}}())} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00048}00048 == \textcolor{stringliteral}{"{}2147483647"{}}sv.length());} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00049}00049 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(\mbox{\hyperlink{namespacekblib_1_1nums_a24555c4b07301e176bf192adf4e80499}{std::numeric\_limits::min}}())} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00050}00050 == \textcolor{stringliteral}{"{}-\/2147483648"{}}sv.length());} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00051}00051 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00052}00052 \textcolor{keywordflow}{for} (\textcolor{keyword}{auto} i : \mbox{\hyperlink{namespacekblib_a72690688922a2d4ae9a3e46ff0c1a9a9}{kblib::range}}(std::numeric\_limits::digits -\/ 1)) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00053}00053 \textcolor{keyword}{auto} v = 1 << i;} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00054}00054 \textcolor{comment}{// std::clog << v << "{}(1<<"{} << i << "{}) "{};}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00055}00055 \mbox{\hyperlink{format_8cpp_a4ec0059820fbfe1b795098072560c292}{REQUIRE\_DIGITS}}(v);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00056}00056 \textcolor{keywordflow}{for} (\textcolor{keyword}{auto} d : \mbox{\hyperlink{namespacekblib_a72690688922a2d4ae9a3e46ff0c1a9a9}{kblib::range}}(1, 100)) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00057}00057 \mbox{\hyperlink{format_8cpp_a4ec0059820fbfe1b795098072560c292}{REQUIRE\_DIGITS}}(v + v / d);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00058}00058 REQUIRE(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(v + v / d)} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00059}00059 == \mbox{\hyperlink{namespacekblib_a1909a64960663825ccc4eb98e4637df2}{kblib::to\_signed}}(\mbox{\hyperlink{namespacekblib_a9d7e23caee89855a71fb84c7f245f977}{std::to\_string}}(v + v / d).\mbox{\hyperlink{namespacekblib_a793fd30664717fe1da593bf53e895e9d}{length}}()));} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00060}00060 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00061}00061 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00062}00062 \textcolor{comment}{// std::clog << '\(\backslash\)n';}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00063}00063 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00064}00064 \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00065}00065 std::int32\_t tens = 10;} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00066}00066 \textcolor{keywordflow}{for} (\textcolor{keyword}{auto} prime : \{7, 71, 701, 7001, 70'001, 700'001, 7'000'003\}) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00067}00067 \mbox{\hyperlink{format_8cpp_a1184311487e3908403328f6c3fa443d7}{test\_count\_digits\_range}}(\mbox{\hyperlink{namespacekblib_a72690688922a2d4ae9a3e46ff0c1a9a9}{kblib::range}}(tens, tens * 100, prime));} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00068}00068 tens *= 10;} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00069}00069 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00070}00070 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00071}00071 \textcolor{keywordflow}{for} (\textcolor{keyword}{auto} v : \mbox{\hyperlink{namespacekblib_a72690688922a2d4ae9a3e46ff0c1a9a9}{kblib::range}}(\mbox{\hyperlink{namespacekblib_1_1nums_a64067ecc4167ce1cfe32860ef5c1dc47}{std::numeric\_limits::max}}() -\/ 1000000,} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00072}00072 \mbox{\hyperlink{namespacekblib_1_1nums_a64067ecc4167ce1cfe32860ef5c1dc47}{std::numeric\_limits::max}}() -\/ 541, 541)) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00073}00073 \textcolor{comment}{// std::clog << v << ' ';}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00074}00074 \mbox{\hyperlink{format_8cpp_a4ec0059820fbfe1b795098072560c292}{REQUIRE\_DIGITS}}(v);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00075}00075 REQUIRE(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(v)} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00076}00076 == \mbox{\hyperlink{namespacekblib_a1909a64960663825ccc4eb98e4637df2}{kblib::to\_signed}}(\mbox{\hyperlink{namespacekblib_a9d7e23caee89855a71fb84c7f245f977}{std::to\_string}}(v).\mbox{\hyperlink{namespacekblib_a793fd30664717fe1da593bf53e895e9d}{length}}()));} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00077}00077 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00078}00078 \textcolor{comment}{// std::clog << '\(\backslash\)n';}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00079}00079 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00080}00080 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00081}00081 SECTION(\textcolor{stringliteral}{"{}unsigned"{}}) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00082}00082 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(0u) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00083}00083 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(1u) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00084}00084 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(3u) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00085}00085 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(10u) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00086}00086 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(42u) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00087}00087 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(99u) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00088}00088 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(std::uint16\_t(-\/1)) == \textcolor{stringliteral}{"{}65535"{}}sv.length());} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00089}00089 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(std::uint32\_t(-\/1)) == \textcolor{stringliteral}{"{}4294967295"{}}sv.length());} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00090}00090 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(std::uint64\_t(-\/1))} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00091}00091 == \textcolor{stringliteral}{"{}18446744073709551615"{}}sv.length());} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00092}00092 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00093}00093 } \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00094}00094 SECTION(\textcolor{stringliteral}{"{}floating-\/point"{}}) \{} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00095}00095 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(1.0) == 1);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00096}00096 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(10.0) == 2);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00097}00097 CHECK(\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{kblib::count\_digits}}(10.1) == 4);} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00098}00098 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00099}00099 \}} \DoxyCodeLine{\Hypertarget{format_8cpp_source_l00100}00100 \textcolor{preprocessor}{\#endif}} \end{DoxyCode}