J
J
Jay Marlow2015-12-14 13:05:26
C++ / C#
Jay Marlow, 2015-12-14 13:05:26

How to test adding an element to a C# list?

Good day!
I can't figure out how to test the following block in the code:

public static class List  //Абстрактный класс работы со списком на основе двух типовых элементов
    {
        public static void Add(ref SimpleElm firstElm, string value)  //Добавить в список простой элемент
        {
            if (value == "") return;
            SimpleElm newElm = new SimpleElm(value);
            if (firstElm == null)
            {
                firstElm = newElm;
                return;
            }
            SimpleElm elm = firstElm;
            while (elm.next != null)
                elm = elm.next;
            elm.next = newElm;
        }
}

Actually SimpleElm itself is below:
public class SimpleElm // Простой класс
    {
        public string Value
        {
            get;
            set;
        }
        public SimpleElm next;
        public SimpleElm(string value = "")
        {
            Value = value;
            next = null;
        }
        public SimpleElm this[int index]
        {
            get
            {
                if (index < 0) throw new IndexOutOfRangeException();
                var item = this;
                for (int i = 0; i < index; i++)
                    if (item.next != null) 
                        item = item.next;
                return item;
            }
            set
            {
                if (index < 0) throw new IndexOutOfRangeException();
                var item = this;
                for (int i = 0; i < index; i++)
                    if (item.next != null)
                        item = item.next;
                item = value;
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-12-14
@kolumbou

You can't start testing just because you decide to write code first and then test it. This is not true! When you decide to write code, you need to at least describe its task somewhere. TDD almost one to one took the approach from mathematicians. Mathematicians also have "Given" - this is an analogue of SetUp and "Need to be done" - this is an analogue of your code under test, i.e. what you implement and "To satisfy the conditions...." - This is very similar to Assert.
Throw away your code as if you haven't written it yet. Read the formulations of several mathematical problems. Check out their style. How do they shape thoughts when presenting the essence of the problem. And try to follow exactly the same! That is, describe your programming problem before starting the solution, as mathematicians do.
/offtop:
I recommend that you write down possible cases when adding an element to the list. For example:
Positive cases:
* Add a normal element;
etc. etc.
Negative:
* Add Null;
* Add when the list is already full - is this possible?;
* Add when the list is not fully created - is this possible?
etc. etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question