Download Ada in Action
Transcript
Chapter 5. Testing Software Components and Programs 132 location near the origin where a division by a value near 0 occurs), and several other points that are moderately over threshold. The first method will tell you many points failed, but won't tell you the magnitude of the one spectacular failure. You might look at four or five points, see they are all just barely out of tolerance, and decide it's close enough. The second method will tell you the magnitude of the failure at that one awful point, but not tell you that there were a dozen other points that were 20% over the threshold. You might look at the one wild point, decide it is a pathological case that can't occur in normal operation, and think everything is OK. The even functions, such as square root, are partially symmetrical. You can square 2 to get 4 and take the square root of 4 to get 2 again, but the method doesn't work when the original value is -2. This doesn't mean you can't use the method on partially symmetrical functions, you just have to be more careful. When testing the square root, for example, you could test it with positive values expecting to get the original value back. Then test a second time with all negative values, expecting to get the absolute value back. When testing trigonometric functions, you may want to test one quadrant at a time because inverse functions return mirror images of the input angle in certain quadrants. (The Arcsine of the Sine of 100 degrees is 80 degrees). Inverse testing may leave some gaps in the test suite. The square root test described above won't tell you what will happen if you try to take the square root of a negative number because all the input values were created by squaring a positive or negative number, the result of which can never be negative. Furthermore, if you are testing a 32-bit integer square root, the test cases will be sparse at the higher values. If you square 46,339 it will yield the test input 2,147,302,921. Squaring 46,340 results in 2,147,395,600. There are 92,679 values between 2,147,302,921 and 2,147,395,600 that can never be tested using this approach. 5.2.3. Manufactured Data Embedded computers are often used in applications where the input signals are corrupted by noise. An algorithm must separate the true signal from the noise. Again the question is, What is truth? This sort of situation doesn't lend itself to the inverse function testing we just discussed. You can put noisy data in the input of a filter and get clean data out, but you usually can't shove the clean data in the output of the filter to reproduce the same noisy input. If you have an established algorithm that is believed to work, you can put faith in its results, using the first method we discussed. You just record some noisy, real-world data, process it with both algorithms, and compare the results. But if you are developing an entirely new algorithm, then you don't have an old one to tell you what the right answer is. If you do have an old one, and it gives different results than the new one, then it might test your faith. Are you really sure the old algorithm gives the right answers? There is likely to be some doubt in your mind. In situations like these I like to use a different approach. I manufacture some realistic input data by starting with clean data and adding artificially generated, known noise. Then when I use an algorithm to separate the signal from the noise, I know exactly what the correct answer should be. The description of a valid input signal for your algorithm depends entirely on the problem, but generally you can create a pure, valid input signal by adding several sine waves with various amplitudes, frequencies, and phase differences. You can compute the value of this input signal at the moments of interest and put it in a file called CLEAN.DAT. Use this as the input to your algorithm and store the resulting output in TRUTH.DAT. The next step is to create some realistic corruption. Maybe the signal is corrupted by frequencies that are out of band, or periodic noise spikes. If the signal is likely to be corrupted by gaussian noise, you can use the RANDOM_NUMBERS.Noise function to create a noise waveform. If appropriate you can add a bias Ada in Action