Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IanLalonde committed May 28, 2024
1 parent c679ce2 commit 75d1d05
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ output/

# Editor Config #
.vscode/
.vs/
.spyproject/
.spyderproject/

Expand Down
12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ classifiers=[
]

[project.urls]
Homepage = "https://github.com/SherbyRobotics/pyro/tree/master"
Homepage = "https://github.com/SherbyRobotics/pyro/tree/master"

[tool.pytest.ini_options]
testpaths = "test"
addopts = "-v -ra -q"
log_cli = true
log_cli_level = "INFO"
log_format = "%(asctime)s %(levelname)s %(message)s"
log_date_format = "%Y-%m-%d %H:%M:%S"
minversion = "6.0"
filterwarnings = "ignore"
Empty file added test/__init__.py
Empty file.
84 changes: 84 additions & 0 deletions test/test_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import pytest
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from pyro.dynamic import ContinuousDynamicSystem # Replace 'your_module' with the actual module name

matplotlib.use('Template')
# Mock subclass to implement the f method
class MockContinuousDynamicSystem(ContinuousDynamicSystem):
def f(self, x, u, t):
# Simple dynamic equation for testing
return -x + u

# Test cases
@pytest.fixture
def system():
return MockContinuousDynamicSystem(n=2, m=2, p=2)

@pytest.fixture
def system_3d():
return MockContinuousDynamicSystem(n=3, m=2, p=2)

def test_initialization(system):
assert system.n == 2
assert system.m == 2
assert system.p == 2
assert system.state_label == ["State 0", "State 1"]
assert system.input_label == ["Input 0", "Input 1"]
assert system.output_label == ["Output 0", "Output 1"]

def test_f_method(system):
x = np.array([1.0, 2.0])
u = np.array([0.5, 1.5])
t = 0
dx = system.f(x, u, t)
assert np.array_equal(dx, -x + u)

def test_h_method(system):
x = np.array([1.0, 2.0])
u = np.array([0.5, 1.5])
t = 0
y = system.h(x, u, t)
assert np.array_equal(y, x)

def test_t2u_method(system):
t = 0
u = system.t2u(t)
assert np.array_equal(u, system.ubar)

def test_isavalidstate(system):
x_valid = np.array([0.0, 0.0])
x_invalid = np.array([11.0, 0.0])
assert system.isavalidstate(x_valid) is True
assert system.isavalidstate(x_invalid) is False

def test_isavalidinput(system):
x = np.array([0.0, 0.0])
u_valid = np.array([0.0, 0.0])
u_invalid = np.array([2.0, 0.0])
assert system.isavalidinput(x, u_valid) is True
assert system.isavalidinput(x, u_invalid) is False

def test_fsim(system):
x = np.array([1.0, 2.0])
t = 0
dx = system.fsim(x, t)
expected_dx = -x + system.ubar
assert np.array_equal(dx, expected_dx)

def test_x_next(system):
x = np.array([1.0, 2.0])
u = np.array([0.5, 1.5])
t = 0
dt = 0.1
steps = 1
x_next = system.x_next(x, u, t, dt, steps)
expected_x_next = x + dt * (-x + u)
assert np.allclose(x_next, expected_x_next)

def test_animate_simulation(system):
system.compute_trajectory(tf=1.0, n=11)
animation = system.animate_simulation()
assert animation is not None
plt.close('all')
7 changes: 7 additions & 0 deletions test/test_with_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# test_with_pytest.py

def test_always_passes():
assert True

def test_always_fails():
assert False

0 comments on commit 75d1d05

Please sign in to comment.