2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 18:12:43 +00:00
Files
python/test/slice.cpp
Eric Niebler 7772b8894f Merged revisions 43810-43907 via svnmerge from
https://svn.boost.org/svn/boost/trunk

................
  r43810 | eric_niebler | 2008-03-23 14:51:40 -0700 (Sun, 23 Mar 2008) | 1 line
  
  mark up new foreach tests
................
  r43823 | fmhess | 2008-03-24 08:07:00 -0700 (Mon, 24 Mar 2008) | 3 lines
  
  Fixed compile error with new enable_shared_from_this code,
  reported by Tim Blechmann 
................
  r43825 | ramey | 2008-03-24 08:46:09 -0700 (Mon, 24 Mar 2008) | 1 line
  
  removed EXIT_SUCCESS
................
  r43826 | ramey | 2008-03-24 08:46:43 -0700 (Mon, 24 Mar 2008) | 1 line
  
  fixed minor typo
................
  r43829 | pdimov | 2008-03-24 09:00:28 -0700 (Mon, 24 Mar 2008) | 1 line
  
  New enable_shared_from_this tests, fix.
................
  r43838 | danieljames | 2008-03-24 10:03:15 -0700 (Mon, 24 Mar 2008) | 12 lines
  
  Merge new changes to unordered & hash.
  
   - Unordered tests can run lightweight test or Boost.Test (at least
     theoretically).
   - Workaround Open BSD's incorrect numeric_limits.
   - Move the hash extensions in their own file.
   - Various small improvements to the unordered docs.
   - Fix some unordered examples.
  
  Merged revisions 43117-43837 via svnmerge from 
  https://svn.boost.org/svn/boost/branches/unordered/trunk
................
  r43845 | dave | 2008-03-24 11:27:22 -0700 (Mon, 24 Mar 2008) | 1 line
  
  Work around vc9 bugs
................
  r43847 | anthonyw | 2008-03-24 14:44:36 -0700 (Mon, 24 Mar 2008) | 1 line
  
  removed forward declaration for undefined type exclusive_lock
................
  r43852 | hkaiser | 2008-03-25 06:40:53 -0700 (Tue, 25 Mar 2008) | 1 line
  
  Wave: Removed an assertion causing compilation problems on certain platforms.
................
  r43856 | pdimov | 2008-03-25 08:46:40 -0700 (Tue, 25 Mar 2008) | 1 line
  
  _internal_accept_owner now checks if _owned isn't already true.
................
  r43861 | dave | 2008-03-25 13:47:38 -0700 (Tue, 25 Mar 2008) | 2 lines
  
  Work around intel-10.0-win compiler bug
................
  r43864 | dave | 2008-03-25 15:28:59 -0700 (Tue, 25 Mar 2008) | 2 lines
  
  Account for intel 10.x compiler bug
................
  r43865 | dave | 2008-03-25 16:06:50 -0700 (Tue, 25 Mar 2008) | 2 lines
  
  Work around intel-win-10.0 preprocessor bug
................
  r43866 | danieljames | 2008-03-26 02:10:29 -0700 (Wed, 26 Mar 2008) | 3 lines
  
  Boost.Thread's documentation no longer has a build section - so just link to
  the library's documentation like we do for other libraries.
................
  r43867 | bemandawes | 2008-03-26 08:59:52 -0700 (Wed, 26 Mar 2008) | 1 line
  
  Initial commit
................
  r43873 | pdimov | 2008-03-26 11:34:29 -0700 (Wed, 26 Mar 2008) | 1 line
  
  Added "Throws: nothing" to get_deleter.
................
  r43875 | bemandawes | 2008-03-26 14:26:55 -0700 (Wed, 26 Mar 2008) | 1 line
  
  Initial commit
................
  r43876 | danieljames | 2008-03-26 22:49:45 -0700 (Wed, 26 Mar 2008) | 2 lines
  
  Update the thread links in the generated getting started documentation.
