diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-06-08 21:13:09 -0700 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-09-13 00:53:43 -0700 |
commit | fca077c7e2fc8f21c639ba479267a34eb16a2810 (patch) | |
tree | e026e76d42a13126d92d095d1498595dd93dabbe /numpy/core/tests | |
parent | ff11d0127c5906be5bb67ec3fc86e075943124aa (diff) | |
download | numpy-fca077c7e2fc8f21c639ba479267a34eb16a2810.tar.gz |
MAINT: Respond to review comments on gh-7473
This:
* Inlines the macros in loops.c.src
* Replaces 8 with `CHAR_BIT `. The `NPY_SIZEOF_*` macros are not used here because it's too much work to apply them to the signed types, and they expand to the same thing anyway.
* Removes the reduce loop specializations which likely no one cares about
* Uses pytest.mark.parametrize to shorten the test
Diffstat (limited to 'numpy/core/tests')
-rw-r--r-- | numpy/core/tests/test_scalarmath.py | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index 7a1771ed8..854df5590 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -668,31 +668,27 @@ class TestAbs(object): class TestBitShifts(object): - def test_left_shift(self): + @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 - for dt in np.typecodes['AllInteger']: - arr = np.array([5, -5], dtype=dt) - scl_pos, scl_neg = arr - for shift in np.array([arr.dtype.itemsize * 8], dtype=dt): - res_pos = scl_pos << shift - res_neg = scl_neg << shift - assert_equal(res_pos, 0) - assert_equal(res_neg, 0) - # Result on scalars should be the same as on arrays - assert_array_equal(arr << shift, [res_pos, res_neg]) - - def test_right_shift(self): - # gh-2449 - for dt in np.typecodes['AllInteger']: - arr = np.array([5, -5], dtype=dt) - scl_pos, scl_neg = arr - for shift in np.array([arr.dtype.itemsize * 8], dtype=dt): - res_pos = scl_pos >> shift - res_neg = scl_neg >> shift - assert_equal(res_pos, 0) - if dt in np.typecodes['UnsignedInteger']: - assert_equal(res_neg, 0) + 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_neg, -1) + assert_equal(res_scl, 0) + # Result on scalars should be the same as on arrays - assert_array_equal(arr >> shift, [res_pos, res_neg], dt) + 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) |