Skip to content

Commit

Permalink
SpiceyPy Adding more unit tests
Browse files Browse the repository at this point in the history
* Added tests for support types
* Fixed some issues with spice cell indexing
  • Loading branch information
AndrewAnnex committed Feb 7, 2016
1 parent 6df528a commit 6aa9d63
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 14 deletions.
24 changes: 10 additions & 14 deletions spiceypy/support_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ def from_ndarray(self, param):
def from_array(self, param):
if param.typecode != 'd':
raise TypeError('must be an array of doubles')
ptr, _ = param.buffer_info()
return cast(ptr, POINTER(c_double))
return self.from_list(param)


class DoubleMatrixType:
Expand Down Expand Up @@ -270,8 +269,7 @@ def from_ndarray(self, param):
def from_array(self, param):
if param.typecode != 'i':
raise TypeError('must be an array of ints')
ptr, _ = param.buffer_info()
return cast(ptr, POINTER(c_int))
return self.from_list(param)


class BoolArrayType:
Expand Down Expand Up @@ -328,7 +326,7 @@ def constant(self):
return self._constant

def __str__(self):
return '<Plane: normal=%s; constant=%s>' % (', '.join([str(x) for x in self._normal]), self._constant)
return '<SpicePlane: normal=%s; constant=%s>' % (', '.join([str(x) for x in self._normal]), self._constant)


class Ellipse(Structure):
Expand Down Expand Up @@ -591,20 +589,18 @@ def __contains__(self, key):

def __getitem__(self, key):
getter = SpiceCell.DATATYPES_GET[self.dtype]
length, card, data = self.length, self.card, self.data
if isinstance(key, slice):
start, stop, step = key.start or 0, key.stop or -1, key.step or 1
#TODO Typechecking
if card == 0:
if self.card == 0:
return []
else:
return list(getter(data, i, length)
for i in range(start % card, stop % card + 1, step))
if key in range(-card, card):
return getter(data, key, length)
start, stop, step = key.indices(self.card)
return [getter(self.data, i, self.length) for i in range(start, stop, step)]
elif key in range(-self.card, self.card):
index = key if key >= 0 else self.card - abs(key)
return getter(self.data, index, self.length)
elif not isinstance(key, int):
msg = 'SpiceCell inices must be integers, not {}'.format(type(key))
raise TypeError(msg)
raise TypeError('SpiceCell indices must be integers, not {}'.format(type(key)))
else:
raise IndexError('SpiceCell index out of range')

Expand Down
137 changes: 137 additions & 0 deletions test/test_support_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import spiceypy as spice
import spiceypy.support_types as stypes
import pytest
import ctypes
import numpy as np
import numpy.testing as npt
import array


def test_SpiceEllipse():
viewpt = [2.0, 0.0, 0.0]
limb = spice.edlimb(np.sqrt(2), 2.0 * np.sqrt(2), np.sqrt(2), viewpt)
expectedSMinor = [0.0, 0.0, -1.0]
expectedSMajor = [0.0, 2.0, 0.0]
expectedCenter = [1.0, 0.0, 0.0]
npt.assert_array_almost_equal(limb.center, expectedCenter)
npt.assert_array_almost_equal(limb.semi_major, expectedSMajor)
npt.assert_array_almost_equal(limb.semi_minor, expectedSMinor)
assert str(limb).startswith("<SpiceEllipse")


def test_SpicePlane():
norm = [0.0, 0.0, 1.0]
orig = [0.0, 0.0, 0.0]
plane = spice.nvp2pl(norm, orig)
npt.assert_array_almost_equal(plane.normal, norm)
assert plane.constant == 0.0
assert str(plane).startswith("<SpicePlane")


def test_SpiceCell():
testCell = stypes.SPICEINT_CELL(8)
spice.appndi(1, testCell)
spice.appndi(2, testCell)
spice.appndi(3, testCell)
assert [x for x in testCell] == [1, 2, 3]
assert len(testCell) == 3
assert 1 in testCell
assert 2 in testCell
assert 3 in testCell
assert 4 not in testCell
with pytest.raises(TypeError):
testCell.__getitem__('a')
with pytest.raises(IndexError):
testCell.__getitem__(3)
assert str(testCell).startswith("<SpiceCell")


def test_EmptySpiceCellSlicing():
testCell = stypes.SPICEDOUBLE_CELL(1)
assert testCell[0:1] == []