................
  r43879 | chris_kohlhoff | 2008-03-27 07:18:07 -0700 (Thu, 27 Mar 2008) | 3 lines
  
  Fix double-free error that occurs when an exception is thrown from a
  handler that has been dispatched (i.e. not posted) through a strand.
................
  r43880 | ramey | 2008-03-27 08:53:37 -0700 (Thu, 27 Mar 2008) | 1 line
  
  removed suppression of builds for certain platforms with shared libraries
................
  r43882 | ramey | 2008-03-27 10:11:23 -0700 (Thu, 27 Mar 2008) | 1 line
  
  tweaks to sneak past PGI compiler error message
................
  r43883 | ramey | 2008-03-27 10:12:22 -0700 (Thu, 27 Mar 2008) | 1 line
  
  added test to check warnings on type trait
................
  r43884 | dgregor | 2008-03-27 12:44:37 -0700 (Thu, 27 Mar 2008) | 1 line
  
  Change Boost.Function allocator behavior, from Emil Dotchevski
................
  r43887 | pdimov | 2008-03-27 15:13:55 -0700 (Thu, 27 Mar 2008) | 1 line
  
  Silence unused parameter warning.
................
  r43888 | pdimov | 2008-03-27 15:20:11 -0700 (Thu, 27 Mar 2008) | 1 line
  
  detail::yield(k) added.
................
  r43895 | danieljames | 2008-03-27 16:38:01 -0700 (Thu, 27 Mar 2008) | 25 lines
  
  Merged revisions 43838-43894 via svnmerge from 
  https://svn.boost.org/svn/boost/branches/unordered/trunk
  
  ........
    r43840 | danieljames | 2008-03-24 17:25:07 +0000 (Mon, 24 Mar 2008) | 1 line
    
    Fix a g++ warning.
  ........
    r43844 | danieljames | 2008-03-24 17:56:28 +0000 (Mon, 24 Mar 2008) | 1 line
    
    It's a new-ish year.
  ........
    r43885 | danieljames | 2008-03-27 20:36:10 +0000 (Thu, 27 Mar 2008) | 1 line
    
    The release script doesn't need to copy images and css - because that's now done in the jamfiles. Also tweak the shell script a tad bit.
  ........
    r43890 | danieljames | 2008-03-27 23:01:40 +0000 (Thu, 27 Mar 2008) | 1 line
    
    Starting to add a docbook bibliography.
  ........
    r43894 | danieljames | 2008-03-27 23:24:18 +0000 (Thu, 27 Mar 2008) | 1 line
    
    Redeclare 'data' in iterator_base to help compilers which have trouble with accessing the nested typedef.
  ........
................
  r43900 | noel_belcourt | 2008-03-27 19:10:04 -0700 (Thu, 27 Mar 2008) | 4 lines
  
  Fix address-model support for 32/64 bit code generation.
  Replaced -mcmodel with -m32 / -m64.
................
  r43901 | bemandawes | 2008-03-27 19:11:13 -0700 (Thu, 27 Mar 2008) | 1 line
  
  Remove per email from Dave
................
  r43906 | eric_niebler | 2008-03-27 23:10:55 -0700 (Thu, 27 Mar 2008) | 1 line
  
  proto support for BOOST_PROTO_MAX_FUNCTION_CALL_ARITY
................


[SVN r43910]
2008-03-28 14:52:50 +00:00

141 lines
4.8 KiB
C++

