Bastardo
Bastardo

Reputation: 4152

Converting list of strings to list of ints capacity of lists are different causes not to pass unit test

I was trying to write a piece of code for converting a list of strings to a list of ints I got the List<int> list = listOfStr.ConvertAll<int>(delegate(string s) { return ConvertStringToInt(s); }); line from ConvertAll .

    public static List<int> ConvertListOfStringToListOfInt(List<string> listOfStr)
    {
        List<int> list = listOfStr.ConvertAll<int>(delegate(string s) { return ConvertStringToInt(s); });

        return list;
    }
    /// <summary>
    /// converts the given str to integer
    /// </summary>
    /// <param name="str">string to be converted</param>
    /// <returns>returns the int value of the given string</returns>
    public static int ConvertStringToInt(string str)
    {
        int convertedValue = int.Parse(str);

        //(int.TryParse(str, out convertedValue))
        return convertedValue;
    }

The code is working fine except one thing.

I created Unit Test for the above method, the TestMethod is below

        /// <summary>
        ///A test for ConvertListOfStringToListOfInt
        ///</summary>
        [TestMethod()]
        public void ConvertListOfStringToListOfIntTest()
        {
            List<string> listOfStr = new List<string> { "1", "2", "3"}; // TODO: Initialize to an appropriate value
            List<int> expected = new List<int> { 1, 2, 3}; // TODO: Initialize to an appropriate value
            List<int> actual;
            actual = Conversions.ConvertListOfStringToListOfInt(listOfStr);
            Assert.AreEqual(expected, actual);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

I have given the same values into the list I pass the method and expected list, just different by type(expected is a list of integer and passed list is a list of strings). I run the test and got this Error Message:

Assert.AreEqual failed. Expected:<System.Collections.Generic.List`1[System.Int32]>. Actual:<System.Collections.Generic.List`1[System.Int32]>.

Well, the types are actually equal so I thought there might be something different inside the lists and I debugged it.

It turned out listOfStr.Capacity is 4 and has null item as the [3] item in it's items member, for same place in items member expected has a 0 and expected.Capacity is 4,too.

However, actual.Capacity is 3 and it actually does have 3 items in it's items member.

I tried to newing actual before filling it and using list.Add() for adding values to expected and newing list in the ConvertListOfStringToListOfInt method. But the capacities are still the same. I do not want to set the capacity manually because this method will not be used for lists which has determined capaties.

What can I do to pass this test? Why is the capacities different? And how is the capacity of lists are determined, what do they depend on?

This is .NET Framework 4.0.

Upvotes: 0

Views: 654

Answers (3)

John Koerner
John Koerner

Reputation: 38077

Try this:

Assert.IsTrue(actual.SequenceEqual(expected));

The reason being is that lists are complex objects and equality for lists is not determined by each element being the same, rather it is defined as are they pointing to the same list. The sequence equal method iterates the lists for you and determines equality at the item level.

If the items of the list were complex types, then it would use the default equality comparer for those items.

Upvotes: 2

FrankE
FrankE

Reputation: 313

Have you tried:

Assert.IsTrue(expected.SequenceEqual<int>(actual));

Upvotes: 1

Bas
Bas

Reputation: 27095

The capacity of a List<T> is 4 by default. It will double every time an element is added that exceeds the current capacity if items are added one by one.

http://msdn.microsoft.com/en-us/library/y52x03h2.aspx

In my opinion, you shouldn't worry about the capacity in this case. To make your unit test pass you could change the initialization of the list:

List<int> expected = new List<int>(3) { 1, 2, 3};

Due to optimizations in the ConvertAll method will take the old capacity.

One tip:

If you are not concerned about optimizing every bit of performance, you could replace this method by

listOfStr.Select(s => int.Parse(s)).ToList();

Just to have it more maintainable.

Upvotes: 2

Related Questions