Docsity

Analyzing 'triangle', 'isPrime' Functions & Leap Year: Testing & Boundary Cases in C, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Worksheet 5: Testing and Boundary Cases Name:
An Active Learning approach to Data Structures using C
1
int triangle(int a, int b, int c) {
if (a == b)
if (b == c) return 1;
else
return 2;
else if (b == c)
return 2;
else
return 3;
}
boolean isPrime (in t n) { // assume n > 2
for (int i = 2; i * i < n; i++) {
if (0 == n % i)
return false;
}
return true;
}
Worksheet 5: Testing and Boundary Cases
In preparation: Read Chapter 3 to learn more about debugging, testing and program
proofs.
Imagine you were testing the function triangle from
an earlier lesson. What are some boundary cases for
this program? How many different test cases do you
think it will take to completely exercise the
program? Did you include all possible ways that
two values could be equal but not equal to the third?
Did you include values that were not a triangle?
Would your testing uncover the error in the
program?
Develop test cases for the isPrime function
shown at right. Then simulate execution of
the program on your test cases. Did your test
cases discover the error in this function?
pf2

Partial preview of the text

Download Analyzing 'triangle', 'isPrime' Functions & Leap Year: Testing & Boundary Cases in C and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 5: Testing and Boundary Cases Name:

An Active Learning approach to Data Structures using C 1

int triangle(int a, int b, int c) { if (a == b) if (b == c) return 1; else return 2; else if (b == c) return 2; else return 3; } boolean isPrime (int n) { // assume n > 2 for (int i = 2; i * i < n; i++) { if (0 == n % i) return false; } return true; }

Worksheet 5: Testing and Boundary Cases

In preparation : Read Chapter 3 to learn more about debugging, testing and program

proofs.

Imagine you were testing the function triangle from

an earlier lesson. What are some boundary cases for

this program? How many different test cases do you

think it will take to completely exercise the

program? Did you include all possible ways that

two values could be equal but not equal to the third?

Did you include values that were not a triangle?

Would your testing uncover the error in the

program?

Develop test cases for the isPrime function

shown at right. Then simulate execution of

the program on your test cases. Did your test

cases discover the error in this function?

Worksheet 5: Testing and Boundary Cases Name:

An Active Learning approach to Data Structures using C 2

A year is a leap year if it is divisible by 4, unless it is divisible by 100, in which case it is

not, unless it is also divisible by 400. From this description write several test cases for the

a leap year function. The code shown preports to test this condition. Would your test

cases reveal the error in this code?

int binarySearch(double data [ ], int n, double test) { /* assume data is sorted / int low = 0; int high = n; while (low < high) { mid = (low + high) / 2; if (data[mid] == test) return 1; / return true / if (data[mid] < testValue) low = mid + 1; else high = mid; } return 0; / return false */ }

Think about testing the binary search algorithm. What are some boundary cases? Write a

test harness and identify the boundary condition you are checking in each test case.

int leapDays (int year) { if ((year % r) == 0) || ((year % 100 == 0) && (year % 400 == 0)) return 28; else return 29; }