1 | #include <boost/python.hpp> |
---|
2 | #include <boost/python/slice.hpp> |
---|
3 | #include <boost/python/str.hpp> |
---|
4 | #include <vector> |
---|
5 | |
---|
6 | // Copyright (c) 2004 Jonathan Brandmeyer |
---|
7 | // Use, modification and distribution are subject to the |
---|
8 | // Boost Software License, Version 1.0. (See accompanying file |
---|
9 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
10 | |
---|
11 | using namespace boost::python; |
---|
12 | |
---|
13 | #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) |
---|
14 | # define make_tuple boost::python::make_tuple |
---|
15 | #endif |
---|
16 | |
---|
17 | // These checks are only valid under Python 2.3 |
---|
18 | // (rich slicing wasn't supported for builtins under Python 2.2) |
---|
19 | bool check_string_rich_slice() |
---|
20 | { |
---|
21 | object s("hello, world"); |
---|
22 | |
---|
23 | // default slice |
---|
24 | if (s[slice()] != "hello, world") |
---|
25 | return false; |
---|
26 | |
---|
27 | // simple reverse |
---|
28 | if (s[slice(_,_,-1)] != "dlrow ,olleh") |
---|
29 | return false; |
---|
30 | |
---|
31 | // reverse with mixed-sign offsets |
---|
32 | if (s[slice(-6,1,-1)] != " ,oll") |
---|
33 | return false; |
---|
34 | |
---|
35 | // all of the object.cpp check_string_slice() checks should work |
---|
36 | // with the form that omits the step argument. |
---|
37 | if (s[slice(_,-3)] != "hello, wo") |
---|
38 | return false; |
---|
39 | if (s[slice(-3,_)] != "rld") |
---|
40 | return false; |
---|
41 | if (", " != s[slice(5,7)]) |
---|
42 | return false; |
---|
43 | |
---|
44 | return s[slice(2,-1)][slice(1,-1)] == "lo, wor"; |
---|
45 | } |
---|
46 | |
---|
47 | // Tried to get more info into the error message (actual array |
---|
48 | // contents) but Numeric complains that treating an array as a boolean |
---|
49 | // value doesn't make any sense. |
---|
50 | #define ASSERT_EQUAL( e1, e2 ) \ |
---|
51 | if (!all((e1) == (e2))) \ |
---|
52 | return "assertion failed: " #e1 " == " #e2 "\nLHS:\n%s\nRHS:\n%s" % make_tuple(e1,e2); \ |
---|
53 | else |
---|
54 | |
---|
55 | // These tests work with Python 2.2, but you must have Numeric installed. |
---|
56 | object check_numeric_array_rich_slice( |
---|
57 | char const* module_name, char const* array_type_name, object all) |
---|
58 | { |
---|
59 | using numeric::array; |
---|
60 | array::set_module_and_type(module_name, array_type_name); |
---|
61 | |
---|
62 | array original = array( make_tuple( make_tuple( 11, 12, 13, 14), |
---|
63 | make_tuple( 21, 22, 23, 24), |
---|
64 | make_tuple( 31, 32, 33, 34), |
---|
65 | make_tuple( 41, 42, 43, 44))); |
---|
66 | array upper_left_quadrant = array( make_tuple( make_tuple( 11, 12), |
---|
67 | make_tuple( 21, 22))); |
---|
68 | array odd_cells = array( make_tuple( make_tuple( 11, 13), |
---|
69 | make_tuple( 31, 33))); |
---|
70 | array even_cells = array( make_tuple( make_tuple( 22, 24), |
---|
71 | make_tuple( 42, 44))); |
---|
72 | array lower_right_quadrant_reversed = array( |
---|
73 | make_tuple( make_tuple(44, 43), |
---|
74 | make_tuple(34, 33))); |
---|
75 | |
---|
76 | // The following comments represent equivalent Python expressions used |
---|
77 | // to validate the array behavior. |
---|
78 | // original[::] == original |
---|
79 | ASSERT_EQUAL(original[slice()],original); |
---|
80 | |
---|
81 | // original[:2,:2] == array( [[11, 12], [21, 22]]) |
---|
82 | ASSERT_EQUAL(original[make_tuple(slice(_,2), slice(_,2))],upper_left_quadrant); |
---|
83 | |
---|
84 | // original[::2,::2] == array( [[11, 13], [31, 33]]) |
---|
85 | ASSERT_EQUAL(original[make_tuple( slice(_,_,2), slice(_,_,2))],odd_cells); |
---|
86 | |
---|
87 | // original[1::2, 1::2] == array( [[22, 24], [42, 44]]) |
---|
88 | ASSERT_EQUAL(original[make_tuple( slice(1,_,2), slice(1,_,2))],even_cells); |
---|
89 | |
---|
90 | // original[:-3:-1, :-3,-1] == array( [[44, 43], [34, 33]]) |
---|
91 | ASSERT_EQUAL(original[make_tuple( slice(_,-3,-1), slice(_,-3,-1))],lower_right_quadrant_reversed); |
---|
92 | |
---|
93 | return object(1); |
---|
94 | } |
---|
95 | |
---|
96 | // Verify functions accepting a slice argument can be called |
---|
97 | bool accept_slice( slice) { return true; } |
---|
98 | |
---|
99 | #if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1400)) \ |
---|
100 | || BOOST_WORKAROUND( BOOST_INTEL_WIN, == 710) |
---|
101 | int check_slice_get_indicies(slice index); |
---|
102 | #endif |
---|
103 | int check_slice_get_indicies(const slice index) |
---|
104 | { |
---|
105 | // A vector of integers from [-5, 5]. |
---|
106 | std::vector<int> coll(11); |
---|
107 | typedef std::vector<int>::iterator coll_iterator; |
---|
108 | |
---|
109 | for (coll_iterator i = coll.begin(); i != coll.end(); ++i) { |
---|
110 | *i = i - coll.begin() - 5; |
---|
111 | } |
---|
112 | |
---|
113 | slice::range<std::vector<int>::iterator> bounds; |
---|
114 | try { |
---|
115 | bounds = index.get_indicies(coll.begin(), coll.end()); |
---|
116 | } |
---|
117 | catch (std::invalid_argument) { |
---|
118 | return 0; |
---|
119 | } |
---|
120 | int sum = 0; |
---|
121 | while (bounds.start != bounds.stop) { |
---|
122 | sum += *bounds.start; |
---|
123 | std::advance( bounds.start, bounds.step); |
---|
124 | } |
---|
125 | sum += *bounds.start; |
---|
126 | return sum; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | BOOST_PYTHON_MODULE(slice_ext) |
---|
131 | { |
---|
132 | def( "accept_slice", accept_slice); |
---|
133 | def( "check_numeric_array_rich_slice", check_numeric_array_rich_slice); |
---|
134 | def( "check_string_rich_slice", check_string_rich_slice); |
---|
135 | def( "check_slice_get_indicies", check_slice_get_indicies); |
---|
136 | } |
---|