Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.

Similar presentations


Presentation on theme: "Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial."— Presentation transcript:

1 Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial credit. See the web page for details. Late assignments will not be accepted, so please turn in early, turn in often, and double check your work. 1

2 CSc 352: Testing and Code Coverage

3 Testing and test cases int main() { read x; if (x is odd) { compute payroll data; } else { delete all files; send rude email to boss; crash computer; } 3 make sure you use at least fifty different test inputs 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, … It isn’t enough to have a lot of test cases. We have to make sure our tests “cover” the program adequately.

4 gcov: a code coverage analyzer 4 test coverage program – indicates how many times each line was executed – marks code that did not get executed – cumulative over a set of tests helps you understand – how effectively your current test cases “cover” the code – what additional test inputs you need in order to get better coverage needs the program to be compiled with additional gcc options

5 testing didn’t execute all of the code An example 5 input additional compiler flags

6 testing didn’t execute all of the code An example 6 input

7 An example 7 input

8 An example 8 input

9 Code coverage and testing Just because every line has been executed does not mean the program has been tested thoroughly – we may want to test the same line of code under different conditions e.g.: a loop should be tested with values that cause 0, 1, and “many” iterations However, if some lines are not executed the program is definitely not thoroughly tested – gcov helps us identify and fix this exception: “system errors” that may be difficult to create 9

10 Example of not enough testing 10 This is (almost) the program we wrote in class to convert all lower case letters in a string to upper case.

11 Example of not enough testing 11 Compile and test it. 100% of code is executed and the result is correct. So the code has no bugs, right?

12 Example of not enough testing 12 Compile and test it. 100% of code is executed and the result is correct. So the code has no bugs, right? Oops! There were still errors.

13 gcov: summary code coverage testing tool – works with gcc; needs additional compiler flags gcc –fprofile-arcs –ftest-coverage … shows execution frequency of each line of code – reports % of lines covered by tests coverage values are cumulative delete *.gcda file to start afresh – how many times each line was executed – highlights lines not executed 13

14 Pointers A pointer in C holds the memory address of a value – the value of a pointer is an address – the value of the memory location pointed at can be obtained by “dereferencing the pointer” (retrieving the contents of that address) 14 10004 memory locationaddress 12910000 7710001 3154210002 10810003 9710004 54210005 10065210006 pointer dereferencing: * p p

15 C pointers vs. Java references C pointers a pointer is the address of a memory location – no explicit type information associated with it arithmetic on pointers is allowed, e.g.: *(p+27) Java references a reference is an alias for an object – references have associated type information arithmetic on references not allowed 15

16 Declaring pointer variables Two new operators (unary, prefix): – & : “address of” * : “dereference” Declarations: – a pointer x to something of type T is declared as T *x Example: int *p; // p: pointer to an int char **w; // w: pointer to a pointer to a char 16

17 Using pointer-related operators If x is a variable, &x is the address of x If p is a pointer, *p is the value of whatever points to *(&p)  p always 17

18 Arrays An array in C is just a contiguous sequence of memory locations – size of each element depends on type – the length of the array is not part of the array type – the language does not require that array accesses be checked to be within the array bounds out-of-bound accesses result in bugs, security flaws (“buffer overflow vulnerabilities”) 18

19 More arrays Consider an array declared as: int A[20]; – the value of A[ i ] is the contents of the memory location occupied by element i of A; – the value of A is the address of the array A, i.e., &(A[0]); this does not have size information associated with it. 19

20 More arrays To pass an array as an argument to a function, you pass the array name – since the value of the array name is the address of the array, what is actually passed is a pointer to the array This does not have size information associated – the called function does not know how big the array is – need to provide a mechanism for callee to figure this out: either pass the size of the array separately; or terminate the array with a known value (e.g., 0) 20

21 scanf() and pointers To read input using scanf(), we have to provide: – a format string with conversion specifications (%d, %s, etc.) that says what kind of value is being read in; and – a pointer to (i.e., the address of) a memory area where the value is to be placed Reading in an integer: int x; scanf(“%d”, &x); // &x  address of x Reading in a string: char str[…]; scanf(“%s”, str); // str  address of the array str 21

22 Example 1 22 str_rev is a function of type “char *”, i.e., returns a pointer to a character the argument is an array (its size is not part of its type) string library functions array  pointer

23 Example 1… 23 figure out where the ‘\0’ is use this to control how many array elements to processes

24 Example 1… 24

25 Example 2: string reversal using pointers 25 array  pointer

26 Example 2… 26 abcd\0 s tptr

27 Example 2… 27


Download ppt "Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial."

Similar presentations


Ads by Google