-
Notifications
You must be signed in to change notification settings - Fork 22
[wanivi] Adding tests #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vaish-W
wants to merge
1
commit into
Simulation-Software-Engineering:main
Choose a base branch
from
Vaish-W:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This file marks the directory as a Python package. |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This file marks the directory as a Python package. |
Binary file not shown.
Binary file added
BIN
+4.76 KB
tests/integration/__pycache__/test_diffusion2d.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,61 @@ | ||
""" | ||
Tests for functionality checks in class SolveDiffusion2D | ||
""" | ||
import sys | ||
import os | ||
|
||
# Add the root directory to the Python path | ||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) | ||
from diffusion2d import SolveDiffusion2D | ||
import numpy as np | ||
|
||
|
||
def test_initialize_physical_parameters(): | ||
""" | ||
Checks function SolveDiffusion2D.initialize_domain | ||
Integration test for initialize_physical_parameters and initialize_domain | ||
""" | ||
solver = SolveDiffusion2D() | ||
|
||
# Initialize the domain | ||
solver.initialize_domain(w=10.0, h=10.0, dx=0.1, dy=0.1) | ||
|
||
# Initialize physical parameters | ||
D, T_cold, T_hot = 2.5, 250.0, 750.0 | ||
solver.initialize_physical_parameters(d=D, T_cold=T_cold, T_hot=T_hot) | ||
|
||
# Manually compute expected dt | ||
dx2, dy2 = solver.dx ** 2, solver.dy ** 2 | ||
expected_dt = dx2 * dy2 / (2 * D * (dx2 + dy2)) | ||
|
||
# Assert dt is correctly calculated | ||
assert np.isclose(solver.dt, expected_dt, atol=1e-6), f"Expected dt: {expected_dt}, but got: {solver.dt}" | ||
|
||
|
||
def test_set_initial_condition(): | ||
""" | ||
Checks function SolveDiffusion2D.get_initial_function | ||
Integration test for set_initial_condition and initialize_domain | ||
""" | ||
solver = SolveDiffusion2D() | ||
|
||
# Initialize the domain | ||
solver.initialize_domain(w=10.0, h=10.0, dx=1.0, dy=1.0) | ||
|
||
# Initialize physical parameters | ||
solver.T_cold = 300.0 | ||
solver.T_hot = 700.0 | ||
|
||
# Set initial conditions | ||
u = solver.set_initial_condition() | ||
|
||
# Manually compute the expected initial condition array | ||
expected_u = np.full((10, 10), 300.0) # Initialize with T_cold | ||
r, cx, cy = 2, 5, 5 | ||
r2 = r ** 2 | ||
for i in range(10): | ||
for j in range(10): | ||
p2 = (i - cx) ** 2 + (j - cy) ** 2 | ||
if p2 < r2: | ||
expected_u[i, j] = 700.0 # Set T_hot for points inside the circle | ||
|
||
# Assert the computed field matches the expected field | ||
assert np.array_equal(u, expected_u), "The initial condition array is incorrect" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
requires = ["tox>=4"] | ||
env_list = ["unittest", "integrationtest", "coverage"] | ||
|
||
[testenv] | ||
description = "Base environment for running tests" | ||
deps = ["pytest>=8", "matplotlib", "numpy", "coverage"] | ||
|
||
[env.unittest] | ||
description = "Run Unit Tests" | ||
commands = [["pytest", "tests/unit/test_diffusion2d_functions.py"]] | ||
|
||
[env.integrationtest] | ||
description = "Run Integration Tests" | ||
commands = [["pytest", "tests/integration/test_diffusion2d.py"]] | ||
|
||
[env.coverage] | ||
description = "Check Coverage" | ||
commands = [ | ||
["coverage", "run", "-m", "pytest", "tests/"], | ||
["coverage", "html", "-d", "coverage-html"], | ||
["coverage", "report", "-m"] | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This file marks the directory as a Python package. |
Binary file not shown.
Binary file added
BIN
+8.05 KB
tests/unit/__pycache__/test_diffusion2d_functions.cpython-312-pytest-8.3.3.pyc
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+3.65 KB
tests/unit/__pycache__/test_diffusion2d_functions_unittest.cpython-312.pyc
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
""" | ||
Tests for functions in class SolveDiffusion2D | ||
Tests for functions in class SolveDiffusion2D using pytest | ||
""" | ||
import sys | ||
import os | ||
|
||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) | ||
|
||
from diffusion2d import SolveDiffusion2D | ||
|
||
|
@@ -11,16 +15,44 @@ def test_initialize_domain(): | |
""" | ||
solver = SolveDiffusion2D() | ||
|
||
solver.initialize_domain(w=4.0, h=6.0, dx=0.2, dy=0.3) | ||
|
||
# Assert the values of the domain parameters | ||
assert solver.nx == 20 # 4.0 / 0.2 | ||
assert solver.ny == 20 # 6.0 / 0.3 | ||
|
||
|
||
def test_initialize_physical_parameters(): | ||
""" | ||
Checks function SolveDiffusion2D.initialize_domain | ||
""" | ||
solver = SolveDiffusion2D() | ||
solver.initialize_domain(w=10.0, h=10.0, dx=0.5, dy=0.5) | ||
solver.initialize_physical_parameters(d=1.0, T_cold=300.0, T_hot=700.0) | ||
|
||
# Assert the values of the physical parameters | ||
assert solver.D == 1.0 | ||
assert solver.T_cold == 300.0 | ||
assert solver.T_hot == 700.0 | ||
|
||
# Assert the computed time step | ||
dx2, dy2 = solver.dx ** 2, solver.dy ** 2 | ||
expected_dt = dx2 * dy2 / (2 * solver.D * (dx2 + dy2)) | ||
assert solver.dt == expected_dt | ||
|
||
|
||
def test_set_initial_condition(): | ||
""" | ||
Checks function SolveDiffusion2D.get_initial_function | ||
""" | ||
solver = SolveDiffusion2D() | ||
solver.initialize_domain(w=10.0, h=10.0, dx=1.0, dy=1.0) | ||
solver.initialize_physical_parameters(d=1.0, T_cold=300.0, T_hot=700.0) | ||
u = solver.set_initial_condition() | ||
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
|
||
# Assert the shape of the initial condition array | ||
assert u.shape == (10, 10) | ||
|
||
# Assert the initial temperature values in the domain | ||
assert (u == 300.0).sum() > 0 | ||
assert (u == 700.0).sum() > 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A unit test should only have the call of the function that is being tested. The necessary variables should be set manually.