def test_bisec(): def f(x): if x < 5: return -1 elif x >= 6: return 1 else: return 0 r=bisec(f, 0, 10, 1) print(r) assert r==5
def test_result2(): f = lambda x: x x = bisec(f, -2, 1) assert abs(x) < 1e-4
def test_result4(): f = lambda x: -x**3 + 1 x = bisec(f, -2, 1.5) assert abs(x) < 1e-4
def test_result3(): f = lambda x: x**3 - 1 x = bisec(f, -2, 1.5) assert abs(x) < 1e-4
def test_result(tol): f = lambda x: x x = bisec(f, -1, 1, tol=tol) assert abs(x) < tol
def test_a_equal_b_equal_root(tol): f = lambda x: x**2 - 1 x = bisec(f, -1, -1, tol=tol) assert abs(x) < tol
def test_a_equal_b(tol): f = lambda x: x**2 - 1 with pytest.raises(ValueError): x = bisec(f, -2, -2, tol=tol)
def test_discont(tol): f = lambda x: -1 if x < 0 else 1 with pytest.raises(ValueError): x = bisec(f, -2, 1.5, tol=tol)
def test_zero_right(tol): f = lambda x: x**2 x = bisec(f, -1, 0, tol=tol) assert abs(x) < tol
def test_zero_left(tol): f = lambda x: x**2 x = bisec(f, 0, 1, tol=tol) assert abs(x) < tol
def test_no_zero(tol): f = lambda x: x**2 + 1 with pytest.raises(ValueError): x = bisec(f, -2, 1.5, tol=tol)
def test_plus_minus(tol): f = lambda x: -x**3 + 1 x = bisec(f, -2, 1.5, tol=tol) assert abs(x) < tol
def test_zero_right(): f = lambda x: x**2 x = bisec(f, -1, 0) assert abs(x) < 1e-4
def test_zero_left(): f = lambda x: x**2 x = bisec(f, 0, 1) assert abs(x) < 1e-4
def test_plus_minus(): f = lambda x: -x**3 + 1 x = bisec(f, -2, 1.5) assert abs(x) < 1e-4
def test_a_equal_b_equal_root(): f = lambda x: x**2 - 1 x = bisec(f, -1, -1) assert abs(x) < 1e-4