/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; // Introduction to Software Testing, www.introsoftwaretesting.com // Authors: Paul Ammann & Jeff Offutt // Chapter 2, section 2.3, slide 15 internal class stats { // Example for data flow. Has some issues. // The calculation of mean fails with an empty numbers list. // The calculation of var fails with a numbers list of length one. public static void computeStats(int[] numbers) { int length = numbers.Length; double med, @var, sd, mean, sum, varsum; sum = 0.0; for (int i = 0; i < length; i++) { sum += numbers[i]; } med = numbers[length / 2]; mean = sum / (double)length; varsum = 0.0; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers[i] - mean) * (numbers[i] - mean)); } @var = varsum / (length - 1); sd = Math.Sqrt(@var); Console.WriteLine("median: " + med); Console.WriteLine("variance: " + @var); Console.WriteLine("standard deviation: " + sd); } public static void Main(string[] argv) { // Driver method for computeStats int[] numbers1 = new int[] { 1, 5, 7, 9, 10, 15 }; Console.WriteLine("TEST 1"); Console.WriteLine("Input: " + numbers1.ToString()); computeStats(numbers1); Console.WriteLine("\n----------------------------------"); int[] numbers2 = new int[] { 42 }; Console.WriteLine("TEST 2"); Console.WriteLine("Input: " + numbers2.ToString()); computeStats(numbers2); Console.WriteLine("\n----------------------------------"); int[] numbers3 = new int[] { }; Console.WriteLine("TEST 3"); Console.WriteLine("Input: " + numbers3.ToString()); try { // Expect ArrayIndexOutOfBoundsException computeStats(numbers3); // Throws ArrayIndexOutOfBoundsException } catch (System.IndexOutOfRangeException) { Console.WriteLine("ArrayIndexOutOfBoundsException thrown."); } // This doesn't compile //Console.WriteLine ("\n----------------------------------"); //int [] numbers4; //Console.WriteLine("TEST 4"); //Console.WriteLine("Input: " + numbers4.ToString()); //computeStats (numbers4); } }