Added tests for indexing

This commit is contained in:
Ankit Daftery 2011-06-24 12:58:57 +00:00
parent 085f574d6e
commit 419b6ec973
3 changed files with 55 additions and 0 deletions

View File

@ -18,5 +18,6 @@ test-suite numpy
[ numpy-test ufunc ]
[ numpy-test shapes ]
[ numpy-test ndarray ]
[ numpy-test indexing ]
;

View File

@ -0,0 +1,21 @@
import unittest
import numpy
import indexing_mod
class TestIndexing(unittest.TestCase):
def testSingle(self):
x = numpy.arange(0,10)
for i in range(0,10):
indexing_mod.single(x,i,i)
for i in range(-10,0):
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]
indexing_mod.slice(x,sl,b)
if __name__=="__main__":
unittest.main()

View File

@ -0,0 +1,33 @@
#include <boost/python/numpy.hpp>
#include <assert.h>
#include <boost/python/slice.hpp>
namespace bp = boost::python;
void single(bp::numpy::ndarray ndarr, int i,bp::object value) {
bp::object element = bp::extract<bp::object>(ndarr[i]);
assert(element == value);
}
void slice(bp::numpy::ndarray ndarr, bp::slice sl,bp::object val) {
// bp::object element = bp::extract<bp::object>(ndarr[sl]);
int start = bp::extract<int>(sl.start());
int stop = bp::extract<int>(sl.stop());
unsigned j=0;
for (int i = start; i < stop; i++)
{
bp::object element = bp::extract<bp::object>(ndarr[i]);
bp::object value = bp::extract<bp::object>(val[j]);
assert(element == value);
++j;
}
}
BOOST_PYTHON_MODULE(indexing_mod) {
bp::numpy::initialize();
bp::def("single",&single);
bp::def("slice",&slice);
}