Files
spirit_x4/test/x4/symbols3.cpp
Nana Sakisaka 05bf2eb30b Migrate to Catch2 (#48)
* Migrate to Catch2

* Enable colors in Catch2

* CI: Cache Catch2

* Remove unused variable

* Supply compiler flags to `Catch2WithMain`

* CI: Fix Catch2 cache

* Fix styling
2025-09-25 17:26:35 +09:00

98 lines
2.5 KiB
C++

/*=============================================================================
Copyright (c) 2013 Carl Barron
Copyright (c) 2015 Joel de Guzman
Copyright (c) 2025 Nana Sakisaka
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)
=============================================================================*/
#define BOOST_SPIRIT_X4_DEBUG
#define BOOST_SPIRIT_X4_UNICODE
#include "test.hpp"
#include <boost/spirit/x4/symbols.hpp>
#include <boost/spirit/x4/char_encoding/unicode.hpp>
#include <boost/spirit/x4/operator/optional.hpp>
#include <boost/spirit/x4/operator/sequence.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <optional>
#include <numeric>
struct roman
{
std::optional<int> a;
std::optional<int> b;
std::optional<int> c;
};
BOOST_FUSION_ADAPT_STRUCT(roman,
a, b, c
)
namespace {
int eval(roman const & c)
{
return c.a.value_or(0) + c.b.value_or(0) + c.c.value_or(0);
}
} // anonymous
TEST_CASE("symbols3")
{
using x4::shared_symbols;
{
// construction from initializer-list
shared_symbols<int> const ones = {
{"I", 1}, {"II", 2}, {"III", 3}, {"IV", 4},
{"V", 5}, {"VI", 6}, {"VII", 7}, {"VIII", 8},
{"IX", 9}
};
shared_symbols<int> const tens = {
{"X", 10}, {"XX", 20}, {"XXX", 30}, {"XL", 40},
{"L", 50}, {"LX", 60}, {"LXX", 70}, {"LXXX", 80},
{"XC", 90}
};
shared_symbols<int> const hundreds {
{"C", 100}, {"CC", 200}, {"CCC", 300}, {"CD", 400},
{"D", 500}, {"DC", 600}, {"DCC", 700}, {"DCCC", 800},
{"CM", 900}
};
auto number = -hundreds >> -tens >> -ones;
roman r;
REQUIRE(parse("CDXLII", number, r));
CHECK(eval(r) == 442);
}
{
// construction from initializer-list without attribute
shared_symbols<> foo = {"a1", "a2", "a3"};
CHECK(parse("a3", foo));
}
{
// assignment from initializer-list
shared_symbols<> foo;
foo = {"a1", "a2", "a3"};
CHECK(parse("a3", foo));
}
{
// unicode | construction from initializer-list
x4::shared_symbols_parser<x4::char_encoding::unicode, int> foo = {{U"a1", 1}, {U"a2", 2}, {U"a3", 3}};
int r = 0;
REQUIRE(parse(U"a3", foo, r));
CHECK(r == 3);
}
}