I just decided to write a unit test, and see how to do it. This is my unit test:
[TestMethod]
public void TestSearchSort()
{
System.IO.File.WriteAllText(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", string.Empty);
var TestInstance = new Program();
var titles = new List<string>();
string[] query = { "main", "menu" };
Program.Search(ref titles, ref query);
var expectedTitles = new List<string>();
string[] expectedTitleStrings = { "The Main Menu", "OneNote", "The Text Menu",
"The Text Block Menu", "The Table Menu", "The Table Cells Menu",
"The Draw Menu", "The Drawn Items Menu", "The Picture Menu",
"The File Menu", "Draw", "Windows Phone Notebooks",
"Windows Phone Sections", "Windows Phone Pages" };
expectedTitles.AddRange(expectedTitleStrings);
using (var file = new System.IO.StreamWriter(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", true))
{
file.WriteLine("Expected Count: " + expectedTitles.Count);
file.WriteLine("Actual Count: " + titles.Count);
}
Assert.AreEqual(expectedTitles.Count, titles.Count);
for (var i = 0; i < titles.Count; i++)
{
using (var file = new System.IO.StreamWriter(@"C:\Users\Hosch250\Documents\Visual Studio 2013\Projects\ConsoleApplication14\ConsoleApplication14\TestFile.cs", true))
{
file.WriteLine("Expected Title: " + expectedTitles[i]);
file.WriteLine("Actual Title: " + titles[i]);
}
Assert.AreEqual(expectedTitles[i], titles[i]);
}
}
This is the method it tests:
public static void Search(ref List<string> resultTitles, ref string[] query)
{
List<int> weight = new List<int>();
int position = -1;
foreach (string[] array in SearchKeys.Keys)
{
position++;
int length = array.Length;
int middle = length / 2;
char firstCharMidArray = array[middle][0];
foreach (string s in query)
{
int min = array[middle][0] < s[0] ? middle : 0;
int max = array[middle][0] <= s[0] ? array.Length : middle + 1;
for (int i = min; i < max; i++)
{
weight.Add(0);
if (array[i] == s)
{
if (weight[position] == 0)
{
resultTitles.Add(array[0]);
}
weight[position]++;
}
}
}
}
StableSort(ref resultTitles, ref weight);
}
I write to the file in the test method so I can see where/how it failed. How did I do? Should I be doing anything different? Should I have more tests? I most certainly cannot run every possible combination of search terms.