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

📄 regularexpressiontest.cpp

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 CPP
字号:
//
// RegularExpressionTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/RegularExpressionTest.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#include "RegularExpressionTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/RegularExpression.h"
#include "Foundation/Exception.h"


using Foundation::RegularExpression;
using Foundation::RegularExpressionException;


RegularExpressionTest::RegularExpressionTest(const std::string& name): CppUnit::TestCase(name)
{
}


RegularExpressionTest::~RegularExpressionTest()
{
}


void RegularExpressionTest::testIndex()
{
	RegularExpression re("[0-9]+");
	RegularExpression::Match match;
	assert (re.match("", 0, match) == 0);
	assert (re.match("123", 3, match) == 0);
}


void RegularExpressionTest::testMatch1()
{
	RegularExpression re("[0-9]+");
	assert (re.match("123"));
	assert (!re.match("123cd"));
	assert (!re.match("abcde"));
	assert (re.match("ab123", 2));
}


void RegularExpressionTest::testMatch2()
{
	RegularExpression re("[0-9]+");
	RegularExpression::Match match;
	assert (re.match("123", 0, match) == 1);
	assert (match.offset == 0);
	assert (match.length == 3);

	assert (re.match("abc123def", 0, match) == 1);
	assert (match.offset == 3);
	assert (match.length == 3);

	assert (re.match("abcdef", 0, match) == 0);
	assert (match.offset == std::string::npos);
	assert (match.length == 0);

	assert (re.match("abc123def", 3, match) == 1);
	assert (match.offset == 3);
	assert (match.length == 3);
}


void RegularExpressionTest::testMatch3()
{
	RegularExpression re("[0-9]+");
	RegularExpression::MatchVec match;
	assert (re.match("123", 0, match) == 1);
	assert (match.size() == 1);
	assert (match[0].offset == 0);
	assert (match[0].length == 3);

	assert (re.match("abc123def", 0, match) == 1);
	assert (match.size() == 1);
	assert (match[0].offset == 3);
	assert (match[0].length == 3);

	assert (re.match("abcdef", 0, match) == 0);
	assert (match.size() == 0);

	assert (re.match("abc123def", 3, match) == 1);
	assert (match.size() == 1);
	assert (match[0].offset == 3);
	assert (match[0].length == 3);
}


void RegularExpressionTest::testMatch4()
{
	RegularExpression re("([0-9]+) ([0-9]+)");
	RegularExpression::MatchVec matches;
	assert (re.match("123 456", 0, matches) == 3);
	assert (matches.size() == 3);
	assert (matches[0].offset == 0);
	assert (matches[0].length == 7);
	assert (matches[1].offset == 0);
	assert (matches[1].length == 3);
	assert (matches[2].offset == 4);
	assert (matches[2].length == 3);

	assert (re.match("abc123 456def", 0, matches) == 3);
	assert (matches.size() == 3);
	assert (matches[0].offset == 3);
	assert (matches[0].length == 7);
	assert (matches[1].offset == 3);
	assert (matches[1].length == 3);
	assert (matches[2].offset == 7);
	assert (matches[2].length == 3);
}


void RegularExpressionTest::testMatch5()
{
	std::string digits = "0123";
	assert (RegularExpression::match(digits, "[0-9]+"));
	std::string alphas = "abcd";
	assert (!RegularExpression::match(alphas, "[0-9]+"));
}


void RegularExpressionTest::testExtract()
{
	RegularExpression re("[0-9]+");
	std::string str;
	assert (re.extract("123", str) == 1);
	assert (str == "123");

	assert (re.extract("abc123def", 0, str) == 1);
	assert (str == "123");

	assert (re.extract("abcdef", 0, str) == 0);
	assert (str == "");

	assert (re.extract("abc123def", 3, str) == 1);
	assert (str == "123");
}


void RegularExpressionTest::testSplit1()
{
	RegularExpression re("[0-9]+");
	std::vector<std::string> strings;
	assert (re.split("123", 0, strings) == 1);
	assert (strings.size() == 1);
	assert (strings[0] == "123");

	assert (re.split("abc123def", 0, strings) == 1);
	assert (strings.size() == 1);
	assert (strings[0] == "123");

	assert (re.split("abcdef", 0, strings) == 0);
	assert (strings.empty());

	assert (re.split("abc123def", 3, strings) == 1);
	assert (strings.size() == 1);
	assert (strings[0] == "123");
}


