In: Computer Science
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with MSTest. My program is below. I just need screen shots of the unit test code in Visual Studio 2019 with MSTest. Please and thank you.
so easentially I need someone to write some unit tests
with my program in MSTest visual studio 2019. Then I need the unit
tests supplied as answers.
using System;
namespace SelectionSortAlgorithm
{
public class SelectionSort
{
public static void Main(string[] args)
{
int[] dataSet = new int[5] { 2, 99, 27, 68, 3 }; // 5 element array
initialized
int n = 5; // variable declar for elements in loop /
initialized
for (int i = 0; i < n; i++) //iterates elements in
dataSet
{
Console.WriteLine(dataSet[i]); // prints unsorted data
}
int sortedElements, lowestValue; //variable declar / used in next
lines of code for sorting elements
// After every iteration, the unsorted array reduces
by 1
for (int i = 0; i < n - 1; i++)
{
lowestValue = i; // initialize variable / used for
lowest value element
// As array is being sorted, add 1 element to sorted array
for (int j = i + 1; j < n; j++)
{
if (dataSet[j] < dataSet[lowestValue])
{
lowestValue = j;
}
}
sortedElements = dataSet[lowestValue];
dataSet[lowestValue] = dataSet[i];
dataSet[i] = sortedElements;
for (int i = 0; i < n; i++)
{
Console.WriteLine(dataSet[i]);
}
}
}
}
// SelectionSort.cs using System; namespace SelectionSortAlgorithm { public class SelectionSort { public static void sort(int[] dataSet) { int sortedElements, lowestValue; //variable declar / used in next lines of code for sorting elements // After every iteration, the unsorted array reduces by 1 for (int i = 0; i < dataSet.Length - 1; i++) { lowestValue = i; // initialize variable / used for lowest value element // As array is being sorted, add 1 element to sorted array for (int j = i + 1; j < dataSet.Length; j++) { if (dataSet[j] < dataSet[lowestValue]) { lowestValue = j; } } sortedElements = dataSet[lowestValue]; dataSet[lowestValue] = dataSet[i]; dataSet[i] = sortedElements; } } public static void Main(string[] args) { int[] dataSet = new int[5] { 2, 99, 27, 68, 3 }; // 5 element array initialized sort(dataSet); for (int i = 0; i < dataSet.Length; i++) { Console.WriteLine(dataSet[i]); } } } } // SelectionSortTest.cs using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class SelectionSortTest { [TestMethod] public void testSorting() { int[] data = new int[] { 5, 3, 7, 2, 4 }; SelectionSortAlgorithm.SelectionSort.sort(data); CollectionAssert.AreEqual(new int[] { 2, 3, 4, 5, 7 }, data); } [TestMethod] public void testBlankArray() { int[] data = new int[] { }; SelectionSortAlgorithm.SelectionSort.sort(data); CollectionAssert.AreEqual(new int[] { }, data); } [TestMethod] public void testSingleElement() { int[] data = new int[] { 10 }; SelectionSortAlgorithm.SelectionSort.sort(data); CollectionAssert.AreEqual(new int[] { 10 }, data); } [TestMethod] public void testWithNegativeElements() { int[] data = new int[] { -22, 10, -3, -8, 4, 9 }; SelectionSortAlgorithm.SelectionSort.sort(data); CollectionAssert.AreEqual(new int[] { -22, -8, -3, 4, 9, 10 }, data); } }
************************************************** I Have changed SelectionSort class to a little, because While testing, You should have a method, That automatically does the sorting.. Earlier, Your method was taking the input from console, which is not possible in case of unit testing. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.