// Copyright Eric Niebler 2009 // // 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/mpl for documentation. // $Id: string.cpp 49240 2009-04-01 09:21:07Z eric_niebler $ // $Date: 2009-04-01 02:21:07 -0700 (Wed, 1 Apr 2009) $ // $Revision: 49240 $ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace mpl = boost::mpl; // Accept a string as a template parameter! template struct greeting { std::string say_hello() const { return sz; } }; struct push_char { push_char(std::string &str) : str_(&str) {} void operator()(char ch) const { this->str_->push_back(ch); } std::string *str_; }; void test1() { BOOST_TEST(0 == std::strcmp(mpl::string<'Hell','o wo','rld!'>::c_str, "Hello world!")); BOOST_TEST((12 == mpl::size >::type::value)); BOOST_TEST(('w' == mpl::at_c, 6>::type::value)); // test using a string as a template parameter greeting::c_str> g; BOOST_TEST("Hello world!" == g.say_hello()); BOOST_TEST(0 == std::strcmp("", mpl::string<>::c_str)); std::string result; mpl::for_each >(push_char(result)); BOOST_TEST("Hello world!" == result); BOOST_MPL_ASSERT((mpl::empty >)); BOOST_MPL_ASSERT_NOT((mpl::empty >)); BOOST_TEST(('h' == mpl::front >::type())); BOOST_TEST(('!' == mpl::back >::type())); } // testing push_back void test2() { typedef mpl::push_back, mpl::char_<'a'> >::type t1; BOOST_TEST(0 == std::strcmp("a", t1::c_str)); typedef mpl::push_back >::type t2; BOOST_TEST(0 == std::strcmp("ab", t2::c_str)); typedef mpl::push_back >::type t3; BOOST_TEST(0 == std::strcmp("abc", t3::c_str)); BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t4; BOOST_TEST(0 == std::strcmp("abcd", t4::c_str)); typedef mpl::push_back >::type t5; BOOST_TEST(0 == std::strcmp("abcde", t5::c_str)); BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> almost_full; BOOST_TEST(0 == std::strcmp("aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaa", almost_full::c_str)); typedef mpl::push_back >::type t6; BOOST_TEST(0 == std::strcmp("aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaX", t6::c_str)); } // testing push_front void test3() { typedef mpl::push_front, mpl::char_<'a'> >::type t1; BOOST_TEST(0 == std::strcmp("a", t1::c_str)); typedef mpl::push_front >::type t2; BOOST_TEST(0 == std::strcmp("ba", t2::c_str)); typedef mpl::push_front >::type t3; BOOST_TEST(0 == std::strcmp("cba", t3::c_str)); typedef mpl::push_front >::type t4; BOOST_TEST(0 == std::strcmp("dcba", t4::c_str)); typedef mpl::push_front >::type t5; BOOST_TEST(0 == std::strcmp("edcba", t5::c_str)); typedef mpl::string<'aaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa'> almost_full; BOOST_TEST(0 == std::strcmp("aaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa", almost_full::c_str)); typedef mpl::push_front >::type t6; BOOST_TEST(0 == std::strcmp("Xaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa", t6::c_str)); } void test4() { // back-inserter with copy typedef mpl::vector_c rgc; typedef mpl::copy > >::type str; BOOST_TEST(0 == std::strcmp("abcde", str::c_str)); } // test insert_range and erase void test5() { typedef mpl::string<'Hell','o wo','rld!'> hello; typedef mpl::advance_c::type, 5>::type where; typedef mpl::string<' cru','el'> cruel; typedef mpl::insert_range::type hello_cruel; BOOST_TEST(0 == std::strcmp("Hello cruel world!", hello_cruel::c_str)); typedef mpl::erase::type, where>::type erased1; BOOST_TEST(0 == std::strcmp(" world!", erased1::c_str)); } int main() { test1(); test2(); test3(); test4(); test5(); return boost::report_errors(); }