MCPcopy Create free account
hub / github.com/numpy/numpy / test_sum

Method test_sum

numpy/core/tests/test_ufunc.py:706–738  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

704
705 @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
706 def test_sum(self):
707 for dt in (int, np.float16, np.float32, np.float64, np.longdouble):
708 for v in (0, 1, 2, 7, 8, 9, 15, 16, 19, 127,
709 128, 1024, 1235):
710 # warning if sum overflows, which it does in float16
711 with warnings.catch_warnings(record=True) as w:
712 warnings.simplefilter("always", RuntimeWarning)
713
714 tgt = dt(v * (v + 1) / 2)
715 overflow = not np.isfinite(tgt)
716 assert_equal(len(w), 1 * overflow)
717
718 d = np.arange(1, v + 1, dtype=dt)
719
720 assert_almost_equal(np.sum(d), tgt)
721 assert_equal(len(w), 2 * overflow)
722
723 assert_almost_equal(np.sum(d[::-1]), tgt)
724 assert_equal(len(w), 3 * overflow)
725
726 d = np.ones(500, dtype=dt)
727 assert_almost_equal(np.sum(d[::2]), 250.)
728 assert_almost_equal(np.sum(d[1::2]), 250.)
729 assert_almost_equal(np.sum(d[::3]), 167.)
730 assert_almost_equal(np.sum(d[1::3]), 167.)
731 assert_almost_equal(np.sum(d[::-2]), 250.)
732 assert_almost_equal(np.sum(d[-1::-2]), 250.)
733 assert_almost_equal(np.sum(d[::-3]), 167.)
734 assert_almost_equal(np.sum(d[-1::-3]), 167.)
735 # sum with first reduction entry != 0
736 d = np.ones((1,), dtype=dt)
737 d += d
738 assert_almost_equal(d, 2.)
739
740 def test_sum_complex(self):
741 for dt in (np.complex64, np.complex128, np.clongdouble):

Callers

nothing calls this directly

Calls 4

assert_equalFunction · 0.90
assert_almost_equalFunction · 0.90
dtClass · 0.70
sumMethod · 0.45

Tested by

no test coverage detected