void RegularExpressionTest::testSplit2()
{
	RegularExpression re("([0-9]+) ([0-9]+)");
	std::vector<std::string> strings;
	assert (re.split("123 456", 0, strings) == 3);
	assert (strings.size() == 3);
	assert (strings[0] == "123 456");
	assert (strings[1] == "123");
	assert (strings[2] == "456");

	assert (re.split("abc123 456def", 0, strings) == 3);
	assert (strings.size() == 3);
	assert (strings[0] == "123 456");
	assert (strings[1] == "123");
	assert (strings[2] == "456");
}


void RegularExpressionTest::testSubst1()
{
	RegularExpression re("[0-9]+");
	std::string s = "123";
	assert (re.subst(s, "ABC") == 1);
	assert (s == "ABC");
	assert (re.subst(s, "123") == 0);

	s = "123";
	assert (re.subst(s, "AB$0CD") == 1);
	assert (s == "AB123CD");

	s = "123";
	assert (re.subst(s, "AB$1CD") == 1);
	assert (s == "ABCD");

	s = "123";
	assert (re.subst(s, "AB$2CD") == 1);
	assert (s == "ABCD");

	s = "123";
	assert (re.subst(s, "AB$$CD") == 1);
	assert (s == "AB$$CD");

	s = "123";
	assert (re.subst(s, "AB$0CD", RegularExpression::RE_NO_VARS) == 1);
	assert (s == "AB$0CD");
}


void RegularExpressionTest::testSubst2()
{
	RegularExpression re("([0-9]+) ([0-9]+)");
	std::string s = "123 456";
	assert (re.subst(s, "$2-$1") == 1);
	assert (s == "456-123");
}


void RegularExpressionTest::testSubst3()
{
	RegularExpression re("[0-9]+");
	std::string s = "123 456 789";
	assert (re.subst(s, "n", RegularExpression::RE_GLOBAL) == 3);
	assert (s == "n n n");
}


void RegularExpressionTest::testSubst4()
{
	RegularExpression re("[0-9]+");
	std::string s = "ABC 123 456 789 DEF";
	assert (re.subst(s, "n", RegularExpression::RE_GLOBAL) == 3);
	assert (s == "ABC n n n DEF");
}


void RegularExpressionTest::testError()
{
	try
	{
		RegularExpression re("(0-9]");
		failmsg("bad regexp - must throw exception");
	}
	catch (RegularExpressionException&)
	{
	}
}


void RegularExpressionTest::setUp()
{
}


void RegularExpressionTest::tearDown()
{
}


CppUnit::Test* RegularExpressionTest::suite()
{
	CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RegularExpressionTest");

	CppUnit_addTest(pSuite, RegularExpressionTest, testIndex);
	CppUnit_addTest(pSuite, RegularExpressionTest, testMatch1);
	CppUnit_addTest(pSuite, RegularExpressionTest, testMatch2);
	CppUnit_addTest(pSuite, RegularExpressionTest, testMatch3);
	CppUnit_addTest(pSuite, RegularExpressionTest, testMatch4);
	CppUnit_addTest(pSuite, RegularExpressionTest, testMatch5);
	CppUnit_addTest(pSuite, RegularExpressionTest, testExtract);
	CppUnit_addTest(pSuite, RegularExpressionTest, testSplit1);
	CppUnit_addTest(pSuite, RegularExpressionTest, testSplit2);
	CppUnit_addTest(pSuite, RegularExpressionTest, testSubst1);
	CppUnit_addTest(pSuite, RegularExpressionTest, testSubst2);
	CppUnit_addTest(pSuite, RegularExpressionTest, testSubst3);
	CppUnit_addTest(pSuite, RegularExpressionTest, testSubst4);
	CppUnit_addTest(pSuite, RegularExpressionTest, testError);

	return pSuite;
}

⌨️ 快捷键说明

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