Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/slice.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

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