indexing.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. # Copyright Jim Bosch & Ankit Daftery 2010-2012.
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import unittest
  7. import numpy
  8. import indexing_ext
  9. class TestIndexing(unittest.TestCase):
  10. def testSingle(self):
  11. x = numpy.arange(0,10)
  12. for i in range(0,10):
  13. numpy.testing.assert_equal(indexing_ext.single(x,i), i)
  14. for i in range(-10,0):
  15. numpy.testing.assert_equal(indexing_ext.single(x,i),10+i)
  16. def testSlice(self):
  17. x = numpy.arange(0,10)
  18. sl = slice(3,8)
  19. b = [3,4,5,6,7]
  20. numpy.testing.assert_equal(indexing_ext.slice(x,sl), b)
  21. def testStepSlice(self):
  22. x = numpy.arange(0,10)
  23. sl = slice(3,8,2)
  24. b = [3,5,7]
  25. numpy.testing.assert_equal(indexing_ext.slice(x,sl), b)
  26. def testIndex(self):
  27. x = numpy.arange(0,10)
  28. chk = numpy.array([3,4,5,6])
  29. numpy.testing.assert_equal(indexing_ext.indexarray(x,chk),chk)
  30. chk = numpy.array([[0,1],[2,3]])
  31. numpy.testing.assert_equal(indexing_ext.indexarray(x,chk),chk)
  32. x = numpy.arange(9).reshape(3,3)
  33. y = numpy.array([0,1])
  34. z = numpy.array([0,2])
  35. chk = numpy.array([0,5])
  36. numpy.testing.assert_equal(indexing_ext.indexarray(x,y,z),chk)
  37. x = numpy.arange(0,10)
  38. b = x>4
  39. chk = numpy.array([5,6,7,8,9])
  40. numpy.testing.assert_equal(indexing_ext.indexarray(x,b),chk)
  41. x = numpy.arange(9).reshape(3,3)
  42. b = numpy.array([0,2])
  43. sl = slice(0,3)
  44. chk = numpy.array([[0,1,2],[6,7,8]])
  45. numpy.testing.assert_equal(indexing_ext.indexslice(x,b,sl),chk)
  46. if __name__=="__main__":
  47. unittest.main()