kblib 0.2.3
General utilities library for modern C++
main.cpp
Go to the documentation of this file.
1#include "kblib/containers.h"
2#include "kblib/icu.h"
3#include "kblib/kblib.h"
4
5#include <array>
6#include <iostream>
7#include <list>
8#include <map>
9#include <set>
10#include <string_view>
11
12#include "catch.hpp"
13
14#if KBLIB_USE_CXX17
15template <class T>
16constexpr auto type_name_f() -> std::string_view {
17 using namespace std;
18 auto sz = sizeof("type_name_f") - 1;
19# ifdef __clang__
20 string_view p = __PRETTY_FUNCTION__;
21 auto begin = 25 + sz;
22 return string_view(p.data() + begin, p.size() - begin - 1);
23# elif defined(__GNUC__)
24 string_view p = __PRETTY_FUNCTION__;
25# ifdef __INTEL_COMPILER
26 auto begin = 76 + sz;
27 return string_view(p.data() + begin, p.size() - begin - 1);
28# elif __cplusplus < 201402
29 auto begin = 17 + sz;
30 return string_view(p.data() + begin, p.size() - begin - 1);
31# else
32 auto begin = 40 + sz;
33 return string_view(p.data() + begin, p.find(';', begin) - begin);
34# endif
35# elif defined(_MSC_VER)
36 string_view p = __FUNCSIG__;
37 auto begin = 75 + sz;
38 return string_view(p.data() + begin, p.size() - begin - 7);
39# endif
40}
41static_assert(type_name_f<char>() == "char");
42#endif
43
44template <typename C>
45constexpr const char type_name[] = "unknown";
46
47template <>
48KBLIB_UNUSED constexpr const char type_name<char>[] = "char";
49template <>
50KBLIB_UNUSED constexpr const char type_name<unsigned char>[] = "unsigned char";
51template <>
52KBLIB_UNUSED constexpr const char type_name<signed char>[] = "signed char";
53
54template <int depth>
56 int* p;
57 constexpr auto operator->() const noexcept -> bad_iterator<depth - 1> {
58 return {p};
59 }
60};
61
62template <>
63struct bad_iterator<0> {
64 int* p;
65 constexpr auto operator->() const noexcept -> int* { return p; }
66};
67
68constexpr auto test() noexcept -> bool {
69 int i{};
70 return &i == kblib::to_pointer(&i)
74}
75
76static_assert(test(), "");
77
78auto test(std::streamsize s) -> void { std::cout << s << '\n'; }
79void test_trie();
80
81TEST_CASE("main") {
82
83 for (unsigned b = 0; b < 62; ++b) {
84 std::uint64_t i = 1ull << b;
85 std::pair<int, int> lengths[] = {
86 {std::to_string(i).length(), kblib::count_digits(i)},
87 {std::to_string(-i).length(), kblib::count_digits(-i)},
88 };
89 for (auto test : lengths) {
90 if (test.first != test.second) {
91 std::cout << "count_digits failure at: " << i << ".\nExpected "
92 << test.first << ", got " << test.second << ".\n";
93 break;
94 }
95 }
96 }
97
98 // test_trie();
99}
Provides generic operations for containers, as well as kblib::stack.
Provides some basic interfaces to make using ICU smoother.
Includes most other headers in kblib.
KBLIB_UNUSED constexpr const char type_name< signed char >[]
Definition: main.cpp:52
KBLIB_UNUSED constexpr const char type_name< char >[]
Definition: main.cpp:48
constexpr auto type_name_f() -> std::string_view
Definition: main.cpp:16
KBLIB_UNUSED constexpr const char type_name< unsigned char >[]
Definition: main.cpp:50
constexpr auto test() noexcept -> bool
Definition: main.cpp:68
constexpr const char type_name[]
Definition: main.cpp:45
void test_trie()
TEST_CASE("main")
Definition: main.cpp:81
constexpr auto to_pointer(P &&p) noexcept -> auto *
Gets a raw pointer out of any smart pointer or iterator you might pass in, without dereferencing it o...
Definition: iterators.h:72
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
Definition: bits.h:721
constexpr auto operator->() const noexcept -> int *
Definition: main.cpp:65
constexpr auto operator->() const noexcept -> bad_iterator< depth - 1 >
Definition: main.cpp:57
int * p
Definition: main.cpp:56
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.
Definition: tdecl.h:130