Reputation: 9229
I am trying to test some C++ code, using Visual Studio 2015's built in unit test framework. I have written some code which is testing an hourly, daily & weekly model, to see which one most reliably fits a set of real life data, by calculating the Coefficient of Determination (R^2).
The real life data needs to be "Read" off of a graph, I have a tested and validated routine which does this.
I know that I am expecting the weekly model to fit perfectly, and the other two won't. To dot his on paper would require ~300 equation calcs to get the values I am working with and then work out the R^2 value. I would rather not do that...
How can I test this code? I am new to Unit Testing as a concept and am still understanding the paradigms used when testing functions.
My Code:
Where History and Graph are Vectors of Vectors.
float calculateDeviation(int period) {
int x1 = history.front()[0];
int x2 = x1 + period;
int i = 0, minimum, maximum, SSres = 0, SStot = 0, j=0, R2 =0;
float average = 0;
int endTime = history.back()[0];
while (x2 <= history.back()[0]) {
average += averageGradient(x1, x2);
x1 = x2;
x2 += period;
}
x1 = history.front()[0];
x2 = x1 + period;
i++;
while (j < i) {
SSres += pow((graph[i][0]-((graph[i][2]*graph[i][0])+graph[i][3])), 2);
SStot += pow(graph[i][0] - average, 2);
x1 = x2;
x2 =+ period;
j++;
}
R2 = 1 - (SSres / SStot);
return history.back()[0];
}
Upvotes: 0
Views: 93
Reputation: 11
I'm a practicing Test Engineer, and based on what you've provided, you should be able to write a basic unit test with the following:
Remember to prepare several sets of input data, including boundaries (minimums, maximums, and values that should fail). This is often the breaking point, so be sure to run tests that require error handling. If you can think of a way to break your code, then you've successfully identified an area that needs some tweaking. And that, my friend, is the entire point. :)
Upvotes: 1