From fa51b58cd69d1b0c9b94bae12a323520f348f586 Mon Sep 17 00:00:00 2001 From: Ankit Daftery Date: Mon, 27 Jun 2011 04:09:12 +0000 Subject: [PATCH] Added test for slices with steps. Auto-detection of step not implemented yet --- libs/python/numpy/test/indexing.py | 6 ++++++ libs/python/numpy/test/indexing_mod.cpp | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/libs/python/numpy/test/indexing.py b/libs/python/numpy/test/indexing.py index 0b4986c5..0515784d 100644 --- a/libs/python/numpy/test/indexing.py +++ b/libs/python/numpy/test/indexing.py @@ -17,5 +17,11 @@ class TestIndexing(unittest.TestCase): b = [3,4,5,6,7] indexing_mod.slice(x,sl,b) + def testStepSlice(self): + x = numpy.arange(0,10) + sl = slice(3,8,2) + b = [3,5,7] + indexing_mod.step_slice(x,sl,b) + if __name__=="__main__": unittest.main() diff --git a/libs/python/numpy/test/indexing_mod.cpp b/libs/python/numpy/test/indexing_mod.cpp index 9adf7674..c65f3e7e 100644 --- a/libs/python/numpy/test/indexing_mod.cpp +++ b/libs/python/numpy/test/indexing_mod.cpp @@ -11,7 +11,6 @@ void single(bp::numpy::ndarray ndarr, int i,bp::object value) { void slice(bp::numpy::ndarray ndarr, bp::slice sl,bp::object val) { -// bp::object element = bp::extract(ndarr[sl]); int start = bp::extract(sl.start()); int stop = bp::extract(sl.stop()); unsigned j=0; @@ -21,13 +20,29 @@ void slice(bp::numpy::ndarray ndarr, bp::slice sl,bp::object val) { bp::object value = bp::extract(val[j]); assert(element == value); ++j; - } - + } } + +void step_slice(bp::numpy::ndarray ndarr, bp::slice sl,bp::object val) { + int start = bp::extract(sl.start()); + int stop = bp::extract(sl.stop()); + int step = bp::extract(sl.step()); + unsigned j=0; + for (int i = start; i < stop; i=i+step) + { + bp::object element = bp::extract(ndarr[i]); + bp::object value = bp::extract(val[j]); + assert(element == value); + ++j; + } +} + + BOOST_PYTHON_MODULE(indexing_mod) { bp::numpy::initialize(); bp::def("single",&single); bp::def("slice",&slice); + bp::def("step_slice",&step_slice); }