-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_function_plotter.py
More file actions
67 lines (52 loc) · 1.65 KB
/
test_function_plotter.py
File metadata and controls
67 lines (52 loc) · 1.65 KB
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
from function_plotter import FunctionPlotter
import pytest
import numpy as np
# Test the validate_input function
def test_validate_input():
# Valid input
testClass = FunctionPlotter()
function_str = "x"
min_x = "0"
max_x = "10"
result = testClass.validate_input(function_str, min_x, max_x)
assert result == (0, 10)
# Empty function field
with pytest.raises(ValueError):
function_str = ""
testClass.validate_input(function_str, min_x, max_x)
# Invalid function expression
with pytest.raises(ValueError):
function_str = "x$2"
testClass.validate_input(function_str, min_x, max_x)
# Invalid min_x or max_x value
with pytest.raises(ValueError):
min_x = "abc"
testClass.validate_input(function_str, min_x, max_x)
# min_x >= max_x
with pytest.raises(ValueError):
min_x = "10"
max_x = "5"
testClass.validate_input(function_str, min_x, max_x)
# Clean up
testClass.exit_program()
# Test the plot_function function
def test_plot_function():
plotter = FunctionPlotter()
# Test a valid function and range inputs
plotter.function_input.setText("x**2")
plotter.min_input.setText("0")
plotter.max_input.setText("10")
plotter.plot_function()
x_input = 5
y = 25
# Find the y-axis value for the given x-coordinate
y_output = None
for x, y in zip(plotter.x_values, plotter.y_values):
if np.isclose(x, x_input, atol=1e-6):
y_output = y
break
# Assert the plot is generated correctly
assert y_output == y
# Run all the tests
if __name__ == "__main__":
pytest.main()