aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/CPlusPlusEleven.cpp
blob: fb3e765f1cec7c90b3ac10972e16b175b865cf6a (plain)
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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkScopeExit.h"
#include "SkTypes.h"
#include "Test.h"

#include <utility>

namespace {
class Moveable {
public:
    Moveable() {}
    Moveable(Moveable&&) {}
    Moveable& operator=(Moveable&&) { return *this; }
private:
    Moveable(const Moveable&);
    Moveable& operator=(const Moveable&);
};
template <typename T> void deleter(T*) { }
template <typename T> struct Deleter {
    void operator()(T* t) { delete static_cast<const Moveable*>(t); }
};
} // namespace

DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) {
    Moveable src1; Moveable dst1(std::move(src1));
    Moveable src2, dst2; dst2 = std::move(src2);
}

DEF_TEST(CPlusPlusEleven_constexpr, r) {
    static constexpr int x = Sk32ToBool(50);
    REPORTER_ASSERT(r, x == 1);
    static constexpr int y = SkTPin<int>(100, 0, 10);
    REPORTER_ASSERT(r, y == 10);
}

namespace {
struct MoveableCopyable {
    bool fCopied;
    MoveableCopyable() : fCopied(false) {}
    MoveableCopyable(const MoveableCopyable &o) : fCopied(true) {}
    MoveableCopyable(MoveableCopyable &&o) : fCopied(o.fCopied) {}
};
struct TestClass {
    MoveableCopyable fFoo;
};
}  // namespace

DEF_TEST(CPlusPlusEleven_default_move, r) {
    TestClass a;
    TestClass b(a);
    TestClass c(std::move(a));
    REPORTER_ASSERT(r, b.fFoo.fCopied);
    REPORTER_ASSERT(r, !c.fFoo.fCopied);
}

DEF_TEST(SkAtScopeExit, r) {
    int x = 5;
    {
        SK_AT_SCOPE_EXIT(x--);
        REPORTER_ASSERT(r, x == 5);
    }
    REPORTER_ASSERT(r, x == 4);
}