Python: Add tests for bounce and bowling

This commit is contained in:
Martin Thoma
2022-03-23 11:44:46 +01:00
parent 0064fd328a
commit e66d2df028
4 changed files with 360 additions and 14 deletions

View File

@@ -6,7 +6,7 @@ A physics simulation
Ported by Dave LeCompte
"""
from typing import Tuple
from typing import Tuple, List
PAGE_WIDTH = 64
@@ -51,8 +51,8 @@ def print_at_tab(line: str, tab: int, s: str) -> str:
return line
def run_simulation(delta_t, v0, coeff_rest) -> None:
bounce_time = [0] * 20 # time of each bounce
def run_simulation(delta_t: float, v0: float, coeff_rest: float) -> None:
bounce_time: List[float] = [0] * 20 # time of each bounce
print("FEET")
print()
@@ -67,7 +67,7 @@ def run_simulation(delta_t, v0, coeff_rest) -> None:
line = ""
if int(h) == h:
line += str(int(h))
total_time = 0
total_time: float = 0
for i in range(1, sim_dur + 1):
tm: float = 0
while tm <= bounce_time[i]:

View File

@@ -0,0 +1,72 @@
import io
from _pytest.monkeypatch import MonkeyPatch
from _pytest.capture import CaptureFixture
from bounce import main
def test_bounce(monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]) -> None:
time_increment = 0.1
velocity = 30
coefficient = 1
monkeypatch.setattr(
"sys.stdin",
io.StringIO(f"{time_increment:0.1f}\n{velocity}\n{coefficient}\n"),
)
main()
actual = capsys.readouterr().out
expected = """ BOUNCE
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
THIS SIMULATION LETS YOU SPECIFY THE INITIAL VELOCITY
OF A BALL THROWN STRAIGHT UP, AND THE COEFFICIENT OF
ELASTICITY OF THE BALL. PLEASE USE A DECIMAL FRACTION
COEFFICIENCY (LESS THAN 1).
YOU ALSO SPECIFY THE TIME INCREMENT TO BE USED IN
'STROBING' THE BALL'S FLIGHT (TRY .1 INITIALLY).
TIME INCREMENT (SEC)?
VELOCITY (FPS)?
COEFFICIENT?
FEET
14 000 000 000
0 0 0
13 0 0 0 0 0 0
12 0 0 0 0 0 0
11 0 0 0
0 0 0
10
0 0 0
9 0 0 0
8
0 0 0
7 0 0 0
6
0 0 0
5 0 0 0
4
3 0 0 0
2 0 0 0
1
00 0 0
...................................................................
0 1 2 3 4 5 6
SECONDS
""" # noqa: W291
assert actual.split("\n") == expected.split("\n")