Presentation & Discussion on Testing and Debugging Share your experience Learn from each other
Common Testing and Debugging Problems in Candiceland The Gold Star Award - Only applies when you discover that the reason nothing you try works is because you did something so silly that you’ve been overlooking it for hours - Common examples include simple logic errors, forgotten semicolons or curly braces - This simple problem produces an incomprehensible error message - For each hour that this simple error stumps you, you get a gold star! The Unhandled Exception - Can sometimes be solved by using the debugger to step through the code line by line and carefully keeping track of where everything is - This requires a phenomenal amount of patience, time, and dedication
How to Program with Fewer Errors - Do not try to program when you are tired, have had too much sugar, or are trying to knit -The more incomprehensible the error message, the simpler the solution to the problem and the harder it is to find - Keep careful track of variables and dynamic memory
Common Mistakes in Code: Having brackets in the wrong place By misplacing a bracket a whole function might not work or work incorrectly Not checking things (ex: assert checks, if statements, etc.) If values are not checked before the function continues functions won’t properly run and the rest of your program will run into errors when a wrong value is passed Katie Ouellette
How to find errors in your code: Make test code to run through the program Brake the program down into steps ex: When test code is given for you and you don’t know where your code has an error, add a cin statement so that the code will stop and prompt you. This way you know exactly where the code had a problem and you can work from there Print the code out and work through each function by hand. Tracing the values and make sure everything is in the right order. Draw pictures of what is happening with your data and your assignments Test each function with all different kinds of test data to stretch the code. Maybe the error is occurring at the edges of the code’s capability
Katie Ouellette To Write Code Without Errors: Plan the code out as much as you can before hand Write pseudo code Walk through the pseudo code to see how it will work Don’t go to fast! - This will just lead you to not see any errors you make in your code Step away from your code for awhile if you are having a problem. Then when you come back you can see it with fresh eyes.
TESTING AND DEBUGGING By Tasnia Tahsin Common mistakes in my code: Wrong pseudocode- often miss certain conditions that have to be considered Syntax errors- mainly placing semi colons or brackets at wrong places How I discover and fix errors in my code Testing: Test each function in my code with the test program using inputs that are most likely to cause errors like boundary inputs.Test each function in my code with the test program using inputs that are most likely to cause errors like boundary inputs. Choose different inputs to execute each line of codeChoose different inputs to execute each line of code Check code using ‘exam’ fileCheck code using ‘exam’ file Debugging- use debugging tool to fix errors
TESTING AND DEBUGGING By Tasnia Tahsin The debugging tool I use and how Use Visual Studio debugging tool First place breakpoints before functions which contain bugsFirst place breakpoints before functions which contain bugs Step through each line of code and step into functions present there until the exact point where the bug is present is foundStep through each line of code and step into functions present there until the exact point where the bug is present is found Fix the bugFix the bug How to write a program with less bugs in the first place Take into account the different conditions that have to be handled within the function Reason carefully and write a good pseodocode Remember the invariant of the class while writing the code
Nina’s Slides Common Mistakes of My Programs -Not thinking about error checking when I first type out the code. -Usually leads to segmentation faults. ☺ -Simply forgetting to watch out for syntax errors when I first write the code. -The compiler usually catches these. Discover and Fix -When the compiler catches the syntax problems, I can usually just go to the line with the syntax error and correct it. -This usually takes a few minutes. -But when I get a segmentation fault from a run-time error, it usually takes me a while to step through the code to find where my code hit and error. -This can take a good ten minutes if I’m lucky. But during the time I do my homework, writing the code takes 30 minutes, debugging takes an hour or more.
Debugging Tool Used -When debugging, I usually run the program in debug mode, so when it hits an error, I can break from the running program to look at the code and where the error is happening. -If it is not as easy as looking at the code where the error happened, I look at values of variables to see what made the program angry. Then I try to find the last function of mine that manipulated the data in that variable. -If all else fails, I step through the program, watching the values of the variables for each step, until I see where the erroneous value comes in. How to write a program with less bugs -To write a program with less bugs, I try to look more that how bad data will affect my functions. Using this knowledge I try to program more error checking into my functions, so that segmentation faults can happen less. -Also, I try to comment every line I write to make sense to me. If the way I wrote the program can’t be articulated into something that makes sense, I delete it and write a new line to take it’s place that does make sense.
My Common Errors a tragedy in two acts by Becca Groveman Think before you code! Gold Star Problems A common mistake: –can you find the gold star? class Example { … }
My Common Errors ACT TWO The Debugger and Why I Am Reluctant To Use It
Testing and Debugging Silvia-Dana Marin
The common mistakes in my programs Boundary limits in loops (for loops, while-loops) Dangling pointers (within linked lists) Forgetting to add preconditions to the methods
How discovered and fixed them While testing the methods with the test program By verifying the post and pre conditions for the methods in the documentation file Through the exam files →Most errors could be fixed by re-writing the pseudo-code for the methods (especially in the case of loop-boundaries) and by drawing the way the memory of the computer works →Some of the errors were discovered and fixed by using the debugger
The debugging tool you use and how I use the Debugger Toolbar provided with Visual Studio 6.0 for C++
How to write a program with less bugs in the first place By verifying that all the preconditions and post conditions are fulfilled by the method By testing the pseudo-code before beginning coding (especially in the case of boundary limits or pointers and deletions of pointers) By testing each function at a time And by beginning with a smaller number of functions for each class
Debugging Technique Self-testing Test boundary values Test random values When testing Identify where is wrong Correct only that part and try again
Example1 void sequence::operator =(const sequence& source) { //if (data == source.data) return; list_clear(data); list_copy(source.data, data, tail); if (source.prev != NULL) prev = list_search(data, source.prev->data()); else prev = NULL; if (source.curr != NULL) curr = list_search(data, source.curr->data()); else curr = NULL; tail = data; if (tail != NULL) while (tail->link() != NULL) tail = tail->link(); total = source.total; } SELF-ASSIGNMENT TEST FAILED – it ’ s easy to see that the commented statement is missing
Example2 void sequence::operator =(const sequence& source) { if (data == source.data) return; //list_clear(data); //list_copy(source.data, data, tail); if (source.prev != NULL) prev = list_search(data, source.prev- >data()); else prev = NULL; if (source.curr != NULL) curr = list_search(data, source.curr- >data()); else curr = NULL; tail = data; if (tail != NULL) while (tail->link() != NULL) tail = tail->link(); total = source.total; }
Example2 TEST1 (attach/insert) Failed! (in addition to assignment operator, heap leak testing, and you get a crash) … (the first, second case okay) I am now using attach to put 10,20,30 in an empty sequence. Then I move the cursor to the start and insert 5. Testing that size() returns 4... Passed. Testing that is_item() returns true... Passed. The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct... Cursor fell off the list too soon. Failed. Test of the list's items failed.
Example2 int test1( ) { sequence empty; // An empty list sequence test; // A list to add items to … // Test the insert function to add an item at the front of a list cout << "I am now using attach to put 10,20,30 in an empty sequence.\n"; cout << "Then I move the cursor to the start and insert 5." << endl; test = empty; test.attach(10); test.attach(20); test.attach(30); test.start( ); test.insert(5); if (!correct(test, 4, 0, items1)) return 0; }
common mistakes for loops (flipping greater than and less than signs) not moving current to the right spot so entry gets attached/inserting to the wrong spot (one off) increasing used twice CLS
finding errors Run exam files to find if you have incorrect output. If so, run test file and/or debug My debugging technique: If applicable, comment out all functions except the one you're testing and essential functions (constructors, destructors, etc.) Test input for a certain function If the output is incorrect, use cout statements to pinpoint location of error Use either given test files or write your own to test specific functions CLS
debugging tool Eclipse has a debugger: allows you to set breakpoints allows you to trace variables I don't use this often cout statements work better for me CLS
for fewer errors before you start: write pseudocode make a list of common possible errors that may occur with a given function and take those into consideration when writing the code for your function write functions one at a time and try them out one at a time CLS