Resources

Code Optimization and Testing in Visual C++

Testing computer software has the obvious impact of detecting bugs or errors in code, but it also can have other benefits such as code simplification and improvements in performance. These secondary benefits are best achieved through having a testing procedure that not only detects bugs but also identifies which functions are being executed the most frequently and which code, if any, never executes under any input conditions. Computer programs often evolve over a long time period and the code can be maintained by a number of different programmers. This can lead to code that is overly complicated, difficult to maintain, and that has poor or moderate performance characteristics.

The Microsoft Visual C++ compiler is packaged with a profiling utility that can play a big part in a testing plan and that can help solve the problems described above. A profiler is a program that will analyze a computer program as it runs. The Visual C++ profiler utility program can be setup to identify a variety of information about a computer program while the program is running. It can tell you how many times each function in your program is executed. This can be a great benefit in determining where code optimization will have the most benefit. The profiler can also be setup to identify which functions in your program execute while a series of testing routines are run. It can also determine if your test cases are testing all parts of your program.

The term 'coverage' is often used to indicate what portion of the code in a program executes when a particular series of test cases is run. A series of test cases that cause every line of code in your program to execute is said to have 100% coverage. There are a couple of different methods that are frequently used to produce test cases that have 100% or nearly 100% coverage. Both methods involve testing the functions that makeup your program individually. One method is to create a default test case for each function in your program and then randomly vary the inputs. The idea is to randomly vary the function inputs enough times to cause every line of code in each function to run. The other method is to review the code for each function that makes up your program and create a minimal set of input cases that will cause every line of code in the function to execute. You can determine if 100% coverage is achieved by running the profiler utility with the appropriate settings against your testing program.

Both methods of testing have their advantages and disadvantages. The randomization of inputs method is good at creating unusual input values that you might not expect the functions to receive. It effectively tests how the function will behave with bad inputs or abnormal inputs. However, it may be necessary to run a very large number of random test cases before every line of code in a particular function will execute. The method of reviewing the code for the function has the advantage of achieving 100% coverage with a minimal number of test cases. However, this method is poor at finding problems that are caused by bad inputs or unexpected inputs. Using both methods together is an effective way to achieve good testing results. Although having test cases that achieve 100% coverage doesn't guarantee that a program is bug free, it is certainly a step in the right direction.

Code profiling tools are also available for other programming languages and platforms. These programs may be distributed with the compiler or they may need to be purchased from third party toolkit vendors. Internet search engines can help identify how or if these tools can be provided for a particular programming language on a specific platform. In summary, a good testing procedure not only finds the bugs but also helps simplify and optimize code and make your programs more robust. Testing provides one of the best opportunities to increase customer satisfaction with your software. Many developers play both the role of program developer and program tester. For such a developer the lessons learned from a good testing procedure will benefit him/her immediately. In cases where a programmer is solely responsible for software testing, it is important to convey the information gathered during the testing process back to the software development team so that everyone may benefit from the information. Doing testing the right way and communicating the results can teach developers to write simpler, faster code and reduce the complexity of testing future versions of a product.

Disclaimer

Your use of the information in this article is at your own risk. The information in this article is provided on an "as is" basis and without any representation, obligation, or warranty from FINCAD of any kind, whether express or implied. We hope that such information will assist you, but it should not be used or relied upon as a substitute for your own independent research.

For more information or a customized demonstration of the software, contact a FINCAD Representative.