Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bfa97b

Browse files
take care of x=0 as input to log10(x)
1 parent e1ccb86 commit 0bfa97b

File tree

2 files changed

+51
-128
lines changed

2 files changed

+51
-128
lines changed

‎src/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def plot_function(self):
101101
return
102102

103103
x = np.linspace(min_x, max_x, 400)
104+
if "log10" in function:
105+
x = np.where(
106+
x == 0, 1e-15, x
107+
) # Add a small value to x where x is 0 if log10 is in the function
104108
try:
105109
y = eval(self.prepare_function(function, x))
106110
except Exception as e:

‎tests/test_function_plotter.py

Lines changed: 47 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -43,174 +43,93 @@ def function_plotter(app_instance, qtbot):
4343
return plotter
4444

4545

46-
# good input tests
46+
# test functions
4747

4848

49-
def test_valid_function_input(function_plotter, qtbot):
50-
"""
51-
Test plotting a valid function.
52-
"""
53-
function_plotter.function_input.setText("5*x^3 + 2*x")
54-
function_plotter.min_input.setText("0")
55-
function_plotter.max_input.setText("10")
56-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
57-
assert function_plotter.ax.has_data()
58-
59-
60-
def test_function_with_logarithm(function_plotter, qtbot):
61-
"""
62-
Test plotting a function with a logarithm.
63-
"""
64-
function_plotter.function_input.setText("log10(x)")
65-
function_plotter.min_input.setText("1")
66-
function_plotter.max_input.setText("100")
67-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
68-
assert function_plotter.ax.has_data()
69-
7049

71-
def test_function_with_square_root(function_plotter, qtbot):
50+
def test_basic_function_input(function_plotter, qtbot):
7251
"""
73-
Test plotting a function with a square root.
52+
Test plotting a basic function.
7453
"""
75-
function_plotter.function_input.setText("sqrt(x)")
54+
function_plotter.function_input.setText("5*x^3 + 2*x")
7655
function_plotter.min_input.setText("0")
77-
function_plotter.max_input.setText("25")
78-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
79-
assert function_plotter.ax.has_data()
80-
81-
82-
def test_function_with_combined_operators(function_plotter, qtbot):
83-
"""
84-
Test plotting a function with combined operators.
85-
"""
86-
function_plotter.function_input.setText("sqrt(x) + log10(x) + x^2")
87-
function_plotter.min_input.setText("1")
8856
function_plotter.max_input.setText("10")
8957
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
9058
assert function_plotter.ax.has_data()
9159

9260

93-
def test_large_range_of_x(function_plotter, qtbot):
94-
"""
95-
Test plotting a function over a large range of x values.
96-
"""
97-
function_plotter.function_input.setText("x^2")
98-
function_plotter.min_input.setText("-1000")
99-
function_plotter.max_input.setText("1000")
100-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
101-
assert function_plotter.ax.has_data()
102-
103-
104-
def test_small_range_of_x(function_plotter, qtbot):
105-
"""
106-
Test plotting a function over a small range of x values.
107-
"""
108-
function_plotter.function_input.setText("x^2")
109-
function_plotter.min_input.setText("0")
110-
function_plotter.max_input.setText("1")
111-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
112-
assert function_plotter.ax.has_data()
113-
114-
115-
def test_negative_x_values(function_plotter, qtbot):
116-
"""
117-
Test plotting a function with negative x values.
118-
"""
119-
function_plotter.function_input.setText("x^3")
120-
function_plotter.min_input.setText("-10")
121-
function_plotter.max_input.setText("0")
122-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
123-
assert function_plotter.ax.has_data()
61+
def test_invalid_character_in_function(function_plotter, qtbot):
62+
with qtbot.wait_signal(
63+
function_plotter.error_message_signal, timeout=5000
64+
) as blocker:
65+
function_plotter.function_input.setText("5*x^3 + 2*x + !")
66+
function_plotter.min_input.setText("0")
67+
function_plotter.max_input.setText("10")
68+
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
69+
assert blocker.args[0] == "Function contains invalid characters."
12470

12571

126-
def test_high_degree_polynomial(function_plotter, qtbot):
72+
def test_complex_formula_function(function_plotter, qtbot):
12773
"""
128-
Test plotting a high-degree polynomial function.
74+
Test plotting a complex formula function.
12975
"""
130-
function_plotter.function_input.setText("x^6 - 2*x^4 + x^2")
76+
function_plotter.function_input.setText("5*x^3 + 2*x - 4/x + 7")
13177
function_plotter.min_input.setText("-10")
13278
function_plotter.max_input.setText("10")
13379
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
13480
assert function_plotter.ax.has_data()
13581

13682

