diff options
author | Alan McIntyre <alan.mcintyre@local> | 2008-07-21 06:29:58 +0000 |
---|---|---|
committer | Alan McIntyre <alan.mcintyre@local> | 2008-07-21 06:29:58 +0000 |
commit | 77effddb80562fa5e2de16ac4fb5470477bda652 (patch) | |
tree | fb09e7605dbc28bb8a0146119f695be9ceb18b22 | |
parent | bd2f3c0ddad61d78eda6257dbf1cbeb608aa1c82 (diff) | |
download | numpy-77effddb80562fa5e2de16ac4fb5470477bda652.tar.gz |
Added tests to improve coverage of numpy.core.
Removed unused methods from Vec class in test_numeric.py.
Renamed test in test_regression so it would be seen by nose.
Removed commented test_vector_elements test in test_defmatrix.py (same
tems are now tested in test_matrix_element).
-rw-r--r-- | numpy/core/tests/test_defmatrix.py | 68 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 23 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 60 | ||||
-rw-r--r-- | numpy/core/tests/test_records.py | 13 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 3 |
5 files changed, 153 insertions, 14 deletions
diff --git a/numpy/core/tests/test_defmatrix.py b/numpy/core/tests/test_defmatrix.py index ec8b5cb4a..8e28dcbe1 100644 --- a/numpy/core/tests/test_defmatrix.py +++ b/numpy/core/tests/test_defmatrix.py @@ -17,10 +17,36 @@ class TestCtor(TestCase): assert all(B.A == D) assert all(C.A == D) + E = array([[5,6],[7,8]]) + AEresult = matrix([[1,2,5,6],[3,4,7,8]]) + assert all(bmat([A,E]) == AEresult) + vec = arange(5) mvec = matrix(vec) assert mvec.shape == (1,5) + def test_bmat_nondefault_str(self): + A = array([[1,2],[3,4]]) + B = array([[5,6],[7,8]]) + Aresult = array([[1,2,1,2], + [3,4,3,4], + [1,2,1,2], + [3,4,3,4]]) + Bresult = array([[5,6,5,6], + [7,8,7,8], + [5,6,5,6], + [7,8,7,8]]) + mixresult = array([[1,2,5,6], + [3,4,7,8], + [5,6,1,2], + [7,8,3,4]]) + assert all(bmat("A,A;A,A") == Aresult) + assert all(bmat("A,A;A,A",ldict={'A':B}) == Aresult) + assert_raises(TypeError, bmat, "A,A;A,A",gdict={'A':B}) + assert all(bmat("A,A;A,A",ldict={'A':A},gdict={'A':B}) == Aresult) + b2 = bmat("A,B;C,D",ldict={'A':A,'B':B},gdict={'C':B,'D':A}) + assert all(b2 == mixresult) + class TestProperties(TestCase): def test_sum(self): @@ -38,6 +64,34 @@ class TestProperties(TestCase): assert_array_equal(sum1, M.sum(axis=1)) assert sumall == M.sum() + + def test_prod(self): + x = matrix([[1,2,3],[4,5,6]]) + assert x.prod() == 720 + assert all(x.prod(0) == matrix([[4,10,18]])) + assert all(x.prod(1) == matrix([[6],[120]])) + + y = matrix([0,1,3]) + assert y.prod() == 0 + + def test_max(self): + x = matrix([[1,2,3],[4,5,6]]) + assert x.max() == 6 + assert all(x.max(0) == matrix([[4,5,6]])) + assert all(x.max(1) == matrix([[3],[6]])) + + def test_min(self): + x = matrix([[1,2,3],[4,5,6]]) + assert x.min() == 1 + assert all(x.min(0) == matrix([[1,2,3]])) + assert all(x.min(1) == matrix([[1],[4]])) + + def test_ptp(self): + x = np.arange(4).reshape((2,2)) + assert x.ptp() == 3 + assert all(x.ptp(0) == array([2, 2])) + assert all(x.ptp(1) == array([1, 1])) + def test_basic(self): import numpy.linalg as linalg @@ -55,6 +109,13 @@ class TestProperties(TestCase): assert all(array(transpose(B) == mB.T)) assert all(array(conjugate(transpose(B)) == mB.H)) + def test_pinv(self): + x = matrix(arange(6).reshape(2,3)) + xpinv = matrix([[-0.77777778, 0.27777778], + [-0.11111111, 0.11111111], + [ 0.55555556, -0.05555556]]) + assert_almost_equal(x.I, xpinv) + def test_comparisons(self): A = arange(100).reshape(10,10) mA = matrix(A) @@ -241,14 +302,9 @@ class TestNewScalarIndexing(TestCase): assert isinstance(x, matrix) assert_equal(x, matrix([[4, 3], [1, 2]])) -# def test_vector_element(self): -# x = matrix([[1,2,3],[4,5,6]]) -# assert_equal(x[0][0],1) -# assert_equal(x[0].shape,(1,3)) -# assert_equal(x[:,0].shape,(2,1)) - def test_matrix_element(self): x = matrix([[1,2,3],[4,5,6]]) + assert_equal(x[0][0],matrix([[1,2,3]])) assert_equal(x[0][0].shape,(1,3)) assert_equal(x[0].shape,(1,3)) assert_equal(x[:,0].shape,(2,1)) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 6c91e1ecb..50a02058e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -397,6 +397,29 @@ class TestMethods(TestCase): # d.sort(axis=None) #assert_equal(d, c, "test sort with axis=None") + + def test_sort_order(self): + # Test sorting an array with fields + x1=np.array([21,32,14]) + x2=np.array(['my','first','name']) + x3=np.array([3.1,4.5,6.2]) + r=np.rec.fromarrays([x1,x2,x3],names='id,word,number') + + r.sort(order=['id']) + assert_equal(r.id, array([14,21,32])) + assert_equal(r.word, array(['name','my','first'])) + assert_equal(r.number, array([6.2,3.1,4.5])) + + r.sort(order=['word']) + assert_equal(r.id, array([32,21,14])) + assert_equal(r.word, array(['first','my','name'])) + assert_equal(r.number, array([4.5,3.1,6.2])) + + r.sort(order=['number']) + assert_equal(r.id, array([21,32,14])) + assert_equal(r.word, array(['my','first','name'])) + assert_equal(r.number, array([3.1,4.5,6.2])) + def test_argsort(self): # all c scalar argsorts use the same code with different types # so it suffices to run a quick check with one type. The number diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index da8378784..83aaf5f45 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -23,13 +23,6 @@ class Vec: return out def __rmul__(self,other): return self*other - def __abs__(self): - out=Vec() - out.array=abs(self.array) - return out - def __repr__(self): - return "Vec("+repr(self.array.tolist())+")" - __str__=__repr__ class TestDot(TestCase): @@ -142,6 +135,59 @@ class TestDot(TestCase): assert_equal(zeros[1].array, zeros_test[1].array) +class TestResize(TestCase): + def test_copies(self): + A = array([[1,2],[3,4]]) + Ar1 = array([[1,2,3,4],[1,2,3,4]]) + assert_equal(resize(A, (2,4)), Ar1) + + Ar2 = array([[1,2],[3,4],[1,2],[3,4]]) + assert_equal(resize(A, (4,2)), Ar2) + + Ar3 = array([[1,2,3],[4,1,2],[3,4,1],[2,3,4]]) + assert_equal(resize(A, (4,3)), Ar3) + + def test_zeroresize(self): + A = array([[1,2],[3,4]]) + Ar = resize(A, (0,)) + assert_equal(Ar, array([])) + + +class TestNonarrayArgs(TestCase): + # check that non-array arguments to functions wrap them in arrays + def test_squeeze(self): + A = [[[1,1,1],[2,2,2],[3,3,3]]] + assert squeeze(A).shape == (3,3) + + def test_cumproduct(self): + A = [[1,2,3],[4,5,6]] + assert all(cumproduct(A) == array([1,2,6,24,120,720])) + + def test_size(self): + A = [[1,2,3],[4,5,6]] + assert size(A) == 6 + assert size(A,0) == 2 + assert size(A,1) == 3 + + def test_mean(self): + A = [[1,2,3],[4,5,6]] + assert mean(A) == 3.5 + assert all(mean(A,0) == array([2.5,3.5,4.5])) + assert all(mean(A,1) == array([2.,5.])) + + def test_std(self): + A = [[1,2,3],[4,5,6]] + assert_almost_equal(std(A), 1.707825127659933) + assert_almost_equal(std(A,0), array([1.5, 1.5, 1.5])) + assert_almost_equal(std(A,1), array([0.81649658, 0.81649658])) + + def test_var(self): + A = [[1,2,3],[4,5,6]] + assert_almost_equal(var(A), 2.9166666666666665) + assert_almost_equal(var(A,0), array([2.25, 2.25, 2.25])) + assert_almost_equal(var(A,1), array([0.66666667, 0.66666667])) + + class TestBoolScalar(TestCase): def test_logical(self): f = False_ diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 7e7c5e0f5..8239da5c7 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -115,6 +115,19 @@ class TestRecord(TestCase): self.failUnlessRaises(AttributeError,assign_invalid_column,a) +def test_find_duplicate(): + l1 = [1,2,3,4,5,6] + assert np.rec.find_duplicate(l1) == [] + + l2 = [1,2,1,4,5,6] + assert np.rec.find_duplicate(l2) == [1] + + l3 = [1,2,1,4,1,6,2,3] + assert np.rec.find_duplicate(l3) == [1,2] + + l3 = [2,2,1,4,1,6,2,3] + assert np.rec.find_duplicate(l3) == [2,1] + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index af3dee3a0..bbaa4fe5e 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1,3 +1,4 @@ + from StringIO import StringIO import pickle import sys @@ -1166,7 +1167,7 @@ class TestRegression(TestCase): x = np.array([('a',u'b')], dtype=t) assert_equal(str(x), "[('a', u'b')]", err_msg=msg) - def check_sign_for_complex_nan(self, level=rlevel): + def test_sign_for_complex_nan(self, level=rlevel): """Ticket 794.""" C = np.array([-np.inf, -2+1j, 0, 2-1j, np.inf, np.nan]) have = np.sign(C) |