Like Tree1Likes
  • 1 Post By SirPrattlepod

At a loss

This is a discussion on At a loss within the C Programming forums, part of the General Programming Boards category; Okay, so I'm not asking you to do my homework for me, I just need help. It has to do ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    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.

    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]);
                }
            }
        }
    }

    Any help at all is very much appreciated! Thank you in advance!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    589
    How about starting with a main? Create an unordered array and pass it to the function along with the length. Start with simple test that should work, then you can try to throw some curve balls to make it fail.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    2
    I know how to write the main() I just don't know how to write the actual tests. I don't understand the concept of what they do. I'm the kind of person who learns by seeing examples and I've never seen a test written in C before

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    589
    If you have to do it in functions then just write them like you would a main. The tests are just to see if the code worked the way you expected. You can try and make the array of length 0 to see how it handles it, and other little curve balls.

    Code:
    void test1(void)
    {
         //DECLARE LOCAL ARRAY
         // fill array with random values
         // print unordered array
         //Pass array and legth the bubble function
         //print array to see if it is now ordered
    }

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I don't understand. You say are used to writing the tests first and that you're accustomed to that. In that case pretend your professor hasn't given you the implementation and... write the tests.
    Sebastiani likes this.

  6. #6
    Registered User (^Burt^)'s Avatar
    Join Date
    Sep 2013
    Posts
    26
    I would go for dryrun testing. Traditionally we would create a "DryRun" test sheet with each variable in a column of its own. We would then work our way through a structure diagram of the program manually updating the variable on the test sheet as if they were being done by the program its self.

    Part of the test design would be to select what inputs we were going to use (based on the scope of the question) and what our expected outcome would be.

    Then using the inputs to work through the structure and recording on the test sheet we test that the outcome is the expected outcome.


    This made perfect sense in my head, lets hope it helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loss of zero
    By Freshquiz in forum C Programming
    Replies: 6
    Last Post: 01-31-2011, 12:00 PM
  2. Possible loss of data?
    By KnightAdz in forum C++ Programming
    Replies: 4
    Last Post: 03-07-2008, 12:45 PM
  3. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 11:25 AM
  4. At a loss
    By Twiggy in forum C Programming
    Replies: 5
    Last Post: 04-02-2003, 08:54 PM
  5. Sad loss
    By C_Coder in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-09-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21