kblib 0.2.3
General utilities library for modern C++
format.cpp
Go to the documentation of this file.
1#include "catch.hpp"
2
3#include "kblib/format.h"
4#include "kblib/iterators.h"
5#include "kblib/stringops.h"
6
7#include <iostream>
8#include <string_view>
9
10#if KBLIB_USE_CXX17
11template <typename T>
13 return Catch::Predicate<T>(
14 [](T v_) -> bool {
15 return kblib::count_digits(v_)
17 },
18 kblib::concat(v, ": digits=", std::to_string(v).length(),
19 "; calc=", kblib::count_digits(v)));
20}
21# define REQUIRE_DIGITS(v) REQUIRE_THAT((v), pred_count_digits((v)))
22
23template <typename T, typename U>
25 // std::clog << kblib::concat("[", *r.begin(), ", ", *r.end(), ") / ",
26 // r.begin().step, "\n");
27 for (auto v : r) {
28 // std::clog << v << ' ';
30 for (auto d : kblib::range(1, 100)) {
31 REQUIRE_DIGITS(v + v / d);
32 }
33 }
34 // std::clog << '\n';
35}
36
37TEST_CASE("count_digits") {
38 using namespace std::literals;
39 SECTION("signed") {
40 CHECK(kblib::count_digits(0) == 1);
41 CHECK(kblib::count_digits(1) == 1);
42 CHECK(kblib::count_digits(3) == 1);
43 CHECK(kblib::count_digits(10) == 2);
44 CHECK(kblib::count_digits(42) == 2);
45 CHECK(kblib::count_digits(99) == 2);
46 CHECK(kblib::count_digits(-1) == 2);
48 == "2147483647"sv.length());
50 == "-2147483648"sv.length());
51
52 for (auto i : kblib::range(std::numeric_limits<int>::digits - 1)) {
53 auto v = 1 << i;
54 // std::clog << v << "(1<<" << i << ") ";
56 for (auto d : kblib::range(1, 100)) {
57 REQUIRE_DIGITS(v + v / d);
58 REQUIRE(kblib::count_digits(v + v / d)
59 == kblib::to_signed(std::to_string(v + v / d).length()));
60 }
61 }
62 // std::clog << '\n';
63
64 {
65 std::int32_t tens = 10;
66 for (auto prime : {7, 71, 701, 7001, 70'001, 700'001, 7'000'003}) {
67 test_count_digits_range(kblib::range(tens, tens * 100, prime));
68 tens *= 10;
69 }
70 }
71 for (auto v : kblib::range(std::numeric_limits<int>::max() - 1000000,
72 std::numeric_limits<int>::max() - 541, 541)) {
73 // std::clog << v << ' ';
75 REQUIRE(kblib::count_digits(v)
77 }
78 // std::clog << '\n';
79 }
80
81 SECTION("unsigned") {
82 CHECK(kblib::count_digits(0u) == 1);
83 CHECK(kblib::count_digits(1u) == 1);
84 CHECK(kblib::count_digits(3u) == 1);
85 CHECK(kblib::count_digits(10u) == 2);
86 CHECK(kblib::count_digits(42u) == 2);
87 CHECK(kblib::count_digits(99u) == 2);
88 CHECK(kblib::count_digits(std::uint16_t(-1)) == "65535"sv.length());
89 CHECK(kblib::count_digits(std::uint32_t(-1)) == "4294967295"sv.length());
90 CHECK(kblib::count_digits(std::uint64_t(-1))
91 == "18446744073709551615"sv.length());
92 }
93
94 SECTION("floating-point") {
95 CHECK(kblib::count_digits(1.0) == 1);
96 CHECK(kblib::count_digits(10.0) == 2);
97 CHECK(kblib::count_digits(10.1) == 4);
98 }
99}
100#endif
A range generator, similar to Python 3's range().
Definition: iterators.h:222
auto test_count_digits_range(kblib::range_t< T, U > r)
Definition: format.cpp:24
auto pred_count_digits(T v)
Definition: format.cpp:12
#define REQUIRE_DIGITS(v)
Definition: format.cpp:21
TEST_CASE("count_digits")
Definition: format.cpp:37
Contains some utilities for manipulating and querying string representations.
This file provides some iterators, ranges, iterator/range adapters, and operations that can be perfor...
constexpr struct kblib::nums::min_t min
constexpr struct kblib::nums::max_t max
constexpr auto to_signed(I x) -> std::make_signed_t< I >
Cast integral argument to corresponding signed type.
Definition: fakestd.h:593
auto concat(F &&f, S &&... ins) -> string
Returns a string consisting of the concatenation of all arguments.
Definition: stringops.h:305
constexpr auto range(Value min, Value max, Delta step=0) -> range_t< Value, Delta >
Constructs a range from beginning, end, and step amount. The range is half-open, that is min is in th...
Definition: iterators.h:621
constexpr auto length(const CharT *str) noexcept -> std::size_t
Definition: stringops.h:234
auto to_string(Int num) -> std::string
Definition: convert.h:71
constexpr auto count_digits(Number val) -> enable_if_t< std::is_floating_point< Number >::value, int >
Calculates the number of decimal digits needed to represent a number, plus one for negative numbers.
Definition: format.h:50
Provides utilities for performing common operations on strings.