• Post Reply Bookmark Topic Watch Topic
  • New Topic

Test driven development - how do you suggest i test this code for correctness ?  RSS feed

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing insertion sort (a type of algorithm for those who dont know). I made the code and it works for one sample input.
Of course, I could test it using more inputs. But, that would be inconvenient, especially if the code is much more complicated that mine.

So, can you suggest some strategies for testing this code and ensuring that it is 100% bug free ? I know that no ("complex") software can be bug free. But, my code can because its so small
and does very little.

For a nice visual tutorial, see the video. The first one is insertion sort -



code -

 
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re your subject line - it's a bit late to apply Test Driven Development when you have already written the code.

There are plenty of on-line papers on testing theory which will cover testing in far more detail than I can here.
There are also a number of testing frameworks available to assist you in developing and running tests such as JUnit.

Looking specifically at your code:
In your class you have 2 constructors so you must provide tests for both of them.
When testing the constructor taking the int parameter you should test with -1, 0, 1 and a number greater than 1. And you should call both methods on the created objects and confirm the output is as expected.
When testing the constructor taking the array parameter at the very least you should test with a null, an empty array, an array of size 1 and some arrays with a size greater than 1. Of these you should have a fully sorted array, a randomised array, and array with duplicate values. And you should call both methods on the created objects and confirm the output is as expected.

Note: some of these tests will throw exceptions but if that is what is supposed to happen then great, the test has passed. In frameworks such as JUnit you can declare a test as throwing an Exception and the test only passes if the specified exception is thrown.
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ! In addition to the useful points you gave me, i was wondering if you could suggest some books that discuss testing techniques and strategies for beginners.

I removed the Sort(int size) constructor, wasn't really needed. I guess I did all the necessary test cases in my main -


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!