]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/python/test/slice.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / python / test / slice.cpp
CommitLineData
7c673cae
FG
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
11using namespace boost::python;
12
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
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)
19bool 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); \
53else
54
55// These tests work with Python 2.2, but you must have Numeric installed.
56object 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
97bool accept_slice( slice) { return true; }
98
99#if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1400)) \
100 || BOOST_WORKAROUND( BOOST_INTEL_WIN, == 710)
101int check_slice_get_indices(slice index);
102#endif
103int check_slice_get_indices(
104#if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
105 const
106#endif
107 slice index)
108{
109 // A vector of integers from [-5, 5].
110 std::vector<int> coll(11);
111 typedef std::vector<int>::iterator coll_iterator;
112
113 for (coll_iterator i = coll.begin(); i != coll.end(); ++i) {
114 *i = i - coll.begin() - 5;
115 }
116
117 slice::range<std::vector<int>::iterator> bounds;
118 try {
119 bounds = index.get_indices(coll.begin(), coll.end());
120 }
121 catch (std::invalid_argument) {
122 return 0;
123 }
124 int sum = 0;
125 while (bounds.start != bounds.stop) {
126 sum += *bounds.start;
127 std::advance( bounds.start, bounds.step);
128 }
129 sum += *bounds.start;
130 return sum;
131}
132
133
134BOOST_PYTHON_MODULE(slice_ext)
135{
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);
140}