2

Hello I trying to do Testing in my Sort

 public class Sort
        {
    public string MergeSort(string Word)
    {
       
        var tempLetter="";
        string arrangedSort = "";
        List<string> ListLetters = new List<string>();
        arrangedSort = "";
        for (int i = 0; i < Word.Length; i++)
        {
            ListLetters.Add(Word.Substring(i, 1));
        }
        for (int i = 0; i < Word.Length; i++)
        {
            for (int j = i; j < Word.Length; j++)
            {
                if (char.Parse(ListLetters[i]) > char.Parse(ListLetters[j]))
                {
                    tempLetter = ListLetters[i];
                    ListLetters[i] = ListLetters[j];
                    ListLetters[j] = tempLetter;
                }
            }
        }
        foreach (var listLetter in ListLetters)
        {
            arrangedSort += listLetter;
        }
        return arrangedSort;
    }
    }

This is my Unit Test

 namespace Sort_Test
{
    [TestClass]
    public class Sort
    {
        [TestMethod]
          
            public void MergeSortTesting()
            {
                Sort sort = new Sort();
                string input = "dcba";
                string output = sort.MergeSort(input);

                Assert.AreEqual("abcd", output);
            }

        private string MergeSort(string input)
        {
            return input;
        }
    }
}

But it didn't work may I know what is the cause why it didn't work. Sorry Beginner in c#. Kindly help me what is wrong with the code? I also tried searching the solution how to unit testing work but I can't get the solution the expected result is abcd but in the testing dcba.

enter image description here

srk
  • 1,625
  • 1
  • 10
  • 26
anony
  • 49
  • 4
  • In your unit test you call the `MergeSort` method with a string parameter, but the `MegerSort` method you've posted doesn't take any parameters. Please post a [mre]. – gunr2171 Oct 31 '21 at 04:48
  • 1
    I'm pretty sure that `var sort = new Sort();` inside the unit test method just makes a class of itself, not the code you want to test. – gunr2171 Oct 31 '21 at 04:49
  • 1
    FYI "doesn't work" is not a good problem description. Do you get compile time errors or runtime errors? Then post the exact error message. Unexpected result? What did you expect, what did you get? – Hans Kesting Oct 31 '21 at 07:11

3 Answers3

5

First, lets clean up your class a little bit, remove code that you don't need. The less code, the less code there is to read and understand later. In this case, you don't need a class at all. Well, C# being an OOP language forces you to put all code in a class out of principle, but it has no state to hold, so it can be a static class. Static classes don't need to be instantiated, as you will see later. In addition, getting a character array from a string and transforming a character array to a string is not something you have to do by hand. When you remove all that stuff, your algorithm becomes much more clear and concise. I remember merge sort differently, but that is up to you to figure out I guess.

public static class SortAlgorithms
{
    public static string MergeSort(string word)
    {
       var listLetters = word.ToArray();

       for (int i = 0; i < listLetters.Length; i++)
       {
           for (int j = i; j < listLetters.Length; j++)
           {
               if (listLetters[i] > listLetters[j])
               {
                   var tempLetter = listLetters[i];
                   listLetters[i] = listLetters[j];
                   listLetters[j] = tempLetter;
               }
           }
       }

       return new string(listLetters);
    }
}

Now on to the tests: Since we renamed the test class to no longer conflict with your actual class and change the output from int to string, it will actually compile. You also don't need this detour of creating and initializing an object any more.

[TestClass]
public class SortAlgorithmTests
{
    [TestMethod]
    public void MergeSortTesting()
    {
        var input = "dcba";
        var output = SortAlgorithms.MergeSort(input);

        Assert.AreEqual("abcd", output);
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • *In addition, getting a character array from a string and transforming a character array to a string is not something you have to do by hand* - [I tried that too](https://stackoverflow.com/questions/69776976/quick-sort-using-c-sharp) – Caius Jard Oct 31 '21 at 08:31
1

Because you don't initialize the Word Also, The unit test class must have a different name from the main class Sort. and also don't forget [TestMethod] on each method the in unit tests class.
Try this:

public class Sort
{

    public string Word { get; set; }

    public Sort(string word)
    {
        Word = word;
    }

    public string MergeSort()
    {
   
            var tempLetter = "";
            string arrangedSort = "";
           
        List<string> ListLetters = new List<string>();
            arrangedSort = "";
            for (int i = 0; i < Word.Length; i++)
            {
                ListLetters.Add(Word.Substring(i, 1)); 
            }
               for (int i = 0; i < Word.Length; i++)
                            {
                                for (int j = i; j < Word.Length; j++)
                                {
                                    if (char.Parse(ListLetters[i]) > char.Parse(ListLetters[j]))
                                    {
                                        tempLetter = ListLetters[i];
                                        ListLetters[i] = ListLetters[j];
                                        ListLetters[j] = tempLetter;
                                    }
                                }
                            }

                        
            foreach (var listLetter in ListLetters)
            {
                arrangedSort += listLetter;
            }
            return arrangedSort;
    }
}

Then in your unit test:

[TestClass]
public class SortTests
{
    [TestMethod]
    public void MergeSortTesting()
    {
        var sort = new Sort("dcba");

        int result = sort.MergeSort();

        Assert.AreEqual("abcd", result);
    }
}
Milad Dastan Zand
  • 1,062
  • 1
  • 10
  • 21
0

Take a look at the following code:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void MergeSortTesting()
    {
        string input = "dcba";
        string output = Sort.MergeSort(input);

        Assert.AreEqual("abcd", output);

    }
}
class Sort
{
    public static string MergeSort(string Word)
    {
         ...        
    }
}

Set MergeSort method static and don't new Sort class.

Xingyu Zhao
  • 625
  • 7
  • 27