File: decomposition.cpp

package info (click to toggle)
doctest 2.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,436 kB
  • sloc: cpp: 13,546; python: 261; makefile: 30; sh: 1
file content (63 lines) | stat: -rw-r--r-- 1,535 bytes parent folder | download
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
#include <doctest/doctest.h>

class MoveOnly {
    public:
        MoveOnly(int iIn) : i(iIn) { }
        MoveOnly(MoveOnly&&) = default;
        MoveOnly(const MoveOnly&) = delete;
        MoveOnly& operator=(MoveOnly&&) = default;
        MoveOnly& operator=(const MoveOnly&) = default;
        ~MoveOnly() = default;
        // NOLINTNEXTLINE(readability-make-member-function-const)
        operator bool() const { // both non-const and const can call
            return i == 42;
        }

    private:
        int i;
};

static MoveOnly genType(bool b) {
    return { b ? 42 : 0 };
}

TEST_CASE("Move Only Type") {
    CHECK(genType(true));
    CHECK(genType(false));

    MoveOnly a{ 0 };
    CHECK(a);
}


struct int_pointer {
    int* p = nullptr;
    int_pointer() = default;

    // non-const ref used in constructor
    // we don't want to accidentally construct a dangling pointer from a temporary
    int_pointer(int& i) : p(&i) { }

    // NOLINTNEXTLINE(readability-make-member-function-const)
    explicit operator bool() { return !!p; }
    int val() const { return *p; }
};

struct int_holder {
    int i;
    bool operator==(int_pointer p) const {
        if (!p) return false;
        return i == p.val();
    }
};

DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4866) // eval order
TEST_CASE("Impl cast from non-const value") {
    int_holder h{ 8 };
    int i = 8;

    // this comparison is safe
    // int_pointer can be implicitly constructed from this int here
    CHECK(h == i);
}
DOCTEST_MSVC_SUPPRESS_WARNING_POP