I have created a program which uses the bubble sort to generate random numbers and sort them using a bubble sort. I then created two functions which would count the number of compares and swaps, Te problem is I have absolutely no idea on how to test if the number of compares and swaps are correct. How would i go about testing for this?
I would start by black box testing. Are the elements properly sorted afterward?
Additionally, you could create a small data set for a specific scenario. Such as 1 3 2; you know how many comparison and swaps there will be to sort it.
Lastly, you could post your function here and we will review it.
Thank you for replying, so you are saying I need to enter in say a sort for 1 number, 2 numbers and three numbers, then see if it performs the correct number of compares and swaps?
Also, if this does give the correct number of cmpares and swaps, would that mean that the program would output correct results for others? such as 50 and 100?
You'll have to verify the compares and swaps yourself but I have a great suggestion for testing the result of function. Generate a random-length vector/array of random integers and make a separate copy of it. Then sort one with your function and one with the STL sort algorithm. Afterward, compare the results... Automate this process and let it loop for a while to see if you find a counterexample data set that breaks your function. This will not guarentee that your function works; the test is based on the fact that it is very difficult to prove something but it only takes a single counterexample to disprove something. Good luck.