📄 streamtokenizertest.cpp
字号:
//
// StreamTokenizerTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/StreamTokenizerTest.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 "StreamTokenizerTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/StreamTokenizer.h"
#include "Foundation/Token.h"
#include <ctype.h>
#include <sstream>
using Foundation::StreamTokenizer;
using Foundation::Token;
using Foundation::InvalidToken;
using Foundation::EOFToken;
using Foundation::WhitespaceToken;
class IdentifierToken: public Token
{
public:
IdentifierToken()
{
}
~IdentifierToken()
{
}
Class tokenClass() const
{
return Token::IDENTIFIER_TOKEN;
}
bool start(char c, std::istream& istr)
{
if (isalpha(c))
{
_value = c;
return true;
}
else return false;
}
void finish(std::istream& istr)
{
int c = istr.peek();
while (isalnum(c))
{
istr.get();
_value += c;
c = istr.peek();
}
}
};
class IntLiteralToken: public Token
{
public:
IntLiteralToken()
{
}
~IntLiteralToken()
{
}
Class tokenClass() const
{
return Token::INTEGER_LITERAL_TOKEN;
}
bool start(char c, std::istream& istr)
{
if (isdigit(c))
{
_value = c;
return true;
}
else return false;
}
void finish(std::istream& istr)
{
int c = istr.peek();
while (isdigit(c))
{
istr.get();
_value += c;
c = istr.peek();
}
}
};
StreamTokenizerTest::StreamTokenizerTest(const std::string& name): CppUnit::TestCase(name)
{
}
StreamTokenizerTest::~StreamTokenizerTest()
{
}
void StreamTokenizerTest::testTokenizer1()
{
std::string data = "";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::testTokenizer2()
{
std::string data = "foo";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "foo");
next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::testTokenizer3()
{
std::string data = "foo bar";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "foo");
next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "bar");
next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::testTokenizer4()
{
std::string data = "foo 123";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "foo");
next = tokenizer.next();
assert (next->tokenClass() == Token::INTEGER_LITERAL_TOKEN);
assert (next->asInteger() == 123);
next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::testTokenizer5()
{
std::string data = "foo # 123";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "foo");
next = tokenizer.next();
assert (next->tokenClass() == Token::INVALID_TOKEN);
assert (next->tokenString() == "#");
next = tokenizer.next();
assert (next->tokenClass() == Token::INTEGER_LITERAL_TOKEN);
assert (next->asInteger() == 123);
next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::testTokenizer6()
{
std::string data = "foo 123 #";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "foo");
next = tokenizer.next();
assert (next->tokenClass() == Token::INTEGER_LITERAL_TOKEN);
assert (next->asInteger() == 123);
next = tokenizer.next();
assert (next->tokenClass() == Token::INVALID_TOKEN);
assert (next->tokenString() == "#");
next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::testTokenizer7()
{
std::string data = " foo 123 ";
std::istringstream istr(data);
StreamTokenizer tokenizer(istr);
tokenizer.addToken(new WhitespaceToken());
tokenizer.addToken(new IdentifierToken());
tokenizer.addToken(new IntLiteralToken());
const Token* next = tokenizer.next();
assert (next->tokenClass() == Token::IDENTIFIER_TOKEN);
assert (next->tokenString() == "foo");
next = tokenizer.next();
assert (next->tokenClass() == Token::INTEGER_LITERAL_TOKEN);
assert (next->asInteger() == 123);
next = tokenizer.next();
assert (next->tokenClass() == Token::EOF_TOKEN);
}
void StreamTokenizerTest::setUp()
{
}
void StreamTokenizerTest::tearDown()
{
}
CppUnit::Test* StreamTokenizerTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StreamTokenizerTest");
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer1);
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer2);
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer3);
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer4);
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer5);
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer6);
CppUnit_addTest(pSuite, StreamTokenizerTest, testTokenizer7);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -