1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

format: fix parsing of complex values in villas.humas format (closes #261)

This commit is contained in:
Steffen Vogel 2019-09-03 22:54:10 +02:00
parent 1673368c0b
commit e1e9b2e1fc
3 changed files with 104 additions and 8 deletions

View file

@ -585,7 +585,7 @@ int signal_data_parse_str(union signal_data *data, const struct signal *sig, con
break;
case SignalType::COMPLEX: {
float real, imag;
float real, imag = 0;
real = strtod(ptr, end);
if (*end == ptr)
@ -593,14 +593,22 @@ int signal_data_parse_str(union signal_data *data, const struct signal *sig, con
ptr = *end;
imag = strtod(ptr, end);
if (*end == ptr)
return -1;
if (*ptr == 'i' || *ptr == 'j') {
imag = real;
real = 0;
if (**end != 'i')
return -1;
(*end)++;
}
else if (*ptr == '-' || *ptr == '+') {
imag = strtod(ptr, end);
if (*end == ptr)
return -1;
(*end)++;
if (**end != 'i' && **end != 'j')
return -1;
(*end)++;
}
data->z = std::complex<float>(real, imag);
break;

View file

@ -30,13 +30,14 @@ set(TEST_SRC
pool.cpp
queue.cpp
queue_signalled.cpp
signal.cpp
)
add_executable(unit-tests ${TEST_SRC})
target_link_libraries(unit-tests PUBLIC
PkgConfig::CRITERION
villas
Threads::Threads
villas
)
add_custom_target(run-unit-tests

87
tests/unit/signal.cpp Normal file
View file

@ -0,0 +1,87 @@
/** Unit tests for memory management
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <criterion/criterion.h>
#include <villas/signal.h>
extern void init_memory();
Test(signal, parse, .init = init_memory) {
int ret;
struct signal sig;
union signal_data sd;
const char *str;
char *end;
str = "1";
sig.type = SignalType::INTEGER;
ret = signal_data_parse_str(&sd, &sig, str, &end);
cr_assert_eq(ret, 0);
cr_assert_eq(end, str + strlen(str));
cr_assert_eq(sd.i, 1);
str = "1.2";
sig.type = SignalType::FLOAT;
ret = signal_data_parse_str(&sd, &sig, str, &end);
cr_assert_eq(ret, 0);
cr_assert_eq(end, str + strlen(str));
cr_assert_float_eq(sd.f, 1.2, 1e-6);
str = "1";
sig.type = SignalType::BOOLEAN;
ret = signal_data_parse_str(&sd, &sig, str, &end);
cr_assert_eq(ret, 0);
cr_assert_eq(end, str + strlen(str));
cr_assert_eq(sd.b, 1);
str = "1";
sig.type = SignalType::COMPLEX;
ret = signal_data_parse_str(&sd, &sig, str, &end);
cr_assert_eq(ret, 0);
cr_assert_eq(end, str + strlen(str));
cr_assert_float_eq(std::real(sd.z), 1, 1e-6);
cr_assert_float_eq(std::imag(sd.z), 0, 1e-6);
str = "-1-3i";
sig.type = SignalType::COMPLEX;
ret = signal_data_parse_str(&sd, &sig, str, &end);
cr_assert_eq(ret, 0);
cr_assert_eq(end, str + strlen(str));
cr_assert_float_eq(std::real(sd.z), -1, 1e-6);
cr_assert_float_eq(std::imag(sd.z), -3, 1e-6);
str = "-3i";
sig.type = SignalType::COMPLEX;
ret = signal_data_parse_str(&sd, &sig, str, &end);
cr_assert_eq(ret, 0);
cr_assert_eq(end, str + strlen(str));
cr_assert_float_eq(std::real(sd.z), 0, 1e-6);
cr_assert_float_eq(std::imag(sd.z), -3, 1e-6);
}