TraceTableUnit2

来源:互联网 发布:jira类似的软件 编辑:程序博客网 时间:2024/05/28 16:15

CExampleTest.cpp

******************************************************************************
  File Name     :
  Version       :
  Author        :
  Created       : 2009/6/1
  Last Modified :
  Description   :
  Function List :
             
  History       :
  1.Date        : 2009/6/1
    Author      :
    Modification: Created file

******************************************************************************/
#include <cppunit/config/SourcePrefix.h>
#include "CExampleTest.h"

// 注册测试套到CppUnit
CPPUNIT_TEST_SUITE_REGISTRATION( CExampleTest );

// 测试用例实现示例

// SetUp: 在每个用例前执行一次
void CExampleTest::setUp()
{
    m_strOrbit = "";
}

// tearDown: 在每个用例后执行一次
void CExampleTest::tearDown()
{
    DestroyTable(); // 销毁数据表  
}

// 用例1:
void CExampleTest::TestCase01()
{
    int iRowLenth    = 0;
    int iColumnLenth = 1;
   
    int iRet = InitTable(iRowLenth, iColumnLenth); // 初始化数据表, 行非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);

    DestroyTable();                                // 销毁数据表

    iRowLenth    = 10;
    iColumnLenth = 1;
    iRet = InitTable(iRowLenth, iColumnLenth);     // 初始化数据表, 行非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);
}

// 用例2:
void CExampleTest::TestCase02()
{
    int iRowLenth    = 1;
    int iColumnLenth = 0;
   
    int iRet = InitTable(iRowLenth, iColumnLenth); // 初始化数据表, 列非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);   

    DestroyTable();                                // 销毁数据表

    iRowLenth    = 1;
    iColumnLenth = 10;
    iRet = InitTable(iRowLenth, iColumnLenth);     // 初始化数据表, 列非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);
}

// 用例3:
void CExampleTest::TestCase03()
{
    int iRowLenth    = 1;
    int iColumnLenth = 1;
   
    int iRet = InitTable(iRowLenth, iColumnLenth); // 初始化数据表, 行列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = InitTable(iRowLenth, iColumnLenth);     // 重复初始化数据表
    CPPUNIT_ASSERT_EQUAL(-1, iRet); 

   
    DestroyTable();                                // 销毁数据表

    iRowLenth    = 9;
    iColumnLenth = 9;
    iRet = InitTable(iRowLenth, iColumnLenth);     // 初始化数据表, 行列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = InitTable(iRowLenth, iColumnLenth);     // 重复初始化数据表
    CPPUNIT_ASSERT_EQUAL(-1, iRet);
}

// 用例4:
void CExampleTest::TestCase04()
{
    int iRowLenth    = 2;
    int iColumnLenth = 2;
    int iRet = 0;
    TABLE_NODE stNode00 = {0, 0};
    TABLE_NODE stNode01 = {0, 1};
    TABLE_NODE stNode02 = {0, 2};
    TABLE_NODE stNode09 = {0, 9};
 
    iRet = InitTable(iRowLenth, iColumnLenth); // 初始化数据表, 行列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = SwapUnit(stNode00, stNode01);       // 交换数据表单元, Node00与Node01
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = SwapUnit(stNode01, stNode00);       // 交换数据表单元, Node01与Node00
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = SwapUnit(stNode00, stNode00);       // 交换数据表单元, Node00与Node00自身交换
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = SwapUnit(stNode00, stNode02);       // 交换数据表单元, Node02非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);

    iRet = SwapUnit(stNode00, stNode09);       // 交换数据表单元, Node09非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);
}

// 用例5:
void CExampleTest::TestCase05()
{
    int iRowLenth    = 7;
    int iColumnLenth = 7;
    int iRet = 0;
 
    iRet = InitTable(iRowLenth, iColumnLenth); // 初始化数据表, 行列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = InsertColumnToTable(8);             // 插入列, 列非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);

    iRet = InsertColumnToTable(7);             // 插入列, 列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);
 
    iRet = InsertColumnToTable(8);             // 插入列, 数据表已变化, 列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = InsertColumnToTable(0);             // 插入列, 数据表已达到最大规格, 非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);
}

