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

📄 newline_test.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// (C) Copyright Jonathan Turkanis 2004
// Distributed under 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.)

// See http://www.boost.org/libs/iostreams for documentation.

#include <string>
#include <boost/iostreams/compose.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/newline.hpp>
#include <boost/iostreams/filter/test.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/utility/base_from_member.hpp>

namespace io = boost::iostreams;
using boost::unit_test::test_suite;

const std::string posix =
    "When I was one-and-twenty\n"
    "I heard a wise man say,\n"
    "'Give crowns and pounds and guineas\n"
    "But not your heart away;\n"
    "\n"
    "Give pearls away and rubies\n"
    "But keep your fancy free.'\n"
    "But I was one-and-twenty,\n"
    "No use to talk to me.\n"
    "\n"
    "When I was one-and-twenty\n"
    "I heard him say again,\n"
    "'The heart out of the bosom\n"
    "Was never given in vain;\n"
    "\n"
    "'Tis paid with sighs a plenty\n"
    "And sold for endless rue.'\n"
    "And I am two-and-twenty,\n"
    "And oh, 'tis true, 'tis true.\n";

const std::string dos =
    "When I was one-and-twenty\r\n"
    "I heard a wise man say,\r\n"
    "'Give crowns and pounds and guineas\r\n"
    "But not your heart away;\r\n"
    "\r\n"
    "Give pearls away and rubies\r\n"
    "But keep your fancy free.'\r\n"
    "But I was one-and-twenty,\r\n"
    "No use to talk to me.\r\n"
    "\r\n"
    "When I was one-and-twenty\r\n"
    "I heard him say again,\r\n"
    "'The heart out of the bosom\r\n"
    "Was never given in vain;\r\n"
    "\r\n"
    "'Tis paid with sighs a plenty\r\n"
    "And sold for endless rue.'\r\n"
    "And I am two-and-twenty,\r\n"
    "And oh, 'tis true, 'tis true.\r\n";

const std::string mac =
    "When I was one-and-twenty\r"
    "I heard a wise man say,\r"
    "'Give crowns and pounds and guineas\r"
    "But not your heart away;\r"
    "\r"
    "Give pearls away and rubies\r"
    "But keep your fancy free.'\r"
    "But I was one-and-twenty,\r"
    "No use to talk to me.\r"
    "\r"
    "When I was one-and-twenty\r"
    "I heard him say again,\r"
    "'The heart out of the bosom\r"
    "Was never given in vain;\r"
    "\r"
    "'Tis paid with sighs a plenty\r"
    "And sold for endless rue.'\r"
    "And I am two-and-twenty,\r"
    "And oh, 'tis true, 'tis true.\r";

const std::string no_final_newline =
    "When I was one-and-twenty\n"
    "I heard a wise man say,\n"
    "'Give crowns and pounds and guineas\n"
    "But not your heart away;\n"
    "\n"
    "Give pearls away and rubies\n"
    "But keep your fancy free.'\n"
    "But I was one-and-twenty,\n"
    "No use to talk to me.\n"
    "\n"
    "When I was one-and-twenty\n"
    "I heard him say again,\n"
    "'The heart out of the bosom\n"
    "Was never given in vain;\n"
    "\n"
    "'Tis paid with sighs a plenty\n"
    "And sold for endless rue.'\n"
    "And I am two-and-twenty,\n"
    "And oh, 'tis true, 'tis true.";

const std::string mixed =
    "When I was one-and-twenty\n"
    "I heard a wise man say,\r\n"
    "'Give crowns and pounds and guineas\r"
    "But not your heart away;\n"
    "\r\n"
    "Give pearls away and rubies\r"
    "But keep your fancy free.'\n"
    "But I was one-and-twenty,\r\n"
    "No use to talk to me.\r"
    "\r"
    "When I was one-and-twenty\r\n"
    "I heard him say again,\r"
    "'The heart out of the bosom\n"
    "Was never given in vain;\r\n"
    "\r"
    "'Tis paid with sighs a plenty\n"
    "And sold for endless rue.'\r\n"
    "And I am two-and-twenty,\r"
    "And oh, 'tis true, 'tis true.\n";