137-
def test_floating_point_coefficients(function_plotter, qtbot):
83+
def test_using_sqrt_and_log10_in_function(function_plotter, qtbot):
13884
"""
139-
Test floating point coefficients function.
85+
Test plotting a function using sqrt and log10.
14086
"""
141-
function_plotter.function_input.setText("0.5*x^2 + 2.5*x")
142-
function_plotter.min_input.setText("-5")
143-
function_plotter.max_input.setText("5")
144-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
145-
assert function_plotter.ax.has_data()
146-
147-
148-
def test_function_with_spaces(function_plotter, qtbot):
149-
"""
150-
Test function with spaces.
151-
"""
152-
function_plotter.function_input.setText(" x ^ 2 + 3 * x ")
153-
function_plotter.min_input.setText("-5")
154-
function_plotter.max_input.setText("5")
87+
function_plotter.function_input.setText("2*x^4 - log10(x) + sqrt(x)")
88+
function_plotter.min_input.setText("0")
89+
function_plotter.max_input.setText("50")
15590
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
15691
assert function_plotter.ax.has_data()
15792

15893

159-
# bad input tests
160-
161-
162-
def test_invalid_function_input_characters(function_plotter, qtbot):
94+
def test_negative_input_to_sqrt_or_log10(function_plotter, qtbot):
16395
with qtbot.wait_signal(
16496
function_plotter.error_message_signal, timeout=5000
16597
) as blocker:
166-
function_plotter.function_input.setText("5*x^3 + 2*x + !")
167-
function_plotter.min_input.setText("0")
168-
function_plotter.max_input.setText("10")
98+
function_plotter.function_input.setText("2*x^4 - log10(x) + sqrt(x)")
99+
function_plotter.min_input.setText("-50")
100+
function_plotter.max_input.setText("500")
169101
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
170-
assert blocker.args[0] == "Function contains invalid characters."
102+
assert blocker.args[0] == "Min value must be non-negative for functions with log10 or sqrt."
171103

172104

173-
def test_empty_function_input(function_plotter, qtbot):
105+
def test_wrong_input_for_min_and_max_values(function_plotter, qtbot):
174106
with qtbot.wait_signal(
175107
function_plotter.error_message_signal, timeout=5000
176108
) as blocker:
177-
function_plotter.function_input.setText("")
178-
function_plotter.min_input.setText("0")
179-
function_plotter.max_input.setText("10")
180-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
181-
assert blocker.args[0] == "Function cannot be empty."
182-
183-
184-
def test_invalid_min_max_values(function_plotter, qtbot):
185-
with qtbot.wait_signal(
186-
function_plotter.error_message_signal, timeout=5000
187-
) as blocker:
188-
function_plotter.function_input.setText("5*x^3 + 2*x")
189-
function_plotter.min_input.setText("10")
190-
function_plotter.max_input.setText("0")
191-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
192-
assert blocker.args[0] == "Min value must be less than Max value."
193-
194-
195-
def test_non_numeric_min_max_values(function_plotter, qtbot):
196-
with qtbot.wait_signal(
197-
function_plotter.error_message_signal, timeout=5000
198-
) as blocker:
199-
function_plotter.function_input.setText("5*x^3 + 2*x")
109+
function_plotter.function_input.setText("x")
200110
function_plotter.min_input.setText("a")
201-
function_plotter.max_input.setText("b")
111+
function_plotter.max_input.setText("2")
202112
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
203113
assert blocker.args[0] == "Min and Max values must be numbers."
204114

205115

206-
def test_empty_min_max_values(function_plotter, qtbot):
207-
withqtbot.wait_signal(
208-
function_plotter.error_message_signal, timeout=5000
209-
) asblocker:
210-
function_plotter.function_input.setText("x^2")
211-
function_plotter.min_input.setText("")
212-
function_plotter.max_input.setText("")
213-
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
214-
assert blocker.args[0] =="Min and Max values cannot be empty."
116+
def test_using_infinity_for_min_and_max_values(function_plotter, qtbot):
117+
"""
118+
Test plotting a function with -inf and +inf for min and max values.
119+
"""
120+
function_plotter.function_input.setText("x")
121+
function_plotter.min_input.setText("-inf")
122+
function_plotter.max_input.setText("inf")
123+
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
124+
assert function_plotter.ax.has_data()
215125

216126

127+
def test_constant_function_formula(function_plotter, qtbot):
128+
"""
129+
Test plotting a constant function.
130+
"""
131+
function_plotter.function_input.setText("5")
132+
function_plotter.min_input.setText("-10")
133+
function_plotter.max_input.setText("10")
134+
qtbot.mouseClick(function_plotter.plot_button, Qt.LeftButton)
135+
assert function_plotter.ax.has_data()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /