1 #include <boost/python.hpp>
2 #include <boost/python/slice.hpp>
3 #include <boost/python/str.hpp>
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)
11 using namespace boost::python
;
13 #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
14 # define make_tuple boost::python::make_tuple
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()
21 object
s("hello, world");
24 if (s
[slice()] != "hello, world")
28 if (s
[slice(_
,_
,-1)] != "dlrow ,olleh")
31 // reverse with mixed-sign offsets
32 if (s
[slice(-6,1,-1)] != " ,oll")
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")
39 if (s
[slice(-3,_
)] != "rld")
41 if (", " != s
[slice(5,7)])
44 return s
[slice(2,-1)][slice(1,-1)] == "lo, wor";
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); \
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
)
60 array::set_module_and_type(module_name
, array_type_name
);
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),
76 // The following comments represent equivalent Python expressions used
77 // to validate the array behavior.
78 // original[::] == original
79 ASSERT_EQUAL(original
[slice()],original
);
81 // original[:2,:2] == array( [[11, 12], [21, 22]])
82 ASSERT_EQUAL(original
[make_tuple(slice(_
,2), slice(_
,2))],upper_left_quadrant
);
84 // original[::2,::2] == array( [[11, 13], [31, 33]])
85 ASSERT_EQUAL(original
[make_tuple( slice(_
,_
,2), slice(_
,_
,2))],odd_cells
);
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
);
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
);
96 // Verify functions accepting a slice argument can be called
97 bool accept_slice( slice
) { return true; }
99 #if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1400)) \
100 || BOOST_WORKAROUND( BOOST_INTEL_WIN, == 710)
101 int check_slice_get_indices(slice index
);
103 int check_slice_get_indices(
104 #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
109 // A vector of integers from [-5, 5].
110 std::vector
<int> coll(11);
111 typedef std::vector
<int>::iterator coll_iterator
;
113 for (coll_iterator i
= coll
.begin(); i
!= coll
.end(); ++i
) {
114 *i
= i
- coll
.begin() - 5;
117 slice::range
<std::vector
<int>::iterator
> bounds
;
119 bounds
= index
.get_indices(coll
.begin(), coll
.end());
121 catch (std::invalid_argument
) {
125 while (bounds
.start
!= bounds
.stop
) {
126 sum
+= *bounds
.start
;
127 std::advance( bounds
.start
, bounds
.step
);
129 sum
+= *bounds
.start
;
134 BOOST_PYTHON_MODULE(slice_ext
)
136 def( "accept_slice", accept_slice
);
137 def( "check_numeric_array_rich_slice", check_numeric_array_rich_slice
);
138 def( "check_string_rich_slice", check_string_rich_slice
);
139 def( "check_slice_get_indices", check_slice_get_indices
);