// 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 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_; }; int main() { // Test mpl::size of strings { typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> almost_full; typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa'> full; BOOST_MPL_ASSERT_RELATION(0, ==, (mpl::size >::value)); BOOST_MPL_ASSERT_RELATION(1, ==, (mpl::size >::value)); BOOST_MPL_ASSERT_RELATION(2, ==, (mpl::size >::value)); BOOST_MPL_ASSERT_RELATION(2, ==, (mpl::size >::value)); BOOST_MPL_ASSERT_RELATION(4, ==, (mpl::size >::value)); BOOST_MPL_ASSERT_RELATION(5, ==, (mpl::size >::value)); BOOST_MPL_ASSERT_RELATION(31, ==, (mpl::size::value)); BOOST_MPL_ASSERT_RELATION(32, ==, (mpl::size::value)); } // Test mpl::begin and mpl::end with strings { typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> almost_full; typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa'> full; BOOST_MPL_ASSERT(( boost::is_same< mpl::begin >::type , mpl::end >::type > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::begin >::type , mpl::string_iterator, 0, 0> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::end >::type , mpl::string_iterator, 1, 0> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::begin::type , mpl::string_iterator > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::end::type , mpl::string_iterator > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::begin::type , mpl::string_iterator > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::end::type , mpl::string_iterator > )); } // testing push_back { typedef mpl::push_back, mpl::char_<'a'> >::type t1; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t2; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t3; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t4; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t5; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> almost_full; typedef mpl::push_back >::type t6; BOOST_MPL_ASSERT((boost::is_same >)); } // Test mpl::next { typedef mpl::string<'a','bc','def','ghij'> s; typedef mpl::begin::type i0; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i1; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i2; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i3; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i4; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i5; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i6; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i7; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i8; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i9; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::next::type i10; BOOST_MPL_ASSERT((boost::is_same >)); BOOST_MPL_ASSERT((boost::is_same::type>)); } // Test mpl::prior { typedef mpl::string<'a','bc','def','ghij'> s; typedef mpl::end::type i10; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i9; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i8; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i7; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i6; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i5; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i4; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i3; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i2; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i1; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::prior::type i0; BOOST_MPL_ASSERT((boost::is_same >)); BOOST_MPL_ASSERT((boost::is_same::type>)); } // Test mpl::deref { typedef mpl::string<'a','bc','def','ghij'> s; typedef mpl::begin::type i0; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'a'> >)); typedef mpl::next::type i1; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'b'> >)); typedef mpl::next::type i2; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'c'> >)); typedef mpl::next::type i3; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'d'> >)); typedef mpl::next::type i4; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'e'> >)); typedef mpl::next::type i5; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'f'> >)); typedef mpl::next::type i6; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'g'> >)); typedef mpl::next::type i7; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'h'> >)); typedef mpl::next::type i8; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'i'> >)); typedef mpl::next::type i9; BOOST_MPL_ASSERT((boost::is_same::type, mpl::char_<'j'> >)); } // testing push_back { typedef mpl::push_back, mpl::char_<'a'> >::type t1; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t2; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t3; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t4; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_back >::type t5; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> almost_full; typedef mpl::push_back >::type t6; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::string<'a','a','a','a','a','a','a','aaaa'> must_repack; typedef mpl::push_back >::type t7; BOOST_MPL_ASSERT((boost::is_same >)); } BOOST_MPL_ASSERT((mpl::empty >)); BOOST_MPL_ASSERT_NOT((mpl::empty >)); // testing push_front { typedef mpl::push_front, mpl::char_<'a'> >::type t1; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_front >::type t2; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_front >::type t3; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_front >::type t4; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::push_front >::type t5; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::string<'aaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa'> almost_full; typedef mpl::push_front >::type t6; BOOST_MPL_ASSERT((boost::is_same >)); typedef mpl::string<'aaaa','a','a','a','a','a','a','a'> must_repack; typedef mpl::push_front >::type t7; BOOST_MPL_ASSERT((boost::is_same >)); } // Test c_str<> BOOST_TEST(0 == std::strcmp( mpl::c_str >::value , "" )); BOOST_TEST(0 == std::strcmp( mpl::c_str >::value , "Hell" "o wo" "rld!" )); BOOST_TEST(0 == std::strcmp( mpl::c_str >::value , "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaa" "aaaX" )); // test using a string as a template parameter greeting >::value> g; BOOST_TEST("Hello world!" == g.say_hello()); std::string result; mpl::for_each >(push_char(result)); BOOST_TEST("Hello world!" == result); BOOST_TEST(('h' == mpl::front >::type())); BOOST_TEST(('!' == mpl::back >::type())); // back-inserter with copy typedef mpl::vector_c rgc; BOOST_TEST(0 == std::strcmp("abcde", mpl::c_str::value)); typedef mpl::copy > >::type str; BOOST_TEST(0 == std::strcmp("abcde", mpl::c_str::value)); // test insert_range and erase { 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!", mpl::c_str::value)); typedef mpl::erase::type, where>::type erased1; BOOST_TEST(0 == std::strcmp(" world!", mpl::c_str::value)); } // test pop_front { BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<'b'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<'bc'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<'bcd'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<'bcd','e'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<'e'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_front >::type , mpl::string<'aaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa'> > )); } // test pop_back { BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<'a'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<'ab'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<'abc'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<'abcd'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<'d'> > )); BOOST_MPL_ASSERT(( boost::is_same< mpl::pop_back >::type , mpl::string<'aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaaa','aaa'> > )); } { BOOST_TEST(( mpl::at_c< mpl::string<'\x7f'> , 0 >::type::value == (char)0x7f )); BOOST_TEST(( mpl::at_c< mpl::string<'\x80'> , 0 >::type::value == (char)0x80 )); BOOST_TEST(( mpl::at_c< mpl::string< mpl::at_c< mpl::string<'\x7f'> , 0 >::type::value > , 0 >::type::value == (char)0x7f )); BOOST_TEST(( mpl::at_c< mpl::string< mpl::at_c< mpl::string<'\x80'> , 0 >::type::value > , 0 >::type::value == (char)0x80 )); } return boost::report_errors(); }