Blame tests/run/cpp_stl_conversion.pyx

Packit 562c7a
# mode: run
Packit 562c7a
# tag: cpp, werror
Packit 562c7a
# distutils: extra_compile_args=-std=c++0x
Packit 562c7a
Packit 562c7a
import sys
Packit 562c7a
from libcpp.map cimport map
Packit 562c7a
from libcpp.unordered_map cimport unordered_map
Packit 562c7a
from libcpp.set cimport set as cpp_set
Packit 562c7a
from libcpp.unordered_set cimport unordered_set
Packit 562c7a
from libcpp.string cimport string
Packit 562c7a
from libcpp.pair cimport pair
Packit 562c7a
from libcpp.vector cimport vector
Packit 562c7a
from libcpp.list cimport list as cpp_list
Packit 562c7a
Packit 562c7a
py_set = set
Packit 562c7a
py_xrange = xrange
Packit 562c7a
py_unicode = unicode
Packit 562c7a
Packit 562c7a
cdef string add_strings(string a, string b):
Packit 562c7a
    return a + b
Packit 562c7a
Packit 562c7a
def normalize(bytes b):
Packit 562c7a
    if sys.version_info[0] >= 3:
Packit 562c7a
        return b.decode("ascii")
Packit 562c7a
    else:
Packit 562c7a
        return b
Packit 562c7a
Packit 562c7a
def test_string(o):
Packit 562c7a
    """
Packit 562c7a
    >>> normalize(test_string("abc".encode('ascii')))
Packit 562c7a
    'abc'
Packit 562c7a
    >>> normalize(test_string("abc\\x00def".encode('ascii')))
Packit 562c7a
    'abc\\x00def'
Packit 562c7a
    """
Packit 562c7a
    cdef string s = o
Packit 562c7a
    return s
Packit 562c7a
Packit 562c7a
def test_encode_to_string(o):
Packit 562c7a
    """
Packit 562c7a
    >>> normalize(test_encode_to_string('abc'))
Packit 562c7a
    'abc'
Packit 562c7a
    >>> normalize(test_encode_to_string('abc\\x00def'))
Packit 562c7a
    'abc\\x00def'
Packit 562c7a
    """
Packit 562c7a
    cdef string s = o.encode('ascii')
Packit 562c7a
    return s
Packit 562c7a
Packit 562c7a
def test_encode_to_string_cast(o):
Packit 562c7a
    """
Packit 562c7a
    >>> normalize(test_encode_to_string_cast('abc'))
Packit 562c7a
    'abc'
Packit 562c7a
    >>> normalize(test_encode_to_string_cast('abc\\x00def'))
Packit 562c7a
    'abc\\x00def'
Packit 562c7a
    """
Packit 562c7a
    s = <string>o.encode('ascii')
Packit 562c7a
    return s
Packit 562c7a
Packit 562c7a
def test_unicode_encode_to_string(unicode o):
Packit 562c7a
    """
Packit 562c7a
    >>> normalize(test_unicode_encode_to_string(py_unicode('abc')))
Packit 562c7a
    'abc'
Packit 562c7a
    >>> normalize(test_unicode_encode_to_string(py_unicode('abc\\x00def')))
Packit 562c7a
    'abc\\x00def'
Packit 562c7a
    """
Packit 562c7a
    cdef string s = o.encode('ascii')
Packit 562c7a
    return s
Packit 562c7a
Packit 562c7a
def test_string_call(a, b):
Packit 562c7a
    """
Packit 562c7a
    >>> normalize(test_string_call("abc".encode('ascii'), "xyz".encode('ascii')))
Packit 562c7a
    'abcxyz'
Packit 562c7a
    """
Packit 562c7a
    return add_strings(a, b)
Packit 562c7a
Packit 562c7a
def test_c_string_convert(char *c_string):
Packit 562c7a
    """
Packit 562c7a
    >>> normalize(test_c_string_convert("abc".encode('ascii')))
Packit 562c7a
    'abc'
Packit 562c7a
    """
Packit 562c7a
    cdef string s
Packit 562c7a
    with nogil:
Packit 562c7a
        s = c_string
Packit 562c7a
    return s
Packit 562c7a
Packit 562c7a
def test_int_vector(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_int_vector([1, 2, 3])
Packit 562c7a
    [1, 2, 3]
Packit 562c7a
    >>> test_int_vector((1, 10, 100))
Packit 562c7a
    [1, 10, 100]
Packit 562c7a
    >>> test_int_vector(py_xrange(1,10,2))
Packit 562c7a
    [1, 3, 5, 7, 9]
Packit 562c7a
    >>> test_int_vector([10**20])       #doctest: +ELLIPSIS
Packit 562c7a
    Traceback (most recent call last):
Packit 562c7a
    ...
Packit 562c7a
    OverflowError: ...
Packit 562c7a
    """
Packit 562c7a
    cdef vector[int] v = o
Packit 562c7a
    return v
Packit 562c7a
Packit 562c7a
def test_string_vector(s):
Packit 562c7a
    """
Packit 562c7a
    >>> list(map(normalize, test_string_vector('ab cd ef gh'.encode('ascii'))))
Packit 562c7a
    ['ab', 'cd', 'ef', 'gh']
Packit 562c7a
    """
Packit 562c7a
    cdef vector[string] cpp_strings = s.split()
Packit 562c7a
    return cpp_strings
Packit 562c7a
Packit 562c7a
cdef list convert_string_vector(vector[string] vect):
Packit 562c7a
    return vect
Packit 562c7a
Packit 562c7a
def test_string_vector_temp_funcarg(s):
Packit 562c7a
    """
Packit 562c7a
    >>> list(map(normalize, test_string_vector_temp_funcarg('ab cd ef gh'.encode('ascii'))))
Packit 562c7a
    ['ab', 'cd', 'ef', 'gh']
Packit 562c7a
    """
Packit 562c7a
    return convert_string_vector(s.split())
Packit 562c7a
Packit 562c7a
def test_double_vector(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_double_vector([1, 2, 3])
Packit 562c7a
    [1.0, 2.0, 3.0]
Packit 562c7a
    >>> test_double_vector([10**20])
Packit 562c7a
    [1e+20]
Packit 562c7a
    """
Packit 562c7a
    cdef vector[double] v = o
Packit 562c7a
    return v
Packit 562c7a
Packit 562c7a
def test_repeated_double_vector(a, b, int n):
Packit 562c7a
    """
Packit 562c7a
    >>> test_repeated_double_vector(1, 1.5, 3)
Packit 562c7a
    [1.0, 1.5, 1.0, 1.5, 1.0, 1.5]
Packit 562c7a
    """
Packit 562c7a
    cdef vector[double] v = [a, b] * n
Packit 562c7a
    return v
Packit 562c7a
Packit 562c7a
ctypedef int my_int
Packit 562c7a
Packit 562c7a
def test_typedef_vector(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_typedef_vector([1, 2, 3])
Packit 562c7a
    [1, 2, 3]
Packit 562c7a
    >>> test_typedef_vector([1, 2, 3**100])       #doctest: +ELLIPSIS
Packit 562c7a
    Traceback (most recent call last):
Packit 562c7a
    ...
Packit 562c7a
    OverflowError: ...
Packit 562c7a
    >>> test_typedef_vector([1, 2, None])       #doctest: +ELLIPSIS
Packit 562c7a
    Traceback (most recent call last):
Packit 562c7a
    ...
Packit 562c7a
    TypeError: an integer is required
Packit 562c7a
    """
Packit 562c7a
    cdef vector[my_int] v = o
Packit 562c7a
    return v
Packit 562c7a
Packit 562c7a
def test_pair(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_pair((1, 2))
Packit 562c7a
    (1, 2.0)
Packit 562c7a
    """
Packit 562c7a
    cdef pair[long, double] p = o
Packit 562c7a
    return p
Packit 562c7a
Packit 562c7a
def test_list(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_list([1, 2, 3])
Packit 562c7a
    [1, 2, 3]
Packit 562c7a
    """
Packit 562c7a
    cdef cpp_list[int] l = o
Packit 562c7a
    return l
Packit 562c7a
Packit 562c7a
def test_set(o):
Packit 562c7a
    """
Packit 562c7a
    >>> sorted(test_set([1, 2, 3]))
Packit 562c7a
    [1, 2, 3]
Packit 562c7a
    >>> sorted(test_set([1, 2, 3, 3]))
Packit 562c7a
    [1, 2, 3]
Packit 562c7a
    >>> type(test_set([])) is py_set
Packit 562c7a
    True
Packit 562c7a
    """
Packit 562c7a
    cdef cpp_set[long] s = o
Packit 562c7a
    return s
Packit 562c7a
Packit 562c7a
def test_unordered_set(o):
Packit 562c7a
   """
Packit 562c7a
   >>> sorted(test_unordered_set([1, 2, 3]))
Packit 562c7a
   [1, 2, 3]
Packit 562c7a
   >>> sorted(test_unordered_set([1, 2, 3, 3]))
Packit 562c7a
   [1, 2, 3]
Packit 562c7a
   >>> type(test_unordered_set([])) is py_set
Packit 562c7a
   True
Packit 562c7a
   """
Packit 562c7a
   cdef unordered_set[long] s = o
Packit 562c7a
   return s
Packit 562c7a
Packit 562c7a
def test_map(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_map({1: 1.0, 2: 0.5, 3: 0.25})
Packit 562c7a
    {1: 1.0, 2: 0.5, 3: 0.25}
Packit 562c7a
    """
Packit 562c7a
    cdef map[int, double] m = o
Packit 562c7a
    return m
Packit 562c7a
Packit 562c7a
def test_unordered_map(o):
Packit 562c7a
   """
Packit 562c7a
   >>> d = test_map({1: 1.0, 2: 0.5, 3: 0.25})
Packit 562c7a
   >>> sorted(d)
Packit 562c7a
   [1, 2, 3]
Packit 562c7a
   >>> (d[1], d[2], d[3])
Packit 562c7a
   (1.0, 0.5, 0.25)
Packit 562c7a
   """
Packit 562c7a
   cdef unordered_map[int, double] m = o
Packit 562c7a
   return m
Packit 562c7a
Packit 562c7a
def test_nested(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_nested({})
Packit 562c7a
    {}
Packit 562c7a
    >>> d = test_nested({(1.0, 2.0): [1, 2, 3], (1.0, 0.5): [1, 10, 100]})
Packit 562c7a
    >>> type(d) is dict or type(d)
Packit 562c7a
    True
Packit 562c7a
    >>> sorted(d)
Packit 562c7a
    [(1.0, 0.5), (1.0, 2.0)]
Packit 562c7a
    >>> d[(1.0, 0.5)]
Packit 562c7a
    [1, 10, 100]
Packit 562c7a
    >>> d[(1.0, 2.0)]
Packit 562c7a
    [1, 2, 3]
Packit 562c7a
    """
Packit 562c7a
    cdef map[pair[double, double], vector[int]] m = o
Packit 562c7a
    return m
Packit 562c7a
Packit 562c7a
cpdef enum Color:
Packit 562c7a
    RED = 0
Packit 562c7a
    GREEN
Packit 562c7a
    BLUE
Packit 562c7a
Packit 562c7a
def test_enum_map(o):
Packit 562c7a
    """
Packit 562c7a
    >>> test_enum_map({RED: GREEN})
Packit 562c7a
    {0: 1}
Packit 562c7a
    """
Packit 562c7a
    cdef map[Color, Color] m = o
Packit 562c7a
    return m