#include <boost/python.hpp>
#include <boost/python/slice.hpp>
#include <boost/python/str.hpp>
#include <vector>
// Copyright (c) 2004 Jonathan Brandmeyer
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
using namespace boost::python;
#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
# define make_tuple boost::python::make_tuple
#endif
// These checks are only valid under Python 2.3
// (rich slicing wasn't supported for builtins under Python 2.2)
bool check_string_rich_slice()
{
object s("hello, world");
// default slice
if (s[slice()] != "hello, world")
return false;
// simple reverse
if (s[slice(_,_,-1)] != "dlrow ,olleh")
return false;
// reverse with mixed-sign offsets
if (s[slice(-6,1,-1)] != " ,oll")
return false;
// all of the object.cpp check_string_slice() checks should work
// with the form that omits the step argument.
if (s[slice(_,-3)] != "hello, wo")
return false;
if (s[slice(-3,_)] != "rld")
return false;
if (", " != s[slice(5,7)])
return false;
return s[slice(2,-1)][slice(1,-1)] == "lo, wor";
}
// Tried to get more info into the error message (actual array
// contents) but Numeric complains that treating an array as a boolean
// value doesn't make any sense.
#define ASSERT_EQUAL( e1, e2 ) \
if (!all((e1) == (e2))) \
return "assertion failed: " #e1 " == " #e2 "\nLHS:\n%s\nRHS:\n%s" % make_tuple(e1,e2); \
else
// These tests work with Python 2.2, but you must have Numeric installed.
object check_numeric_array_rich_slice(
char const* module_name, char const* array_type_name, object all)
{
using numeric::array;
array::set_module_and_type(module_name, array_type_name);
array original = array( make_tuple( make_tuple( 11, 12, 13, 14),
make_tuple( 21, 22, 23, 24),
make_tuple( 31, 32, 33, 34),
make_tuple( 41, 42, 43, 44)));
array upper_left_quadrant = array( make_tuple( make_tuple( 11, 12),
make_tuple( 21, 22)));
array odd_cells = array( make_tuple( make_tuple( 11, 13),
make_tuple( 31, 33)));
array even_cells = array( make_tuple( make_tuple( 22, 24),
make_tuple( 42, 44)));
array lower_right_quadrant_reversed = array(
make_tuple( make_tuple(44, 43),
make_tuple(34, 33)));
// The following comments represent equivalent Python expressions used
// to validate the array behavior.
// original[::] == original
ASSERT_EQUAL(original[slice()],original);
// original[:2,:2] == array( [[11, 12], [21, 22]])
ASSERT_EQUAL(original[make_tuple(slice(_,2), slice(_,2))],upper_left_quadrant);
// original[::2,::2] == array( [[11, 13], [31, 33]])
ASSERT_EQUAL(original[make_tuple( slice(_,_,2), slice(_,_,2))],odd_cells);
// original[1::2, 1::2] == array( [[22, 24], [42, 44]])
ASSERT_EQUAL(original[make_tuple( slice(1,_,2), slice(1,_,2))],even_cells);
// original[:-3:-1, :-3,-1] == array( [[44, 43], [34, 33]])
ASSERT_EQUAL(original[make_tuple( slice(_,-3,-1), slice(_,-3,-1))],lower_right_quadrant_reversed);
return object(1);
}
// Verify functions accepting a slice argument can be called
bool accept_slice( slice) { return true; }
#if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1400)) \
|| BOOST_WORKAROUND( BOOST_INTEL_WIN, == 710)
int check_slice_get_indicies(slice index);
#endif
int check_slice_get_indicies(
#if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
const
#endif
slice index)
{
// A vector of integers from [-5, 5].
std::vector<int> coll(11);
typedef std::vector<int>::iterator coll_iterator;
for (coll_iterator i = coll.begin(); i != coll.end(); ++i) {
*i = i - coll.begin() - 5;
}
slice::range<std::vector<int>::iterator> bounds;
try {
bounds = index.get_indicies(coll.begin(), coll.end());
}
catch (std::invalid_argument) {
return 0;
}
int sum = 0;
while (bounds.start != bounds.stop) {
sum += *bounds.start;
std::advance( bounds.start, bounds.step);
}
sum += *bounds.start;
return sum;
}
BOOST_PYTHON_MODULE(slice_ext)
{
def( "accept_slice", accept_slice);
def( "check_numeric_array_rich_slice", check_numeric_array_rich_slice);
def( "check_string_rich_slice", check_string_rich_slice);
def( "check_slice_get_indicies", check_slice_get_indicies);
}