How to test the correctness of a function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • istillshine@gmail.com

    How to test the correctness of a function?

    When I want to test if a function foo in a .c file behaves as
    expected, I usually call it in my main.c in the following way.



    if (1) { /* test function foo */
    z = foo(x, y); /* */
    assert(z==1);
    }


    When I want to test other functions, I change if (1) to if (0).


    Are there better (and simpler) ways to test correctness of a function?
  • Ian Collins

    #2
    Re: How to test the correctness of a function?

    istillshine@gma il.com wrote:
    When I want to test if a function foo in a .c file behaves as
    expected, I usually call it in my main.c in the following way.
    >
    >
    >
    if (1) { /* test function foo */
    z = foo(x, y); /* */
    assert(z==1);
    }
    >
    >
    When I want to test other functions, I change if (1) to if (0).
    >
    >
    Are there better (and simpler) ways to test correctness of a function?
    Write decent unit tests before you write the function. Write the
    function to pass the tests, nothing more.

    --
    Ian Collins.

    Comment

    • istillshine@gmail.com

      #3
      Re: How to test the correctness of a function?

      On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.co mwrote:
      Write decent unit tests before you write the function. Write the
      function to pass the tests, nothing more.
      Unit test. I heard of it. Is there any particular tool to do this?
      Or I need write a file such as test_foo.c to test a function called
      foo?

      Comment

      • Ian Collins

        #4
        Re: How to test the correctness of a function?

        istillshine@gma il.com wrote:
        On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.co mwrote:
        >
        >Write decent unit tests before you write the function. Write the
        >function to pass the tests, nothing more.
        >
        Unit test. I heard of it. Is there any particular tool to do this?
        Or I need write a file such as test_foo.c to test a function called
        foo?
        How can you test something without writing tests? There are C unit test
        frameworks like CUnit.

        --
        Ian Collins.

        Comment

        • user923005

          #5
          Re: How to test the correctness of a function?

          On Apr 11, 9:40 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          istillsh...@gma il.com wrote:
          On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.co mwrote:
          >
          Write decent unit tests before you write the function.  Write the
          function to pass the tests, nothing more.
          >
          Unit test.  I heard of it.  Is there any particular tool to do this?
          Or I need write a file such as test_foo.c to test a function called
          foo?
          >
          How can you test something without writing tests?  There are C unit test
          frameworks like CUnit.
          As a P.S.:
          If the total amount of input into the function is 4 bytes or less, you
          can usually write an exhaustive test for every possible input.
          At 6 bytes of input and above (booleans only count as 1 bit), you will
          often have to tone it down to statistical tests + edge cases.

          Comment

          • Malcolm McLean

            #6
            Re: How to test the correctness of a function?

            <istillshine@gm ail.comwrote in message
            On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.co mwrote:
            >
            >Write decent unit tests before you write the function. Write the
            >function to pass the tests, nothing more.
            >
            Unit test. I heard of it. Is there any particular tool to do this?
            Or I need write a file such as test_foo.c to test a function called
            foo?
            >
            Generally you want the test code in a different file to the function code.

            If the function has no dependencies and performs no IO then tests are
            usually quite straightforward s. You need to write cases so that each
            boundary is checked - try for zero, one, a normal number, maximum, and
            maximum plus one. Each line of code should be executed at least once.

            If the function has dependencies then the process is much more difficult. It
            might depend on substantial portions of the program, so the test harness
            becomes effectively the whole program. Input might be so difficult to set up
            that the unit test becomes extremely difficult to devise. If the function
            performs IO then of course you need the harware as well as part of your test
            harness.

            --
            Free games and programming goodies.


            Comment

            Working...