Commit 003f9f9b authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

#1468 Rewrite completely the way to compute the list of entry keys. It needs heavy-clean-up though!

parent 80337e43
......@@ -55,180 +55,179 @@ PRAGMA_DIAGNOSTIC(ignored "-Wdisabled-macro-expansion")
# endif // __clang__
//BOOST_FIXTURE_TEST_CASE(parameters_properly_read, TestNS::FixtureNS::Environment)
//{
// decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
//
// decltype(auto) input_file =
// environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
//
// std::unique_ptr<LuaOptionFile> ptr;
// /* BOOST_REQUIRE_NO_THROW */(ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__)));
//
// auto& lua_option_file = *(ptr.get());
//
// BOOST_CHECK(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "root_value", __FILE__, __LINE__),
// 2.44));
//
// BOOST_CHECK(ReadLuaOptionFile<std::string>(lua_option_file, "section1.string_value", __FILE__, __LINE__) == "string");
// BOOST_CHECK(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "section1.double_value",
// __FILE__, __LINE__),
// 5.215));
//
// BOOST_CHECK(ReadLuaOptionFile<unsigned int>(lua_option_file, "section1.int_value", __FILE__, __LINE__) == 10u);
// BOOST_CHECK_THROW(ReadLuaOptionFile<unsigned int>(lua_option_file, "section1.double_value", __FILE__, __LINE__),
// std::exception);
//
// BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
// __FILE__, __LINE__).size()
// == 3ul);
// BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
// __FILE__, __LINE__)[0]
// == "foo");
// BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
// __FILE__, __LINE__)[1]
// == "bar");
// BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
// __FILE__, __LINE__)[2]
// == "baz");
// BOOST_CHECK_THROW(ReadLuaOptionFile<int>(lua_option_file, "section1.vector_value", __FILE__, __LINE__),
// std::exception);
// BOOST_CHECK_THROW(ReadLuaOptionFile<std::vector<int>>(lua_option_file, "section1.int_value", __FILE__, __LINE__),
// std::exception);
//
// BOOST_CHECK(ReadLuaOptionFile<std::string>(lua_option_file, "section1.string_value", __FILE__, __LINE__)
// == "string");
//
// BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
// __FILE__, __LINE__).size()
// == 3ul));
// BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
// __FILE__, __LINE__)[3]
// == 5));
// BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
// __FILE__, __LINE__)[4]
// == 7));
// BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
// __FILE__, __LINE__)[5]
// == 8));
//
// BOOST_CHECK_THROW(ReadLuaOptionFile<std::string>(lua_option_file, "unknown_key", __FILE__, __LINE__),
// std::exception);
// BOOST_CHECK_THROW(ReadLuaOptionFile<double>(lua_option_file, "section1.string_value", __FILE__, __LINE__),
// std::exception);
//
// BOOST_CHECK_THROW(ReadLuaOptionFile<double>(lua_option_file, "section1.invalid_value", __FILE__, __LINE__),
// std::exception);
//
// // When I have time to investigate how to do so in Boost
// // CHECK_ABORT(ReadLuaOptionFile<double>(lua_option_file, __FILE__, __LINE__));
//}
//
//
//BOOST_FIXTURE_TEST_CASE(constraints, TestNS::FixtureNS::Environment)
//{
// decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
//
// decltype(auto) input_file =
// environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
//
// std::unique_ptr<LuaOptionFile> ptr;
// /* BOOST_REQUIRE_NO_THROW */(ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__)));
// auto& lua_option_file = *(ptr.get());
//
// BOOST_CHECK_THROW(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "root_value", __FILE__, __LINE__,
// "v > 5."),
// 2.44),
// std::exception);
// /* BOOST_CHECK_NO_THROW */(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "root_value",
// __FILE__, __LINE__, "v < 3."),
// 2.44));
//
// /* BOOST_CHECK_NO_THROW */(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
// __FILE__, __LINE__, "value_in(v, { 'foo', 'bar', 'baz' })"));
//
// BOOST_CHECK_THROW(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
// __FILE__, __LINE__, "value_in(v, { 'bar', 'baz' })"),
// std::exception);
//
//}
//
//
////TEST_CASE("Forgotten call to Open()")
////{
//// decltype(auto) input_file =
//// environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
////
//// LuaOptionFile lua_option_file;
//// BOOST_CHECK_THROW(NumericNS::AreEqual(ReadLuaOptionFile<double>(("root_value", "v > 5.", __FILE__, __LINE__), 2.44));
////
////}
//
//
//BOOST_FIXTURE_TEST_CASE(lua_functions, TestNS::FixtureNS::Environment)
//{
// decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
//
// decltype(auto) input_file =
// environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
//
// std::unique_ptr<LuaOptionFile> ptr;
// ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__));
// auto& lua_option_file = *(ptr.get());
//
// decltype(auto) one_arg_fct =
// ReadLuaOptionFile<Utilities::InputDataNS::LuaFunction<double(double)>>(lua_option_file,
// "section2.one_arg_function",
// __FILE__, __LINE__);
//
// BOOST_CHECK(NumericNS::AreEqual(one_arg_fct(3.), -3.));
//
// decltype(auto) several_arg_function =
// ReadLuaOptionFile<Utilities::InputDataNS::LuaFunction<double(double, double, double)>>(lua_option_file,
// "section2.several_arg_function",
// __FILE__, __LINE__);
//
// BOOST_CHECK(NumericNS::AreEqual(several_arg_function(3., 4., 5.), 2.));
//
//}
//
//
//
//BOOST_FIXTURE_TEST_CASE(redundant, TestNS::FixtureNS::Environment)
//{
// decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
//
// decltype(auto) input_file =
// environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/redundancy.lua");
//
// std::unique_ptr<LuaOptionFile> ptr = nullptr;
// BOOST_REQUIRE_THROW(ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__)), std::exception);
//
//}
//
//
//
//BOOST_FIXTURE_TEST_CASE(invalid_lua, TestNS::FixtureNS::Environment)
BOOST_FIXTURE_TEST_CASE(parameters_properly_read, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
decltype(auto) input_file =
environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
std::unique_ptr<LuaOptionFile> ptr;
/* BOOST_REQUIRE_NO_THROW */(ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__)));
auto& lua_option_file = *(ptr.get());
BOOST_CHECK(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "root_value", __FILE__, __LINE__),
2.44));
BOOST_CHECK(ReadLuaOptionFile<std::string>(lua_option_file, "section1.string_value", __FILE__, __LINE__) == "string");
BOOST_CHECK(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "section1.double_value",
__FILE__, __LINE__),
5.215));
BOOST_CHECK(ReadLuaOptionFile<unsigned int>(lua_option_file, "section1.int_value", __FILE__, __LINE__) == 10u);
BOOST_CHECK_THROW(ReadLuaOptionFile<unsigned int>(lua_option_file, "section1.double_value", __FILE__, __LINE__),
std::exception);
BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
__FILE__, __LINE__).size()
== 3ul);
BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
__FILE__, __LINE__)[0]
== "foo");
BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
__FILE__, __LINE__)[1]
== "bar");
BOOST_CHECK(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
__FILE__, __LINE__)[2]
== "baz");
BOOST_CHECK_THROW(ReadLuaOptionFile<int>(lua_option_file, "section1.vector_value", __FILE__, __LINE__),
std::exception);
BOOST_CHECK_THROW(ReadLuaOptionFile<std::vector<int>>(lua_option_file, "section1.int_value", __FILE__, __LINE__),
std::exception);
BOOST_CHECK(ReadLuaOptionFile<std::string>(lua_option_file, "section1.string_value", __FILE__, __LINE__)
== "string");
BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
__FILE__, __LINE__).size()
== 3ul));
BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
__FILE__, __LINE__)[3]
== 5));
BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
__FILE__, __LINE__)[4]
== 7));
BOOST_CHECK((ReadLuaOptionFile<std::map<int, int>>(lua_option_file, "section1.map_value",
__FILE__, __LINE__)[5]
== 8));
BOOST_CHECK_THROW(ReadLuaOptionFile<std::string>(lua_option_file, "unknown_key", __FILE__, __LINE__),
std::exception);
BOOST_CHECK_THROW(ReadLuaOptionFile<double>(lua_option_file, "section1.string_value", __FILE__, __LINE__),
std::exception);
BOOST_CHECK_THROW(ReadLuaOptionFile<double>(lua_option_file, "section1.invalid_value", __FILE__, __LINE__),
std::exception);
// When I have time to investigate how to do so in Boost
// CHECK_ABORT(ReadLuaOptionFile<double>(lua_option_file, __FILE__, __LINE__));
}
BOOST_FIXTURE_TEST_CASE(constraints, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
decltype(auto) input_file =
environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
std::unique_ptr<LuaOptionFile> ptr;
/* BOOST_REQUIRE_NO_THROW */(ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__)));
auto& lua_option_file = *(ptr.get());
BOOST_CHECK_THROW(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "root_value", __FILE__, __LINE__,
"v > 5."),
2.44),
std::exception);
/* BOOST_CHECK_NO_THROW */(NumericNS::AreEqual(ReadLuaOptionFile<double>(lua_option_file, "root_value",
__FILE__, __LINE__, "v < 3."),
2.44));
/* BOOST_CHECK_NO_THROW */(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
__FILE__, __LINE__, "value_in(v, { 'foo', 'bar', 'baz' })"));
BOOST_CHECK_THROW(ReadLuaOptionFile<std::vector<std::string>>(lua_option_file, "section1.vector_value",
__FILE__, __LINE__, "value_in(v, { 'bar', 'baz' })"),
std::exception);
}
//TEST_CASE("Forgotten call to Open()")
//{
// decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
//
// decltype(auto) input_file =
// environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
//
// std::unique_ptr<LuaOptionFile> ptr;
// ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__));
// auto& lua_option_file = *(ptr.get());
// LuaOptionFile lua_option_file;
// BOOST_CHECK_THROW(NumericNS::AreEqual(ReadLuaOptionFile<double>(("root_value", "v > 5.", __FILE__, __LINE__), 2.44));
//
// BOOST_REQUIRE_THROW(ReadLuaOptionFile<Utilities::InputDataNS::LuaFunction<double(double)>>(lua_option_file,
// "section3.invalid_function",
// __FILE__, __LINE__),
// std::exception);
//}
BOOST_FIXTURE_TEST_CASE(lua_functions, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
decltype(auto) input_file =
environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
std::unique_ptr<LuaOptionFile> ptr;
ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__));
auto& lua_option_file = *(ptr.get());
decltype(auto) one_arg_fct =
ReadLuaOptionFile<Utilities::InputDataNS::LuaFunction<double(double)>>(lua_option_file,
"section2.one_arg_function",
__FILE__, __LINE__);
BOOST_CHECK(NumericNS::AreEqual(one_arg_fct(3.), -3.));
decltype(auto) several_arg_function =
ReadLuaOptionFile<Utilities::InputDataNS::LuaFunction<double(double, double, double)>>(lua_option_file,
"section2.several_arg_function",
__FILE__, __LINE__);
BOOST_CHECK(NumericNS::AreEqual(several_arg_function(3., 4., 5.), 2.));
}
BOOST_FIXTURE_TEST_CASE(redundant, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
decltype(auto) input_file =
environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/redundancy.lua");
std::unique_ptr<LuaOptionFile> ptr = nullptr;
BOOST_REQUIRE_THROW(ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__)), std::exception);
}
BOOST_FIXTURE_TEST_CASE(invalid_lua, TestNS::FixtureNS::Environment)
{
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
decltype(auto) input_file =
environment.SubstituteValues("${MOREFEM_ROOT}/Sources/Test/Utilities/LuaOptionFile/demo_lua_option_file.lua");
std::unique_ptr<LuaOptionFile> ptr;
ptr.reset(new LuaOptionFile(input_file, __FILE__, __LINE__));
auto& lua_option_file = *(ptr.get());
BOOST_REQUIRE_THROW(ReadLuaOptionFile<Utilities::InputDataNS::LuaFunction<double(double)>>(lua_option_file,
"section3.invalid_function",
__FILE__, __LINE__),
std::exception);
}
BOOST_FIXTURE_TEST_CASE(map_in_vector, TestNS::FixtureNS::Environment)
{
std::cout << "================================== TMP =================================" << std::endl;
// Introduced after #1468, in which two braces on the same line wreak havoc...
decltype(auto) environment = Utilities::Environment::CreateOrGetInstance(__FILE__, __LINE__);
......@@ -239,7 +238,7 @@ BOOST_FIXTURE_TEST_CASE(map_in_vector, TestNS::FixtureNS::Environment)
std::ostringstream oconv;
Utilities::PrintContainer<>::Do(lua.GetEntryKeyList(), oconv);
Utilities::PrintContainer<>::Do(lua.GetEntryKeyList(), oconv, ", ", "[", "]");
BOOST_CHECK_EQUAL(oconv.str(),
"[ValueOutsideBrace, "
......@@ -260,7 +259,7 @@ BOOST_FIXTURE_TEST_CASE(map_in_vector, TestNS::FixtureNS::Environment)
// LuaOptionFile lua(input_file, __FILE__, __LINE__);
//
// std::ostringstream oconv;
// Utilities::PrintContainer<>::Do(lua.GetEntryKeyList(), oconv);
// Utilities::PrintContainer<>::Do(lua.GetEntryKeyList(), oconv, ", ", "[", "]");
//
// BOOST_CHECK_EQUAL(oconv.str(),
// "[a, b, c, d, e, f]");
......
......@@ -127,6 +127,18 @@ namespace MoReFEM
};
struct Section
{
std::string name;
std::size_t min;
std::size_t max;
};
void AddValueIn(lua_State* lua_state)
{
std::string code = "function value_in(v, table)\
......@@ -358,6 +370,8 @@ namespace MoReFEM
// Filter out the braces without values inside: they were Lua vectors and not sections.
std::vector<std::pair<int, int>> filtered_pair_list;
for (const auto& pair : pair_list)
{
const auto opening = pair.first;
......@@ -398,23 +412,38 @@ namespace MoReFEM
}
std::vector<Section> section_list;
for (auto i = 0ul; i < Npair; ++i)
{
const auto opening_brace = filtered_pair_list[i].first;
auto reverse_it = name_list.rbegin();
// Using standard iterators instead of reverse ones as I want to use erase and conversion with
// normal iterators is shall we say headache prone...
auto it = name_list.end();
--it;
auto end = name_list.begin();
--end;
for (; reverse_it != name_list.rend(); ++reverse_it)
for (; it != end; --it)
{
if (reverse_it->position < opening_brace)
if (it->position < opening_brace)
break;
}
std::cout << reverse_it->name << "\n";
assert(reverse_it != name_list.rend());
std::cout << it->name << "\n";
assert(it != end);
Section section;
section.min = opening_brace;
section.max = filtered_pair_list[i].second;
section.name = it->name;
section_list.push_back(section);
std::cout << "PAIR " << opening_brace << " => lower = " << it->position << " and " << it->name << std::endl;
std::cout << "PAIR " << opening_brace << " => lower = " << reverse_it->position << " and " << reverse_it->name << std::endl;
name_list.erase(it);
// std::cout << name_list[it - equal_position_list.cbegin()] << " -> ";
// Utilities::PrintTuple(pair_list[i]);
......@@ -425,27 +454,34 @@ namespace MoReFEM
}
std::sort(section_list.begin(),
section_list.end(),
[](const auto& lhs, const auto& rhs)
{
return lhs.min < rhs.min;
});
// Reconstruct names.
std::vector<std::string> ret;
for (const auto& name : name_list)
{
// auto Nopening_brace = (std::count(content.cbegin(), content.cend(), '{'));
// auto Nclosing_brace = (std::count(content.cbegin(), content.cend(), '}'));
//
// if (Nopening_brace != Nclosing_brace)
// {
// oconv << "Invalid Lua file (" << path << "): " << Nopening_brace << " '{' were found whereas there "
// "were " << Nclosing_brace << " '}'; the numbers should match!";
// throw Exception(oconv.str(), __FILE__, __LINE__);
// }
oconv.str("");
std::cout << oconv.str() << std::endl;
for (const auto& section : section_list)
{
if (name.position > section.min && name.position < section.max)
oconv << section.name << '.';
}
std::vector<std::string> ret;
oconv << name.name;
ret.push_back(oconv.str());
}
return ret;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment