2
0
mirror of https://github.com/boostorg/url.git synced 2026-02-13 12:52:14 +00:00
Files
url/test/unit/test_rule.hpp
2022-08-01 13:41:07 -07:00

127 lines
2.6 KiB
C++

//
// 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_RULE_HPP
#define BOOST_URL_TEST_RULE_HPP
#include <boost/url/grammar/charset.hpp>
#include <boost/url/grammar/parse.hpp>
#include <boost/url/grammar/type_traits.hpp>
#include <boost/url/string_view.hpp>
#include <initializer_list>
#include <type_traits>
#include "test_suite.hpp"
namespace boost {
namespace urls {
template<class F>
void
for_each_char(F const& f)
{
unsigned char u = 0;
do
{
f(static_cast<
char>(u));
}
while(++u != 0);
}
template<class CharSet>
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_EQ(n, s.size());
// test find_if and find_if_not
for_each_char(
[&cs](char c)
{
if(cs(0) || ! cs(c))
{
if(cs(c))
{
BOOST_TEST(grammar::find_if(
&c, &c+1, cs) == &c);
BOOST_TEST(grammar::find_if_not(
&c, &c+1, cs) == &c+1);
}
else
{
BOOST_TEST(grammar::find_if(
&c, &c+1, cs) == &c+1);
BOOST_TEST(grammar::find_if_not(
&c, &c+1, cs) == &c);
}
return;
}
char buf[40];
std::memset(
buf, 0, sizeof(buf));
buf[19] = c;
buf[22] = c;
BOOST_TEST(grammar::find_if(
buf, buf + sizeof(buf), cs) ==
buf + 19);
std::memset(
buf, c, sizeof(buf));
buf[19] = 0;
buf[22] = 0;
BOOST_TEST(grammar::find_if_not(
buf, buf + sizeof(buf), cs) ==
buf + 19);
});
}
//------------------------------------------------
// rule must match the string
template<class R>
typename std::enable_if<
grammar::is_rule<R>::value>::type
ok( R const& r,
string_view s)
{
BOOST_TEST(grammar::parse(s, r).has_value());
}
// rule must fail the string
template<class R>
typename std::enable_if<
grammar::is_rule<R>::value>::type
bad(
R const& r,
string_view s)
{
BOOST_TEST(grammar::parse(s, r).has_error());
}
} // urls
} // boost
#endif