"vscode:/vscode.git/clone" did not exist on "b64294f0e81c76b50c8b8a55ba8a1a89a0773a88"
test_variant_unique_shared.py 1.49 KB
Newer Older
1
2
3
4
5
6
7
# -*- coding: utf-8 -*-
import pytest

from pybind11_tests import variant_unique_shared as m


def test_default_constructed():
8
    v = m.vptr_double()
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
    assert v.ownership_type() == 0
    assert v.get_value() == -1


def test_from_raw():
    v = m.from_raw()
    assert v.ownership_type() == 0
    assert v.get_value() == 3


def test_from_unique():
    v = m.from_unique()
    assert v.ownership_type() == 0
    assert v.get_value() == 5


def test_from_shared():
    v = m.from_shared()
    assert v.ownership_type() == 1
    assert v.get_value() == 7


def test_promotion_to_shared():
    v = m.from_raw()
    v.get_unique()
    assert v.ownership_type() == 0
    v.get_shared()  # Promotion to shared_ptr.
    assert v.ownership_type() == 1
    v.get_shared()  # Existing shared_ptr.
    with pytest.raises(RuntimeError) as exc_info:
        v.get_unique()
    assert str(exc_info.value) == "get_unique failure."
    v.get_shared()  # Still works.


def test_shared_from_birth():
    v = m.from_shared()
    assert v.ownership_type() == 1
    with pytest.raises(RuntimeError) as exc_info:
        v.get_unique()
    assert str(exc_info.value) == "get_unique failure."
    v.get_shared()  # Still works.
51
52
53
54
55
56
57
58
59
60
61


def test_promotion_of_disowned_to_shared():
    v = m.from_unique()
    assert v.get_value() == 5
    v.disown_unique()
    assert v.ownership_type() == 0
    assert v.get_value() == -1
    v.get_shared()  # Promotion of disowned to shared_ptr.
    assert v.ownership_type() == 1
    assert v.get_value() == -1