【Poco】Poco::Core中的例子

#include "../tmain.hpp"

/* 操作系统
#define POCO_OS_FREE_BSD      0x0001
#define POCO_OS_AIX           0x0002
#define POCO_OS_HPUX          0x0003
#define POCO_OS_TRU64         0x0004
#define POCO_OS_LINUX         0x0005
#define POCO_OS_MAC_OS_X      0x0006
#define POCO_OS_NET_BSD       0x0007
#define POCO_OS_OPEN_BSD      0x0008
#define POCO_OS_IRIX          0x0009
#define POCO_OS_SOLARIS       0x000a
#define POCO_OS_QNX           0x000b
#define POCO_OS_VXWORKS       0x000c
#define POCO_OS_CYGWIN        0x000d
#define POCO_OS_UNKNOWN_UNIX  0x00ff
#define POCO_OS_WINDOWS_NT    0x1001
#define POCO_OS_WINDOWS_CE    0x1011
#define POCO_OS_VMS           0x2001
*/

/* 硬件架构
#define POCO_ARCH_ALPHA   0x01
#define POCO_ARCH_IA32    0x02
#define POCO_ARCH_IA64    0x03
#define POCO_ARCH_MIPS    0x04
#define POCO_ARCH_HPPA    0x05
#define POCO_ARCH_PPC     0x06
#define POCO_ARCH_POWER   0x07
#define POCO_ARCH_SPARC   0x08
#define POCO_ARCH_AMD64   0x09
#define POCO_ARCH_ARM     0x0a
#define POCO_ARCH_M68K    0x0b
#define POCO_ARCH_S390    0x0c
#define POCO_ARCH_SH      0x0d
#define POCO_ARCH_NIOS2   0x0e
*/

void test_CorePlatForm()
{
	std::cout << "POCO_OS  : " << POCO_OS << std::endl;
	std::cout << "POCO_ARCH: " << POCO_ARCH << std::endl;
	
	assert(POCO_OS == POCO_OS_WINDOWS_NT);
	assert(POCO_ARCH == POCO_ARCH_IA32);
}

void test_Environment()
{
	// 获取环境变量
	if (Poco::Environment::has("JAVA_HOME"))
		std::cout << Poco::Environment::get("JAVA_HOME") << std::endl;

	// 另一种方法, 没有时会抛异常...
	try
	{
		std::string v = Poco::Environment::get("JAVA_HOME");
	}
	catch (...)
	{
	}

	std::cout << "OS Name:            " << Poco::Environment::osName() << std::endl;
	std::cout << "OS Display Name:    " << Poco::Environment::osDisplayName() << std::endl;
	std::cout << "OS Version:         " << Poco::Environment::osVersion() << std::endl;
	std::cout << "OS Architecture:    " << Poco::Environment::osArchitecture() << std::endl;
	std::cout << "Node Name:          " << Poco::Environment::nodeName() << std::endl;
	std::cout << "Node ID:            " << Poco::Environment::nodeId() << std::endl;
	std::cout << "Number of CPUs:     " << Poco::Environment::processorCount() << std::endl;
	std::cout << "Libraries version:  " << Poco::Environment::libraryVersion() << std::endl;
}

void test_CoreBuffer()
{
	size_t s = 10;
	Poco::Buffer<int> bf(s);

	std::vector<int> vect;
	for (int i = 0; i < s; ++i)
		vect.push_back(i);

	std::memcpy(bf.begin(), &vect[0], sizeof(int) * vect.size());
	assert(bf.size() == s);
	for(int i = 0; i < s; ++i)
		assert(bf[i] == i);
}

void test_CoreNullable()
{
	// Poco::Nullable是一个简单的包装类, 这个类引用了Null值.
	// 初始值是Null
	Poco::Nullable<int> n1;
	assert(n1.isNull());
	assert(n1.value(42) == 42);

	Poco::Nullable<std::string> nstr;
	assert(nstr.isNull());
	assert(nstr.value("Nullable Test") == "Nullable Test");

	try
	{
		int tmp = n1.value();
	}
	catch (...)
	{
	}

	n1 = 1;
	assert (!n1.isNull());
	assert (n1.value() == 1);

	Poco::Nullable<int> n2(42);
	assert (!n2.isNull());
	assert (n2.value() == 42);
	assert (n2.value(99) == 42);

	n1 = n2;
	assert (!n1.isNull());
	assert (n1.value() == 42);

	n1.clear();
	assert (n1.isNull());
}

void test_CoreAscii()
{
	// POCO中提供的ASCII与C标准库中的函数类似.
	assert (Ascii::isAscii('A'));
	assert (!Ascii::isAscii(-1));
	assert (!Ascii::isAscii(128));
	assert (!Ascii::isAscii(222));

	assert (Ascii::isSpace(' '));
	assert (Ascii::isSpace('\t'));
	assert (Ascii::isSpace('\r'));
	assert (Ascii::isSpace('\n'));
	assert (!Ascii::isSpace('A'));
	assert (!Ascii::isSpace(-1));
	assert (!Ascii::isSpace(222));

	assert (Ascii::isDigit('0'));
	assert (Ascii::isDigit('1'));
	assert (Ascii::isDigit('2'));
	assert (Ascii::isDigit('3'));
	assert (Ascii::isDigit('4'));
	assert (Ascii::isDigit('5'));
	assert (Ascii::isDigit('6'));
	assert (Ascii::isDigit('7'));
	assert (Ascii::isDigit('8'));
	assert (Ascii::isDigit('9'));
	assert (!Ascii::isDigit('a'));

	assert (Ascii::isHexDigit('0'));
	assert (Ascii::isHexDigit('1'));
	assert (Ascii::isHexDigit('2'));
	assert (Ascii::isHexDigit('3'));
	assert (Ascii::isHexDigit('4'));
	assert (Ascii::isHexDigit('5'));
	assert (Ascii::isHexDigit('6'));
	assert (Ascii::isHexDigit('7'));
	assert (Ascii::isHexDigit('8'));
	assert (Ascii::isHexDigit('9'));
	assert (Ascii::isHexDigit('a'));
	assert (Ascii::isHexDigit('b'));
	assert (Ascii::isHexDigit('c'));
	assert (Ascii::isHexDigit('d'));
	assert (Ascii::isHexDigit('e'));
	assert (Ascii::isHexDigit('f'));
	assert (Ascii::isHexDigit('A'));
	assert (Ascii::isHexDigit('B'));
	assert (Ascii::isHexDigit('C'));
	assert (Ascii::isHexDigit('D'));
	assert (Ascii::isHexDigit('E'));
	assert (Ascii::isHexDigit('F'));
	assert (!Ascii::isHexDigit('G'));

	assert (Ascii::isPunct('.'));
	assert (Ascii::isPunct(','));
	assert (!Ascii::isPunct('A'));

	assert (Ascii::isAlpha('a'));
	assert (Ascii::isAlpha('Z'));
	assert (!Ascii::isAlpha('0'));

	assert (Ascii::isLower('a'));
	assert (!Ascii::isLower('A'));

	assert (Ascii::isUpper('A'));
	assert (!Ascii::isUpper('a'));

	assert (Ascii::toLower('A') == 'a');
	assert (Ascii::toLower('z') == 'z');
	assert (Ascii::toLower('0') == '0');

	assert (Ascii::toUpper('a') == 'A');
	assert (Ascii::toUpper('0') == '0');
	assert (Ascii::toUpper('Z') == 'Z');
}


你可能感兴趣的:(【Poco】Poco::Core中的例子)