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

📄 multi_pass_tests.cpp

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*=============================================================================
    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/iterator/multi_pass.hpp>
#include <boost/scoped_ptr.hpp>
#include <iterator>
#include <string>
#include <cassert>
#include "impl/sstream.hpp"

using namespace std;
using namespace boost::spirit;

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_pass
class 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));

        assert(*mp1 == *mp2);
        assert(*mp1 >= *mp2);
        assert(*mp1 <= *mp2);
        for (int i = 0; i < 4; ++i)
        {
            res << **mp1;
            ++*mp1;
        }

        assert(*mp1 != *mp2);
        assert(*mp1 > *mp2);
        assert(*mp1 >= *mp2);
        assert(*mp2 < *mp1);
        assert(*mp2 <= *mp1);
        while (*mp2 != *mp1)
        {
            res << **mp2;
            ++*mp2;
        }

        assert(*mp1 == *mp2);
        assert(*mp1 >= *mp2);
        assert(*mp1 <= *mp2);
        while (*mp1 != *mpend)
        {
            res << **mp1;
            ++*mp1;
        }

        assert(*mp1 != *mp2);
        assert(*mp1 > *mp2);
        assert(*mp1 >= *mp2);
        assert(*mp2 < *mp1);
        assert(*mp2 <= *mp1);
        while (*mp2 != *mpend)
        {
            res << **mp2;
            ++*mp2;
        }

        assert(*mp1 == *mp2);
        assert(*mp1 >= *mp2);
        assert(*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));
        assert(*mp1 != *mp2);
        ++*mp1;
        assert(*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
            assert(0);
        }
        catch (const boost::spirit::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));

        assert(*mp1 == *mp2);
        assert(*mp1 >= *mp2);
        assert(*mp1 <= *mp2);
        for (int i = 0; i < 4; ++i)
        {
            res << **mp1;
            ++*mp1;
        }

        assert(*mp1 != *mp2);
        assert(*mp1 > *mp2);
        assert(*mp1 >= *mp2);

⌨️ 快捷键说明

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