diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2683ee7..d984768 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,6 @@ set(TEST_NAMES test_traits + test_utility test_value test_to_toml test_from_toml diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp new file mode 100644 index 0000000..a36ff38 --- /dev/null +++ b/tests/test_utility.cpp @@ -0,0 +1,64 @@ +#define BOOST_TEST_MODULE "test_acceptor" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST +#include +#else +#define BOOST_TEST_NO_LIB +#include +#endif +#include +#include +#include + +BOOST_AUTO_TEST_CASE(test_resize) +{ + { + typedef std::vector resizable_type; + typedef std::array non_resizable_type; + BOOST_CHECK(toml::detail::has_resize_method::value); + BOOST_CHECK(not toml::detail::has_resize_method::value); + } + + { + bool thrown = false; + std::vector v; + try + { + toml::resize(v, 10); + } + catch(std::exception& ex) + { + thrown = true; + } + BOOST_CHECK(not thrown); + BOOST_CHECK_EQUAL(v.size(), 10); + } + + { + bool thrown = false; + std::array a; + try + { + toml::resize(a, 10); + } + catch(std::exception& ex) + { + thrown = true; + } + BOOST_CHECK(not thrown); + BOOST_CHECK_EQUAL(a.size(), 15); + } + + { + bool thrown = false; + std::array a; + try + { + toml::resize(a, 20); + } + catch(std::exception& ex) + { + thrown = true; + } + BOOST_CHECK(thrown); + } +} diff --git a/toml/utility.hpp b/toml/utility.hpp index 80b5843..0df0d07 100644 --- a/toml/utility.hpp +++ b/toml/utility.hpp @@ -1,5 +1,6 @@ #ifndef TOML11_UTILITY #define TOML11_UTILITY +#include "traits.hpp" #include #include @@ -12,5 +13,31 @@ inline std::unique_ptr make_unique(Ts&& ... args) return std::unique_ptr(new T(std::forward(args)...)); } +namespace detail +{ + +template +inline void resize_impl(T& container, std::size_t N, std::true_type) +{ + container.resize(N); + return ; +} + +template +inline void resize_impl(T& container, std::size_t N, std::false_type) +{ + if(container.size() >= N) return; + else throw std::invalid_argument("not resizable type"); +} + +} + +template +inline void resize(T& container, std::size_t N) +{ + if(container.size() == N) return; + else return detail::resize_impl(container, N, detail::has_resize_method()); +} + }// toml #endif // TOML11_UTILITY