Test

来源:互联网 发布:番今年交通事故数据 编辑:程序博客网 时间:2024/05/14 02:17
#include <list>#include <stdio.h>using namespace std;typedef list<string> LIST;LIST Host_List;void DissectHostName(LIST & Domain_List, const char * host_name){string strHostName = host_name, strTmp;basic_string<char>::size_type StartPos = 0, EndPos;while(string::npos != (EndPos = strHostName.find('.', StartPos))){char * pTmp = (char *)malloc(EndPos - StartPos);strncpy(pTmp, host_name + StartPos, EndPos - StartPos);pTmp[EndPos - StartPos] = 0;strTmp = pTmp;Domain_List.push_front(strTmp);StartPos = EndPos + 1;}basic_string<char>::size_type TopDomainLen = strHostName.length() - StartPos;char * pTmp = (char *)malloc(TopDomainLen);strncpy(pTmp, host_name + StartPos, TopDomainLen);pTmp[TopDomainLen] = 0;strTmp = pTmp;Domain_List.push_front(strTmp);return;}/*****************************************************************************Description : 添加主机名Input Param : host_name 主机名字符串,非空串Output Param : 无Return Value : 成功返回0,失败返回-1*****************************************************************************/int add_host_name(const char* host_name){LIST ListDomain;LIST ListTmp;DissectHostName(ListDomain, host_name);// 与已有列表的比较LIST::iterator ite;for (ite = Host_List.begin(); ite != Host_List.end(); ite++){const char * pTmp = (*ite).data();DissectHostName(ListTmp, pTmp);bool bFind = false, bNext = false;LIST::iterator iteDomain = ListDomain.begin(), iteTmp = ListTmp.begin();do {if (0 > strcmp((*iteDomain).data(), (*iteTmp).data())){bFind = true;break;}if (0 < strcmp((*iteDomain).data(), (*iteTmp).data())){bNext = true;break;}iteDomain++;iteTmp++;} while (iteDomain != ListDomain.end() && iteTmp != ListTmp.end());if (!bFind && bNext){ListTmp.clear();continue;}if (bFind /*域名字典顺序优先*/ || iteDomain == ListDomain.end() /*短域名优先*/){break;}}ListDomain.clear();char * pTmp = (char *)malloc(strlen(host_name) + 1);strncpy(pTmp, host_name, strlen(host_name));pTmp[strlen(host_name)] = 0;string strTmp = pTmp;if (ite == Host_List.end()){Host_List.push_back(strTmp);}else{Host_List.insert(ite, strTmp);}return 0;}/*****************************************************************************Description : 获取主机名Input Param : serial_number 排序后的序列号,从1开始host_name_max_length host_name的最大长度,包括'\0'Output Param : host_name 主机名字符串,必须包括’\0’,内存由调用者分配和释放Return Value : 成功返回0,失败返回-1(如:serial_number超出范围、最大长度不够)*****************************************************************************/int get_host_name(int serial_number, int host_name_max_length, char* host_name){/* 在这里实现功能 */if (serial_number > Host_List.size()){return -1;}LIST::iterator ite;for (ite = Host_List.begin(); ite != Host_List.end(); ite++){if (--serial_number > 0){continue;}int nLen = strlen((*ite).data());if (nLen >= host_name_max_length){return -1;}strncpy(host_name, (*ite).data(), nLen);host_name[nLen] = 0;break;}return 0;}/*****************************************************************************Description : 清空所有主机名Input Param : 无Output Param : 无Return Value : 无*****************************************************************************/void clear(void){/* 在这里实现功能 */Host_List.clear();} 


 

void CExampleTest::TestCase01()
{
 char out_str[20];

    CPPUNIT_ASSERT(0 == add_host_name("mail.huawei.com"));
 CPPUNIT_ASSERT(0 == add_host_name("huawei.com"));
 CPPUNIT_ASSERT(0 == add_host_name("teltalk.org"));
 CPPUNIT_ASSERT(0 == add_host_name("google.com.hk"));
 CPPUNIT_ASSERT(0 == add_host_name("imail.huawei.com"));

    CPPUNIT_ASSERT(0 == get_host_name(4, sizeof(out_str), out_str));
 CPPUNIT_ASSERT(0 == strcmp(out_str, "google.com.hk"));

 clear();
}

0 0
原创粉丝点击