Skip to content
Snippets Groups Projects
Commit f81f633e authored by Damien Lacoste's avatar Damien Lacoste
Browse files

Improve tests to check data inserted for image format.

Note that this could be further improved by checking the dim_y and dim_x
parameters.
parent cf3ef220
No related branches found
No related tags found
No related merge requests found
...@@ -172,14 +172,20 @@ public: ...@@ -172,14 +172,20 @@ public:
// currently. Copy the str into a std::string so we can work with it more easily. // currently. Copy the str into a std::string so we can work with it more easily.
std::string in(str + 1, str + (strlen(str) - 1)); std::string in(str + 1, str + (strlen(str) - 1));
// count commas and add one to deduce elements in the string, // count opening brackets and add one to deduce elements in the string,
// note we reduce string size to remove the brace at each end // note we reduce string size to remove the brace at each end
auto dim_y = std::count(in.begin(), in.end(), '{');
if(dim_y == 0)
{
// Regular array
auto items = std::count(in.begin(), in.end(), ','); auto items = std::count(in.begin(), in.end(), ',');
value.clear(); value.dim_y = dim_y;
value.dim_x = items + 1;
// preallocate all the items in the vector, we can then // preallocate all the items in the vector, we can then
// simply set each in turn // simply set each in turn
value.resize(items + 1); value.resize(value.dim_x);
auto element = 0; auto element = 0;
std::string::size_type comma = 0; std::string::size_type comma = 0;
...@@ -192,6 +198,30 @@ public: ...@@ -192,6 +198,30 @@ public:
comma = next_comma; comma = next_comma;
} }
} }
else
{
// 2D array, or image
auto items = std::count(in.begin(), in.end(), ',');
value.dim_y = dim_y;
value.dim_x = (items + 1) / dim_y;
// Look for enclosing brackets to extract rows
std::string::size_type bracket = 0;
std::string::size_type close_bracket = 0;
// loop and copy out each value from between the separators
while ((bracket = in.find_first_of('{', close_bracket)) != std::string::npos)
{
close_bracket = in.find_first_of('}', bracket);
// We clear the value so we need to extract in another vector
hdbpp_internal::TangoValue<T> row;
string_traits<hdbpp_internal::TangoValue<T>>::from_string(in.substr(bracket, close_bracket - bracket + 1).c_str(), row);
value.insert(value.end(), row.begin(), row.end());
}
}
}
// NOLINTNEXTLINE (readability-identifier-naming) // NOLINTNEXTLINE (readability-identifier-naming)
static std::string to_string(const hdbpp_internal::TangoValue<T> &value) static std::string to_string(const hdbpp_internal::TangoValue<T> &value)
...@@ -212,14 +242,11 @@ public: ...@@ -212,14 +242,11 @@ public:
std::stringstream result; std::stringstream result;
result << "{"; result << "{";
for(std::size_t i = 0; i != value.dim_y; ++i)
{
if (i > 0)
{
result << ",";
}
result << "{" << separated_list(",", std::next(value.begin(), i * value.dim_x), std::next(value.begin(), (i+1) * value.dim_x)) << "}"; result << "{" << separated_list(",", value.begin(), std::next(value.begin(), value.dim_x)) << "}";
for(std::size_t i = 1; i != value.dim_y; ++i)
{
result << ", {" << separated_list(",", std::next(value.begin(), i * value.dim_x), std::next(value.begin(), (i+1) * value.dim_x)) << "}";
} }
result << "}"; result << "}";
return result.str(); return result.str();
...@@ -256,30 +283,31 @@ public: ...@@ -256,30 +283,31 @@ public:
value.clear(); value.clear();
value.dim_x = 0;
value.dim_y = 0;
std::pair<array_parser::juncture, std::string> output; std::pair<array_parser::juncture, std::string> output;
// use pqxx array parser features to get each element from the array // use pqxx array parser features to get each element from the array
array_parser parser(str); array_parser parser(str);
output = parser.get_next(); output = parser.get_next();
if (output.first == array_parser::juncture::row_start) while (output.first != array_parser::juncture::done)
{ {
output = parser.get_next(); output = parser.get_next();
if (output.first == array_parser::juncture::string_value)
// loop and extract each string in turn
while (output.first == array_parser::juncture::string_value)
{ {
value.push_back(output.second); value.push_back(output.second);
output = parser.get_next(); continue;
if (output.first == array_parser::juncture::row_end)
break;
if (output.first == array_parser::juncture::done)
break;
} }
if(output.first == array_parser::juncture::row_start)
{
++(value.dim_y);
continue;
} }
} }
value.dim_x = value.dim_y == 0 ? value.size() : (value.size() / value.dim_y);
}
// NOLINTNEXTLINE (readability-identifier-naming) // NOLINTNEXTLINE (readability-identifier-naming)
static std::string to_string(const hdbpp_internal::TangoValue<std::string> &value) static std::string to_string(const hdbpp_internal::TangoValue<std::string> &value)
...@@ -323,13 +351,24 @@ public: ...@@ -323,13 +351,24 @@ public:
// testing only. Copy the str into a std::string so we can work // testing only. Copy the str into a std::string so we can work
// with it more easily. // with it more easily.
std::string in(str + 1, str + (strlen(str) - 1)); std::string in(str + 1, str + (strlen(str) - 1));
// count opening brackets and add one to deduce elements in the string,
// note we reduce string size to remove the brace at each end
auto dim_y = std::count(in.begin(), in.end(), '{');
if(dim_y == 0)
{
// Regular array
auto items = std::count(in.begin(), in.end(), ',');
value.dim_y = dim_y;
value.dim_x = items + 1;
std::string::size_type comma = 0; std::string::size_type comma = 0;
// loop and copy out each value from between the separators // loop and copy out each value from between the separators
while ((comma = in.find_first_not_of(',', comma)) != std::string::npos) while ((comma = in.find_first_not_of(',', comma)) != std::string::npos)
{ {
auto next_comma = in.find_first_of(',', comma); auto next_comma = in.find_first_of(',', comma);
// we can not pass an element of the vector, since vector<bool> is not // we can not pass an element of the vector, since vector<bool> is not
// in fact a container, but some kind of bit field. In this case, we // in fact a container, but some kind of bit field. In this case, we
// have to create a local variable to read the value into, then push this // have to create a local variable to read the value into, then push this
...@@ -337,10 +376,33 @@ public: ...@@ -337,10 +376,33 @@ public:
bool field; bool field;
string_traits<bool>::from_string(in.substr(comma, next_comma - comma).c_str(), field); string_traits<bool>::from_string(in.substr(comma, next_comma - comma).c_str(), field);
value.push_back(field); value.push_back(field);
comma = next_comma; comma = next_comma;
} }
} }
else
{
// 2D array, or image
auto items = std::count(in.begin(), in.end(), ',');
value.dim_y = dim_y;
value.dim_x = (items + 1) / dim_y;
// Look for enclosing brackets to extract rows
std::string::size_type bracket = 0;
std::string::size_type close_bracket = 0;
// loop and copy out each value from between the separators
while ((bracket = in.find_first_of('{', close_bracket)) != std::string::npos)
{
close_bracket = in.find_first_of('}', bracket);
// We clear the value so we need to extract in another vector
hdbpp_internal::TangoValue<bool> row;
string_traits<hdbpp_internal::TangoValue<bool>>::from_string(in.substr(bracket, close_bracket - bracket + 1).c_str(), row);
value.insert(value.end(), row.begin(), row.end());
}
}
}
// NOLINTNEXTLINE (readability-identifier-naming) // NOLINTNEXTLINE (readability-identifier-naming)
static std::string to_string(const hdbpp_internal::TangoValue<bool> &value) static std::string to_string(const hdbpp_internal::TangoValue<bool> &value)
...@@ -348,9 +410,28 @@ public: ...@@ -348,9 +410,28 @@ public:
if (value.empty()) if (value.empty())
return {}; return {};
if(value.dim_y < 2)
{
// simply use the pqxx utilities for this, rather than reinvent the wheel // simply use the pqxx utilities for this, rather than reinvent the wheel
return "{" + separated_list(",", value.begin(), value.end()) + "}"; return "{" + separated_list(",", value.begin(), value.end()) + "}";
} }
// In case of image, unwrap the vector.
assert(value.dim_x != 0);
assert(value.dim_y * value.dim_x != value.size());
std::stringstream result;
result << "{";
result << "{" << separated_list(",", value.begin(), std::next(value.begin(), value.dim_x)) << "}";
for(std::size_t i = 1; i != value.dim_y; ++i)
{
result << ", {" << separated_list(",", std::next(value.begin(), i * value.dim_x), std::next(value.begin(), (i+1) * value.dim_x)) << "}";
}
result << "}";
return result.str();
}
}; };
// This specialisation is for bool, since it is not a normal container class, but // This specialisation is for bool, since it is not a normal container class, but
......
...@@ -326,6 +326,12 @@ void DbConnectionTestsFixture::checkStoreTestEventData( ...@@ -326,6 +326,12 @@ void DbConnectionTestsFixture::checkStoreTestEventData(
REQUIRE(compareVector(data_row.at(schema::DatColValueR).as<TangoValue<T>>(), get<0>(data)) == true); REQUIRE(compareVector(data_row.at(schema::DatColValueR).as<TangoValue<T>>(), get<0>(data)) == true);
} }
if (traits.isImage() && traits.hasReadData())
{
REQUIRE(data_row.at(schema::DatColValueR).size() > 0);
REQUIRE(compareVector(data_row.at(schema::DatColValueR).as<TangoValue<T>>(), get<0>(data)) == true);
}
if (traits.isScalar() && traits.hasWriteData()) if (traits.isScalar() && traits.hasWriteData())
{ {
REQUIRE(data_row.at(schema::DatColValueW).size() > 0); REQUIRE(data_row.at(schema::DatColValueW).size() > 0);
...@@ -337,6 +343,12 @@ void DbConnectionTestsFixture::checkStoreTestEventData( ...@@ -337,6 +343,12 @@ void DbConnectionTestsFixture::checkStoreTestEventData(
REQUIRE(data_row.at(schema::DatColValueW).size() > 0); REQUIRE(data_row.at(schema::DatColValueW).size() > 0);
REQUIRE(compareVector(data_row.at(schema::DatColValueW).as<TangoValue<T>>(), get<1>(data)) == true); REQUIRE(compareVector(data_row.at(schema::DatColValueW).as<TangoValue<T>>(), get<1>(data)) == true);
} }
if (traits.isImage() && traits.hasWriteData())
{
REQUIRE(data_row.at(schema::DatColValueW).size() > 0);
REQUIRE(compareVector(data_row.at(schema::DatColValueW).as<TangoValue<T>>(), get<1>(data)) == true);
}
} }
}; // namespace pqxx_conn_test }; // namespace pqxx_conn_test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment