cpp_stl_list.pyx 2.81 KB
Newer Older
Alex Huszagh's avatar
Alex Huszagh committed
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
# mode: run
# tag: cpp, werror

from cython.operator cimport dereference as deref
from cython.operator cimport preincrement as incr

from libcpp.list cimport list as cpp_list
from libcpp cimport bool as cbool


def simple_test(double x):
    """
    >>> simple_test(55)
    3
    """
    l = new cpp_list[double]()
    try:
        l.push_back(1.0)
        l.push_back(x)
        from math import pi
        l.push_back(pi)
        return l.size()
    finally:
        del l

def pylist_test(L):
    """
    >>> pylist_test([1,2,4,8])
    (4, 4)
    >>> pylist_test([])
    (0, 0)
    >>> pylist_test([-1] * 1000)
    (1000, 1000)
    """
    l = new cpp_list[int]()
    try:
        for a in L:
            l.push_back(a)
        return len(L), l.size()
    finally:
        del l


def iteration_test(L):
    """
    >>> iteration_test([1,2,4,8])
    1
    2
    4
    8
    """
    l = new cpp_list[int]()
    try:
        for a in L:
            l.push_back(a)
        it = l.begin()
        while it != l.end():
            a = deref(it)
            incr(it)
            print(a)
    finally:
        del l

def reverse_iteration_test(L):
    """
    >>> reverse_iteration_test([1,2,4,8])
    8
    4
    2
    1
    """
    l = new cpp_list[int]()
    try:
        for a in L:
            l.push_back(a)
        it = l.rbegin()
        while it != l.rend():
            a = deref(it)
            incr(it)
            print(a)
    finally:
        del l

def nogil_test(L):
    """
    >>> nogil_test([1,2,3])
    3
    """
    cdef int a
    with nogil:
        l = new cpp_list[int]()
    try:
        for a in L:
            with nogil:
                l.push_back(a)
        return l.size()
    finally:
        del l


cdef list to_pylist(cpp_list[int]& l):
    cdef list L = []
    it = l.begin()
    while it != l.end():
        L.append(deref(it))
        incr(it)
    return L


def item_ptr_test(L, int x):
    """
    >>> item_ptr_test(range(10), 100)
    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    """
    cdef cpp_list[int] l = L
    cdef int* li_ptr = &l.front()
    li_ptr[0] = x
    return to_pylist(l)

def test_value_type(x):
    """
    >>> test_value_type(2)
    2.0
    >>> test_value_type(2.5)
    2.5
    """
    cdef cpp_list[double].value_type val = x
    return val

def test_value_type_complex(x):
    """
    >>> test_value_type_complex(2)
    (2+0j)
    """
    cdef cpp_list[double complex].value_type val = x
    return val

def test_insert():
    """
    >>> test_insert()
    """
    cdef cpp_list[int] l
    cdef cpp_list[int].size_type count = 5
    cdef int value = 0

    l.insert(l.end(), count, value)

    assert l.size() == count
    for element in l:
        assert element == value, '%s != %s' % (element, count)


#  Tests GitHub issue #1788.
cdef cppclass MyList[T](cpp_list):
    pass

cdef cppclass Ints(MyList[int]):
    pass