home

Creating an Algorithm Test Program in C#

These lab notes describe a program written in C# to test various cryptographic algorithms.

Step 1. Program Entry.

We've defined a new project with a single source file, ktest.cs. We'll start each source file with a simple flower box that identifies the file by name, and includes a description of the program's purpose. For commercial software, follow your company's standards for documentation. Be sure to include appropriate copyright statements.

C# programmers will immediately notice the lack of any "using" statements. We'll introduce them as they become necessary. Avoid automatically inserting "using" statements into your programs if they are not required.

We define a class for our application. Remember that unlike Java, C# does not require that the class name be the file name. For simplicity however, we name our class ktest.

Within the class, we define one public constant and one method. We define a symbolic name, KTEST_SUCCESS, with a value of zero as a public constant. C++ programmers may be more used to defining symbolic literals with the "#define" directive or as enums. Both #define and enum are available in C# but are more restricted in their use.

Our C# program requries an entry point called Main that returns an integer. This method is static so that it can be called outside the context of any ktest class instance.




Step 2. Instantiating a Class Instance.

Next we will create an instance of our ktest class within Main. We will also define a default constructor and destructor for ktest. Since we calling WriteLine, we add a using statement to reference the System namespace. Note that C# destructors are not explicitely called. The C# garbage collector will call the ktest destructor at the end of our program. We include calls to System.Console.WriteLine in the constructor and the destructor. Placing a breakpoint on these calls will illustrate when the class destructor is called.






Step 3. Adding Helper Methods

Our test program will output test results to the console. We consolidate calls to the System.Console.WriteLine method into one ktest method. Isolating system interfaces is a technique that allows more control later over how our program uses .NET resources. We also introduce a new class, krandom, that will encapsulate a pseudo-random number generator. This will be the first algorithm tested by our program. For each algorithm, a separate class, or inherited class, will be defined.

In our class constants, we define strings that will be combined to produce report output. We add a new member to the ktest class, the m_prng member, that will be an instance of krandom. The LogLine method wraps the interface to WriteLine. LogBanner and LogDataTypeSizes construct sets of output lines. Note that by combining the System.String.Format method and our own LogLine method, we can reasonably approximate the C printf function. In the default constructor for ktest, we create an instance of krandom and call our LogBanner and LogDataTypeSizes methods.





The output of running our test program to this point on a 32-bit platform is shown below.




Step 4. Adding a Test Subroutine.

Next, we add four more methods to complete our framework for an algorithm test program. First, we add the Log method to call System.Console.Write. Second, we add the method LogFunctionHeader, which displays a header for each test routine. Third, we add a method, QueryTestRun, which prompts the user whether to run a given test. Fourth, we add the method TestPseudoRandom, which is where we will place our logic to test our pseudo-random algorithm.

We will call LogFunctionHeader at the top of each such algorithm test method. We will call QueryTestRun for each algorithm method. To allow QueryTestRun to refer to an algorithm method, we define a "delegate" called fnT. We pass a reference to the test method to QueryTestRun as the "fn" argument. We can then call "fn()" because the argument was a delegate type.







The output of our test program now displays the prompt "Run Fast Pseudo-Random Tests?". This prompt was passsed to QueryTestRun as the cPrompt argument. A referece to TestPseudoRandom was passed as the delegate argument fn. Console.ReadLine collects user input. If the user entered "y" or "Y" and Enter, the delegate function is called.




In the next part of this series, we will add code to our TestPseudoRandom method to begin exploring pseudo-random number generation.