aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/cxx11.cc
blob: 250e7e2b68f1b0d1b95c1cb756615aa1dd985bf0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <qpdf/assert_test.h>

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <regex>
#include <type_traits>
#include <vector>

// Functional programming

// Function that returns a callable in the form of a lambda
std::function<int(int)>
make_adder(int x)
{
    return ([=](int a) -> int { return x + a; });
}

void
do_functional()
{
    // Lambda with no capture
    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; };
    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));

    // Capture by reference
    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);

    // Get a callable from a function
    auto add3 = make_adder(3);
    assert(add3(5) == 8);

    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);
    std::function<void()> f2 = nullptr;
    assert(!f2);
}

// Integer types, type traits

template <typename T>
void
check_size(size_t size, bool is_signed)
{
    assert(sizeof(T) == size);
    assert(std::is_signed<T>::value == is_signed);
}

void
do_inttypes()
{
    // static_assert is a compile-time check
    static_assert(1 == sizeof(int8_t), "int8_t check");
    check_size<int8_t>(1, true);
    check_size<uint8_t>(1, false);
    check_size<int16_t>(2, true);
    check_size<uint16_t>(2, false);
    check_size<int32_t>(4, true);
    check_size<uint32_t>(4, false);
    check_size<int64_t>(8, true);
    check_size<uint64_t>(8, false);

    // auto, decltype
    auto x = 5LL;
    check_size<decltype(x)>(8, true);
    assert((std::is_same<long long, decltype(x)>::value));
}

class A
{
  public:
    static constexpr auto def_value = 5;
    A(int x) :
        x(x)
    {
    }
    // Constructor delegation
    A() :
        A(def_value)
    {
    }
    int
    getX() const
    {
        return x;
    }

  private:
    int x;
};

void
do_iteration()
{
    // Initializers, foreach syntax, auto for iterators
    std::vector<int> v = {1, 2, 3, 4};
    assert(v.size() == 4);
    int sum = 0;
    for (auto& i: v) {
        sum += i;
    }
    assert(10 == sum);
    for (auto i = v.begin(); i != v.end(); ++i) {
        sum += *i;
    }
    assert(20 == sum);

    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)
{
    assert(a1 == 12);
}

template <class A1, class A2>
void
variadic1(A1 const& a1, A2 const& a2)
{
    assert(a1 == a2);
}

template <class... Args>
void
variadic(Args... args)
{
    variadic1(args...);
}

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)
{
    return pairwise_equal(a, b) && pairwise_equal(rest...);
}

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));
}

// deleted, default

class B
{
  public:
    B(int x) :
        x(x)
    {
    }
    B() :
        B(5)
    {
    }
    int
    getX() const
    {
        return x;
    }

    virtual ~B() = default;
    B(B const&) = delete;
    B& operator=(B const&) = delete;

  private:
    int x;
};

void
do_default_deleted()
{
    B b1;
    assert(5 == b1.getX());
    assert(std::is_copy_constructible<A>::value);
    assert(!std::is_copy_constructible<B>::value);
}

// smart pointers

class C
{
  public:
    C(int id = 0) :
        id(id)
    {
        incr(id);
    }
    ~C()
    {
        decr(id);
    }
    C(C const& rhs) :
        C(rhs.id)
    {
    }
    C&
    operator=(C const& rhs)
    {
        if (&rhs != this) {
            decr(id);
            id = rhs.id;
            incr(id);
        }
        return *this;
    }
    static void
    check(size_t size, int v, int count)
    {
        assert(m.size() == size);
        auto p = m.find(v);
        if (p != m.end()) {
            assert(p->second == count);
        }
    }

  private:
    void
    incr(int i)
    {
        ++m[i];
    }
    void
    decr(int i)
    {
        if (--m[i] == 0) {
            m.erase(i);
        }
    }

    static std::map<int, int> m;
    int id;
};

std::map<int, int> C::m;

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)
{
    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) {
        pp[i] = C(is.at(i));
    }
    return p;
}

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}) {
        C::check(5, i, 1);
    }
    {
        C::check(5, 1, 1);
        C c3(*p1);
        C::check(5, 1, 2);
    }
    C::check(5, 1, 1);
    p1 = nullptr;
    C::check(4, 1, 0);
    p2 = nullptr;
    C::check(0, 0, 0);
    {
        std::unique_ptr<C> p3(new C(6));
        C::check(1, 6, 1);
    }
    C::check(0, 0, 0);
}

// Regular expressions

void
do_regex()
{
    // Basic match/search. Match matches whole string; search searches
    // within string. Use std::smatch for matching std::string and
    // std::cmatch for matching char const*.

    std::regex expr1("([0-9]+)");
    std::regex expr2("([0-9]+)\\s*([a-z]+)[[:space:]]*([0-9]+)");
    std::string str1("one 234 fifth 678 nine");
    std::string str2("234 five 678 nine");
    std::string str3("one 234 five 678");
    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(str4, match1, expr1));
    assert(match1[0].first == match1[1].first);
    assert(match1[0].second == match1[1].second);
    std::string s;
    s.assign(match1[1].first, match1[1].second);
    assert("234" == s);
    assert(s == match1[1].str());
    assert(std::regex_match(str5, match1, expr2));
    assert("234 five 678" == match1[0].str());
    assert("234" == match1[1].str());
    assert("five" == match1[2].str());
    assert("678" == match1[3].str());
    assert(std::regex_search(str1, match1, expr2));
    assert("234 fifth 678" == match1[0].str());
    assert("234" == match1[1].str());
    assert("fifth" == match1[2].str());
    assert("678" == match1[3].str());

    // Iterator
    std::regex expr3("[[:digit:]]+");
    std::string str6 = "asdf234erasdf9453.kgdl423asdf";
    std::sregex_iterator m1(str6.begin(), str6.end(), expr3);
    std::sregex_iterator m2;
    s.clear();
    for (std::sregex_iterator iter = m1; iter != m2; ++iter) {
        std::smatch const& match2 = *iter;
        s += match2[0].str() + "|";
    }
    assert("234|9453|423|" == s);

    // Submatches
    std::regex expr4("(?:(asdf)|(qwer))");
    char const* str7 = "0asdf1qwer2";
    std::cregex_iterator m3(str7, str7 + std::strlen(str7), expr4);
    assert("asdf" == (*m3)[0].str());
    assert((*m3)[1].matched);
    assert(!(*m3)[2].matched);
    ++m3;
    assert("qwer" == (*m3)[0].str());
    assert(!(*m3)[1].matched);
    assert((*m3)[2].matched);
}

static long operator""_x(char const* v)
{
    return strtol(v, nullptr, 16);
}

static std::string operator""_y(char const* v, size_t len)
{
    return "y" + std::string(v, len) + "y";
}

void
do_user_defined_literals()
{
    assert(10_x == 16);         // operator""_x("10")
    assert("abc"_y == "yabcy"); // operator""_y("abc", 3)
    // Raw literals. Optional matching label before and after ()
    // allows embedding something that looks like the default end
    // delimiter in the string.
    assert(R"(abc)"_y == "yabcy");

    // This construct does not work in MSVC as of version 2019.
    // assert(R"x(a)"bc)x"_y == "ya)\"bcy");
}

int
main()
{
    do_functional();
    do_inttypes();
    do_iteration();
    do_variadic();
    do_default_deleted();
    do_smart_pointers();
    do_regex();
    do_user_defined_literals();
    std::cout << "assertions passed\n";
    return 0;
}