summaryrefslogtreecommitdiff
path: root/tests/run/cpp_stl_numeric_ops_cpp17.pyx
blob: e89540d35715bcd998799f640e1ad7dd7c5d2b43 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# mode: run
# tag: cpp, werror, cpp17, cppexecpolicies

from libcpp.numeric cimport (reduce, transform_reduce, inclusive_scan, 
                             exclusive_scan, transform_inclusive_scan, 
                             transform_exclusive_scan, gcd, lcm)
from libcpp.execution cimport seq
from libcpp.vector cimport vector

# 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

# Multiplies a integer with 2
cdef int multiply_with_2(int val):
    return 2*val

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

def test_reduce_with_bin_op(vector[int] v, int init):
    """
    Test reduce with a binary operation (subtraction). 
     0 - 1 = -1
    -1 - 2 = -3
    -3 - 3 = -6
    >>> test_reduce_with_bin_op([1, 2, 3], 0)
    -6
    """
    return reduce(v.begin(), v.end(), init, subtract_integers)

# def test_reduce_with_execpolicy(vector[int] v, int init):
#     """
#     Test reduce with execution policy. 
#      0 + 1 = 1
#      1 + 2 = 3
#      3 + 3 = 6
#     >>> test_reduce_with_execpolicy([1, 2, 3], 0)
#     6
#     """
#     return reduce(seq, v.begin(), v.end(), init)

def test_reduce_with_bin_op_and_execpolicy(vector[int] v, int init):
    """
    Test reduce with execution policy and a binary operation (subtraction). 
     0 - 1 = -1
    -1 - 2 = -3
    -3 - 3 = -6
    >>> test_reduce_with_bin_op_and_execpolicy([1, 2, 3], 0)
    -6
    """
    return reduce(seq, v.begin(), v.end(), init, subtract_integers)

def test_transform_reduce(vector[int] v1, vector[int] v2, int init):
    """
    Test transform_reduce
    >>> test_transform_reduce([1, 2, 3], [1, 2, 3], 0)
    14
    """
    return transform_reduce(v1.begin(), v1.end(), v2.begin(), init)

def test_transform_reduce_with_bin_red_op_and_bin_tran_op(vector[int] v1, vector[int] v2, int init):
    """
    Test transform_reduce with a binary reduce and transform operations
    >>> test_transform_reduce_with_bin_red_op_and_bin_tran_op([1, 2, 3], [1, 2, 3], 0)
    14
    """
    return transform_reduce(v1.begin(), v1.end(), v2.begin(), init, add_integers, multiply_integers)

def test_transform_reduce_with_bin_op_and_unary_op(vector[int] v1, vector[int] v2, int init):
    """
    Test transform_reduce with a binary reduction and a unary transform operation
    >>> test_transform_reduce_with_bin_op_and_unary_op([1, 2, 3], [1, 2, 3], 0)
    12
    """
    return transform_reduce(v1.begin(), v1.end(), init, add_integers, multiply_with_2)

# def test_transform_reduce_with_execpolicy(vector[int] v1, vector[int] v2, int init):
#     """
#     Test transform_reduce with a execution policy
#     >>> test_transform_reduce_with_execpolicy([1, 2, 3], [1, 2, 3], 0)
#     14
#     """
#     return transform_reduce(seq, v1.begin(), v1.end(), v2.begin(), init)

def test_transform_reduce_with_execpolicy_bin_red_op_and_bin_tran_op(vector[int] v1, vector[int] v2, int init):
    """
    Test transform_reduce with a execution policy and binary reduce and transform operations
    >>> test_transform_reduce_with_execpolicy_bin_red_op_and_bin_tran_op([1, 2, 3], [1, 2, 3], 0)
    14
    """
    return transform_reduce(seq, v1.begin(), v1.end(), v2.begin(), init, add_integers, multiply_integers)

# def test_transform_reduce_with_execpolicy_bin_op_and_unary_op(vector[int] v1, vector[int] v2, int init):
#     """
#     Test transform_reduce with a execution policy and binary reduction and a unary transform operation
#     >>> test_transform_reduce_with_execpolicy_bin_op_and_unary_op([1, 2, 3], [1, 2, 3], 0)
#     12
#     """
#     return transform_reduce(seq, v1.begin(), v1.end(), v2.begin(), init, add_integers, multiply_with_2)

def test_inclusive_scan(vector[int] v):
    """
    Test inclusive_scan
    >>> test_inclusive_scan([1, 2, 3, 4])
    [1, 3, 6, 10]
    """
    cdef vector[int] out = vector[int](v.size())
    inclusive_scan(v.begin(), v.end(), out.begin())
    return out

# def test_inclusive_scan_with_execpolicy(vector[int] v):
#     """
#     Test inclusive_scan with a execution policy
#     >>> test_inclusive_scan_with_execpolicy([1, 2, 3, 4])
#     [1, 3, 6, 10]
#     """
#     cdef vector[int] out
#     inclusive_scan(seq, v.begin(), v.end(), out.begin())
#     return out

def test_inclusive_scan_with_bin_op(vector[int] v):
    """
    Test inclusive_scan with a binary operation
    >>> test_inclusive_scan_with_bin_op([1, 2, 3, 4])
    [1, 3, 6, 10]
    """
    cdef vector[int] out = vector[int](v.size())
    inclusive_scan(v.begin(), v.end(), out.begin(), add_integers)
    return out

# def test_inclusive_scan_with_execpolicy_and_bin_op(vector[int] v):
#     """
#     Test inclusive_scan with a execution policy and a binary operation
#     >>> test_inclusive_scan_with_execpolicy_and_bin_op([1, 2, 3, 4])
#     [1, 3, 6, 10]
#     """
#     cdef vector[int] out
#     inclusive_scan(seq, v.begin(), v.end(), out.begin(), add_integers)
#     return out

