Testing Madly
In which, your intrepid author parameterizes a test. P.S. But not enough! But a quick patch saves me.
In a fit of madness, I wrote a highly-parameterized test to go with the others:
@pytest.mark.parametrize('radius, wheel_radians,piston_radians,lead_radians',
[(r, w*math.pi/180, p*math.pi/180, l*math.pi/180)
for r in [0.5, 1, 2]
for w in [0,30,45,135,275, -15, -37]
for p in [0,30,45,135,275, -15, -37]
for l in [0,30,45,135,275, -15, -37]])
def test_madly(self, radius, wheel_radians,piston_radians,lead_radians):
origin = vector(0,0)
wheel_radians = 60*math.pi/180
piston_radians = 30*math.pi/180
lead_radians = 60*math.pi/180
length = 5
start, finish = con_rod_ends_complete(
piston_radians=piston_radians, origin=origin,
radius=radius, lead_radians=lead_radians,
wheel_radians=wheel_radians, length=length)
print(f'\n{radius=}\n{start=}\n{finish=}')
self.verify_constraints(radius, start, finish, origin, length, piston_radians)
def verify_constraints(self, radius, start, finish, origin, length, piston_radians):
assert finish.distance(start) == pytest.approx(length)
assert start.distance(origin) == pytest.approx(radius)
zero_based = finish - origin
finish_radians = math.atan2(zero_based.y, zero_based.x)
finish_degrees = finish_radians * 180/math.pi
assert finish_radians == pytest.approx(piston_radians)
And I added a verification for the start point, ensuring that it is at the correct radius from the origin. I think we’re doing 1079 combinations. 7*7*7*3.
Are these tests sufficient? Well, I could pass them by returning a constant result for each radial value, but since the code doesn’t do that, the tests do give me great confidence. I say that with certainty, because before I ran the madly test I was terrified that it would break.
But all the combinations pass, so our next step, surely, will be to embed this calculation into an object that we can use in our linkages in place of the current ConRod. I’ll save that for tomorrow.
See you then!
P.S. And guess what … it doesn’t work for non-zero origins. Which I only tested once.
P.P.S. I think I see why: I’m compensating for the origin before compensating for the rotation. Has to be done after. Back to the code face, after I rest. Perhaps even tomorrow.
P.P.P.S. Perhaps even now. A quick patch fixes it and the tests run. Whew!