DBC Lecture Code Examples
Table of Contents
|
Example 1 - find an element in an array
Note that they most important part is the SPECIFICATION which is in the comments above the function
#include <iostream> #include <cassert> using namespace std; /** * Function Specification: * This function finds the number in a given array and returns * its first index in the array. * Pre-conditions: * 1. array argument must not be null * 2. arraysize argument must not be zero * Post-conditions: * 1. if number is found the found index is between 0 * and size of the array - 1 * 2. if number is NOT found the found index is -1 */ int findElement(int *arr, int arrsize, int number) { //check pre-conditions assert(arr != NULL); assert(arrsize != 0); bool bFound = false; int foundIndex = -1; for(int i = 0; i < arrsize; i++) { if (arr[i] == number) { foundIndex = i; bFound = true; break; } } //foundIndex++; //to test the first assert below //foundIndex--; //to test the second assert below //check post conditions and invariant foundIndex if (bFound) assert(foundIndex >=0 && foundIndex < arrsize); else assert(foundIndex == -1); return foundIndex; }
Testing the findElement() function
Positive test case
int main() { int a[] = {1,2,3}; cout << findElement(a, 3, 3) << endl; }
Negative test case
int main() { int a[] = {1,2,3}; cout << findElement(a, 3, 7) << endl; }
Negative test case for assert when array is NULL
int main() { int *a; a = NULL; cout << findElement(a, 3, 7) << endl; }
Negative test case for assert when array size is 0
int main() { int a[] = {1,2,3}; cout << findElement(a, 0, 3) << endl; }
Example 2: function to check if two arrays are same
#include <iostream> #include <cassert> using namespace std; /** * Function SPECIFICATION: * check if two arrays are the same element for element. * Pre-conditions: * 1. First array is not null * 2. Second array is not null * 3. size of first array is more than 0 * 4. size of second array is more than 0 * Post-conditions: * 1. returns bool true of if arrays have same size * and also elements in the same order * 2. returns bool false if arrays are not of same size * or elements are different or in different order */ bool areArraysSame(int a[], int asize, int b[], int bsize) { assert(a != NULL || b != NULL); assert(asize != 0 || bsize != 0); bool bSame = true; //assume that the arrays are the same //check if really the arrays are the same if (asize != bsize) { bSame = false; } else { //sizes are the same now compare both the array elements for(int i = 0; i < asize; i++) { if (a[i] != b[i]) { bSame = false; break; } } } return bSame; }
Positive test
int main() { int a[] = {1,2,3,4}; int b[] = {1,2,3,4}; if(areArraysSame(a, 4, b, 4)) cout << "Arrays are the same" << endl; else cout << "Arrays are different" << endl; }
Negative test
int main() { int a[] = {1,2,5,4}; int b[] = {1,2,3,4}; if(areArraysSame(a, 4, b, 4)) cout << "Arrays are the same" << endl; else cout << "Arrays are different" << endl; }
Negative test with one array as null
int main() { int *a; a = NULL; int b[] = {1,2,3,4}; if(areArraysSame(a, 4, b, 4)) cout << "Arrays are the same" << endl; else cout << "Arrays are different" << endl; }
Negative test with one of array's size as 0
int main() { int a[] = {1,2,5,4}; int b[] = {1,2,3,4}; if(areArraysSame(a, 4, b, 0)) cout << "Arrays are the same" << endl; else cout << "Arrays are different" << endl; }
page revision: 11, last edited: 15 Dec 2013 11:53