You are here:

C++/Verification of Algorithms

Advertisement


Question
Hello Eddie,
 In school, if we write a function like: "int factorial(int x) {}", they will have us verify it by calling the function with a couple different values and then computing factorials by hand and seeing if they match.  But if you have something more complicated, especially if functions have [psuedo] random based algorithms, what kinds of techniques can I use to verify the algorithm was implemented correctly (aside from doing it by hand)?

Answer
Hello Jed, thank you for the question.

What I tend to do is write extensive unit tests for methods like this that test a range of values with a range of outputs. There is a handy tool called CppUnit to help with that. For instance, if you wrote a method that returned a random number between 2 params, you can use hard coded parameters to test, and then test the output to ensure it is between them.

Obviously there is no set in stone way to verify that random algorithms always work, but this can give you a good idea.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.