aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/cxx11.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-04-02 23:14:10 +0200
committerJay Berkenbilt <ejb@ql.org>2022-04-04 14:10:40 +0200
commit12f1eb15ca3fed6310402847559a7c99d3c77847 (patch)
tree8935675b623c6f3b4914b8b44f7fa5f2816a9241 /libtests/cxx11.cc
parentf20fa61eb4c323eb1642c69c236b3d9a1f8b2cdb (diff)
downloadqpdf-12f1eb15ca3fed6310402847559a7c99d3c77847.tar.zst
Programmatically apply new formatting to code
Run this: for i in **/*.cc **/*.c **/*.h **/*.hh; do clang-format < $i >| $i.new && mv $i.new $i done
Diffstat (limited to 'libtests/cxx11.cc')
-rw-r--r--libtests/cxx11.cc167
1 files changed, 92 insertions, 75 deletions
diff --git a/libtests/cxx11.cc b/libtests/cxx11.cc
index 5ad3e6ed..fbf3cc38 100644
--- a/libtests/cxx11.cc
+++ b/libtests/cxx11.cc
@@ -1,13 +1,13 @@
-#include <iostream>
-#include <cstring>
+#include <cstdint>
#include <cstdlib>
+#include <cstring>
#include <functional>
-#include <type_traits>
-#include <cstdint>
-#include <vector>
+#include <iostream>
#include <map>
#include <memory>
#include <regex>
+#include <type_traits>
+#include <vector>
#ifdef NDEBUG
// We need assert even in a release build for test code.
@@ -18,29 +18,31 @@
// Functional programming
// Function that returns a callable in the form of a lambda
-std::function<int (int)> make_adder(int x)
+std::function<int(int)>
+make_adder(int x)
{
- return ([=](int a) -> int{ return x + a; });
+ return ([=](int a) -> int { return x + a; });
}
-void do_functional()
+void
+do_functional()
{
// Lambda with no capture
- auto simple_lambda = [](int a){ return a + 3; };
+ auto simple_lambda = [](int a) { return a + 3; };
assert(simple_lambda(5) == 8);
// Capture by value
int x = 5;
- auto by_value = [x](int a){ return a + x; };
+ auto by_value = [x](int a) { return a + x; };
assert(by_value(1) == 6);
x = 7; // change not seen by lambda
assert(by_value(1) == 6);
// Also >> at end of template
- assert((std::is_convertible<decltype(by_value),
- std::function<int(int)>>::value));
+ assert((std::is_convertible<decltype(by_value), std::function<int(int)>>::
+ value));
// Capture by reference
- auto by_reference = [&x](int a){ return a + x; };
+ auto by_reference = [&x](int a) { return a + x; };
assert(by_reference(1) == 8);
x = 8; // change seen my lambda
assert(by_reference(1) == 9);
@@ -49,29 +51,29 @@ void do_functional()
auto add3 = make_adder(3);
assert(add3(5) == 8);
- auto make_addr_lambda = [](int a) {
- return [a](int b) { return a + b; };
- };
+ auto make_addr_lambda = [](int a) { return [a](int b) { return a + b; }; };
assert(make_addr_lambda(6)(8) == 14);
// nullptr and {} are empty functions
std::function<void()> f1 = {};
- assert(! f1);
+ assert(!f1);
std::function<void()> f2 = nullptr;
- assert(! f2);
+ assert(!f2);
}
// Integer types, type traits
template <typename T>
-void check_size(size_t size, bool is_signed)
+void
+check_size(size_t size, bool is_signed)
{
assert(sizeof(T) == size);
assert(std::is_signed<T>::value == is_signed);
}
-void do_inttypes()
+void
+do_inttypes()
{
// static_assert is a compile-time check
static_assert(1 == sizeof(int8_t), "int8_t check");
@@ -99,10 +101,12 @@ class A
{
}
// Constructor delegation
- A() : A(def_value)
+ A() :
+ A(def_value)
{
}
- int getX() const
+ int
+ getX() const
{
return x;
}
@@ -111,66 +115,71 @@ class A
int x;
};
-void do_iteration()
+void
+do_iteration()
{
// Initializers, foreach syntax, auto for iterators
- std::vector<int> v = { 1, 2, 3, 4 };
+ std::vector<int> v = {1, 2, 3, 4};
assert(v.size() == 4);
int sum = 0;
- for (auto& i: v)
- {
+ for (auto& i : v) {
sum += i;
}
assert(10 == sum);
- for (auto i = v.begin(); i != v.end(); ++i)
- {
+ for (auto i = v.begin(); i != v.end(); ++i) {
sum += *i;
}
assert(20 == sum);
- std::vector<A> v2 = { A(), A(3) };
+ std::vector<A> v2 = {A(), A(3)};
assert(5 == v2.at(0).getX());
assert(3 == v2.at(1).getX());
}
// Variadic template
-template<class A1>
-void variadic1(A1 const& a1)
+template <class A1>
+void
+variadic1(A1 const& a1)
{
assert(a1 == 12);
}
-template<class A1, class A2>
-void variadic1(A1 const& a1, A2 const& a2)
+template <class A1, class A2>
+void
+variadic1(A1 const& a1, A2 const& a2)
{
assert(a1 == a2);
}
-template<class ...Args>
-void variadic(Args... args)
+template <class... Args>
+void
+variadic(Args... args)
{
variadic1(args...);
}
-template<class A>
-bool pairwise_equal(A const& a, A const& b)
+template <class A>
+bool
+pairwise_equal(A const& a, A const& b)
{
return (a == b);
}
-template<class T, class ...Rest>
-bool pairwise_equal(T const& a, T const& b, Rest... rest)
+template <class T, class... Rest>
+bool
+pairwise_equal(T const& a, T const& b, Rest... rest)
{
return pairwise_equal(a, b) && pairwise_equal(rest...);
}
-void do_variadic()
+void
+do_variadic()
{
variadic(15, 15);
variadic(12);
assert(pairwise_equal(5, 5, 2.0, 2.0, std::string("a"), std::string("a")));
- assert(! pairwise_equal(5, 5, 2.0, 3.0));
+ assert(!pairwise_equal(5, 5, 2.0, 3.0));
}
// deleted, default
@@ -182,10 +191,12 @@ class B
x(x)
{
}
- B() : B(5)
+ B() :
+ B(5)
{
}
- int getX() const
+ int
+ getX() const
{
return x;
}
@@ -198,12 +209,13 @@ class B
int x;
};
-void do_default_deleted()
+void
+do_default_deleted()
{
B b1;
assert(5 == b1.getX());
assert(std::is_copy_constructible<A>::value);
- assert(! std::is_copy_constructible<B>::value);
+ assert(!std::is_copy_constructible<B>::value);
}
// smart pointers
@@ -220,38 +232,40 @@ class C
{
decr(id);
}
- C(C const& rhs) : C(rhs.id)
+ C(C const& rhs) :
+ C(rhs.id)
{
}
- C& operator=(C const& rhs)
+ C&
+ operator=(C const& rhs)
{
- if (&rhs != this)
- {
+ if (&rhs != this) {
decr(id);
id = rhs.id;
incr(id);
}
return *this;
}
- static void check(size_t size, int v, int count)
+ static void
+ check(size_t size, int v, int count)
{
assert(m.size() == size);
auto p = m.find(v);
- if (p != m.end())
- {
+ if (p != m.end()) {
assert(p->second == count);
}
}
private:
- void incr(int i)
+ void
+ incr(int i)
{
++m[i];
}
- void decr(int i)
+ void
+ decr(int i)
{
- if (--m[i] == 0)
- {
+ if (--m[i] == 0) {
m.erase(i);
}
}
@@ -262,29 +276,30 @@ class C
std::map<int, int> C::m;
-std::shared_ptr<C> make_c(int id)
+std::shared_ptr<C>
+make_c(int id)
{
return std::make_shared<C>(id);
}
-std::shared_ptr<C> make_c_array(std::vector<int> const& is)
+std::shared_ptr<C>
+make_c_array(std::vector<int> const& is)
{
auto p = std::shared_ptr<C>(new C[is.size()], std::default_delete<C[]>());
C* pp = p.get();
- for (size_t i = 0; i < is.size(); ++i)
- {
+ for (size_t i = 0; i < is.size(); ++i) {
pp[i] = C(is.at(i));
}
return p;
}
-void do_smart_pointers()
+void
+do_smart_pointers()
{
auto p1 = make_c(1);
C::check(1, 1, 1);
auto p2 = make_c_array({2, 3, 4, 5});
- for (auto i: {1, 2, 3, 4, 5})
- {
+ for (auto i : {1, 2, 3, 4, 5}) {
C::check(5, i, 1);
}
{
@@ -306,7 +321,8 @@ void do_smart_pointers()
// Regular expressions
-void do_regex()
+void
+do_regex()
{
// Basic match/search. Match matches whole string; search searches
// within string. Use std::smatch for matching std::string and
@@ -320,9 +336,9 @@ void do_regex()
std::string str4("234");
std::string str5("234 five 678");
std::smatch match1;
- assert(! std::regex_match(str1, match1, expr1));
- assert(! std::regex_match(str2, match1, expr1));
- assert(! std::regex_match(str3, match1, expr1));
+ assert(!std::regex_match(str1, match1, expr1));
+ assert(!std::regex_match(str2, match1, expr1));
+ assert(!std::regex_match(str3, match1, expr1));
assert(std::regex_match(str4, match1, expr1));
assert(match1[0].first == match1[1].first);
assert(match1[0].second == match1[1].second);
@@ -347,8 +363,7 @@ void do_regex()
std::sregex_iterator m1(str6.begin(), str6.end(), expr3);
std::sregex_iterator m2;
s.clear();
- for (std::sregex_iterator iter = m1; iter != m2; ++iter)
- {
+ for (std::sregex_iterator iter = m1; iter != m2; ++iter) {
std::smatch const& match2 = *iter;
s += match2[0].str() + "|";
}
@@ -360,24 +375,25 @@ void do_regex()
std::cregex_iterator m3(str7, str7 + std::strlen(str7), expr4);
assert("asdf" == (*m3)[0].str());
assert((*m3)[1].matched);
- assert(! (*m3)[2].matched);
+ assert(!(*m3)[2].matched);
++m3;
assert("qwer" == (*m3)[0].str());
- assert(! (*m3)[1].matched);
+ assert(!(*m3)[1].matched);
assert((*m3)[2].matched);
}
-static long operator ""_x(char const* v)
+static long operator""_x(char const* v)
{
return strtol(v, nullptr, 16);
}
-static std::string operator ""_y(char const* v, size_t len)
+static std::string operator""_y(char const* v, size_t len)
{
return "y" + std::string(v, len) + "y";
}
-void do_user_defined_literals()
+void
+do_user_defined_literals()
{
assert(10_x == 16); // operator""_x("10")
assert("abc"_y == "yabcy"); // operator""_y("abc", 3)
@@ -390,7 +406,8 @@ void do_user_defined_literals()
// assert(R"x(a)"bc)x"_y == "ya)\"bcy");
}
-int main()
+int
+main()
{
do_functional();
do_inttypes();