From 2979e4b0621d48b3e968cada5019af9c05c70cc0 Mon Sep 17 00:00:00 2001 From: Ankit Daftery Date: Thu, 30 Jun 2011 02:50:40 +0000 Subject: [PATCH] Added index array and boolean tests --- libs/python/numpy/test/indexing.py | 55 +++++++++++++++++-------- libs/python/numpy/test/indexing_mod.cpp | 13 ++++-- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/libs/python/numpy/test/indexing.py b/libs/python/numpy/test/indexing.py index b11e86e9..0533d0f2 100644 --- a/libs/python/numpy/test/indexing.py +++ b/libs/python/numpy/test/indexing.py @@ -4,24 +4,45 @@ import indexing_mod class TestIndexing(unittest.TestCase): - def testSingle(self): - x = numpy.arange(0,10) - for i in range(0,10): - numpy.testing.assert_equal(indexing_mod.single(x,i), i) - for i in range(-10,0): - numpy.testing.assert_equal(indexing_mod.single(x,i),10+i) + def testSingle(self): + x = numpy.arange(0,10) + for i in range(0,10): + numpy.testing.assert_equal(indexing_mod.single(x,i), i) + for i in range(-10,0): + numpy.testing.assert_equal(indexing_mod.single(x,i),10+i) - def testSlice(self): - x = numpy.arange(0,10) - sl = slice(3,8) - b = [3,4,5,6,7] - numpy.testing.assert_equal(indexing_mod.slice(x,sl), b) + def testSlice(self): + x = numpy.arange(0,10) + sl = slice(3,8) + b = [3,4,5,6,7] + numpy.testing.assert_equal(indexing_mod.slice(x,sl), b) - def testStepSlice(self): - x = numpy.arange(0,10) - sl = slice(3,8,2) - b = [3,5,7] - numpy.testing.assert_equal(indexing_mod.slice(x,sl), b) + def testStepSlice(self): + x = numpy.arange(0,10) + sl = slice(3,8,2) + b = [3,5,7] + numpy.testing.assert_equal(indexing_mod.slice(x,sl), b) + + def testIndex(self): + x = numpy.arange(0,10) + chk = numpy.array([3,4,5,6]) + numpy.testing.assert_equal(indexing_mod.indexarray(x,chk),chk) + chk = numpy.array([[0,1],[2,3]]) + numpy.testing.assert_equal(indexing_mod.indexarray(x,chk),chk) +# x = numpy.arange(9).reshape(3,3) +# y = numpy.array([0,1]) +# z = numpy.array([0,2]) +# chk = numpy.array([0,5]) +# numpy.testing.assert_equal(indexing_mod.indexarray(x,y,z),chk) # This throws an assertion error, indicates shape mismatch + x = numpy.arange(0,10) + b = x>4 + chk = numpy.array([5,6,7,8,9]) + numpy.testing.assert_equal(indexing_mod.indexarray(x,b),chk) + x = numpy.arange(9).reshape(3,3) + b = numpy.array([0,2]) + sl = slice(0,2) + chk = numpy.array([[0,1,2],[6,7,8]]) + numpy.testing.assert_equal(indexing_mod.indexslice(x,b,sl),chk) if __name__=="__main__": - unittest.main() + unittest.main() diff --git a/libs/python/numpy/test/indexing_mod.cpp b/libs/python/numpy/test/indexing_mod.cpp index 533a20a3..02f79cbd 100644 --- a/libs/python/numpy/test/indexing_mod.cpp +++ b/libs/python/numpy/test/indexing_mod.cpp @@ -5,10 +5,17 @@ namespace bp = boost::python; bp::object single(bp::numpy::ndarray ndarr, int i) { return ndarr[i];} bp::object slice(bp::numpy::ndarray ndarr, bp::slice sl) { return ndarr[sl];} +bp::object indexarray(bp::numpy::ndarray ndarr, bp::numpy::ndarray d1) { return ndarr[d1];} +bp::object indexarray_2d(bp::numpy::ndarray ndarr, bp::numpy::ndarray d1,bp::numpy::ndarray d2) { return ndarr[d1][d2];} +bp::object indexslice(bp::numpy::ndarray ndarr, bp::numpy::ndarray d1,bp::slice sl) { return ndarr[d1][sl];} BOOST_PYTHON_MODULE(indexing_mod) { - bp::numpy::initialize(); - bp::def("single", single); - bp::def("slice", slice); + bp::numpy::initialize(); + bp::def("single", &single); + bp::def("slice", &slice); + bp::def("indexarray", &indexarray); + bp::def("indexarray", &indexarray_2d); + bp::def("indexslice", &indexslice); + }