// 用例6:
void CExampleTest::TestCase06()
{
    int iRowLenth    = 3;
    int iColumnLenth = 3;
    int iRet = 0;
    std::string expected;
 std::string actual;
 ORBIT_LIST *pOrbit;
    TABLE_NODE stNode00 = {0, 0};
    TABLE_NODE stNode22 = {2, 2};
    TABLE_NODE stNode33 = {3, 3};

    iRet = InitTable(iRowLenth, iColumnLenth);     // 初始化数据表, 行列合法   
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    expected = "[0,0]";
    iRet = QueryOrbitOfUnitData(stNode00, &pOrbit); // 查询数据表单元Node00变化轨迹
 actual = GetOrbitStr(pOrbit);
    CPPUNIT_ASSERT_EQUAL(0, iRet);
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    expected = "[2,2]";
    iRet = QueryOrbitOfUnitData(stNode22, &pOrbit); // 查询数据表单元Node22变化轨迹
 actual = GetOrbitStr(pOrbit);
    CPPUNIT_ASSERT_EQUAL(0, iRet);
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    iRet = QueryOrbitOfUnitData(stNode33, &pOrbit); // 查询数据表单元Node33变化轨迹, 非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);

    iRet = InsertRowToTable(0);                    // 插入行, 行合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = InsertColumnToTable(0);                 // 插入列, 列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = QueryOrbitOfUnitData(stNode33, &pOrbit); // 查询数据表单元Node33变化轨迹, 非法
    CPPUNIT_ASSERT_EQUAL(-1, iRet);
}

// 用例7:
void CExampleTest::TestCase07()
{
    int iRowLenth    = 3;
    int iColumnLenth = 3;
    int iRet = 0;
    std::string expected;
 std::string actual;
 ORBIT_LIST *pOrbit;
    TABLE_NODE stNode02 = {0, 2};
    TABLE_NODE stNode11 = {1, 1};
 
    iRet = InitTable(iRowLenth, iColumnLenth);     // 初始化数据表, 行列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = SwapUnit(stNode11, stNode11);           // 交换数据表单元, Node11与Node11自身交换
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    expected = "[1,1]";
    iRet = QueryOrbitOfUnitData(stNode11, &pOrbit); // 查询数据表单元Node11变化轨迹, 无变化
    CPPUNIT_ASSERT_EQUAL(0, iRet);
    actual = GetOrbitStr(pOrbit);
    CPPUNIT_ASSERT_EQUAL(expected, actual);

    iRet = SwapUnit(stNode02, stNode11);           // 交换数据表单元, Node02与Node11
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    expected = "[0,2]->[1,1]";
    iRet = QueryOrbitOfUnitData(stNode02, &pOrbit); // 查询数据表单元Node02变化轨迹
    CPPUNIT_ASSERT_EQUAL(0, iRet);
 actual = GetOrbitStr(pOrbit);
 CPPUNIT_ASSERT_EQUAL(expected, actual); 
}

// 用例8:
void CExampleTest::TestCase08()
{
    int iRowLenth    = 3;
    int iColumnLenth = 3;
    int iRet = 0;
    std::string expected;
 std::string actual;
 ORBIT_LIST *pOrbit;
    TABLE_NODE stNode11 = {1, 1};
    TABLE_NODE stNode12 = {1, 2};
    TABLE_NODE stNode23 = {2, 3};

    iRet = InitTable(iRowLenth, iColumnLenth);     // 初始化数据表, 行列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = InsertColumnToTable(1);                 // 插入列, 列合法
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    iRet = SwapUnit(stNode12, stNode23);           // 交换数据表单元, Node12与Node23
    CPPUNIT_ASSERT_EQUAL(0, iRet);

    expected = "[1,1]->[1,2]->[2,3]";
    iRet = QueryOrbitOfUnitData(stNode11, &pOrbit); // 查询数据表单元Node11变化轨迹
    CPPUNIT_ASSERT_EQUAL(0, iRet);
 actual = GetOrbitStr(pOrbit);
 CPPUNIT_ASSERT_EQUAL(expected, actual);

 if(NULL != pOrbit)
 {
  FreeOrbitOfUnitData(pOrbit);
 }

}

// 在这里添加测试用例实现
// void CExampleTest::TestCasexx()
// {
//
// }


