Menu

Testing 'C' functions

Help
2003-01-14
2003-01-15
  • LJ Kyle Brost

    LJ Kyle Brost - 2003-01-14

    Hello,

    I'm developing on a project that is mixed C/C++ and I'm trying to figure out how to write test cases for C functions.  I've looked at the Cookbook and I cannot see how to do this.  Could someone give me some pointers?

    Thanks,

    Kyle Brost
    ----

     
    • Tom Plunket

      Tom Plunket - 2003-01-15

      What doesn't make sense?  If I had mixed C/C++, I'd still write the tests in CppUnit/C++, just assert that the returns from the extern "C" functions were as I expected.

       
      • LJ Kyle Brost

        LJ Kyle Brost - 2003-01-15

        I should have been more specific in my message.  Attached is a sample of code that has a class and a simple function.  The 'main' contains a runner for the class.

        My question is, How do I register the test_IsEven_* functions with CppUnit and have it automatically execute and collect the results?

        Thanks,

        Kyle Brost
        ----

        #include "cppunit/TextTestResult.h"
        #include "cppunit/TestCase.h"
        #include <cppunit/extensions/HelperMacros.h>

        #include <stdio.h>
        #include <iostream.h>

        class MyMath
        {
          public:
            bool IsOdd(int value_)
            {
              if(value_ % 2 == 1)
            return true;
              else
            return false;
            }
        };

        class MyMathTest : public CppUnit::TestCase
        {
          public:
            void runTest()
            {
              MyMath theMath;
              CPPUNIT_ASSERT( theMath.IsOdd(0) == 0);
              CPPUNIT_ASSERT( theMath.IsOdd(1) == 1);
              CPPUNIT_ASSERT( theMath.IsOdd(2) == 0);
            }
        };

        int IsEven(int value_)
        {
          if(value_ % 2 == 0)
          {
            return 1;
          }
          else
          {
            return 0;
          }
        }

        void test_IsEven_0()
        {
          CPPUNIT_ASSERT(IsEven(0) ==  1);
        }

        void test_IsEven_1()
        {
          CPPUNIT_ASSERT(IsEven(1) ==  0);
        }

        void test_IsEven_2()
        {
          CPPUNIT_ASSERT(IsEven(2) ==  1);
        }

        int
        main(int argc, char** argv)
        {
          CppUnit::TestCaller<MyMathTest> theTest("IsOdd", &MyMathTest::runTest);
          CppUnit::TextTestResult theResult;
          theTest.run( &theResult );
          std::cout << theResult << std::endl;
          return 0;
        }

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.