struct string_source : boost::base_from_member<std::string>, io::array_source {
    typedef io::array_source                      base_type;
    typedef boost::base_from_member<std::string>  pbase_type;
    string_source(const std::string& src)
        : pbase_type(src), base_type(member.data(), member.size())
        { }

    string_source(const string_source& src)
        : pbase_type(src.member), base_type(member.data(), member.size())
        { }
};

void read_newline_filter()
{
    using namespace io;

        // Test converting to posix format.

    BOOST_CHECK(test_input_filter(newline_filter(newline::posix), posix, posix));
    BOOST_CHECK(test_input_filter(newline_filter(newline::posix), dos, posix));
    BOOST_CHECK(test_input_filter(newline_filter(newline::posix), mac, posix));
    BOOST_CHECK(test_input_filter(newline_filter(newline::posix), mixed, posix));

        // Test converting to dos format.

    BOOST_CHECK(test_input_filter(newline_filter(newline::dos), posix, dos));
    BOOST_CHECK(test_input_filter(newline_filter(newline::dos), dos, dos));
    BOOST_CHECK(test_input_filter(newline_filter(newline::dos), mac, dos));
    BOOST_CHECK(test_input_filter(newline_filter(newline::dos), mixed, dos));

        // Test converting to mac format.

    BOOST_CHECK(test_input_filter(newline_filter(newline::mac), posix, mac));
    BOOST_CHECK(test_input_filter(newline_filter(newline::mac), dos, mac));
    BOOST_CHECK(test_input_filter(newline_filter(newline::mac), mac, mac));
    BOOST_CHECK(test_input_filter(newline_filter(newline::mac), mixed, mac));
}

void write_newline_filter()
{
    using namespace io;

        // Test converting to posix format.

    BOOST_CHECK(test_output_filter(newline_filter(newline::posix), posix, posix));
    BOOST_CHECK(test_output_filter(newline_filter(newline::posix), dos, posix));
    BOOST_CHECK(test_output_filter(newline_filter(newline::posix), mac, posix));
    BOOST_CHECK(test_output_filter(newline_filter(newline::posix), mixed, posix));

        // Test converting to dos format.

    BOOST_CHECK(test_output_filter(newline_filter(newline::dos), posix, dos));
    BOOST_CHECK(test_output_filter(newline_filter(newline::dos), dos, dos));
    BOOST_CHECK(test_output_filter(newline_filter(newline::dos), mac, dos));
    BOOST_CHECK(test_output_filter(newline_filter(newline::dos), mixed, dos));

        // Test converting to mac format.

    BOOST_CHECK(test_output_filter(newline_filter(newline::mac), posix, mac));
    BOOST_CHECK(test_output_filter(newline_filter(newline::mac), dos, mac));
    BOOST_CHECK(test_output_filter(newline_filter(newline::mac), mac, mac));
    BOOST_CHECK(test_output_filter(newline_filter(newline::mac), mixed, mac));
}

void test_input_against_flags(int flags, const std::string& input, bool read)
{
    if (read) {
        io::copy(
            io::compose(
                io::newline_checker(flags),
                string_source(input)
            ),
            io::null_sink()
        );
    } else {
        io::copy(
            string_source(input),
            io::compose(io::newline_checker(flags), io::null_sink())
        );
    }
}

void read_newline_checker()
{
    io::filtering_istream in;
    io::newline_checker* checker = 0;

        // Verify properties of ::posix.

    in.push(io::newline_checker(io::newline::posix));
    in.push(string_source(::posix));
    BOOST_CHECK_NO_THROW(io::copy(in, io::null_sink()));
    in.pop(); // pop source.
    checker = BOOST_IOSTREAMS_COMPONENT(in, 0, io::newline_checker);
    BOOST_CHECK(checker->is_posix());
    BOOST_CHECK(!checker->is_dos());
    BOOST_CHECK(!checker->is_mac());
    BOOST_CHECK(!checker->is_mixed());
    BOOST_CHECK(checker->has_final_newline());
    in.pop(); // pop checker.

        // Verify properties of ::dos.

    in.push(io::newline_checker(io::newline::dos));
    in.push(string_source(::dos));
    try {
        io::copy(in, io::null_sink());
    } catch (io::newline_error&) {

⌨️ 快捷键说明

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