write test cases for a code that finds an average of ten numbers in matlab

To write test cases for code that finds an average of ten numbers in Matlab, you can follow the steps below:

  1. Identify the inputs: In this case, the input is an array of ten numbers.

  2. Identify the expected output: The expected output is the average of the ten numbers in the input array.

  3. Write test cases: Write a set of test cases that cover various scenarios. For example, test cases for an input array with positive numbers, negative numbers, and a mix of both.

main.m
% Test Case 1: Test with an array of positive numbers
input1 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
expectedOutput1 = 11;
assert(isequal(calcAvg(input1), expectedOutput1), 'Test Case 1 Failed');

% Test Case 2: Test with an array of negative numbers
input2 = [-2, -4, -6, -8, -10, -12, -14, -16, -18, -20];
expectedOutput2 = -11;
assert(isequal(calcAvg(input2), expectedOutput2), 'Test Case 2 Failed');

% Test Case 3: Test with an array with a mix of positive and negative numbers
input3 = [-2, 4, 6, -8, 10, -12, 14, -16, 18, -20];
expectedOutput3 = -2;
assert(isequal(calcAvg(input3), expectedOutput3), 'Test Case 3 Failed');
630 chars
15 lines
  1. Implement the code: Write the code to calculate the average of the ten numbers in the input array.
main.m
function avg = calcAvg(numbers)
    avg = sum(numbers)/10;
end
63 chars
4 lines
  1. Run the test cases: Run the test cases to make sure that the code works as expected.
main.m
>> runtests('test_calcAvg')
28 chars
2 lines

gistlibby LogSnag