aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/traits/traits_test.cpp
blob: 7e1f3a23d8aff0f4599be35179ad91ca21fd6db1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/traits.h>
#include <vespa/vespalib/util/arrayqueue.hpp>
#include <concepts>

using namespace vespalib;

struct Simple {
    int value;
    int moved;
    explicit Simple(int v) : value(v), moved(0) {}
    Simple(const Simple &rhs) : value(rhs.value), moved(rhs.moved) {}
    Simple(Simple &&rhs) : value(rhs.value), moved(rhs.moved + 1) {}
};
using Hard = std::unique_ptr<Simple>;

struct Base {
    virtual void foo() = 0;
    virtual ~Base() {}
};
struct Child1 : Base {
    void foo() override {}
};
struct Child2 : Base {
    void foo() override {}
};

VESPA_CAN_SKIP_DESTRUCTION(Child2);

TEST("require that copy ctor detection works") {
    EXPECT_EQUAL(std::copy_constructible<Simple>, true);
    EXPECT_EQUAL(std::copy_constructible<Hard>, false);
    EXPECT_EQUAL(std::copy_constructible<ArrayQueue<Simple> >, true);
    EXPECT_EQUAL(std::copy_constructible<ArrayQueue<Hard> >, false);
    EXPECT_EQUAL(std::copy_constructible<std::unique_ptr<Hard> >, false);
}

TEST("require that can_skip_destruction works") {
    EXPECT_EQUAL(can_skip_destruction<Simple>, true);
    EXPECT_EQUAL(can_skip_destruction<Hard>, false);
    EXPECT_EQUAL(can_skip_destruction<Child1>, false);
    EXPECT_EQUAL(can_skip_destruction<Child2>, true);
}

struct NoType {};
struct TypeType { using type = NoType; };
struct NoTypeType { static constexpr int type = 3; };

TEST("require that type type member can be detected") {
    EXPECT_FALSE(has_type_type<NoType>);
    EXPECT_TRUE(has_type_type<TypeType>);
    EXPECT_FALSE(has_type_type<NoTypeType>);
}

TEST_MAIN() { TEST_RUN_ALL(); }