James's profileJames McCaffreyBlogLists Tools Help

Blog


    December 22

    Custom .NET Stream Classes

    Writing and testing custom .NET Stream classes are tasks which are not well understood by developers and testers. One of the first things .NET developers learn is how to read and write text files. For example this code uses a built-in FileStream class to read a text file and display its contents in hexadecimal:
     
    Console.WriteLine("Reading TextFile1.txt with StreamReader.");
    FileStream fs = new FileStream("..\\..\\TextFile1.txt",
      FileMode.Open);
    StreamReader sr = new StreamReader(fs);
    string s = sr.ReadToEnd();
    Console.WriteLine("TextFile1.txt contains: " + s);
    byte[] bytes = Encoding.ASCII.GetBytes(s);
    Console.WriteLine("File in hexadecimal   : " +
      BitConverter.ToString(bytes));
     
    A FileStream object is a programming abstraction that allows you to easily Read from, and Write to, a file without having to deal with the many low-level details that actually occur behind the scenes. Now suppose you write a custom .NET Stream class named RotateStream with a Write method that does some sort of bit-rotation to the right, and a Read method which does an opposite bit-rotation to the left. You can think of the Write method as an encoder and the Read method as a decoder. Now consider this code:
     
    FileStream fs = new FileStream("..\\..\\TextFile1.txt",
      FileMode.Open);
    Console.WriteLine("Filtering TextFile1.txt through custom RotateStream");
    RotateStream rs = new RotateStream(fs);
    int b = rs.ReadByte();
    Console.WriteLine("First byte through the transform is " +
      b.ToString("X") + "h");
     
    This code shows you how to use the RotateStream class. One of the reasons why many application developers and testers are not familiar with custom stream classes is that custom stream classes are not absolutely essential. By this statement I mean you can achieve the result of a custom stream implementation without actually creating a custom stream class -- you can achieve the same effect produced by a custom stream class by using built-in .NET stream classes combined with byte-level programming techniques. So, why write a custom stream class? There are two main reasons. First, because a custom .NET stream is an abstraction of a sequence of bytes creating a custom stream class provides you with a generic view of all types of input and output, isolating you from the specific details of the underlying devices. Second, by wrapping byte-level input, output, and transformation routines into a class, you modularize your code making it easier to test and maintain.
    December 11

    CPU Stress Testing

    CPU stress testing is the process of testing a system, while the machine which is hosting the system has reduced CPU resources. Typically testers have a CPU stressor tool which runs on the side, chewing up CPU cycles. While this is going on testers run their regular functionality tests. This simulates what happens when a user of the system under test uses the system while several other programs are running on the host machine. In general there are three different kinds of CPU stressing. First, you want to use up as many CPU cycles as possible to see what happen when the system under test is starved for CPU time. Second, you want to have as many processes running as possible which will create lots of process-context switching to make sure there aren't any strange timing issues. Third, you want as many process being created and destroyed as possible which creates lots of process-creation switching, again to uncover any hidden timing issues with the system under test.
    December 04

    Permutations in Testing

    Permutations are one of the most important concepts in software testing. A permutation is a rearrangement. For example, a string permutation is a rearrangement of strings -- if you start with { "ant", "bat", "cow" }, then there are a total of 6 permutations:

     

    { "ant", "bat", "cow" }

    { "ant", "cow", "bat" }

    { "bat", "ant", "cow" }

    { "bat", "cow", "ant" }

    { "cow", "ant", "bat" }

    { "cow", "bat", "ant" }

     

    One of the reasons why permutations are so important in software engineering is that because all software systems ultimately boil down to a set of inputs, some processing, and a set of outputs, you cannot assume the inputs are in a particular order and you must test your system by feeding it all permutations of inputs. The ability to programmatically generate permutations is a fundamental software engineering skill and is actually quite tricky. The basic operations on string permutations are: creating the first permutation element, generating the successor element for a given element, determining the last permutation in a set, determining the total number of permutations for an initial set of strings, and generating the kth permutation element for a given initial set of strings. My article "String Permutations" in the December 2006 issue of MSDN Magazine (see http://msdn.microsoft.com/msdnmag/issues/06/12/TestRun) has a detailed explanation of these basic operations.