def test_inclusive_scan_with_bin_op_and_init(vector[int] v, int init):
    """
    Test inclusive_scan with a binary operation and a initial value
    >>> test_inclusive_scan_with_bin_op_and_init([1, 2, 3, 4], 0)
    [1, 3, 6, 10]
    """
    cdef vector[int] out = vector[int](v.size())
    inclusive_scan(v.begin(), v.end(), out.begin(), add_integers, init)
    return out

# def test_inclusive_scan_with_execpolicy_bin_op_and_init(vector[int] v, int init):
#     """
#     Test inclusive_scan with a execution policy, a binary operation and a initial value
#     >>> test_inclusive_scan_with_execpolicy_bin_op_and_init([1, 2, 3, 4], 0)
#     [1, 3, 6, 10]
#     """
#     cdef vector[int] out = vector[int](v.size())
#     inclusive_scan(seq, v.begin(), v.end(), out.begin(), add_integers, init)
#     return out

def test_transform_inclusive_scan(vector[int] v):
    """
    Test transform inclusive_scan
    >>> test_transform_inclusive_scan([1, 2, 3, 4])
    [2, 6, 12, 20]
    """
    cdef vector[int] out = vector[int](v.size())
    transform_inclusive_scan(v.begin(), v.end(), out.begin(), add_integers, multiply_with_2)
    return out

# def test_transform_inclusive_scan_with_execpolicy(vector[int] v):
#     """
#     Test transform inclusive_scan with a execution policy
#     >>> test_transform_inclusive_scan_with_execpolicy([1, 2, 3, 4])
#     [2, 6, 12, 20 ]
#     """
#     cdef vector[int] out
#     transform_inclusive_scan(seq, v.begin(), v.end(), out.begin(), add_integers, multiply_with_2)
#     return out

def test_transform_inclusive_scan_with_init(vector[int] v, int init):
    """
    Test transform inclusive_scan with an initial value
    >>> test_transform_inclusive_scan_with_init([1, 2, 3, 4], 0)
    [2, 6, 12, 20]
    """
    cdef vector[int] out = vector[int](v.size())
    transform_inclusive_scan(v.begin(), v.end(), out.begin(), add_integers, multiply_with_2, init)
    return out

def test_transform_inclusive_scan_with_execpolicy_and_init(vector[int] v, int init):
    """
    Test transform inclusive_scan with an initial value
    >>> test_transform_inclusive_scan_with_execpolicy_and_init([1, 2, 3, 4], 0)
    [2, 6, 12, 20]
    """
    cdef vector[int] out = vector[int](v.size())
    transform_inclusive_scan(seq, v.begin(), v.end(), out.begin(), add_integers, multiply_with_2, init)
    return out

def test_exclusive_scan(vector[int] v, int init):
    """
    Test exclusive_scan
    >>> test_exclusive_scan([1, 2, 3, 4], 0)
    [0, 1, 3, 6]
    """
    cdef vector[int] out = vector[int](v.size())
    exclusive_scan(v.begin(), v.end(), out.begin(), init)
    return out

# def test_exclusive_scan_with_execpolicy(vector[int] v, int init):
#     """
#     Test exclusive_scan with a execution policy
#     >>> test_exclusive_scan_with_execpolicy([1, 2, 3, 4], 0)
#     [0, 1, 3, 6]
#     """
#     cdef vector[int] out
#     exclusive_scan(seq, v.begin(), v.end(), out.begin(), init)
#     return out

def test_exclusive_scan_with_bin_op(vector[int] v, int init):
    """
    Test exclusive_scan with a binary operation
    >>> test_exclusive_scan_with_bin_op([1, 2, 3, 4], 0)
    [0, 1, 3, 6]
    """
    cdef vector[int] out = vector[int](v.size())
    exclusive_scan(v.begin(), v.end(), out.begin(), init, add_integers)
    return out

def test_exclusive_scan_with_execpolicy_and_bin_op(vector[int] v, int init):
    """
    Test exclusive_scan with a execution policy and a binary operation
    >>> test_exclusive_scan_with_execpolicy_and_bin_op([1, 2, 3, 4], 0)
    [0, 1, 3, 6]
    """
    cdef vector[int] out = vector[int](v.size())
    exclusive_scan(seq, v.begin(), v.end(), out.begin(), init, add_integers)
    return out


def test_transform_exclusive_scan_with_execpolicy(vector[int] v, int init):
    """
    Test transform_exclusive_scan
    >>> test_transform_exclusive_scan_with_execpolicy([1, 2, 3, 4], 0)
    [0, 2, 6, 12]
    """
    cdef vector[int] out = vector[int](v.size())
    transform_exclusive_scan(seq, v.begin(), v.end(), out.begin(), init, add_integers, multiply_with_2)
    return out

def test_transform_exclusive_scan_with_execpolicy(vector[int] v, int init):
    """
    Test transform_exclusive_scan with a execution policy
    >>> test_transform_exclusive_scan_with_execpolicy([1, 2, 3, 4], 0)
    [0, 2, 6, 12]
    """
    cdef vector[int] out = vector[int](v.size())
    transform_exclusive_scan(seq, v.begin(), v.end(), out.begin(), init, add_integers, multiply_with_2)
    return out

def test_gcd(int a, int b):
    """
    Test gcd
    >>> test_gcd(12, 18)
    6
    """
    return gcd[int](a, b)

def test_lcm(int a, int b):
    """
    Test lcm
    >>> test_lcm(45, 75)
    225
    """
    return lcm[int](a, b)