private void BtnCompute_Click(object sender, EventArgs e) { IDataValidator DV = new DataValidator(); IDiskSpace DS = new DiskSpace(); if (!DV.validateInputData(txtUsed.Text, txtTotal.Text)) { txtResult.Text = DV.errorDescription; return; } int minimal = DS.minDrives(DV.used, DV.total); if (minimal < 0) { txtResult.Text = "Invalid parameters to calculate minDrives"; return; } txtResult.Text = $"Total drives: {DV.used.Length}{Environment.NewLine}"; txtResult.Text += $"Total disk space: {DS.TotalAvailiableSpace}{Environment.NewLine}"; txtResult.Text += $"Total used space: {DS.TotalUsedSpace}{Environment.NewLine}"; txtResult.Text += $"== MINIMUM DRIVES REQUIRED: {minimal} =="; }
/*----TEST minDrives WITH KNOWN EXAMPLES----*/ public static void exampleTest() { int[] used = null; int[] total = null; int expectedResult = -1; DiskSpace dS = null; int result = -1; /*---ASK THE NUMBER OF THE EXAMPLE TO BE TESTED----*/ System.Console.WriteLine("Enter the number of the example:"); System.Console.Write("> "); string example = Console.ReadLine(); switch (example) { case "0": /*----CASE 0 DATA----*/ used = new int[] { 300, 525, 110 }; total = new int[] { 350, 600, 115 }; expectedResult = 2; printExampleData(used, total, expectedResult); /*----HERE IS THE minDrives CALL---*/ dS = new DiskSpace(); result = dS.minDrives(used, total); verify(expectedResult, result); break; case "1": /*----CASE 1 DATA----*/ used = new int[] { 1, 200, 200, 199, 200, 200 }; total = new int[] { 1000, 200, 200, 200, 200, 200 }; expectedResult = 1; printExampleData(used, total, expectedResult); /*----HERE IS THE minDrives CALL---*/ dS = new DiskSpace(); result = dS.minDrives(used, total); verify(expectedResult, result); break; case "2": /*----CASE 2 DATA----*/ used = new int[] { 750, 800, 850, 900, 950 }; total = new int[] { 800, 850, 900, 950, 1000 }; expectedResult = 5; printExampleData(used, total, expectedResult); /*----HERE IS THE minDrives CALL---*/ dS = new DiskSpace(); result = dS.minDrives(used, total); verify(expectedResult, result); break; case "3": /*----CASE 3 DATA----*/ used = new int[] { 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49 }; total = new int[] { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, }; expectedResult = 49; printExampleData(used, total, expectedResult); /*----HERE IS THE minDrives CALL---*/ dS = new DiskSpace(); result = dS.minDrives(used, total); verify(expectedResult, result); break; case "4": /*----CASE 3 DATA----*/ used = new int[] { 331, 242, 384, 366, 428, 114, 145, 89, 381, 170, 329, 190, 482, 246, 2, 38, 220, 290, 402, 385 }; total = new int[] { 992, 509, 997, 946, 976, 873, 771, 565, 693, 714, 755, 878, 897, 789, 969, 727, 765, 521, 961, 906 }; expectedResult = 6; printExampleData(used, total, expectedResult); /*----HERE IS THE minDrives CALL---*/ dS = new DiskSpace(); result = dS.minDrives(used, total); verify(expectedResult, result); break; default: System.Console.WriteLine("ERROR: case example {0} does not exist.", example); break; } }
/*----TEST minDrives WITH CUSTOM EXAMPLES----*/ public static void customTest() { /*----ASK THE TOTAL NUMBER OF HARD DRIVES AND ASSIGN IT TO totalHardDrives VARIABLE----*/ System.Console.WriteLine("Enter the total number of hard drives. It must be an integer between 1 and 50:"); int totalHardDrives; bool isInteger = false; bool totalHardDrivesError = false; do { System.Console.Write("> "); isInteger = int.TryParse(Console.ReadLine(), out totalHardDrives); /*----VERIFY IF THE TOTAL NUMBER OF HARD DRIVES IS OK----*/ totalHardDrivesError = !isInteger | totalHardDrives < 1 | totalHardDrives > 50; if (totalHardDrivesError) { System.Console.Write("ERROR: the total number of hard drives must be an integer between 1 and 50. Press ENTER and try again..."); Console.ReadLine(); } } while (totalHardDrivesError); int[] totalArr = new int[totalHardDrives]; /*----ASK THE TOTAL CAPACITY OF EACH DRIVE AND ASSIGN IT TO totalArr ARRAY----*/ System.Console.WriteLine("Enter the total capacity of each drive. It must be an integer between 1 and 1000:"); for (int i = 0; i < totalHardDrives; i++) { int totali = 1; bool totaliError = false; do { System.Console.Write("> drive {0}: ", i); isInteger = int.TryParse(Console.ReadLine(), out totali); /*----VERIFY IF THE TOTAL CAPACITY OF DRIVE i IS OK----*/ totaliError = !isInteger | totali < 1 | totali > 1000; if (totaliError) { System.Console.Write("ERROR: the total capacity of drive {0} must be an integer between 1 and 1000. Press ENTER and try again...", i); Console.ReadLine(); } } while (totaliError); totalArr[i] = totali; } int[] usedArr = new int[totalHardDrives]; /*----ASK THE AMOUNT OF DISK SPACE USED ON EACH DRIVE AND ASSIGN IT TO usedArr ARRAY----*/ System.Console.WriteLine("Enter the amount of disk space used on each drive. It must be an integer between 1 and 1000 and less than or equal to its total capacity:"); for (int i = 0; i < totalHardDrives; i++) { int usedi = 1; bool usediError = false; do { System.Console.Write("> drive {0}: ", i); isInteger = int.TryParse(Console.ReadLine(), out usedi); /*----VERIFY IF THE AMOUNT OF DISK SAPCE USED ON DRIVE i IS OK----*/ usediError = !isInteger | usedi > totalArr[i] | usedi < 1 | usedi > 1000; if (usediError) { System.Console.Write("ERROR: the amount of disk space used on drive {0} must be an integer between 1 and 1000, and less than or equal to {1}. Press ENTER and try again...", i, totalArr[i]); Console.ReadLine(); } } while (usediError); usedArr[i] = usedi; } /*----ASK THE EXPECTED RESULT AND ASSIGN IT TO expectedResult VARIABLE----*/ System.Console.WriteLine("Enter the expected result. It must be an integer between 1 and {0}:", totalHardDrives); int expectedResult = -1; bool expectedResultError = false; do { System.Console.Write("> "); isInteger = int.TryParse(Console.ReadLine(), out expectedResult); /*----VERIFY IF THE expectedResult IS OK----*/ expectedResultError = !isInteger | expectedResult <1 | expectedResult> totalHardDrives; if (expectedResultError) { System.Console.Write("ERROR: the expected result must be an integer between 1 and {0}. Press ENTER and try again...", totalHardDrives); Console.ReadLine(); } } while (expectedResultError); printExampleData(usedArr, totalArr, expectedResult); /*----HERE IS THE minDrives CALL---*/ DiskSpace ds = new DiskSpace(); int result = ds.minDrives(usedArr, totalArr); verify(expectedResult, result); }