def test_SpiceCellSliceInts():
testVals = [1, 2, 3]
testCell = stypes.SPICEINT_CELL(5)
spice.appndi(testVals, testCell)
assert testCell[0] == testVals[0]
assert testCell[1] == testVals[1]
assert testCell[2] == testVals[2]
assert testCell[-1] == testVals[-1]
assert testCell[-2] == testVals[-2]
assert testCell[-3] == testVals[-3]
assert testCell[0:1] == testVals[0:1]
assert testCell[1:2] == testVals[1:2]
assert testCell[2:3] == testVals[2:3]
assert testCell[0:2] == testVals[0:2]
assert testCell[0:3] == testVals[0:3]
assert testCell[0:5] == testVals[0:5]
assert testCell[::2] == testVals[::2]
assert testCell[5:10] == testVals[5:10]
assert testCell[::-1] == testVals[::-1]
assert testCell[::-2] == testVals[::-2]
assert testCell[2:-1] == testVals[2:-1]


def test_toBoolVector():
madeFromList = stypes.toBoolVector([False, True, False])
assert len(madeFromList) == 3
madeFromTuple = stypes.toBoolVector((False, True, False))
assert len(madeFromTuple) == 3
madeFromNumpyArray = stypes.toBoolVector(np.ones((3, 1), dtype=bool))
assert len(madeFromNumpyArray) == 3
TestArray3 = ctypes.c_bool * 3
madeFromCtypesArray = stypes.toBoolVector(TestArray3(False, True, False))
assert len(madeFromCtypesArray) == 3
with pytest.raises(TypeError):
stypes.toBoolVector("ABCD")


def test_toDoubleVector():
madeFromList = stypes.toDoubleVector([1.0, 2.0, 3.0])
assert len(madeFromList) == 3
madeFromTuple = stypes.toDoubleVector((1.0, 2.0, 3.0))
assert len(madeFromTuple) == 3
madeFromNumpyArray = stypes.toDoubleVector(np.array([1.0, 2.0, 3.0]))
assert len(madeFromNumpyArray) == 3
madeFromPythonArray = stypes.toDoubleVector(array.array('d', [1.0, 2.0, 3.0]))
assert len(madeFromPythonArray) == 3
TestArray3 = ctypes.c_double * 3
madeFromCtypesArray = stypes.toDoubleVector(TestArray3(1.0, 2.0, 3.0))
assert len(madeFromCtypesArray) == 3
with pytest.raises(TypeError):
stypes.toDoubleVector("ABCD")
with pytest.raises(TypeError):
stypes.toDoubleVector(array.array('i', [1, 2, 3]))


def test_toIntVector():
madeFromList = stypes.toIntVector([1, 2, 3])
assert len(madeFromList) == 3
madeFromTuple = stypes.toIntVector((1, 2, 3))
assert len(madeFromTuple) == 3
madeFromNumpyArray = stypes.toIntVector(np.array([1, 2, 3]))
assert len(madeFromNumpyArray) == 3
madeFromPythonArray = stypes.toIntVector(array.array('i', [1, 2, 3]))
assert len(madeFromPythonArray) == 3
TestArray3 = ctypes.c_int * 3
madeFromCtypesArray = stypes.toIntVector(TestArray3(1, 2, 3))
assert len(madeFromCtypesArray) == 3
with pytest.raises(TypeError):
stypes.toIntVector("ABCD")
with pytest.raises(TypeError):
stypes.toIntVector(array.array('d', [1.0, 2.0, 3.0]))


def test_toDoubleMatrix():
madeFromList = stypes.toDoubleMatrix([[1.0, 2.0], [3.0, 4.0]])
assert len(madeFromList) == 2
madeFromTuple = stypes.toDoubleMatrix(((1.0, 2.0), (3.0, 4.0)))
assert len(madeFromTuple) == 2
madeFromNumpyArray = stypes.toDoubleMatrix(np.array([[1.0, 2.0], [3.0, 4.0]]))
assert len(madeFromNumpyArray) == 2
madeFromNumpyMatrix = stypes.toDoubleMatrix(np.matrix([[1.0, 2.0], [3.0, 4.0]]))
assert len(madeFromNumpyMatrix) == 2
with pytest.raises(TypeError):
stypes.toDoubleMatrix("ABCD")

0 comments on commit 6aa9d63

Please sign in to comment.