⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utf_tests.cpp

📁 utf8转换工具
💻 CPP
字号:
/*
* Written by Boby Thomas Pazheparampil. (March 2007.)
* Platform independent code.  (I hope so)
* Tested with Windows 2000, XP, Cygwin and Linux Debian.
*/


#include "utf_tests.h"


char * testValues[] = {
	"7f","7f",
	"80","c280",
	"88","c288",
	"d7ff","ed9fbf",
	"e000","ee8080",
	"fffd","efbfbd",
	"10ffff","f48fbfbf",
	"110000","f4908080",
	"d800","eda080",
	"db7f","edadbf",
	"db80","edae80",
	"dbff","edafbf",
	"dc00","edb080",
	"df80","edbe80",
	"dfff","edbfbf"
};

char * testValuesLength[] = {
	"7f","1",
	"c2","2",
	"c2","2",
	"c2","2",
	"c2","2",
	"ed","3",
	"ee","3",
	"ef","3",
	"f4","4",
	"f4","4",
	"f8","5",
	"f8","5",
	"fc","6",
	"ed","3",
	"ed","3",
	"ed","3",
	"ed","3",
	"ed","3",
	"ed","3",
	"ed","3"
};


char * testBinary2HexValues[] = {
	"1010","0a",
	"10100000","a0",
	"00000000000000000","00",
	"00000000000000001","01",
	"1111111111111110","fffe",
	"0001111111111111","1fff"
};

char * testHex2BinaryValues[] = {
	"a","1010",
	"a0","10100000",
	"00000000000000000","0",
	"0","0",
	"ff","11111111",
	"fffffff0","11111111111111111111111111110000"
};


bool TESTconvertHex2UTF()
{
	cout<<"\n\nTesting convertHex2UTF....\n";

	long arraySize = sizeof(testValues)/sizeof(char*);
	long iTmp = 0;
	long cntFailed = 0;

	while(iTmp < arraySize)
	{
		string sInput = testValues[iTmp++];
		string sExpected = testValues[iTmp++];
		string sResult = convertHex2UTF(sInput);
		if(sResult.compare(sExpected)==0)
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<"  -> OK\n";
		else
		{
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<" Expected:"<<sExpected.c_str()<<"  -> NOK\n";
			cntFailed++;
		}
	}
	cout <<"\nNumber of tests    :"<<arraySize/2;
	cout <<"\nFailures           :"<<cntFailed;
	cout <<"\nSuccess rate       :"<<(100-(cntFailed*100/(arraySize/2))) <<" %\n";

	if(cntFailed > 0)
		return false;
	else
		return true;
}

bool TESTconvertUTF2Hex()
{
	cout<<"\n\nTesting convertUTF2Hex....\n";

	long arraySize = sizeof(testValues)/sizeof(char*);
	long iTmp = 0;
	long cntFailed = 0;

	while(iTmp < arraySize)
	{
		string sExpected = testValues[iTmp++];
		string sInput = testValues[iTmp++];

		string sResult = convertUTF2Hex(sInput);

		if(sResult.compare(sExpected)==0)
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<"  -> OK\n";
		else
		{
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<" Expected:"<<sExpected.c_str()<<"  -> NOK\n";
			cntFailed++;
		}
	}
	cout <<"\nNumber of tests    :"<<arraySize/2;
	cout <<"\nFailures           :"<<cntFailed;
	cout <<"\nSuccess rate       :"<<(100-(cntFailed*100/(arraySize/2))) <<" %\n";

	if(cntFailed > 0)
		return false;
	else
		return true;

}


bool TESTfindLengthUTF()
{
	cout<<"\n\nTesting findLengthUTF....\n";
	long arraySize = sizeof(testValues)/sizeof(char*);
	long iTmp = 0;
	long cntFailed = 0;

	while(iTmp < arraySize)
	{
		string sInput = testValuesLength[iTmp++];
		long lExpected = atol(testValuesLength[iTmp++]);
		long lResult = findLengthUTF(sInput);

		if(lResult == lExpected)
			cout<<sInput.c_str()<<" : "<<lResult<<"  -> OK\n";
		else
		{
			cout<<sInput.c_str()<<" : "<<lResult<<" Expected:"<<lExpected<<"  -> NOK\n";
			cntFailed++;
		}
	}
	cout <<"\nNumber of tests    :"<<arraySize/2;
	cout <<"\nFailures           :"<<cntFailed;
	cout <<"\nSuccess rate       :"<<(100-(cntFailed*100/(arraySize/2))) <<" %\n";

	if(cntFailed > 0)
		return false;
	else
		return true;
}

bool TESTgenerateUTFFileDetails()
{
	cout<<"\n\nTesting generateUTFFileDetails....\n";
	long cntFailed = 0;

	cout <<"\nNumber of tests    :"<<0;
	cout <<"\nFailures           :"<<cntFailed;
	cout <<"\nSuccess rate       :"<<(100-(cntFailed*100/1)) <<" %\n";

	if(cntFailed > 0)
		return false;
	else
		return true;
}

bool TESThex2binary()
{
	cout<<"\n\nTesting hex2binary....\n";

	long arraySize = sizeof(testHex2BinaryValues)/sizeof(char*);
	long iTmp = 0;
	long cntFailed = 0;

	while(iTmp < arraySize)
	{
		string sInput = testHex2BinaryValues[iTmp++];
		string sExpected = testHex2BinaryValues[iTmp++];

		string sResult = hex2binary(sInput);

		if(sResult.compare(sExpected)==0)
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<"  -> OK\n";
		else
		{
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<" Expected:"<<sExpected.c_str()<<"  -> NOK\n";
			cntFailed++;
		}
	}
	cout <<"\nNumber of tests    :"<<arraySize/2;
	cout <<"\nFailures           :"<<cntFailed;
	cout <<"\nSuccess rate       :"<<(100-(cntFailed*100/(arraySize/2))) <<" %\n";

	if(cntFailed > 0)
		return false;
	else
		return true;
}

bool TESTbinary2hex()
{
	cout<<"\n\nTesting binary2hex....\n";

	long arraySize = sizeof(testBinary2HexValues)/sizeof(char*);
	long iTmp = 0;
	long cntFailed = 0;

	while(iTmp < arraySize)
	{
		string sInput = testBinary2HexValues[iTmp++];
		string sExpected = testBinary2HexValues[iTmp++];

		string sResult = binary2hex(sInput);

		if(sResult.compare(sExpected)==0)
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<"  -> OK\n";
		else
		{
			cout<<sInput.c_str()<<" : "<<sResult.c_str()<<" Expected:"<<sExpected.c_str()<<"  -> NOK\n";
			cntFailed++;
		}
	}
	cout <<"\nNumber of tests    :"<<arraySize/2;
	cout <<"\nFailures           :"<<cntFailed;
	cout <<"\nSuccess rate       :"<<(100-(cntFailed*100/(arraySize/2))) <<" %\n";

	if(cntFailed > 0)
		return false;
	else
		return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -