////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2004-2013. 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/container for documentation. // ////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include using namespace boost::container; template void test_stored_size_type_impl() { #ifndef BOOST_NO_EXCEPTIONS StringType s; typedef typename StringType::size_type size_type; typedef typename StringType::value_type value_type; //We need a bit to store short/long info, minus the null terminator size_type const max = Unsigned(-1)/2u - 1u; s.resize(5); s.resize(max); BOOST_TEST_THROWS(s.resize(max+1), std::exception); BOOST_TEST_THROWS(s.push_back(value_type(1)), std::exception); BOOST_TEST_THROWS(s.insert(s.begin(), value_type(1)), std::exception); BOOST_TEST_THROWS(s.reserve(max+1), std::exception); #endif } template void test_stored_size_type() { #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) using options_t = string_options_t< stored_size >; #else typedef typename string_options < stored_size >::type options_t; #endif basic_string, void> default_string_t; //Test first with a typical allocator { typedef basic_string, void, options_t> string_t; BOOST_CONTAINER_STATIC_ASSERT(sizeof(string_t) < sizeof(default_string_t)); test_stored_size_type_impl(); } //Test with a V2 allocator { basic_string, allocator, options_t> string_t; BOOST_CONTAINER_STATIC_ASSERT(sizeof(string_t) < sizeof(default_string_t)); //test_stored_size_type_impl(); } { //Test initial capacity typedef basic_string, void, options_t> string_t; const std::size_t theoretical_long_repr_t = ((2 * sizeof(Unsigned) - 1u) / sizeof(void*) + 1u) * sizeof(void*) + sizeof(void*); BOOST_CONTAINER_STATIC_ASSERT(sizeof(string_t) == theoretical_long_repr_t); const std::size_t theoretical_sso_cap = theoretical_long_repr_t - 2u; //-2 --> 1 for short header, 1 for null terminator string_t s; BOOST_ASSERT(s.capacity() == theoretical_sso_cap); } } void test_growth_factor_50() { #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) using options_t = string_options_t< growth_factor >; #else typedef string_options < growth_factor >::type options_t; #endif basic_string, void, options_t> s; s.resize(5); s.resize(s.capacity()); std::size_t old_capacity = s.capacity(); s.push_back(0); std::size_t new_capacity = s.capacity(); std::size_t old_storage = old_capacity+1u; std::size_t new_storage = new_capacity + 1u; BOOST_TEST(new_storage == (old_storage + old_storage/2)); } void test_growth_factor_60() { #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) using options_t = string_options_t< growth_factor >; #else typedef string_options < growth_factor >::type options_t; #endif basic_string, void, options_t> s; s.resize(5); s.resize(s.capacity()); std::size_t old_capacity = s.capacity(); s.push_back(0); std::size_t new_capacity = s.capacity(); std::size_t old_storage = old_capacity+1u; std::size_t new_storage = new_capacity + 1u; BOOST_TEST(new_storage == (old_storage + 3*old_storage/5)); } void test_growth_factor_100() { #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) using options_t = string_options_t< growth_factor >; #else typedef string_options < growth_factor >::type options_t; #endif basic_string, void, options_t> s; s.resize(5); s.resize(s.capacity()); std::size_t old_capacity = s.capacity(); s.push_back(0); std::size_t new_capacity = s.capacity(); std::size_t old_storage = old_capacity+1u; std::size_t new_storage = new_capacity + 1u; BOOST_TEST(new_storage == 2u*old_storage); } int main() { test_growth_factor_50(); test_growth_factor_60(); test_growth_factor_100(); test_stored_size_type(); test_stored_size_type(); return ::boost::report_errors(); }