summaryrefslogtreecommitdiff
path: root/tests/run/cpp_stl_algo_comparison_ops.pyx
blob: aa7cdc7e8fe888c1cad1eb7e38cb49c13bfac025 (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
# mode: run
# tag: cpp, werror, cpp17, no-cpp-locals

from libcpp cimport bool
from libcpp.algorithm cimport equal, lexicographical_compare
from libcpp.vector cimport vector

cdef bool compare(int a, int b):
    return a == b

cdef bool less_than(char a, char b):
    return a < b

def test_equal(vector[int] v1, vector[int] v2):
    """
    Test equal.

    >>> test_equal([1, 2, 3, 4], [1, 2, 3, 4])
    True
    >>> test_equal([1, 2, 3, 4], [9, 2, 3, 4])
    False
    """
    return equal(v1.begin(), v1.end(), v2.begin())

def test_equal_with_bin_pred(vector[int] v1, vector[int] v2):
    """
    Test equal with binary predicate.

    >>> test_equal_with_bin_pred([1, 2, 3, 4], [1, 2, 3, 4])
    True
    >>> test_equal_with_bin_pred([1, 2, 3, 4], [9, 2, 3, 4])
    False
    """
    return equal(v1.begin(), v1.end(), v2.begin(), compare)

def test_equal_with_second_range_and_bin_pred(vector[int] v1, vector[int] v2):
    """
    Test equal with second range and binary predicate.

    >>> test_equal_with_second_range_and_bin_pred([1, 2, 3, 4], [1, 2, 3, 4])
    True
    >>> test_equal_with_second_range_and_bin_pred([1, 2, 3, 4], [9, 2, 3, 4])
    False
    """
    return equal(v1.begin(), v1.end(), v2.begin(), v2.end(), compare)

def test_lexicographical_compare(vector[int] v1, vector[int] v2):
    """
    Test lexicographical_compare.

    >>> test_lexicographical_compare([1, 2, 3, 4], [5, 6, 7, 8])
    True
    >>> test_lexicographical_compare([1, 2, 3, 4], [1, 1, 3, 4])
    False
    """
    return lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())

def test_lexicographical_compare_with_bin_pred(vector[int] v1, vector[int] v2):
    """
    Test lexicographical_compare with binary predicate

    >>> test_lexicographical_compare_with_bin_pred([1, 2, 3, 4], [5, 6, 7, 8])
    True
    >>> test_lexicographical_compare_with_bin_pred([1, 2, 3, 4], [1, 1, 3, 4])
    False
    """
    return lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end(), less_than)