multi_pass_tests.cpp
来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 766 行 · 第 1/2 页
CPP
766 行
/*============================================================================= Copyright (c) 2001-2003 Daniel Nuffer http://spirit.sourceforge.net/ Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)=============================================================================*/#include <boost/spirit/include/classic_multi_pass.hpp>#include <boost/scoped_ptr.hpp>#include <iterator>#include <string>#include <boost/detail/lightweight_test.hpp>#include "impl/sstream.hpp"using namespace std;using namespace BOOST_SPIRIT_CLASSIC_NS;sstream_t res;typedef multi_pass<istream_iterator<char> > default_multi_pass_t;typedef look_ahead<istream_iterator<char>, 6> fixed_multi_pass_t;typedef multi_pass< istream_iterator<char>, multi_pass_policies::input_iterator, multi_pass_policies::first_owner, multi_pass_policies::buf_id_check, multi_pass_policies::std_deque> first_owner_multi_pass_t;// a functor to test out the functor_multi_passclass my_functor{ public: typedef char result_type; my_functor() : c('A') {} char operator()() { if (c == 'M') return eof; else return c++; } static result_type eof; private: char c;};my_functor::result_type my_functor::eof = '\0';typedef multi_pass< my_functor, multi_pass_policies::functor_input, multi_pass_policies::first_owner, multi_pass_policies::no_check, multi_pass_policies::std_deque> functor_multi_pass_t;void test_default_multi_pass(){ res << "-*= test_default_multi_pass =*-\n"; istream_iterator<char> end; boost::scoped_ptr<default_multi_pass_t> mpend(new default_multi_pass_t(end)); { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<default_multi_pass_t> mp1(new default_multi_pass_t(a)); while (*mp1 != *mpend) { res << *((*mp1)++); } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> b(ss); boost::scoped_ptr<default_multi_pass_t> mp2(new default_multi_pass_t(b)); boost::scoped_ptr<default_multi_pass_t> mp3(new default_multi_pass_t(b)); *mp3 = *mp2; for (int i = 0; i < 4; ++i) { res << **mp2; ++*mp2; } mp3.reset(); while (*mp2 != *mpend) { res << **mp2; ++*mp2; } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<default_multi_pass_t> mp1(new default_multi_pass_t(a)); boost::scoped_ptr<default_multi_pass_t> mp2(new default_multi_pass_t(*mp1)); for (int i = 0; i < 4; ++i) { res << **mp1; ++*mp1; } while (*mp2 != *mpend) { res << **mp2; ++*mp2; } while (*mp1 != *mpend) { res << **mp1; ++*mp1; } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> b(ss); boost::scoped_ptr<default_multi_pass_t> mp2(new default_multi_pass_t(b)); boost::scoped_ptr<default_multi_pass_t> mp3(new default_multi_pass_t(b)); *mp3 = *mp2; for (int i = 0; i < 4; ++i) { res << **mp2; ++*mp2; } mp3.reset(); ++*mp2; while (*mp2 != *mpend) { res << **mp2; ++*mp2; } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<default_multi_pass_t> mp1(new default_multi_pass_t(a)); boost::scoped_ptr<default_multi_pass_t> mp2(new default_multi_pass_t(*mp1)); BOOST_TEST(*mp1 == *mp2); BOOST_TEST(*mp1 >= *mp2); BOOST_TEST(*mp1 <= *mp2); for (int i = 0; i < 4; ++i) { res << **mp1; ++*mp1; } BOOST_TEST(*mp1 != *mp2); BOOST_TEST(*mp1 > *mp2); BOOST_TEST(*mp1 >= *mp2); BOOST_TEST(*mp2 < *mp1); BOOST_TEST(*mp2 <= *mp1); while (*mp2 != *mp1) { res << **mp2; ++*mp2; } BOOST_TEST(*mp1 == *mp2); BOOST_TEST(*mp1 >= *mp2); BOOST_TEST(*mp1 <= *mp2); while (*mp1 != *mpend) { res << **mp1; ++*mp1; } BOOST_TEST(*mp1 != *mp2); BOOST_TEST(*mp1 > *mp2); BOOST_TEST(*mp1 >= *mp2); BOOST_TEST(*mp2 < *mp1); BOOST_TEST(*mp2 <= *mp1); while (*mp2 != *mpend) { res << **mp2; ++*mp2; } BOOST_TEST(*mp1 == *mp2); BOOST_TEST(*mp1 >= *mp2); BOOST_TEST(*mp1 <= *mp2); res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<default_multi_pass_t> mp1(new default_multi_pass_t(a)); boost::scoped_ptr<default_multi_pass_t> mp2(new default_multi_pass_t(a)); BOOST_TEST(*mp1 != *mp2); ++*mp1; BOOST_TEST(*mp1 != *mp2); } { sstream_t ss; ss << "test string"; istream_iterator<char> b(ss); boost::scoped_ptr<default_multi_pass_t> mp2(new default_multi_pass_t(b)); boost::scoped_ptr<default_multi_pass_t> mp3(new default_multi_pass_t(b)); *mp3 = *mp2; for (int i = 0; i < 4; ++i) { res << **mp2; ++*mp2; } mp2->clear_queue(); while (*mp2 != *mpend) { res << **mp2; ++*mp2; } try { res << **mp3; // this should throw illegal_backtracking BOOST_TEST(0); } catch (const BOOST_SPIRIT_CLASSIC_NS::multi_pass_policies::illegal_backtracking& /*e*/) { } res << endl; }}void test_fixed_multi_pass(){ res << "-*= test_fixed_multi_pass =*-\n"; istream_iterator<char> end; boost::scoped_ptr<fixed_multi_pass_t> mpend(new fixed_multi_pass_t(end)); { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<fixed_multi_pass_t> mp1(new fixed_multi_pass_t(a)); while (*mp1 != *mpend) { res << *((*mp1)++); } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> b(ss); boost::scoped_ptr<fixed_multi_pass_t> mp2(new fixed_multi_pass_t(b)); boost::scoped_ptr<fixed_multi_pass_t> mp3(new fixed_multi_pass_t(*mp2)); for (int i = 0; i < 4; ++i) { res << **mp2; ++*mp2; } mp3.reset(); while (*mp2 != *mpend) { res << **mp2; ++*mp2; } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<fixed_multi_pass_t> mp1(new fixed_multi_pass_t(a)); boost::scoped_ptr<fixed_multi_pass_t> mp2(new fixed_multi_pass_t(*mp1)); for (int i = 0; i < 4; ++i) { res << **mp1; ++*mp1; } while (*mp2 != *mpend) { res << **mp2; ++*mp2; } while (*mp1 != *mpend) { res << **mp1; ++*mp1; } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> b(ss); boost::scoped_ptr<fixed_multi_pass_t> mp2(new fixed_multi_pass_t(b)); boost::scoped_ptr<fixed_multi_pass_t> mp3(new fixed_multi_pass_t(*mp2)); for (int i = 0; i < 4; ++i) { res << **mp2; ++*mp2; } mp3.reset(); ++*mp2; while (*mp2 != *mpend) { res << **mp2; ++*mp2; } res << endl; } { sstream_t ss; ss << "test string"; istream_iterator<char> a(ss); boost::scoped_ptr<fixed_multi_pass_t> mp1(new fixed_multi_pass_t(a)); boost::scoped_ptr<fixed_multi_pass_t> mp2(new fixed_multi_pass_t(*mp1)); BOOST_TEST(*mp1 == *mp2); BOOST_TEST(*mp1 >= *mp2); BOOST_TEST(*mp1 <= *mp2); for (int i = 0; i < 4; ++i) { res << **mp1; ++*mp1; } BOOST_TEST(*mp1 != *mp2); BOOST_TEST(*mp1 > *mp2); BOOST_TEST(*mp1 >= *mp2);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?