summaryrefslogtreecommitdiff
path: root/tests/run/cpp_stl_numeric_ops.pyx
blob: 52f3c58ec9ca715ced333282aff62c24bc7f267d (plain)
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
# mode: run
# tag: cpp, werror, cpp11

from libcpp.numeric cimport inner_product, iota, accumulate, adjacent_difference, partial_sum
from libcpp.vector cimport vector
from libcpp cimport bool

# Subtracts two integers.
cdef int subtract_integers(int lhs, int rhs):
    return lhs - rhs

# Adds two integers.
cdef int add_integers(int lhs, int rhs):
    return lhs + rhs

# Multiplies two integers.
cdef int multiply_integers(int lhs, int rhs):
    return lhs * rhs

# Determines equality for two integers.
# If lhs == rhs, returns true. Returns false otherwise.
cdef bool is_equal(int lhs, int rhs):
    return lhs == rhs

def test_inner_product(vector[int] v1, vector[int] v2, int init):
    """
    Test inner_product with integer values.
    >>> test_inner_product([1, 2, 3], [1, 2, 3], 1)
    15
    """
    return inner_product(v1.begin(), v1.end(), v2.begin(), init)


def test_inner_product_with_zero(vector[int] v1, vector[int] v2, int init):
    """
    Test inner_product with a zero value in the container.
    >>> test_inner_product_with_zero([1, 2, 0], [1, 1, 1], 0)
    3
    """
    return inner_product(v1.begin(), v1.end(), v2.begin(), init)

def test_inner_product_with_bin_op(vector[int] v1, vector[int] v2, int init):
    """
    Test inner_product with two binary operations. In this case,
    Looks at number of pairwise matches between v1 and v2.
    [5, 1, 2, 3, 4]
    [5, 4, 2, 3, 1]
    There are 3 matches (5, 2, 3). So, 1 + 1 + 1 = 3.

    >>> test_inner_product_with_bin_op([5, 1, 2, 3, 4], [5, 4, 2, 3, 1], 0)
    3
    """
    return inner_product(v1.begin(), v1.end(), v2.begin(), init, add_integers, is_equal)

def test_iota(vector[int] v, int value):
    """
    Test iota with beginning value of 0.
    >>> test_iota(range(6), 0)
    [0, 1, 2, 3, 4, 5]
    """
    iota(v.begin(), v.end(), value)
    return v

def test_iota_negative_init(vector[int] v, int value):
    """
    Test iota with a negative beginning value.
    >>> test_iota_negative_init(range(7), -4)
    [-4, -3, -2, -1, 0, 1, 2]
    """
    iota(v.begin(), v.end(), value)
    return v

def test_accumulate(vector[int] v, int init):
    """
    Test accumulate.
     0 + 1 = 1
     1 + 2 = 3
     3 + 3 = 6
    >>> test_accumulate([1, 2, 3], 0)
    6
    """
    return accumulate(v.begin(), v.end(), init)

def test_accumulate_with_subtraction(vector[int] v, int init):
    """
    Test accumulate with subtraction. Note that accumulate is a fold-left operation.
     0 - 1 = -1
    -1 - 2 = -3
    -3 - 3 = -6
    >>> test_accumulate_with_subtraction([1, 2, 3], 0)
    -6
    """
    return accumulate(v.begin(), v.end(), init, subtract_integers)

def test_adjacent_difference(vector[int] v):
    """
    Test adjacent_difference with integer values.
    2 - 0,   -> 2
    4 - 2,   -> 2
    6 - 4,   -> 2
    8 - 6,   -> 2
    10 - 8,  -> 2
    12 - 10  -> 2
    >>> test_adjacent_difference([2, 4, 6, 8, 10, 12])
    [2, 2, 2, 2, 2, 2]
    """
    adjacent_difference(v.begin(), v.end(), v.begin())
    return v

def test_adjacent_difference_with_bin_op(vector[int] v):
    """
    Test adjacent_difference with a binary operation.
    0 + 1 -> 1
    1 + 2 -> 3
    2 + 4 -> 6
    4 + 5 -> 9
    5 + 6 -> 11
    >>> test_adjacent_difference_with_bin_op([1, 2, 4, 5, 6])
    [1, 3, 6, 9, 11]
    """
    adjacent_difference(v.begin(), v.end(), v.begin(), add_integers)
    return v

def test_partial_sum(vector[int] v):
    """
    Test partial_sum with integer values.
    2 + 0   -> 2
    2 + 2   -> 4
    4 + 2   -> 6
    6 + 2   -> 8
    8 + 2   -> 10
    10 + 2  -> 12
    >>> test_partial_sum([2, 2, 2, 2, 2, 2])
    [2, 4, 6, 8, 10, 12]
    """
    partial_sum(v.begin(), v.end(), v.begin())
    return v

def test_partial_sum_with_bin_op(vector[int] v):
    """
    Test partial_sum with a binary operation.
    Using multiply_integers, partial_sum will calculate the first 5 powers of 2.
    >>> test_partial_sum_with_bin_op([2, 2, 2, 2, 2])
    [2, 4, 8, 16, 32]
    """
    partial_sum(v.begin(), v.end(), v.begin(), multiply_integers)
    return v