diff options
author | Matti Picus <matti.picus@gmail.com> | 2019-09-14 20:12:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-14 20:12:29 +0300 |
commit | 31ffdecf07d18ed4dbb66b171cb0f998d4b190fa (patch) | |
tree | 027c82afa6841bf75704df22966df24d46c98258 /numpy/core/tests | |
parent | 79cb45d9c50875c61f3032f9047f02551661efa1 (diff) | |
parent | 6cf6ece43589670a28b765fd03402cc08ada61f0 (diff) | |
download | numpy-31ffdecf07d18ed4dbb66b171cb0f998d4b190fa.tar.gz |
Merge pull request #13739 from eric-wieser/bit_shifts
BUG: Don't produce undefined behavior for a << b if b >= bitsof(a)
Diffstat (limited to 'numpy/core/tests')
-rw-r--r-- | numpy/core/tests/test_scalarmath.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index ebba457e3..854df5590 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -664,3 +664,31 @@ class TestAbs(object): def test_numpy_abs(self): self._test_abs_func(np.abs) + + +class TestBitShifts(object): + + @pytest.mark.parametrize('type_code', np.typecodes['AllInteger']) + @pytest.mark.parametrize('op', + [operator.rshift, operator.lshift], ids=['>>', '<<']) + def test_shift_all_bits(self, type_code, op): + """ Shifts where the shift amount is the width of the type or wider """ + # gh-2449 + dt = np.dtype(type_code) + nbits = dt.itemsize * 8 + for val in [5, -5]: + for shift in [nbits, nbits + 4]: + val_scl = dt.type(val) + shift_scl = dt.type(shift) + res_scl = op(val_scl, shift_scl) + if val_scl < 0 and op is operator.rshift: + # sign bit is preserved + assert_equal(res_scl, -1) + else: + assert_equal(res_scl, 0) + + # Result on scalars should be the same as on arrays + val_arr = np.array([val]*32, dtype=dt) + shift_arr = np.array([shift]*32, dtype=dt) + res_arr = op(val_arr, shift_arr) + assert_equal(res_arr, res_scl) |