cpp_stl_cpp11.pyx 5.09 KB
Newer Older
1 2 3 4 5
# mode: run
# tag: cpp, werror, cpp11

import sys
from libcpp.unordered_map cimport unordered_map
6
from libcpp.unordered_set cimport unordered_set
7
from libcpp.vector cimport vector
Valentin Valls's avatar
Valentin Valls committed
8 9
from libcpp.queue cimport queue
from libcpp.queue cimport priority_queue
10
from libcpp.vector cimport vector
11
from libcpp.pair cimport pair
Valentin Valls's avatar
Valentin Valls committed
12
from libcpp.map cimport map
13
from libcpp.set cimport set
14
from libcpp.deque cimport deque
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

def test_vector_functionality():
    """
    >>> test_vector_functionality()
    'pass'
    """
    cdef:
        vector[int] int_vector = vector[int]()
        int* data
        const int* const_data
    int_vector.push_back(77)
    data = int_vector.data()
    const_data = int_vector.const_data()
    assert data[0] == 77
    assert const_data[0] == 77
    return "pass"


Valentin Valls's avatar
Valentin Valls committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
def test_queue_functionality():
    """
    >>> test_queue_functionality()
    'pass'
    """
    cdef:
        queue[int] int_queue = queue[int]()
        queue[int] int_queue2 = queue[int]()
    int_queue.push(77)
    int_queue.swap(int_queue2)
    assert int_queue.size() == 0
    assert int_queue2.size() == 1
    return "pass"


49 50 51 52 53 54 55 56 57 58 59 60
def test_deque_functionality():
    """
    >>> test_deque_functionality()
    'pass'
    """
    cdef:
        deque[int] int_deque = deque[int]()
    int_deque.push_back(77)
    int_deque.shrink_to_fit()
    return "pass"


Valentin Valls's avatar
Valentin Valls committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
def test_priority_queue_functionality():
    """
    >>> test_priority_queue_functionality()
    'pass'
    """
    cdef:
        priority_queue[int] int_queue = priority_queue[int]()
        priority_queue[int] int_queue2 = priority_queue[int]()
    int_queue.push(77)
    int_queue.swap(int_queue2)
    assert int_queue.size() == 0
    assert int_queue2.size() == 1
    return "pass"


76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
def test_set_functionality():
    """
    >>> test_set_functionality()
    'pass'
    """
    cdef:
        set[int] int_set
        set[int] int_set2
    int_set2.insert(77)
    int_set2.insert(66)
    int_set.insert(int_set2.const_begin(), int_set2.const_end())
    assert int_set.size() == 2
    assert int_set.erase(int_set.const_begin(), int_set.const_end()) == int_set.end()
    return "pass"


Valentin Valls's avatar
Valentin Valls committed
92 93 94 95 96 97 98 99 100 101 102 103 104
def test_map_functionality():
    """
    >>> test_map_functionality()
    'pass'
    """
    cdef:
        map[int, const void*] int_map
        const void* data
    int_map[77] = NULL
    data = int_map.const_at(77)
    return "pass"


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
def test_unordered_set_functionality():
    """
    >>> test_unordered_set_functionality()
    'pass'
    """
    cdef:
        unordered_set[int] int_set = unordered_set[int]()
        unordered_set[int] int_set2
        unordered_set[int].iterator iterator = int_set.begin()
    int_set.insert(1)
    assert int_set.size() == 1
    int_set.erase(int_set.begin(), int_set.end())
    assert int_set.size() == 0
    int_set.insert(1)
    assert int_set.erase(1) == 1 # returns number of elements erased
    assert int_set.size() == 0
    int_set.insert(1)
    iterator = int_set.find(1)
    assert int_set.erase(iterator) == int_set.end()

    int_set2.insert(3)
    int_set2.insert(5)
    int_set.insert(int_set2.begin(), int_set2.end())
    assert int_set.size() == 2

130 131 132
    if sys.platform != 'darwin':
        int_set.max_load_factor(0.5)
        assert int_set.max_load_factor() == 0.5
133 134 135 136 137 138 139 140 141
    int_set.rehash(20)
    int_set.reserve(20)

    int_set.bucket_size(0)
    int_set.bucket_count()
    int_set.max_bucket_count()
    int_set.bucket(3)
    return "pass"

142

143 144 145 146 147
cdef extern from "cpp_unordered_map_helper.h":
    cdef cppclass IntVectorHash:
        pass


148 149 150 151 152 153 154 155 156 157
def test_unordered_map_functionality():
    """
    >>> test_unordered_map_functionality()
    'pass'
    """
    cdef:
        unordered_map[int, int] int_map = unordered_map[int,int]()
        pair[int, int] pair_insert = pair[int, int](1, 2)
        unordered_map[int,int].iterator iterator = int_map.begin()
        pair[unordered_map[int,int].iterator, bint] pair_iter  = int_map.insert(pair_insert)
158
        unordered_map[int, int] int_map2
159 160
        unordered_map[int, int*] intptr_map
        const int* intptr
161 162
        unordered_map[vector[int], int, IntVectorHash] int_vector_map
        vector[int] intvec
163 164 165 166 167 168 169 170 171 172
    assert int_map[1] == 2
    assert int_map.size() == 1
    assert int_map.erase(1) == 1 # returns number of elements erased
    assert int_map.size() == 0
    int_map[1] = 2
    assert int_map.size() == 1
    assert int_map[1] == 2
    iterator = int_map.find(1)
    assert int_map.erase(iterator) == int_map.end()

173 174 175 176 177 178
    int_map2[1] = 2
    int_map2[3] = 3
    int_map.clear()
    int_map.insert(int_map2.begin(), int_map2.end())
    assert int_map.size() == 2
    assert int_map.erase(int_map.begin(), int_map.end()) == int_map.end()
179

180 181 182 183 184 185 186 187 188 189
    int_map.max_load_factor(0.5)
    assert int_map.max_load_factor() == 0.5
    int_map.rehash(20)
    int_map.reserve(20)

    int_map[3] = 3
    int_map.bucket_size(0)
    int_map.bucket_count()
    int_map.max_bucket_count()
    int_map.bucket(3)
190 191 192

    intptr_map[0] = NULL
    intptr = intptr_map.const_at(0)
193 194 195 196 197 198 199 200

    intvec = [1, 2]
    int_vector_map[intvec] = 3
    intvec = [4, 5]
    int_vector_map[intvec] = 6
    assert int_vector_map[intvec] == 6
    intvec = [1, 2]
    assert int_vector_map[intvec] == 3
201
    return "pass"