# pypy / pypy / module / micronumpy / test / test_numarray.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216``` ```import py from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest class AppTestNumArray(BaseNumpyAppTest): def test_type(self): from numpy import array ar = array(range(5)) assert type(ar) is type(ar + ar) def test_init(self): from numpy import zeros a = zeros(15) # Check that storage was actually zero'd. assert a[10] == 0.0 # And check that changes stick. a[13] = 5.3 assert a[13] == 5.3 def test_empty(self): """ Test that empty() works. """ from numpy import empty a = empty(2) a[1] = 1.0 assert a[1] == 1.0 def test_ones(self): from numpy import ones a = ones(3) assert len(a) == 3 assert a[0] == 1 raises(IndexError, "a[3]") a[2] = 4 assert a[2] == 4 def test_iterator_init(self): from numpy import array a = array(range(5)) assert a[3] == 3 def test_getitem(self): from numpy import array a = array(range(5)) raises(IndexError, "a[5]") a = a + a raises(IndexError, "a[5]") assert a[-1] == 8 raises(IndexError, "a[-6]") def test_setitem(self): from numpy import array a = array(range(5)) a[-1] = 5.0 assert a[4] == 5.0 raises(IndexError, "a[5] = 0.0") raises(IndexError, "a[-6] = 3.0") def test_len(self): from numpy import array a = array(range(5)) assert len(a) == 5 assert len(a + a) == 5 def test_shape(self): from numpy import array a = array(range(5)) assert a.shape == (5,) b = a + a assert b.shape == (5,) c = a[:3] assert c.shape == (3,) def test_add(self): from numpy import array a = array(range(5)) b = a + a for i in range(5): assert b[i] == i + i def test_add_other(self): from numpy import array a = array(range(5)) b = array(reversed(range(5))) c = a + b for i in range(5): assert c[i] == 4 def test_add_constant(self): from numpy import array a = array(range(5)) b = a + 5 for i in range(5): assert b[i] == i + 5 def test_subtract(self): from numpy import array a = array(range(5)) b = a - a for i in range(5): assert b[i] == 0 def test_subtract_other(self): from numpy import array a = array(range(5)) b = array([1, 1, 1, 1, 1]) c = a - b for i in range(5): assert c[i] == i - 1 def test_subtract_constant(self): from numpy import array a = array(range(5)) b = a - 5 for i in range(5): assert b[i] == i - 5 def test_mul(self): from numpy import array a = array(range(5)) b = a * a for i in range(5): assert b[i] == i * i def test_mul_constant(self): from numpy import array a = array(range(5)) b = a * 5 for i in range(5): assert b[i] == i * 5 def test_div(self): from numpy import array a = array(range(1, 6)) b = a / a for i in range(5): assert b[i] == 1 def test_div_other(self): from numpy import array a = array(range(5)) b = array([2, 2, 2, 2, 2]) c = a / b for i in range(5): assert c[i] == i / 2.0 def test_div_constant(self): from numpy import array a = array(range(5)) b = a / 5.0 for i in range(5): assert b[i] == i / 5.0 def test_auto_force(self): from numpy import array a = array(range(5)) b = a - 1 a[2] = 3 for i in range(5): assert b[i] == i - 1 a = array(range(5)) b = a + a c = b + b b[1] = 5 assert c[1] == 4 def test_getslice(self): from numpy import array a = array(range(5)) s = a[1:5] assert len(s) == 4 for i in range(4): assert s[i] == a[i+1] def test_getslice_step(self): from numpy import array a = array(range(10)) s = a[1:9:2] assert len(s) == 4 for i in range(4): assert s[i] == a[2*i+1] def test_slice_update(self): from numpy import array a = array(range(5)) s = a[0:3] s[1] = 10 assert a[1] == 10 a[2] = 20 assert s[2] == 20 def test_slice_invaidate(self): # check that slice shares invalidation list with from numpy import array a = array(range(5)) s = a[0:2] b = array([10,11]) c = s + b a[0] = 100 assert c[0] == 10 assert c[1] == 12 d = s + b a[1] = 101 assert d[0] == 110 assert d[1] == 12 def test_mean(self): from numpy import array, mean a = array(range(5)) assert a.mean() == 2.0 assert a[:4].mean() == 1.5 ```