MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Test that the sides of the triangle reduce to 1 and 3. In which case, the angles are


30 ∘, 60∘, and 90∘.


tol = 1e-10;
angles = rightTri([2 2*sqrt(3)]);
assert(abs(angles(1)-30)<= tol)
assert(abs(angles(2)-60)<= tol)
assert(abs(angles(3)-90)<= tol)


Test: Isosceles Triangles


For isosceles triangles, both of the non-right angles must be 45 degrees; otherwise
assert throws an error.


Test that two sides of the triangle are equal. In which case, the corresponding angles are
equal.


angles = rightTri([4 4]);
assert(angles(1) == 45)
assert(angles(1) == angles(2))


To run your tests, best practice is to use the testing framework via the runtests function
instead of the Run button in the Live Editor. The testing framework provides additional
diagnostic information. In the event of a test failure, the framework runs subsequent tests
but the Run button in the Live Editor does not. For example, to run this test at the
MATLAB command prompt, type result =
runtests('TestRightTriLiveScriptExample'). For more information, see
runtests.


See Also


assert | runtests


Related Examples



  • “Write Script-Based Unit Tests” on page 33-6

  • “Write Function-Based Unit Tests” on page 33-25


See Also
Free download pdf