Commit 17bdd442 authored by Robert Bradshaw's avatar Robert Bradshaw

C++ stl conversion tests.

parent a716b4d1
# tag: cpp
import sys
from libcpp.map cimport map
from libcpp.set cimport set as cpp_set
from libcpp.string cimport string
from libcpp.pair cimport pair
from libcpp.vector cimport vector
py_set = set
cdef string add_strings(string a, string b):
return a + b
def normalize(bytes b):
if sys.version_info[0] >= 3:
return b.decode("ascii")
else:
return b
def test_string(o):
"""
>>> normalize(test_string(b"abc"))
'abc'
>>> normalize(test_string(b"abc\\x00def"))
'abc\\x00def'
"""
cdef string s = o
return o
def test_string_call(a, b):
"""
>>> normalize(test_string_call("abc", "xyz"))
'abcxyz'
"""
return add_strings(a, b)
def test_int_vector(o):
"""
>>> test_int_vector([1, 2, 3])
[1, 2, 3]
>>> test_int_vector((1, 10, 100))
[1, 10, 100]
>>> test_int_vector([10**20])
Traceback (most recent call last):
...
OverflowError: Python int too large to convert to C long
"""
cdef vector[int] v = o
return v
def test_double_vector(o):
"""
>>> test_double_vector([1, 2, 3])
[1.0, 2.0, 3.0]
>>> test_double_vector([10**20])
[1e+20]
"""
cdef vector[double] v = o
return v
def test_pair(o):
"""
>>> test_pair((1, 2))
(1, 2.0)
"""
cdef pair[long, double] p = o
return p
def test_set(o):
"""
>>> sorted(test_set([1, 2, 3]))
[1, 2, 3]
>>> sorted(test_set([1, 2, 3, 3]))
[1, 2, 3]
>>> type(test_set([])) is py_set
True
"""
cdef cpp_set[long] s = o
return s
def test_map(o):
"""
>>> test_map({1: 1.0, 2: 0.5, 3: 0.25})
{1: 1.0, 2: 0.5, 3: 0.25}
"""
cdef map[int, double] m = o
return m
def test_nested(o):
"""
>>> test_nested({})
{}
>>> test_nested({(1.0, 2.0): [1, 2, 3], (1.0, 0.5): [1, 10, 100]})
{(1.0, 2.0): [1, 2, 3], (1.0, 0.5): [1, 10, 100]}
"""
cdef map[pair[double, double], vector[int]] m = o
return m
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment