Thread: At a Loss

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    At a Loss


    Okay, so I'm not asking you to do my homework for me, I just need help. It has to do with TDD in C. I was always taught that you need to write the tests first and then write the code. However, my professor gave us the code and told us to write the tests. I'm at a loss, I have no idea where to even start. This portion of the assignment is worth such a small portion of the grade but I can't move on with getting it to "go green" in TDD terms. I asked my professor for help and she basically laughed and told me she doesn't have time to explain. I'm seriously so lost and any help what so ever would be greatly appreciated. The written code that I need to write tests for is a simple swap method and a bubblesort method. If you could just show me an example, like showing me what the test for the swap would look like, I'd definitely be able to do the rest of the assignment. I just need to see examples of tests in C.

    Code:
    /**
    * swaps the values referenced by p and q
    */
       
    void swap (int *p, int *q)
    {
        int tmp;
        tmp = *p;
        *p = *q;
        *q = tmp;
    }
    /**
    * an implementation of bubble sort
    * n is the size of array a
    */
    void bubble (int a[], int n)
    {
        int i, j;
        for (i = 0; i<n-1; i++)
        {
            for (j = n-1; i < j; j--)
            {
                if (a[j-1] > a[j])
                {
                    swap (&a[j-1], &a[j]);
                }
            }
        }
    }

    I know how to write the main() I just don't know how to write the actual tests..
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,378
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Simply create array, for example, of 100 elements and fill them either by entering from console / file - or with random data (using "rand" function).

    Then sort them and after sorting check that array is sorted.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Apparently there is a specific methodology that you are to follow. I doubt that very many here are familiar with that particular methodology, so at most we can just provide the obvious answer to your basic question.

    You have the function and from the parameter list you know what kind of data it expects: an array of int and the number of items in that array. So then the general test procedure that we would follow would be to call that function several times, each time basically doing the following:

    1. Assign an unsorted sequence of integers to an int array. You could simply do this in code, or you could globally declare several int arrays each with an initializer list that you could then either pass directly to the function or else copy into a working array for that purpose, or you could read the test data from a text file and have code that will read it in and stuff the array, or however you want to do it. What would be recommended is that the same sets of data would be used each time you run the test. And, of course, each array would have its length associated with it, however you want to do that.

    2. Print out the contents of each array before the sort, along with whatever other information is pertinent. At first you could just have it display to the monitor, but you would probably want to gravitate towards writing to a text file, in part to handle long reports and in part to have a more permanent record of the results.

    3. Call the function with the test array and its length.

    4. Print out the sorted array and whatever other pertinent information

    5. Move on to the next set of test data, repeating 1 through 4.

    Simple and straightforward. I don't know what else TDD wants you to do.

    Many functions have what are called "boundary conditions" that need to be tested for. I'm sure you must have heard of these. If you can at all do it, you need to present test data that will test those boundary conditions. For example, quicksort is supposed to be efficient, but its efficiency depends on the data that you give it. For example, quicksort is least efficient when it tries to sort a sorted list. So you might want to feed your sort function a sorted list. And maybe also a list sorted in reverse order. If you need to generate a lot of unsorted lists, you might want to think about using a utility program to do that, in which you use rand to create a random list; you could even format the output to be an declaration initialization list in order to save yourself a lot of editing by hand.

    Just some general thoughts.

IMN logo majestic logo threadwatch logo seochat tools logo