File: downcast.cpp

package info (click to toggle)
librepcb 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 58,484 kB
  • sloc: cpp: 267,986; python: 12,100; ansic: 6,899; xml: 234; sh: 215; makefile: 115; perl: 73
file content (66 lines) | stat: -rw-r--r-- 1,508 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2016-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

#include <type_safe/downcast.hpp>

#include <catch.hpp>

using namespace type_safe;

TEST_CASE("downcast")
{
    struct base
    {
        virtual ~base() = default;
    };

    struct derived : base
    {
        ~derived() override = default;
    };

    base    b;
    derived d;

    SECTION("base -> base")
    {
        base& ref = b;

        base& res1 = downcast<base&>(ref);
        REQUIRE(&res1 == &ref);

        base& res2 = downcast(derived_type<base>{}, ref);
        REQUIRE(&res2 == &ref);
    }
    SECTION("const base -> const base")
    {
        const base& ref = b;

        const base& res1 = downcast<const base&>(ref);
        REQUIRE(&res1 == &ref);

        const base& res2 = downcast(derived_type<base>{}, ref);
        REQUIRE(&res2 == &ref);
    }
    SECTION("base -> derived")
    {
        base& ref = d;

        derived& res1 = downcast<derived&>(ref);
        REQUIRE(&res1 == &ref);

        derived& res2 = downcast(derived_type<derived>{}, ref);
        REQUIRE(&res2 == &ref);
    }
    SECTION("const base -> const derived")
    {
        const base& ref = d;

        const derived& res1 = downcast<const derived&>(ref);
        REQUIRE(&res1 == &ref);

        const derived& res2 = downcast(derived_type<derived>{}, ref);
        REQUIRE(&res2 == &ref);
    }
}