// // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) // // 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) // // Official repository: https://github.com/CPPAlliance/url // #ifndef BOOST_URL_TEST_BNF_HPP #define BOOST_URL_TEST_BNF_HPP #include #include #include #include #include "test_suite.hpp" namespace boost { namespace urls { template void for_each_char(F const& f) { unsigned char u = 0; do { f(static_cast< char>(u)); } while(++u != 0); } template void test_char_set( CharSet const& cs, string_view s) noexcept { // each char in s is in the set. for(char c : s) BOOST_TEST(cs(c)); // number of chars in // set equals s.size() std::size_t n = 0; for_each_char( [&cs, &n](char c) { if(cs(c)) ++n; }); BOOST_TEST(n == s.size()); // test find_if and find_if_not for_each_char( [&cs](char c) { if(cs(c)) { BOOST_TEST(bnf::find_if( &c, &c+1, cs) == &c); BOOST_TEST(bnf::find_if_not( &c, &c+1, cs) == &c+1); } else { BOOST_TEST(bnf::find_if( &c, &c+1, cs) == &c+1); BOOST_TEST(bnf::find_if_not( &c, &c+1, cs) == &c); } }); } template< class T, class V> struct test_ref { V v; friend bool parse( char const*& it, char const* end, error_code& ec, test_ref& t) { using bnf::parse; return parse( it, end, ec, T{t.v}); } }; template bool is_valid(string_view s) { T t; error_code ec; return bnf::parse_string( s, ec, t); } bool is_valid(char) = delete; template void validate(string_view s) { if(! is_valid(s)) detail::throw_invalid_argument( BOOST_CURRENT_LOCATION); } template void bad(string_view s) { BOOST_TEST_THROWS( validate(s), std::exception); BOOST_TEST(! is_valid(s)); } template void bad(std::initializer_list< string_view> init) { for(auto s : init) bad(s); } template void good(string_view s) { BOOST_TEST_NO_THROW( validate(s)); BOOST_TEST(is_valid(s)); } template void good(std::initializer_list< string_view> init) { for(auto s : init) good(s); } } // urls } // boost #endif