// 工具方法,请勿修改
void CExampleTest::AppendToOrbitStr(ORBIT_LIST *pOrbit)
{
 ORBIT_LIST *pTemp = pOrbit;

 while (NULL != pTemp)
 {
  std::stringstream os;
  os << "[" << pTemp->node.iRowNo << "," << pTemp->node.iColumnNo << "]";

  if (m_strOrbit != "")
  {
   m_strOrbit = m_strOrbit + "->" + os.str();
  }
  else
  {
   m_strOrbit = os.str();
  }

  pTemp = pTemp->pNext;
 }

 return; 
}

std::string CExampleTest::GetOrbitStr(ORBIT_LIST *pOrbit)
{
 AppendToOrbitStr(pOrbit);

 std::string ouput = m_strOrbit;
 m_strOrbit = "";
 return ouput;
}

 

CExampleTest.h

******************************************************************************
  File Name     :
  Version       :
  Author        :
  Created       : 2009/6/1
  Last Modified :
  Description   :
  Function List :
             
  History       :
  1.Date        : 2009/6/1
    Author      :
    Modification: Created file

******************************************************************************/
#ifndef _CEXAMPLE_TEST_H
#define _CEXAMPLE_TEST_H
#include <cppunit/extensions/HelperMacros.h>

// 数据表节点位置信息定义
typedef struct Table_Node
{
 int  iRowNo;    // 行号, 从0开始编号
 int  iColumnNo; // 列号, 从0开始编号
}TABLE_NODE;

// 节点轨迹变化链表信息
typedef struct Orbit_List
{
 TABLE_NODE   node;           //节点位置信息
 struct Orbit_List *pNext;
}ORBIT_LIST;

// 被测函数声明
extern int InitTable(int iRowLenth, int iColumnLenth);
extern void DestroyTable (void);
extern int SwapUnit (TABLE_NODE stNode1, TABLE_NODE stNode2);
extern int InsertColumnToTable(int iColumnNo);
extern int InsertRowToTable(int iRowNo);
extern int QueryOrbitOfUnitData(TABLE_NODE stNode, ORBIT_LIST** ppOrbit);
void FreeOrbitOfUnitData(void* pOrbit);

class CExampleTest : public CPPUNIT_NS::TestFixture
{
    CPPUNIT_TEST_SUITE( CExampleTest );
    CPPUNIT_TEST( TestCase01 );
    CPPUNIT_TEST( TestCase02 );
    CPPUNIT_TEST( TestCase03 );
    CPPUNIT_TEST( TestCase04 );
    CPPUNIT_TEST( TestCase05 );
    CPPUNIT_TEST( TestCase06 );
    CPPUNIT_TEST( TestCase07 );
    CPPUNIT_TEST( TestCase08 );
    // 在这里添加测试用例声明, 将测试用例加入测试套
    // CPPUNIT_TEST( TestCasexx );

    CPPUNIT_TEST_SUITE_END();

public:
    void setUp();
    void tearDown();
    void TestCase01();
    void TestCase02();
    void TestCase03();
    void TestCase04();
    void TestCase05();
    void TestCase06();
    void TestCase07();
    void TestCase08();
    // 在这里添加测试用例声明, 声明测试类的成员函数
    // void TestCasexx();

private:
 void AppendToOrbitStr(ORBIT_LIST *pOrbit);
 std::string GetOrbitStr(ORBIT_LIST *pOrbit);
 std::string m_strOrbit;
 
};

#endif /*_CEXAMPLE_TEST_H*/

 

 

main.cpp

******************************************************************************/
#include <cppunit/TextOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include "conio.h"

int main(int argc, char* argv[])
{
    // Create the event manager and test controller
    CPPUNIT_NS::TestResult controller;

    // Add a listener that colllects test result
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener( &result );       

    // Add a listener that print dots as test run.
    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener( &progress );     

    // Add the top suite to the test runner
    CPPUNIT_NS::TestRunner runner;
    runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry("All Tests").makeTest() );
    runner.run( controller );

    // Print test in a text format.
    CPPUNIT_NS::TextOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
    outputter.write();

    // This for XML output
    std::ofstream file( "TestResult.xml" );
    CPPUNIT_NS::XmlOutputter xml( &result, file );
    xml.setStyleSheet( "report.xsl" );
    xml.write();
    file.close();
 getch();

    return result.wasSuccessful() ? 0 : 1;
}

 

 

 

 

原创粉丝点击